public Operation exec(Prolog engine) { engine.requireFeature(Prolog.Feature.JAVA_REFLECTION, this, arg1); engine.setB0(); Term a1, a2, a3; a1 = arg1; a2 = arg2; a3 = arg3; Class clazz = null; Object instance = null; Field field = null; Object value = null; try { // 1st. argument (atom or java term) a1 = a1.dereference(); if (a1.isVariable()) { throw new PInstantiationException(this, 1); } else if (a1.isSymbol()) { // class clazz = Class.forName(((SymbolTerm) a1).name()); } else if (a1.isJavaObject()) { // instance instance = ((JavaObjectTerm) a1).object(); clazz = ((JavaObjectTerm) a1).getClazz(); } else { throw new IllegalTypeException(this, 1, "atom_or_java", a1); } // 2nd. argument (atom) a2 = a2.dereference(); if (a2.isVariable()) { throw new PInstantiationException(this, 2); } else if (!a2.isSymbol()) { throw new IllegalTypeException(this, 2, "atom", a2); } field = clazz.getDeclaredField(((SymbolTerm) a2).name()); // 3rd. argument (term) a3 = a3.dereference(); if (a3.isJavaObject()) value = a3.toJava(); else value = a3; field.setAccessible(true); field.set(instance, value); return cont; } catch (ClassNotFoundException e) { // Class.forName throw new JavaException(this, 1, e); } catch (NoSuchFieldException e) { // Class.getField(..) throw new JavaException(this, 2, e); } catch (SecurityException e) { // Class.getField(..) throw new JavaException(this, 2, e); } catch (NullPointerException e) { // Class.getField(..) throw new JavaException(this, 2, e); } catch (IllegalAccessException e) { // Field.get(..) throw new JavaException(this, 2, e); } catch (IllegalArgumentException e) { // Field.get(..) throw new JavaException(this, 2, e); } }
// TODO: update it for rules with non-DL prediciates in the body public Set<Variable> getNonDistinguishedVars() { Set<Variable> nonDistinguishedVars = new HashSet<Variable>(); Set<Term> distinguishedVars = this.getDistinguishedVariables(); for (Atom atom : body) { for (Term term : atom.getTerms()) if (distinguishedVars == null) { // Xiao: can not happen! if (term.isVariable()) nonDistinguishedVars.add((Variable) term); } else if ((term.isVariable()) && !distinguishedVars.contains(term)) nonDistinguishedVars.add((Variable) term); } return nonDistinguishedVars; }
public Set<Term> getDistinguishedVariables() { Set<Term> vars = new HashSet<Term>(); for (Term term : this.head.getTerms()) { if (term.isVariable()) vars.add(term); } return vars; }
/* Term */ public boolean unify(Term t, Trail trail) { t = t.dereference(); if (t.isVariable()) { ((VariableTerm) t).bind(this, trail); return true; } return eq(this, t); }
/** * Compares two terms in <em>Prolog standard order of terms</em>.<br> * It is noted that <code>t1.compareTo(t2) == 0</code> has the same <code>boolean</code> value as * <code>t1.equals(t2)</code>. * * @param anotherTerm the term to compared with. It must be dereferenced. * @return the value <code>0</code> if two terms are identical; a value less than <code>0</code> * if this term is <em>before</em> the <code>anotherTerm</code>; and a value greater than * <code>0</code> if this term is <em>after</em> the <code>anotherTerm</code>. */ public int compareTo(Term anotherTerm) { // anotherTerm must be dereferenced. if (anotherTerm.isVariable() || anotherTerm.isNumber()) return AFTER; if (!anotherTerm.isSymbol()) return BEFORE; if (this == anotherTerm) return EQUAL; int x = name.compareTo(((SymbolTerm) anotherTerm).name()); if (x != 0) return x; int y = this.arity - ((SymbolTerm) anotherTerm).arity(); if (y != 0) return y; throw new InternalException("SymbolTerm is not unique"); }
public CQ computeQprime(TIntHashSet type, Variable x) { CQ qPrime = new CQ(); qPrime.setHead(head); int numberOfIndividual = 0; Constant individual = new Constant(); for (Term term : this.computeRenamingVars(x)) { if (!term.isVariable()) { numberOfIndividual++; individual = (Constant) term; } } if (numberOfIndividual > 1) // cannot merge two individuals return null; // if there is no individual in the variables which should be renamed if (numberOfIndividual == 0) { for (Atom atom : body) { // compute body if (!(atom.getTerms().contains(x))) { // atom that contains x will disappear in the rewrite query List<Term> newTerms = new ArrayList<Term>(); for (Term term : atom.getTerms()) { if (this.computeRenamingVars(x).contains(term)) newTerms.add(x); else newTerms.add(term); } Atom renamedVarAtom = new Atom(atom.getPredicate(), newTerms); if (!qPrime.body.contains(renamedVarAtom)) { qPrime.body.add(renamedVarAtom); } } } // add type(x) to the body Set<Atom> currentBody = qPrime.getBody(); qPrime.body.addAll(addTypeOfXToBody(x, type, currentBody)); // compute head Atom newHead = computeNewHead(x, this.head); qPrime.setHead(newHead); } // if there is an individual in the variables which should be renamed // then all these variables should be renamed as the individual if (numberOfIndividual == 1) { // compute body for (Atom atom : body) { if (!(atom.getTerms().contains(x))) { // atom that contains x will disappear in the rewrite query List<Term> newTerms = new ArrayList<Term>(); for (Term term : atom.getTerms()) { if (this.computeRenamingVars(x).contains(term)) // this term should be renamed newTerms.add(individual); else newTerms.add(term); } Atom renamedVarAtom = new Atom(atom.getPredicate(), newTerms); if (!qPrime.body.contains(renamedVarAtom)) { qPrime.body.add(renamedVarAtom); } } } // at type(x) to the body Set<Atom> currentBody = qPrime.getBody(); qPrime.body.addAll(addTypeOfXToBody(individual, type, currentBody)); // compute head Atom newHead = computeNewHead(x, individual, this.head); qPrime.setHead(newHead); } return qPrime; }