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

astra.reasoner.util.RenameVisitor 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.Map;

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.Inference;
import astra.formula.IsNull;
import astra.formula.ModuleFormula;
import astra.formula.NOT;
import astra.formula.OR;
import astra.formula.Predicate;
import astra.formula.ScopedGoal;
import astra.term.*;

@SuppressWarnings({"unchecked","rawtypes"})
public class RenameVisitor implements LogicVisitor {
	public static interface Handler {
		public Class getType();
		public Object handle(LogicVisitor visitor, T object, String modifier, Map bindings);
	}

	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);
	}
	
	private static  Handler getFormulaHandler(Class cls) {
		return (Handler) formulaHandlers.get(cls);
	}
	
	private static  Handler getTermHandler(Class cls) {
		return (Handler) termHandlers.get(cls);
	}

	String modifier;
	private Map bindings = new HashMap();
	
	public Map bindings() {
		return bindings;
	}
	
	public RenameVisitor(String modifier) {
		this.modifier = modifier;
	}
	
	public Object visit(Formula formula) {
		Handler handler = getFormulaHandler(formula.getClass());
		if (handler == null) {
			return null;
		}
		return handler.handle(this, formula, modifier, bindings);
	}
	
	public Object visit(Term term) {
		Handler handler = getTermHandler(term.getClass());
		if (handler == null) {
			return null;
		}
		return handler.handle(this, term, modifier, bindings);
	}
	
	static{
		addTermHandler(new Handler() {
			public Class getType() { return Variable.class; }
			public Object handle(LogicVisitor visitor, Variable variable, String modifier, Map bindings) {
				Variable v = new Variable(variable.type(), modifier + variable.identifier(), variable.returns());
				bindings.put(variable.id(), v);
				return v;
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return Primitive.class; }
			public Object handle(LogicVisitor visitor, Primitive primitive, String modifier, Map bindings) {
				return primitive;
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return Operator.class; }
			public Object handle(LogicVisitor visitor, Operator operator, String modifier, Map bindings) {
				return Operator.newOperator(operator.op(), (Term) operator.left().accept(visitor), (Term) operator.right().accept(visitor));
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return Funct.class; }
			public Object handle(LogicVisitor visitor, Funct function, String modifier, Map bindings) {
				Term[] terms = new Term[function.size()];
				
				for (int i=0; i < function.size(); i++) {
					terms[i] = (Term) function.getTerm(i).accept(visitor);
				}
				return new Predicate(function.functor(), terms);
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return ModuleTerm.class; }
			public Object handle(LogicVisitor visitor, ModuleTerm term, String modifier, Map bindings) {
				return new ModuleTerm(term.module(), term.type(), (Predicate) term.method().accept(visitor), term.adaptor());
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return ListTerm.class; }
			public Object handle(LogicVisitor visitor, ListTerm list, String modifier, Map bindings) {
				Term[] terms = new Term[list.terms().length];
				for (int i=0;i() {
			public Class getType() { return QueryTerm.class; }
			public Object handle(LogicVisitor visitor, QueryTerm term, String modifier, Map bindings) {
				return new QueryTerm((Formula) term.formula().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return Predicate.class; }
			public Object handle(LogicVisitor visitor, Predicate predicate, String modifier, Map bindings) {
				Term[] terms = new Term[predicate.size()];
				for (int i=0; i < predicate.size(); i++) {
					terms[i] = (Term) predicate.termAt(i).accept(visitor);
				}
				return new Predicate(predicate.predicate(), terms);
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return AND.class; }
			public Object handle(LogicVisitor visitor, AND and, String modifier, Map bindings) {
				return new AND((Formula) and.left().accept(visitor), (Formula) and.right().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return OR.class; }
			public Object handle(LogicVisitor visitor, OR or, String modifier, Map bindings) {
				return new OR((Formula) or.left().accept(visitor), (Formula) or.right().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return NOT.class; }
			public Object handle(LogicVisitor visitor, NOT not, String modifier, Map bindings) {
				return new NOT((Formula) not.formula().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return Goal.class; }
			public Object handle(LogicVisitor visitor, Goal goal, String modifier, Map bindings) {
				return new Goal((Predicate) goal.formula().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return ScopedGoal.class; }
			public Object handle(LogicVisitor visitor, ScopedGoal goal, String modifier, Map bindings) {
				return new ScopedGoal(goal.scope(), (Goal) goal.formula().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return Comparison.class; }
			public Object handle(LogicVisitor visitor, Comparison comparison, String modifier, Map bindings) {
				return new Comparison(comparison.operator(), (Term) comparison.left().accept(visitor), (Term) comparison.right().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return ModuleFormula.class; }
			public Object handle(LogicVisitor visitor, ModuleFormula formula, String modifier, Map bindings) {
				return new ModuleFormula(formula.module(), (Predicate) formula.predicate().accept(visitor), formula.adaptor());
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return FormulaVariable.class; }
			public Object handle(LogicVisitor visitor, FormulaVariable variable, String modifier, Map bindings) {
				return new FormulaVariable((Variable) variable.variable().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return Inference.class; }
			public Object handle(LogicVisitor visitor, Inference inference, String modifier, Map bindings) {
				return new Inference((Predicate) inference.head().accept(visitor), (Formula) inference.body().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return IsNull.class; }
			public Object handle(LogicVisitor visitor, IsNull isNull, String modifier, Map bindings) {
				return new IsNull((Term) isNull.formula().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return BracketFormula.class; }
			public Object handle(LogicVisitor visitor, BracketFormula formula, String modifier, Map bindings) {
				return new BracketFormula((Formula) formula.formula().accept(visitor));
			}
		});
		addFormulaHandler(new Handler() {
			public Class getType() { return Bind.class; }
			public Object handle(LogicVisitor visitor, Bind bind, String modifier, Map bindings) {
				return new Bind((Variable) bind.variable().accept(visitor), (Term) bind.term().accept(visitor));
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return Brackets.class; }
			public Object handle(LogicVisitor visitor, Brackets term, String modifier, Map bindings) {
				return new Brackets((Term) term.term().accept(visitor));
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return ListSplitter.class; }
			public Object handle(LogicVisitor visitor, ListSplitter term, String modifier, Map bindings) {
				return new ListSplitter((Variable) term.head().accept(visitor), (Variable) term.tail().accept(visitor));
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return Head.class; }
			public Object handle(LogicVisitor visitor, Head term, String modifier, Map bindings) {
				return new Head((Term) term.term().accept(visitor), term.type());
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return Tail.class; }
			public Object handle(LogicVisitor visitor, Tail term, String modifier, Map bindings) {
				return new Tail((Term) term.term().accept(visitor));
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return AtIndex.class; }
			public Object handle(LogicVisitor visitor, AtIndex term, String modifier, Map bindings) {
				return new AtIndex((Term) term.term().accept(visitor), (Term) term.index().accept(visitor), term.type());
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return ToJson.class; }
			public Object handle(LogicVisitor visitor, ToJson term, String modifier, Map bindings) {
				return new ToJson((Term) term.term().accept(visitor));
			}
		});
		addTermHandler(new Handler() {
			public Class getType() { return FromJson.class; }
			public Object handle(LogicVisitor visitor, FromJson term, String modifier, Map bindings) {
				return new FromJson((Term) term.term().accept(visitor), term.rawType());
			}
		});
	}


//	@Override
//	public Object visit(CartagoProperty property) {
//		return null;
//	}
//
//	@Override
//	public Object visit(AcreFormula formula) {
//		return new AcreFormula((Term) formula.index().accept(this), (Term) formula.type().accept(this), (Term) formula.performative().accept(this),
//				(Term) formula.cid().accept(this), (Formula) formula.content().accept(this));
//	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy