use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class ConjunctiveQueryWithFixedVariables method computeFixedQuery.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private static InMemoryAtomSet computeFixedQuery(InMemoryAtomSet atomset, Iterable<? extends Term> fixedTerms) {
// create a Substitution for fixed query
InMemoryAtomSet fixedQuery = DefaultAtomSetFactory.instance().create();
Substitution fixSub = DefaultSubstitutionFactory.instance().createSubstitution();
for (Term t : fixedTerms) {
if (t.isVariable()) {
fixSub.put((Variable) t, DefaultTermFactory.instance().createConstant(t.getLabel()));
}
}
// apply substitution
CloseableIteratorWithoutException<Atom> it = atomset.iterator();
while (it.hasNext()) {
Atom a = it.next();
fixedQuery.add(fixSub.createImageOf(a));
}
return fixedQuery;
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class PureQuery method removeAnswerPredicate.
public static void removeAnswerPredicate(ConjunctiveQuery query) {
Term[] ans = query.getAnswerVariables().toArray(new Term[query.getAnswerVariables().size()]);
CloseableIteratorWithoutException<Atom> ita = query.getAtomSet().iterator();
InMemoryAtomSet toRemove = new LinkedListAtomSet();
InMemoryAtomSet toAdd = new LinkedListAtomSet();
while (ita.hasNext()) {
Atom a = ita.next();
if (a.getPredicate().equals(ansPredicate)) {
Term ansTerm = ans[(Integer) ((Literal) a.getTerm(0)).getValue()];
if (!ansTerm.equals(a.getTerm(1))) {
toAdd.add(DefaultAtomFactory.instance().create(Predicate.EQUALITY, ansTerm, a.getTerm(1)));
}
toRemove.add(a);
}
}
query.getAtomSet().removeAll(toRemove);
query.getAtomSet().addAll(toAdd);
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class BackwardChainingTest method issue35.
@Theory
public void issue35(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
try {
RuleSet rules = new LinkedListRuleSet();
Predicate p = new Predicate("p", 2);
Predicate q = new Predicate("q", 3);
Predicate r = new Predicate("r", 2);
rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- q(X,Y,Y), r(X,Y).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
int i = 0;
while (it.hasNext()) {
ConjunctiveQuery next = it.next();
InMemoryAtomSet atomSet = next.getAtomSet();
Set<Predicate> predicates = atomSet.getPredicates();
Assert.assertTrue(predicates.contains(r));
if (predicates.contains(p)) {
Assert.assertEquals(1, atomSet.getTerms().size());
} else if (predicates.contains(q)) {
Assert.assertEquals(query, next);
} else {
Assert.assertFalse(true);
}
++i;
}
Assert.assertEquals(2, i);
} catch (Exception e) {
Assert.assertFalse("There is an error.", true);
}
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class AbstractSubstitution method createImageOf.
@Override
public InMemoryAtomSet createImageOf(InMemoryAtomSet src) {
InMemoryAtomSet dest = DefaultAtomSetFactory.instance().create();
this.apply(src, dest);
return dest;
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class DefaultConjunctiveQueryWithNegatedParts method appendTo.
@Override
public void appendTo(StringBuilder sb) {
sb.append("ANS(");
boolean first = true;
for (Term t : this.responseVariables) {
if (!first) {
sb.append(',');
}
first = false;
sb.append(t);
}
sb.append(") : ");
sb.append(this.positiveAtomSet);
for (InMemoryAtomSet atomset : this.negatedParts) {
sb.append(", \u22A5(");
sb.append(atomset);
sb.append(")");
}
}
Aggregations