All Downloads are FREE. Search and download functionalities are using the official Maven repository.

astra.reasoner.util.VariableVisitor Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package astra.reasoner.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import astra.formula.AND;
import astra.formula.Bind;
import astra.formula.BracketFormula;
import astra.formula.Comparison;
import astra.formula.Formula;
import astra.formula.FormulaVariable;
import astra.formula.Goal;
import astra.formula.ModuleFormula;
import astra.formula.NOT;
import astra.formula.OR;
import astra.formula.Predicate;
import astra.formula.ScopedGoal;
import astra.formula.TestGoal;
import astra.term.Brackets;
import astra.term.FormulaTerm;
import astra.term.Funct;
import astra.term.ListSplitter;
import astra.term.ListTerm;
import astra.term.ModuleTerm;
import astra.term.Operator;
import astra.term.QueryTerm;
import astra.term.Term;
import astra.term.Variable;

public class VariableVisitor implements LogicVisitor {
	public VariableVisitor() {}

	public static interface Handler {
		public Class getType();
		public Object handle(LogicVisitor visitor, T object, Set variables);
	}
	
	private static Map, Handler> formulaHandlers = new HashMap, Handler>();
	private static Map, Handler> termHandlers = new HashMap, Handler>();
	
	public static  void addFormulaHandler(Handler handler) {
		formulaHandlers.put(handler.getType(), handler);
	}
	
	public static  void addTermHandler(Handler handler) {
		termHandlers.put(handler.getType(), handler);
	}
	
	@SuppressWarnings("unchecked")
	private static  Handler getFormulaHandler(Class cls) {
		return (Handler) formulaHandlers.get(cls);
	}
	
	@SuppressWarnings("unchecked")
	private static  Handler getTermHandler(Class cls) {
		return (Handler) termHandlers.get(cls);
	}
	
	Set variables = new HashSet();

	public Set variables() {
		return variables;
	}
	
	public Object visit(Formula formula) {
		Handler handler = getFormulaHandler(formula.getClass());
		if (handler == null) return null;
		return handler.handle(this, formula, variables);
	}
	
	public Object visit(Term term) {
		Handler handler = getTermHandler(term.getClass());
		if (handler == null) return null;
		return handler.handle(this, term, variables);
	}
	
	static {
		addTermHandler(new Handler() {
			@Override public Class getType() { return Variable.class; }
			@Override public Object handle(LogicVisitor visitor, Variable variable, Set variables) {
				variables.add(variable);
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return Funct.class; }
			@Override public Object handle(LogicVisitor visitor, Funct function, Set variables) {
				for (int i=0; i < function.size(); i++) {
					function.getTerm(i).accept(visitor);
				}
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return Variable.class; }
			@Override public Object handle(LogicVisitor visitor, Variable variable, Set variables) {
				variables.add(variable);
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return FormulaTerm.class; }
			@Override public Object handle(LogicVisitor visitor, FormulaTerm formula, Set variables) {
				if (formula.value() != null) formula.value().accept(visitor);
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return Operator.class; }
			@Override public Object handle(LogicVisitor visitor, Operator operator, Set variables) {
				operator.left().accept(visitor);
				operator.right().accept(visitor);
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return ModuleTerm.class; }
			@Override public Object handle(LogicVisitor visitor, ModuleTerm term, Set variables) {
				term.method().accept(visitor);
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return ListTerm.class; }
			@Override public Object handle(LogicVisitor visitor, ListTerm list, Set variables) {
				try {
					for (Term term : list.terms()) {
						term.accept(visitor);
					}
				} catch (Throwable th) {
					System.out.println("list: " + list);
				}
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return QueryTerm.class; }
			@Override public Object handle(LogicVisitor visitor, QueryTerm term, Set variables) {
				term.formula().accept(visitor);
				return null;
			}
		});
//		addTermHandler(new Handler() {
//			@Override public Class getType() { return JsonTerm.class; }
//			@Override public Object handle(LogicVisitor visitor, JsonTerm term, Set variables) {
//				term.().accept(visitor);
//				return null;
//			}
//		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return Brackets.class; }
			@Override public Object handle(LogicVisitor visitor, Brackets brackets, Set variables) {
				brackets.term().accept(visitor);
				return null;
			}
		});
		addTermHandler(new Handler() {
			@Override public Class getType() { return ListSplitter.class; }
			@Override public Object handle(LogicVisitor visitor, ListSplitter term, Set variables) {
				term.head().accept(visitor);
				term.tail().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return Predicate.class; }
			@Override public Object handle(LogicVisitor visitor, Predicate predicate, Set variables) {
				for (Term term : predicate.terms()) {
					term.accept(visitor);
				}
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return Comparison.class; }
			@Override public Object handle(LogicVisitor visitor, Comparison comparison, Set variables) {
				comparison.left().accept(visitor);
				comparison.right().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return AND.class; }
			@Override public Object handle(LogicVisitor visitor, AND and, Set variables) {
				and.left().accept(visitor);
				and.right().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return OR.class; }
			@Override public Object handle(LogicVisitor visitor, OR or, Set variables) {
				or.left().accept(visitor);
				or.right().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return NOT.class; }
			@Override public Object handle(LogicVisitor visitor, NOT not, Set variables) {
				not.formula().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return Goal.class; }
			@Override public Object handle(LogicVisitor visitor, Goal goal, Set variables) {
				goal.formula().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return TestGoal.class; }
			@Override public Object handle(LogicVisitor visitor, TestGoal goal, Set variables) {
				goal.formula().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return ScopedGoal.class; }
			@Override public Object handle(LogicVisitor visitor, ScopedGoal goal, Set variables) {
				goal.formula().accept(visitor);
				return null;
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return ModuleFormula.class; }
			@Override public Object handle(LogicVisitor visitor, ModuleFormula formula, Set variables) {
				return formula.predicate().accept(visitor);
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return FormulaVariable.class; }
			@Override public Object handle(LogicVisitor visitor, FormulaVariable variable, Set variables) {
				return variable.variable().accept(visitor);
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return BracketFormula.class; }
			@Override public Object handle(LogicVisitor visitor, BracketFormula formula, Set variables) {
				return formula.formula().accept(visitor);
			}
		});
		addFormulaHandler(new Handler() {
			@Override public Class getType() { return Bind.class; }
			@Override public Object handle(LogicVisitor visitor, Bind bind, Set variables) {
				bind.variable().accept(visitor);
				bind.term().accept(visitor);
				return null;
			}
		});
	}

//	@Override
//	public Object visit(AcreFormula formula) {
//		formula.cid().accept(this);
//		formula.index().accept(this);
//		formula.type().accept(this);
//		formula.performative().accept(this);
//		formula.content().accept(this);
//		return null;
//	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy