Search in sources :

Example 1 with Chase

use of fr.lirmm.graphik.graal.api.forward_chaining.Chase 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 2 with Chase

use of fr.lirmm.graphik.graal.api.forward_chaining.Chase in project graal by graphik-team.

the class StaticChase method executeChase.

public static void executeChase(AtomSet atomSet, DefaultGraphOfRuleDependencies grd) throws ChaseException {
    Chase chase = new SccChase<AtomSet>(grd, atomSet);
    chase.execute();
}
Also used : Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase)

Example 3 with Chase

use of fr.lirmm.graphik.graal.api.forward_chaining.Chase in project graal by graphik-team.

the class StaticChase method executeOneStepChase.

public static void executeOneStepChase(AtomSet atomSet, DefaultGraphOfRuleDependencies grd) throws ChaseException {
    Chase chase = new SccChase<AtomSet>(grd, atomSet);
    chase.next();
}
Also used : Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase)

Example 4 with Chase

use of fr.lirmm.graphik.graal.api.forward_chaining.Chase in project graal by graphik-team.

the class ChaseTest method test.

@Test
public void test() throws ParseException, ChaseException {
    Ontology onto = new DefaultOntology();
    onto.add(DlgpParser.parseRule("p(X) :- q(X)."));
    onto.add(DlgpParser.parseRule("q(X) :- p(X)."));
    InMemoryAtomSet store = new DefaultInMemoryGraphStore();
    store.add(DlgpParser.parseAtom("p(a)."));
    Chase chase = new BreadthFirstChase(onto, store);
    Assert.assertTrue(chase.hasNext());
    chase.next();
    Assert.assertTrue(chase.hasNext());
    chase.next();
    Assert.assertFalse(chase.hasNext());
}
Also used : Ontology(fr.lirmm.graphik.graal.api.core.Ontology) DefaultOntology(fr.lirmm.graphik.graal.core.ruleset.DefaultOntology) BreadthFirstChase(fr.lirmm.graphik.graal.forward_chaining.BreadthFirstChase) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) DefaultOntology(fr.lirmm.graphik.graal.core.ruleset.DefaultOntology) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) BreadthFirstChase(fr.lirmm.graphik.graal.forward_chaining.BreadthFirstChase) Test(org.junit.Test)

Example 5 with Chase

use of fr.lirmm.graphik.graal.api.forward_chaining.Chase in project graal by graphik-team.

the class ChaseTest method restrictedChaseTest0.

@Theory
public void restrictedChaseTest0(AtomSet atomSet) throws AtomSetException, HomomorphismFactoryException, HomomorphismException, ChaseException, IteratorException, ParseException {
    atomSet.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
    LinkedList<Rule> ruleSet = new LinkedList<>();
    ruleSet.add(DlgpParser.parseRule("<Q>(X,Z) :- <P>(X,X)."));
    Chase chase = new BreadthFirstChase(ruleSet, atomSet);
    chase.execute();
    int size = 0;
    for (CloseableIterator<Atom> it = atomSet.iterator(); it.hasNext(); it.next()) {
        ++size;
    }
    Assert.assertEquals(2, size);
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) LinkedList(java.util.LinkedList) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) Atom(fr.lirmm.graphik.graal.api.core.Atom) Theory(org.junit.experimental.theories.Theory)

Aggregations

Chase (fr.lirmm.graphik.graal.api.forward_chaining.Chase)14 Atom (fr.lirmm.graphik.graal.api.core.Atom)5 Rule (fr.lirmm.graphik.graal.api.core.Rule)5 Theory (org.junit.experimental.theories.Theory)5 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)4 LinkedList (java.util.LinkedList)4 DefaultGraphOfRuleDependencies (fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies)3 GraphOfRuleDependencies (fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)2 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)2 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)2 BreadthFirstChase (fr.lirmm.graphik.graal.forward_chaining.BreadthFirstChase)2 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)1 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)1 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 Ontology (fr.lirmm.graphik.graal.api.core.Ontology)1 AbstractChase (fr.lirmm.graphik.graal.api.forward_chaining.AbstractChase)1 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)1 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)1 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)1