Search in sources :

Example 26 with DefaultInMemoryGraphStore

use of fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore in project graal by graphik-team.

the class DefaultStoreFactory method create.

@Override
public Store create(AtomSet src) throws IteratorException {
    DefaultInMemoryGraphStore atomset = this.create();
    CloseableIterator<Atom> it = src.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        atomset.add(a);
    }
    return atomset;
}
Also used : DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 27 with DefaultInMemoryGraphStore

use of fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore in project graal by graphik-team.

the class DefaultKnowledgeBaseTest method testDefaultKnowledgeBaseAtomSetParserOfObject.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#DefaultKnowledgeBase(fr.lirmm.graphik.graal.api.core.AtomSet, fr.lirmm.graphik.graal.api.io.Parser)}.
 * @throws ParseException
 * @throws AtomSetException
 */
@Test
public void testDefaultKnowledgeBaseAtomSetParserOfObject() throws ParseException, AtomSetException {
    Atom aa = DlgpParser.parseAtom("q(a).");
    Atom ab = DlgpParser.parseAtom("q(b).");
    Atom ac = DlgpParser.parseAtom("q(c).");
    Rule r = DlgpParser.parseRule("[R] p(X) :- q(X).");
    NegativeConstraint nc = DlgpParser.parseNegativeConstraint("[NC] ! :- q(X), p(X).");
    AtomSet store = new DefaultInMemoryGraphStore();
    store.add(aa);
    KnowledgeBase kb = new DefaultKnowledgeBase(store, new DlgpParser("[R] p(X) :- q(X). q(b). q(c). [NC] ! :- q(X), p(X)."));
    Assert.assertTrue(kb.getOntology().contains(r));
    Assert.assertTrue(kb.getOntology().contains(nc));
    Assert.assertTrue(kb.getFacts().contains(aa));
    Assert.assertTrue(kb.getFacts().contains(ab));
    Assert.assertTrue(kb.getFacts().contains(ac));
    kb.close();
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) DlgpParser(fr.lirmm.graphik.graal.io.dlp.DlgpParser) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) Atom(fr.lirmm.graphik.graal.api.core.Atom) NegativeConstraint(fr.lirmm.graphik.graal.api.core.NegativeConstraint) Test(org.junit.Test)

Example 28 with DefaultInMemoryGraphStore

use of fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore 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 29 with DefaultInMemoryGraphStore

use of fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore in project graal by graphik-team.

the class BreadthFirstChase method dispatchNewData.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE CLASS
// /////////////////////////////////////////////////////////////////////////
protected void dispatchNewData(Collection<Atom> newData) throws ChaseException {
    for (Atom a : newData) {
        Predicate p = a.getPredicate();
        for (Rule r : ruleSet.getRulesByBodyPredicate(p)) {
            if (linearRuleCheck(r)) {
                AtomSet set = nextRulesToCheck.get(r);
                if (set == null) {
                    set = new DefaultInMemoryGraphStore();
                    nextRulesToCheck.put(r, set);
                }
                try {
                    set.add(a);
                } catch (AtomSetException e) {
                    throw new ChaseException("Exception while adding data into a tmp store", e);
                }
            } else {
                nextRulesToCheck.put(r, atomSet);
            }
        }
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Aggregations

DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)29 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)21 Test (org.junit.Test)21 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)18 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)11 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)11 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)10 Atom (fr.lirmm.graphik.graal.api.core.Atom)8 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)8 Rule (fr.lirmm.graphik.graal.api.core.Rule)5 NFC2 (fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2)5 CPUTimeProfiler (fr.lirmm.graphik.util.profiler.CPUTimeProfiler)5 LinkedList (java.util.LinkedList)5 Variable (fr.lirmm.graphik.graal.api.core.Variable)4 KnowledgeBase (fr.lirmm.graphik.graal.api.kb.KnowledgeBase)4 Profiler (fr.lirmm.graphik.util.profiler.Profiler)4 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)3 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)3 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)3