Search in sources :

Example 46 with Rule

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

the class BasicChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLICS METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    try {
        this.hasNext = false;
        for (Rule rule : this.ruleSet) {
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().start("saturationTime");
            }
            String key = null;
            if (this.getProfiler().isProfilingEnabled()) {
                key = "Rule " + rule.getLabel() + " application time";
                this.getProfiler().clear(key);
                this.getProfiler().trace(rule.toString());
                this.getProfiler().start(key);
            }
            boolean val = this.getRuleApplier().apply(rule, this.atomSet);
            this.hasNext = this.hasNext || val;
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().stop(key);
            }
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().stop("saturationTime");
            }
        }
    } catch (Exception e) {
        throw new ChaseException("An error occured during saturation step.", e);
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 47 with Rule

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

the class ChaseWithGRD method next.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    Queue<Rule> newQueue = new LinkedList<Rule>();
    List<Atom> newAtomSet = new LinkedList<Atom>();
    try {
        while (!queue.isEmpty()) {
            Rule rule = queue.poll();
            if (rule != null) {
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(rule, this.atomSet);
                if (it.hasNext()) {
                    while (it.hasNext()) {
                        newAtomSet.add(it.next());
                    }
                    for (Rule triggeredRule : this.grd.getTriggeredRules(rule)) {
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("-- -- Dependency: " + triggeredRule);
                        }
                        if (!newQueue.contains(triggeredRule)) {
                            newQueue.add(triggeredRule);
                        }
                    }
                }
                it.close();
            }
        }
        queue = newQueue;
        atomSet.addAll(new CloseableIteratorAdapter<Atom>(newAtomSet.iterator()));
    } catch (Exception e) {
        throw new ChaseException("An error occur pending saturation step.", e);
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 48 with Rule

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

the class SccChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    ++this.level;
    tmpAtom = new LinkedList<Atom>();
    for (Integer scc : layers[level]) {
        Set<Rule> component = this.sccg.getComponent(scc);
        GraphOfRuleDependencies subGraph = this.grd.getSubGraph(component);
        if (component.size() == 1 && !subGraph.hasCircuit()) {
            try {
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(component.iterator().next(), atomSet);
                while (it.hasNext()) {
                    tmpAtom.add(it.next());
                }
                it.close();
            } catch (RuleApplicationException e) {
                throw new ChaseException("", e);
            } catch (IteratorException e) {
                throw new ChaseException("", e);
            }
        } else {
            Chase chase = new ChaseWithGRD<T>(subGraph, atomSet, this.getRuleApplier());
            chase.execute();
        }
    }
    try {
        atomSet.addAll(new CloseableIteratorAdapter<Atom>(tmpAtom.iterator()));
    } catch (AtomSetException e) {
        throw new ChaseException("", e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) AbstractChase(fr.lirmm.graphik.graal.api.forward_chaining.AbstractChase) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)

Example 49 with Rule

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

the class DefaultOntologyTest method testIterator_empty.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.core.ruleset.DefaultOntology#iterator()}.
 */
@Test
public void testIterator_empty() {
    // given
    Ontology onto = new DefaultOntology();
    // when
    Iterator<Rule> it = onto.iterator();
    // then
    Assert.assertFalse(it.hasNext());
}
Also used : Ontology(fr.lirmm.graphik.graal.api.core.Ontology) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Test(org.junit.Test)

Example 50 with Rule

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

the class DefaultOntologyTest method testIterator.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.core.ruleset.DefaultOntology#iterator()}.
 */
@Test
public void testIterator() {
    boolean isThereR1 = false;
    boolean isThereNoName = false;
    // given
    Ontology onto = new DefaultOntology();
    onto.add(r1);
    onto.add(noName);
    // when
    Iterator<Rule> it = onto.iterator();
    while (it.hasNext()) {
        Rule r = it.next();
        if (r == r1 && !isThereR1) {
            isThereR1 = true;
        } else if (r == noName && !isThereNoName) {
            isThereNoName = true;
        } else {
            fail("Not wanted rule: " + r);
        }
    }
    // then
    Assert.assertTrue("isThereR1", isThereR1);
    Assert.assertTrue("isThereNoName", isThereNoName);
}
Also used : Ontology(fr.lirmm.graphik.graal.api.core.Ontology) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Test(org.junit.Test)

Aggregations

Rule (fr.lirmm.graphik.graal.api.core.Rule)141 Test (org.junit.Test)87 Atom (fr.lirmm.graphik.graal.api.core.Atom)64 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)52 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)49 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)26 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)23 LinkedList (java.util.LinkedList)19 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)16 Term (fr.lirmm.graphik.graal.api.core.Term)15 Prefix (fr.lirmm.graphik.util.Prefix)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)9 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)7 KnowledgeBase (fr.lirmm.graphik.graal.api.kb.KnowledgeBase)7 DefaultRule (fr.lirmm.graphik.graal.core.DefaultRule)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6