use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class LiteralsTest method integerType.
@Theory
public void integerType(AtomSet store) throws Exception {
Atom a = DlgpParser.parseAtom("<AGE>(a,1).");
store.add(a);
ConjunctiveQuery q = new DefaultConjunctiveQuery(new LinkedListAtomSet(a), Collections.<Term>emptyList());
Assert.assertTrue(SmartHomomorphism.instance().execute(q, store).hasNext());
Atom b = store.iterator().next();
Assert.assertEquals(a, b);
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery 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();
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method execute.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery query, AtomSet facts) throws HomomorphismException {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(query.toString());
}
if (profiler != null) {
profiler.start("preprocessing time");
}
Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(query);
List<Variable> orderedVars = order(pair.getLeft().getAtomSet().getVariables());
Collection<Atom>[] queryAtomRanked = getAtomRank(pair.getLeft().getAtomSet(), orderedVars);
if (profiler != null) {
profiler.stop("preprocessing time");
}
try {
this.domain = facts.getTerms();
if (profiler != null) {
profiler.start("backtracking time");
}
CloseableIterator<Substitution> results;
if (isHomomorphism(queryAtomRanked[0], facts, new HashMapSubstitution())) {
results = new CloseableIteratorAdapter<Substitution>(homomorphism(pair.getLeft(), queryAtomRanked, facts, new HashMapSubstitution(), orderedVars, 1).iterator());
} else {
// return false
results = new CloseableIteratorAdapter<Substitution>(Collections.<Substitution>emptyList().iterator());
}
if (profiler != null) {
profiler.stop("backtracking time");
}
if (!pair.getRight().getTerms().isEmpty()) {
results = new ConverterCloseableIterator<Substitution, Substitution>(results, new EqualityHandlerConverter(pair.getRight()));
}
return results;
} catch (Exception e) {
throw new HomomorphismException(e.getMessage(), e);
}
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class UnionConjunctiveQueriesSubstitutionIterator method hasNext.
@Override
public boolean hasNext() throws IteratorException {
if (!this.hasNextCallDone) {
this.hasNextCallDone = true;
if (this.tmpIt != null && !this.tmpIt.hasNext()) {
this.tmpIt.close();
this.tmpIt = null;
this.getProfiler().stop("SubQuery" + i++);
}
while ((this.tmpIt == null || !this.tmpIt.hasNext()) && this.cqueryIterator.hasNext()) {
ConjunctiveQuery q = this.cqueryIterator.next();
this.getProfiler().start("SubQuery" + i);
try {
if (this.homomorphism == null) {
this.tmpIt = SmartHomomorphism.instance().execute(q, this.atomSet, this.compilation);
} else {
if (this.compilation != null && !(this.compilation instanceof NoCompilation)) {
if (this.homomorphism instanceof HomomorphismWithCompilation) {
this.tmpIt = ((HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) this.homomorphism).execute(q, this.atomSet, this.compilation);
} else {
throw new IteratorException("There is a compilation and selected homomorphism can't handle it : " + this.homomorphism.getClass());
}
} else {
this.tmpIt = this.homomorphism.execute(q, this.atomSet);
}
}
if (this.isBooleanQuery && this.tmpIt.hasNext()) {
this.cqueryIterator.close();
}
} catch (HomomorphismException e) {
return false;
}
}
}
return this.tmpIt != null && this.tmpIt.hasNext();
}
use of fr.lirmm.graphik.graal.api.core.ConjunctiveQuery in project graal by graphik-team.
the class AdHocRdbmsStore method match.
@Override
public CloseableIterator<Atom> match(Atom atom) throws AtomSetException {
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);
}
}
Aggregations