use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class Rules method criticalInstance.
public static InMemoryAtomSet criticalInstance(final Iterable<Rule> rules) {
InMemoryAtomSet A = new DefaultInMemoryGraphStore();
criticalInstance(rules, A);
return A;
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet 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();
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class ConjunctiveQueryTest method emptyQueryTest.
/**
* Test an empty query that must have an empty substitution
*/
@Theory
public void emptyQueryTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
store.addAll(DlgpParser.parseAtomSet("<P>(a,b), <P>(b,c), <Q>(c,a)."));
InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(queryAtomSet);
CloseableIterator<Substitution> subReader;
Substitution sub;
subReader = h.execute(query, store);
Assert.assertTrue(subReader.hasNext());
sub = subReader.next();
Assert.assertEquals(0, sub.getTerms().size());
Assert.assertFalse(subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class ConjunctiveQueryTest method emptyQueryAndEmptyAtomSetTest.
/**
* Test an empty query with an empty atomSet that must have an empty
* substitution
*/
@Theory
public void emptyQueryAndEmptyAtomSetTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
try {
InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(queryAtomSet);
CloseableIterator<Substitution> subReader;
Substitution sub;
subReader = h.execute(query, store);
Assert.assertTrue(subReader.hasNext());
sub = subReader.next();
Assert.assertEquals(0, sub.getTerms().size());
Assert.assertFalse(subReader.hasNext());
subReader.close();
} catch (Exception e) {
Assert.assertTrue(e.getMessage(), false);
}
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class FrontierRestrictedChaseHaltingCondition method apply.
@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
Set<Term> fixedVars = substitution.getValues();
if (ruleIndex.get(rule) == null) {
ruleIndex.put(rule, _currentRuleIndex++);
}
final int index = ruleIndex.get(rule).intValue();
StringBuilder frontierSb = new StringBuilder();
SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
for (Term t : frontierSet) {
frontierSb.append("_");
frontierSb.append(t.getLabel());
frontierSb.append(substitution.createImageOf(t).getLabel());
}
String frontier = frontierSb.toString();
for (Variable t : rule.getExistentials()) {
substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
}
InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Fixed Query:" + query);
}
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);
}
return newFacts.iterator();
}
Aggregations