Search in sources :

Example 11 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.

the class BreadthFirstChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLICS METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    this.rulesToCheck = this.nextRulesToCheck;
    this.nextRulesToCheck = new TreeMap<Rule, AtomSet>();
    try {
        if (!this.rulesToCheck.isEmpty()) {
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().start("saturationTime");
            }
            for (Entry<Rule, AtomSet> e : this.rulesToCheck.entrySet()) {
                String key = null;
                Rule rule = e.getKey();
                AtomSet data = e.getValue();
                if (this.getProfiler().isProfilingEnabled()) {
                    key = "Rule " + rule.getLabel() + " application time";
                    this.getProfiler().clear(key);
                    this.getProfiler().trace(rule.toString());
                    this.getProfiler().start(key);
                }
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(rule, data, this.atomSet);
                while (it.hasNext()) {
                    tmpData.add(it.next());
                }
                it.close();
                if (this.getProfiler().isProfilingEnabled()) {
                    this.getProfiler().stop(key);
                }
            }
            this.dispatchNewData(this.tmpData);
            this.atomSet.addAll(new CloseableIteratorAdapter<Atom>(this.tmpData.iterator()));
            this.tmpData.clear();
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().stop("saturationTime");
            }
        }
    } catch (Exception e) {
        throw new ChaseException("An error occured during saturation step.", e);
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)

Example 12 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.

the class BreadthFirstChase method init.

private void init(Iterator<Rule> rules) {
    this.nextRulesToCheck = new TreeMap<Rule, AtomSet>();
    while (rules.hasNext()) {
        Rule r = rules.next();
        this.ruleSet.add(r);
        this.nextRulesToCheck.put(r, atomSet);
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule)

Example 13 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.

the class DefaultKnowledgeBase method fesSaturate.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * Run saturation with a timeout at <code>timeout</code> milliseconds for this thread to die. A timeout of 0 means to wait forever.
 * @param timeout in milliseconds
 * @throws ChaseException
 * @throws TimeoutException
 */
protected void fesSaturate(long timeout) throws ChaseException, TimeoutException {
    if (!isFESSaturated) {
        GraphOfRuleDependencies grd = this.getFESGraphOfRuleDependencies();
        ChaseWithGRD<AtomSet> chase = new ChaseWithGRD<>(grd, this.store);
        chase.setProfiler(this.getProfiler());
        chase.execute(timeout);
        this.isFESSaturated = true;
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) ChaseWithGRD(fr.lirmm.graphik.graal.forward_chaining.ChaseWithGRD) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies)

Example 14 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.

the class DefaultKnowledgeBaseTest method testGetRuleNames.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#getRuleNames()}.
 * @throws ParseException
 */
@Test
public void testGetRuleNames() throws ParseException {
    Rule r1 = DlgpParser.parseRule("[R1] p(x) :- q(X).");
    Rule r2 = DlgpParser.parseRule("[R2] q(x) :- r(X).");
    AtomSet store = new DefaultInMemoryGraphStore();
    RuleSet ruleset = new LinkedListRuleSet();
    ruleset.add(r1);
    ruleset.add(r2);
    KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
    Assert.assertTrue(kb.getRuleNames().contains("R1"));
    Assert.assertTrue(kb.getRuleNames().contains("R2"));
    Assert.assertEquals(r1, kb.getRule("R1"));
    Assert.assertEquals(r2, kb.getRule("R2"));
    kb.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) 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) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Test(org.junit.Test)

Example 15 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet in project graal by graphik-team.

the class DefaultKnowledgeBaseTest method testDefaultKnowledgeBaseAtomSetRuleSet.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#DefaultKnowledgeBase(fr.lirmm.graphik.graal.api.core.AtomSet, fr.lirmm.graphik.graal.api.core.RuleSet)}.
 * @throws AtomSetException
 * @throws ParseException
 */
@Test
public void testDefaultKnowledgeBaseAtomSetRuleSet() throws AtomSetException, ParseException {
    Atom aa = DlgpParser.parseAtom("q(a).");
    Atom ab = DlgpParser.parseAtom("q(b).");
    Atom ac = DlgpParser.parseAtom("q(c).");
    Rule r = DlgpParser.parseRule("[R1] p(x) :- q(X).");
    NegativeConstraint nc = DlgpParser.parseNegativeConstraint("[NC] ! :- q(X), p(X).");
    AtomSet store = new DefaultInMemoryGraphStore();
    store.add(aa);
    store.add(ab);
    store.add(ac);
    RuleSet ruleset = new LinkedListRuleSet();
    ruleset.add(r);
    ruleset.add(nc);
    KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
    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 : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) 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) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Atom(fr.lirmm.graphik.graal.api.core.Atom) NegativeConstraint(fr.lirmm.graphik.graal.api.core.NegativeConstraint) Test(org.junit.Test)

Aggregations

AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)32 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)14 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)13 Test (org.junit.Test)13 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)11 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)11 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)11 Atom (fr.lirmm.graphik.graal.api.core.Atom)9 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)9 Rule (fr.lirmm.graphik.graal.api.core.Rule)7 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)7 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)7 Theory (org.junit.experimental.theories.Theory)7 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)5 Term (fr.lirmm.graphik.graal.api.core.Term)5 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)5 CPUTimeProfiler (fr.lirmm.graphik.util.profiler.CPUTimeProfiler)5 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)4