Search in sources :

Example 6 with DefaultConjunctiveQuery

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));
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList)

Example 7 with DefaultConjunctiveQuery

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);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) HomomorphismFactoryException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) HomomorphismFactoryException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismFactoryException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)

Example 8 with DefaultConjunctiveQuery

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");
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Variable(fr.lirmm.graphik.graal.api.core.Variable) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) PatternScheduler(fr.lirmm.graphik.graal.homomorphism.scheduler.PatternScheduler)

Example 9 with DefaultConjunctiveQuery

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();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismWithCompilation(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation) Theory(org.junit.experimental.theories.Theory)

Example 10 with DefaultConjunctiveQuery

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);
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Aggregations

DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)15 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)11 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)10 Theory (org.junit.experimental.theories.Theory)9 Atom (fr.lirmm.graphik.graal.api.core.Atom)8 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)6 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)5 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)5 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)5 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)5 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)4 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)3 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)3 LinkedList (java.util.LinkedList)3 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)2 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)2 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)2 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)2 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)2 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)2