@Override public StartTimerAction applySubst(Substitution substitution) { // Apply substitution to parameters. List<Term> parameters = new ArrayList<>(getParameters().size()); for (Term term : getParameters()) { parameters.add(term.applySubst(substitution)); } // Create new action with instantiated parameters. return new StartTimerAction(parameters, getSourceInfo()); } }
/** * Creates a new macro definition. * * @param name * The name/label of the macro * @param parameters * The parameters of the macro (can be the empty list but not null). * @param definition * The {@link MentalStateCondition} the macro is a shorthand for. If * null, this macro reference still needs to be resolved. Should only * be null for macro instances. * @param info * Source info about this object. */ public Macro(String name, List<Term> parameters, MentalStateCondition definition, SourceInfo info) { super(info); this.name = name; this.parameters = parameters; this.definition = definition; for (Term t : this.parameters) { this.free.addAll(t.getFreeVar()); if (t.isVar() && !t.isClosed()) { this.free.add((Var) t); } } }
private static boolean isBuiltIn(Term conjunct) { String sig = conjunct.getSignature(); if (PrologOperators.prologBuiltin(sig)) { if (sig.equals("not/1")) { Term content = ((PrologCompound) conjunct).getArg(0); return PrologOperators.prologBuiltin(content.getSignature()); } else { return true; } } else { return false; } } }
/** * Creates a {@link Selector} using the given parameters and sets type of * selector to {@link SelectorType#PARAMETERLIST} or * {@link SelectorType#VARIABLE} * * @param parameters * List of {@link Term}s. */ public Selector(List<Term> parameters, SourceInfo info) { super(info); this.parameters = parameters; if (parameters == null || parameters.isEmpty()) { throw new IllegalArgumentException("cannot have parameterlist selector with empty content"); } if (parameters.size() == 1 && parameters.get(0).isVar()) { this.type = SelectorType.VARIABLE; } else { this.type = SelectorType.PARAMETERLIST; } // cache this.free = new LinkedHashSet<>(); for (Term term : parameters) { this.free.addAll(term.getFreeVar()); } }
@Override public Term makeList(List<Term> terms) throws MSTTranslationException { SourceInfo source = null; if (!terms.isEmpty()) { source = terms.get(0).getSourceInfo(); } // Start with element in list, since innermost term of Prolog list is // the last term. Term list = PrologImplFactory.getAtom(JPL.LIST_NIL.name(), source); for (int i = terms.size() - 1; i >= 0; i--) { list = PrologImplFactory.getCompound(JPL.LIST_PAIR, new Term[] { terms.get(i), list }, source); } return list; }
/** * Applies a substitution to this macro. * <p> * If the substitution applied instantiates instead of only renames formal * parameters of this macro, then the resulting macro may have a different * number of formal parameters. For example, applying {X/f(Y,Z)} to a macro with * one formal parameter X will return a new macro with two formal parameters Y * and Z. * </p> * * @return A version of this macro where variables have been renamed. */ @Override public Macro applySubst(Substitution substitution) { List<Term> parameters = new LinkedList<>(); for (Term term : getParameters()) { Term instantiatedTerm = term.applySubst(substitution); parameters.addAll(instantiatedTerm.getFreeVar()); } return new Macro(getName(), parameters, (getDefinition() == null) ? null : getDefinition().applySubst(substitution), getSourceInfo()); }
String signature = term.getSignature(); Term stubfunc = PrologImplFactory.getCompound(content.getName(), new Term[] { anon }, term.getSourceInfo()); terms.add(stubfunc); } else if (signature.equals(";/2") || signature.equals(",/2") || signature.equals("forall/2")) { Term anon = PrologImplFactory.getVar("_", null); Term stubfunc = PrologImplFactory.getCompound(content.getName(), new Term[] { anon, anon, anon }, term.getSourceInfo()); terms.add(stubfunc); } else {
Term term = substitution.get(var).applySubst(substitution); if (!var.equals(term)) { combination.addBinding(var, term); Substitution mgu = substitution.get(var).mgu(get(var)); if (mgu != null) { combination = combination.combine(mgu);
/** * Creates a compound with 1 or more arguments. * * @param name * The compound's name. * @param args * The arguments. * @param info * A source info object. */ PrologCompoundImpl(String name, Term[] args, SourceInfo info) { super(name, args.length); this.args = args; for (int i = 0; i < args.length; ++i) { Term arg = args[i]; setArg(i + 1, (org.jpl7.Term) arg); this.freeVar.addAll(arg.getFreeVar()); } this.info = info; this.hashcode = name.hashCode() + Arrays.hashCode(args); }
public Substitution mgu(Macro other, KRInterface kri) { if (other == null || !getSignature().equals(other.getSignature()) || getParameters().size() != other.getParameters().size()) { return null; } else if (getParameters().isEmpty()) { return kri.getSubstitution(null); } else { // Get mgu for first parameter Substitution substitution = getParameters().get(0).mgu(other.getParameters().get(0)); // Get mgu's for remaining parameters for (int i = 1; i < getParameters().size() && substitution != null; i++) { Substitution mgu = getParameters().get(i).mgu(other.getParameters().get(i)); substitution = substitution.combine(mgu); } return substitution; } }
if (term.isVar()) { boundedVars.add((Var) term);
/** * Validate a Prolog program. Assumes that the parser has been set up properly. * * @return List<DatabaseFormula>, or {@code null} if a parser error occurs. */ public List<DatabaseFormula> program() { List<DatabaseFormula> dbfs = new LinkedList<>(); for (Term t : this.visitor.visitPrologtext()) { if (t instanceof PrologCompound) { try { dbfs.add(SemanticTools.DBFormula((PrologCompound) t)); } catch (ParserException e) { this.errors.add(e); } } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(t), (t == null) ? null : t.getSourceInfo())); } } return dbfs; }
@Override public default Substitution unify(Term x, Substitution s) { if (s == null) { return null; } else if (equals(x)) { return s; } PrologTerm st = (PrologTerm) s.get(this); if (st != null) { return st.unify(x, s); } PrologTerm sx = (x instanceof Var) ? (PrologTerm) s.get((Var) x) : null; if (sx != null) { return unify(sx, s); } else if (x.getFreeVar().contains(this)) { return null; } else { s.addBinding(this, x); return s; } } }
/** * Delegate parsing of PARLIST terminal node to KR parser and checks whether * terms are variables. Reports an error if this is not the case. * * @param pars * String text from PARLIST terminal. * @param ctx * Parser context where PARLIST was found. * @return List of terms. */ public List<Var> visitVARLIST(TerminalNode parlist, ParserRuleContext ctx) { List<Term> parameters = visitPARLIST(parlist, getSourceInfo(ctx)); List<Var> vars = new LinkedList<>(); for (Term term : parameters) { if (!term.isVar()) { reportError(SyntaxError.PARAMETER_NOT_A_VARIABLE, getSourceInfo(ctx), getTheErrorStrategy().prettyPrintRuleContext(ctx.getRuleIndex()), term.toString()); } else { vars.add((Var) term); } } return vars; }
@Override public CancelTimerAction applySubst(Substitution substitution) { // Apply substitution to parameters. List<Term> parameters = new ArrayList<>(getParameters().size()); for (Term term : getParameters()) { parameters.add(term.applySubst(substitution)); } // Create new action with instantiated parameters. return new CancelTimerAction(parameters, getSourceInfo()); } }
/** * Validate the variable. Returns a variable obtained by parsing the input. * * @return {@link Var} or null if error occurred. */ public Var var() { Term term = this.visitor.visitTerm0(); if (term instanceof Var) { return (Var) term; } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_VAR.toReadableString(term), (term == null) ? null : term.getSourceInfo())); return null; } }
break; case 2: signatures.add(compound.getArg(0).getSignature()); break; default: signatures.add(term.getSignature());
@Override public UnsubscribeAction applySubst(Substitution substitution) { // Apply substitution to parameters. List<Term> parameters = new ArrayList<>(getParameters().size()); for (Term term : getParameters()) { parameters.add(term.applySubst(substitution)); } // Create new action with instantiated parameters. return new UnsubscribeAction(parameters, getSourceInfo()); } }
/** * Validate a (possibly empty) query. * * @return A {@link PrologQuery}, or {@code null} if an error occurred. */ public PrologQuery queryOrEmpty() { Term term = this.visitor.visitPossiblyEmptyDisjunct(); if (term instanceof PrologCompound) { try { return SemanticTools.toQuery((PrologCompound) term); } catch (ParserException e) { this.errors.add(e); } } else { this.errors.add(new ParserException(ParserErrorMessages.EXPECTED_COMPOUND.toReadableString(term), (term == null) ? null : term.getSourceInfo())); } return null; }
String signature = term.getSignature();