/** * 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); }
/** * 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()); } }
/** * 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); } } }
@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; } } }
/** * 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()); }