use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class DefaultScheduler method execute.
@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) {
Set<Variable> terms = h.getVariables();
VarSharedData[] vars = new VarSharedData[terms.size() + 2];
int level = 0;
vars[level] = new VarSharedData(level);
Set<Term> alreadyAffected = new TreeSet<Term>();
for (Term t : ans) {
if (t instanceof Variable && !alreadyAffected.contains(t)) {
++level;
vars[level] = new VarSharedData(level);
vars[level].value = (Variable) t;
alreadyAffected.add(t);
}
}
int lastAnswerVariable = level;
for (Term t : terms) {
if (!alreadyAffected.contains(t)) {
++level;
vars[level] = new VarSharedData(level);
vars[level].value = (Variable) t;
}
}
++level;
vars[level] = new VarSharedData(level);
vars[level].previousLevel = lastAnswerVariable;
return vars;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class BackwardChainingTest method getBody2.
/**
* c(X) :- b(X,X).
*
* getBody([X]) => b(X, X).
*
* @throws ParseException
*/
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
boolean isFound = false;
while (rewIt.hasNext()) {
ConjunctiveQuery rew = rewIt.next();
CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
if (it.hasNext()) {
Atom a = it.next();
if (a.getPredicate().equals(new Predicate("q", 2))) {
isFound = true;
List<Term> terms = a.getTerms();
Assert.assertEquals(terms.get(0), terms.get(1));
}
}
}
Assert.assertTrue("Rewrite not found", isFound);
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class Utils method imply.
/**
* This methods test if the first rule logically imply the second. This
* methods works with linear rules (atomic head and body).
*
* @param r1
* @param r2
* @return true, if r1 logically imply r2.
*/
public static boolean imply(Rule r1, Rule r2) {
Atom b1 = r1.getBody().iterator().next();
Atom b2 = r2.getBody().iterator().next();
Atom h1 = r1.getHead().iterator().next();
Atom h2 = r2.getHead().iterator().next();
if (b1.getPredicate().equals(b2.getPredicate()) && h1.getPredicate().equals(h2.getPredicate())) {
Map<Term, Term> s = new TreeMap<Term, Term>();
Term term;
for (int i = 0; i < b1.getPredicate().getArity(); i++) {
term = s.get(b1.getTerm(i));
if (term == null)
s.put(b1.getTerm(i), b2.getTerm(i));
else if (!term.equals(b2.getTerm(i)))
return false;
}
for (int i = 0; i < h1.getPredicate().getArity(); i++) {
term = s.get(h1.getTerm(i));
if (term == null)
s.put(h1.getTerm(i), h2.getTerm(i));
else if (!term.equals(h2.getTerm(i)))
return false;
}
return true;
}
return false;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class Utils method rewrite.
/**
* Rewrite the fact q according to the unifier u.
*
* @param q
* the fact to rewrite
* @param u
* the unifier between q and r
* @return the rewrite of q according to the unifier u.
*/
public static ConjunctiveQuery rewrite(ConjunctiveQuery q, QueryUnifier u) {
InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
ConjunctiveQuery rew = null;
if (ajout != null && restant != null) {
// FIXME
InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
List<Term> ansVar = new LinkedList<Term>();
ansVar.addAll(q.getAnswerVariables());
rew = DefaultConjunctiveQueryFactory.instance().create(res, ansVar);
}
return rew;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class Utils method rewriteWithMark.
/**
* Rewrite the marked fact q according to the unifier u between
*
* @param q
* the fact to rewrite must be a marked fact
* @param u
* the unifier between q and r
* @return the rewrite of q according to the unifier u.
*/
public static MarkedQuery rewriteWithMark(ConjunctiveQuery q, QueryUnifier u) {
InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
MarkedQuery rew = null;
InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
List<Term> ansVar = new LinkedList<Term>();
ansVar.addAll(q.getAnswerVariables());
rew = new MarkedQuery(res, ansVar);
ArrayList<Atom> markedAtoms = new ArrayList<Atom>();
CloseableIteratorWithoutException<Atom> it = ajout.iterator();
while (it.hasNext()) {
Atom a = it.next();
markedAtoms.add(a);
}
rew.setMarkedAtom(markedAtoms);
return rew;
}
Aggregations