use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class HomomorphismTest method test5.
@Test
public void test5() throws HomomorphismException, IteratorException, AtomSetException {
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b), r(b,b), r(c,c), q(c), q(b)."));
Variable x = DefaultTermFactory.instance().createVariable("X");
InMemoryAtomSet positivePart = new LinkedListAtomSet();
positivePart.addAll(DlgpParser.parseAtomSet("p(a,X),r(W,Y),q(Y)."));
positivePart.iterator().next().setTerm(1, x);
InMemoryAtomSet negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(X)."));
negatedPart.iterator().next().setTerm(0, x);
DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, Collections.singletonList(negatedPart), Collections.<Term>emptyList());
BCC bcc = new BCC(new GraphBaseBackJumping(), true);
BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts(bcc.getBCCScheduler(), StarBootstrapper.instance(), new NFC2(), bcc.getBCCBackJumping());
CloseableIterator<Substitution> res = h.execute(query, data);
Assert.assertFalse(res.hasNext());
res.close();
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class AtomTest method testGetVariables.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.core.DefaultAtom#getVariables()}.
*/
@Theory
public void testGetVariables(AtomFactory factory) {
// given
Atom a = factory.create(TestUtils.pXA);
// when
Set<Variable> variables = a.getVariables();
// then
Assert.assertEquals(1, variables.size());
Assert.assertTrue(variables.contains(TestUtils.X));
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class UnifierIterator method extendUnifier.
private Collection<Substitution> extendUnifier(Rule rule, Queue<Atom> atomset, Atom pieceElement, Unifier unifier) {
atomset.remove(pieceElement);
unifier.queryPiece.add(pieceElement);
Collection<Substitution> unifierCollection = new LinkedList<Substitution>();
Set<Variable> frontierVars = rule.getFrontier();
Set<Variable> existentialVars = rule.getExistentials();
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
Substitution u = unifier(unifier.s, pieceElement, atom, frontierVars, existentialVars);
if (u != null) {
unifier = new Unifier(unifier);
unifier.ruleHeadPiece.add(atom);
unifier.s = u;
// look if there exist other element for the current piece
Atom nextPieceElement = getNextPieceElementIfExist(u, atomset, existentialVars);
if (nextPieceElement == null) {
boolean check = true;
for (DependencyChecker c : this.checkers) {
check &= c.isValidDependency(this.source, this.target, unifier.s);
}
if (check) {
unifierCollection.add(unifier.s);
}
} else {
unifierCollection.addAll(extendUnifier(rule, new LinkedList<Atom>(atomset), nextPieceElement, unifier));
}
}
}
return unifierCollection;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RestrictedChaseHaltingCondition method apply.
@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, substitution.createImageOf(rule.getFrontier()));
try {
if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
}
} catch (IteratorException e) {
throw new HomomorphismException("An errors occurs while iterating results", e);
} catch (BacktrackException e) {
throw new HomomorphismException("An errors occurs while iterating results", e);
}
// replace variables by fresh symbol
for (Variable t : rule.getExistentials()) {
substitution.put(t, data.getFreshSymbolGenerator().getFreshSymbol());
}
CloseableIteratorWithoutException<Atom> it = substitution.createImageOf(rule.getHead()).iterator();
return it;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RestrictedChaseRuleApplier method apply.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean apply(Rule rule, T atomSet) throws RuleApplicationException {
try {
boolean res = false;
ConjunctiveQueryWithNegatedParts query = new RuleWrapper2ConjunctiveQueryWithNegatedParts(rule);
CloseableIterator<Substitution> results;
results = SmartHomomorphism.instance().execute(query, atomSet);
while (results.hasNext()) {
res = true;
Substitution proj = results.next();
// replace variables by fresh symbol
for (Variable t : rule.getExistentials()) {
proj.put(t, atomSet.getFreshSymbolGenerator().getFreshSymbol());
}
CloseableIteratorWithoutException<Atom> it = proj.createImageOf(rule.getHead()).iterator();
while (it.hasNext()) {
atomSet.add(it.next());
}
}
return res;
} catch (HomomorphismException e) {
throw new RuleApplicationException("", e);
} catch (AtomSetException e) {
throw new RuleApplicationException("", e);
} catch (IteratorException e) {
throw new RuleApplicationException("", e);
}
}
Aggregations