use of fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery in project graal by graphik-team.
the class DefaultConjunctiveQueryFactory method create.
@Override
public ConjunctiveQuery create(Atom atom) {
LinkedList<Atom> list = new LinkedList<Atom>();
list.add(atom);
return new DefaultConjunctiveQuery(new LinkedListAtomSet(list));
}
use of fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery in project graal by graphik-team.
the class ChaseWithGRDAndUnfiers method next.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
Rule rule, unifiedRule;
Substitution unificator;
Queue<Triple<Rule, Substitution, InMemoryAtomSet>> newQueue = new LinkedList<Triple<Rule, Substitution, InMemoryAtomSet>>();
InMemoryAtomSet newAtomSet = new DefaultInMemoryGraphStore();
try {
while (!queue.isEmpty()) {
Triple<Rule, Substitution, InMemoryAtomSet> pair = queue.poll();
if (pair != null) {
unificator = pair.getMiddle();
InMemoryAtomSet part = pair.getRight();
rule = pair.getLeft();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("\nExecute rule: {} with unificator {}", rule, unificator);
}
unifiedRule = DefaultUnifierAlgorithm.getTargetVariablesSubstitution().createImageOf(rule);
unifiedRule = unificator.createImageOf(unifiedRule);
unifiedRule.getBody().removeAll(part);
unificator = targetToSource(unificator);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(unifiedRule.getBody(), new LinkedList<Term>(unifiedRule.getFrontier()));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Rule to execute: {}", unifiedRule.toString());
LOGGER.debug(" -- Query: {}", query.toString());
}
// Get projections
List<Substitution> projections = Iterators.toList(SmartHomomorphism.instance().execute(query, atomSet));
try {
for (Substitution proj : projections) {
InMemoryAtomSet newFacts = proj.createImageOf(unifiedRule.getHead());
ConjunctiveQuery q = new DefaultConjunctiveQuery(newFacts);
if (!SmartHomomorphism.instance().execute(q, newAtomSet).hasNext()) {
// Existential variables instantiation added to proj
CloseableIterator<Atom> it = hc.apply(unifiedRule, proj, atomSet);
if (it.hasNext()) {
LinkedListAtomSet foundPart = new LinkedListAtomSet();
foundPart.addAll(it);
newAtomSet.addAll(foundPart);
// Makes the projection compatible with triggered rules unifiers
Substitution compatibleProj = targetToSource(proj);
for (Pair<Rule, Substitution> p : this.grd.getTriggeredRulesWithUnifiers(rule)) {
Rule triggeredRule = p.getKey();
Substitution u = p.getValue();
if (u != null) {
Substitution comp = unificator.compose(u);
Substitution aggreg = compatibleProj.aggregate(comp);
aggreg = forgetSource(aggreg);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("-- -- Dependency: {}", triggeredRule);
LOGGER.debug("-- -- Substitution:{} ", compatibleProj);
LOGGER.debug("-- -- Unificator: {}", u);
LOGGER.debug("-- -- Aggregation: {}\n", aggreg);
}
if (aggreg != null) {
newQueue.add(new ImmutableTriple<Rule, Substitution, InMemoryAtomSet>(triggeredRule, aggreg, foundPart));
}
}
}
}
}
}
} catch (HomomorphismFactoryException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (HomomorphismException e) {
throw new RuleApplicationException("Error during rule application", e);
} catch (IteratorException e) {
throw new RuleApplicationException("Error during rule application", e);
}
}
}
queue = newQueue;
atomSet.addAll(newAtomSet);
} catch (Exception e) {
e.printStackTrace();
throw new ChaseException("An error occur pending saturation step.", e);
}
}
use of fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery in project graal by graphik-team.
the class BacktrackIteratorData method preprocessing.
private void preprocessing(Set<Variable> variablesToParameterize, Profiler profiler) throws HomomorphismException {
profiler.start("preprocessingTime");
// Compute order on query variables and atoms
if (variablesToParameterize != null && !variablesToParameterize.isEmpty()) {
assert this.scheduler instanceof PatternScheduler;
PatternScheduler sched = (PatternScheduler) this.scheduler;
this.varsOrder = sched.execute(this.query, variablesToParameterize, ans, this.data, this.compilation);
} else {
this.varsOrder = this.scheduler.execute(this.query, ans, this.data, this.compilation);
}
this.levelMax = varsOrder.length - 2;
// PROFILING
if (profiler.isProfilingEnabled()) {
this.profilingVarOrder(this.varsOrder, profiler);
}
// Index Var structures by original variable object
this.index = new TreeMap<Variable, Integer>();
for (VarSharedData v : this.varsOrder) {
if (v.value != null) {
//
this.index.put(v.value, v.level);
}
}
if (this.ans.isEmpty()) {
this.varsOrder[this.levelMax + 1].previousLevel = -1;
}
computeAtomOrder(this.query, this.varsOrder, this.index);
this.fc.init(this.varsOrder, this.index);
this.bj.init(this.varsOrder);
Set<Variable> allVarsFromH = query.getVariables();
for (InMemoryAtomSet negPart : this.negParts) {
Set<Variable> frontier = SetUtils.intersection(allVarsFromH, negPart.getVariables());
this.varsOrder[maxLevel(frontier)].negatedPartsToCheck.add(BacktrackHomomorphismPattern.instance().prepareHomomorphism(new DefaultConjunctiveQuery(negPart, Collections.<Term>emptyList()), frontier, this.data, this.compilation));
}
profiler.stop("preprocessingTime");
}
use of fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery in project graal by graphik-team.
the class ConjunctiveQueryWithCompilation method issue34.
@Theory
public void issue34(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
store.add(DlgpParser.parseAtom("<Q>(a,b)."));
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("<P>(X,Y) :- <Q>(Y,X)."));
RulesCompilation comp = factory.create();
comp.compile(rules.iterator());
StaticChase.executeChase(store, rules);
InMemoryAtomSet query1 = new LinkedListAtomSet();
query1.add(DlgpParser.parseAtom("<P>(a,Y)."));
CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query1), store, comp);
Assert.assertFalse(results.hasNext());
results.close();
}
use of fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery in project graal by graphik-team.
the class LiteralsTest method doubleType.
@Theory
public void doubleType(AtomSet store) throws Exception {
Atom a = DlgpParser.parseAtom("<AGE>(a,5.2E20).");
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);
}
Aggregations