use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class HomomorphismTest method test6.
@Test
public void test6() throws HomomorphismException, IteratorException, AtomSetException {
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b), p(a,c), r(a,d), q(d), r(a,e)."));
Variable y = DefaultTermFactory.instance().createVariable("Y");
Variable z = DefaultTermFactory.instance().createVariable("Z");
InMemoryAtomSet positivePart = new LinkedListAtomSet();
positivePart.addAll(DlgpParser.parseAtomSet("p(X,Y),r(X,Z)."));
CloseableIteratorWithoutException<Atom> it = positivePart.iterator();
it.next().setTerm(1, y);
it.next().setTerm(1, z);
LinkedList<InMemoryAtomSet> parts = new LinkedList<InMemoryAtomSet>();
InMemoryAtomSet negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(Y)."));
negatedPart.iterator().next().setTerm(0, y);
parts.add(negatedPart);
negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(Z)."));
negatedPart.iterator().next().setTerm(0, z);
parts.add(negatedPart);
DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, parts);
BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts();
CloseableIterator<Substitution> res = h.execute(query, data);
Assert.assertTrue(res.hasNext());
res.next();
Assert.assertFalse(res.hasNext());
res.close();
}
use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class Utils method getSafeCopy.
public static InMemoryAtomSet getSafeCopy(InMemoryAtomSet atomSet) {
Substitution substitution = new TreeMapSubstitution();
for (Variable t : atomSet.getVariables()) {
substitution.put(t, varGen.getFreshSymbol());
}
InMemoryAtomSet safe = new LinkedListAtomSet();
substitution.apply(atomSet, safe);
return safe;
}
use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class Rules method computeAtomicHead.
/**
* Generate a set of atomic head rules equivalent of the specified rule.
*
* @param rule
* @return a Collection of Rule which is a decomposition of the specified rule to atomic head rules.
*/
public static Collection<Rule> computeAtomicHead(Rule rule) {
String label = rule.getLabel();
Collection<Rule> atomicHead = new LinkedList<Rule>();
if (rule.getHead().isEmpty() || hasAtomicHead(rule)) {
return Collections.<Rule>singleton(rule);
} else {
Predicate predicate = new Predicate("aux_" + ++auxIndex, rule.getTerms().size());
Atom aux = DefaultAtomFactory.instance().create(predicate, rule.getTerms().toArray(new Term[rule.getTerms().size()]));
if (label.isEmpty()) {
atomicHead.add(DefaultRuleFactory.instance().create(rule.getBody(), new LinkedListAtomSet(aux)));
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
atomicHead.add(DefaultRuleFactory.instance().create(aux, atom));
}
} else {
int i = -1;
atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, rule.getBody(), new LinkedListAtomSet(aux)));
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, aux, atom));
}
}
}
return atomicHead;
}
use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class NaturalRDBMSStore method match.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Atom> match(Atom atom) throws AtomSetException {
if (!this.check(atom)) {
return Iterators.<Atom>emptyIterator();
}
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom));
SqlHomomorphism solver = SqlHomomorphism.instance();
try {
return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this));
} catch (HomomorphismException e) {
throw new AtomSetException(e);
}
}
use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.
the class RdbmsAtomIterator method hasNext.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean hasNext() throws IteratorException {
if (!this.hasNextCallDone) {
this.hasNextCallDone = true;
while (this.predicateIt.hasNext() && (this.atomIt == null || !this.atomIt.hasNext())) {
Predicate p = predicateIt.next();
List<Term> terms = new LinkedList<Term>();
VariableGenerator gen = new DefaultVariableGenerator("X");
for (int i = 0; i < p.getArity(); ++i) {
terms.add(gen.getFreshSymbol());
}
InMemoryAtomSet atomSet = new LinkedListAtomSet();
Atom atom = new DefaultAtom(p, terms);
atomSet.add(atom);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atomSet);
SqlHomomorphism solver = SqlHomomorphism.instance();
try {
this.atomIt = new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this.store));
} catch (HomomorphismException e) {
throw new IteratorException(e);
}
}
}
return this.atomIt != null && this.atomIt.hasNext();
}
Aggregations