Search in sources :

Example 26 with AtomSet

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

the class ForwardCheckingTest method NFC2Test2.

@Test
public void NFC2Test2() throws HomomorphismException, IteratorException, ParseException {
    Profiler profiler = new CPUTimeProfiler();
    Predicate[] predicates = { new Predicate("p", 2), new Predicate("q", 2), new Predicate("r", 2) };
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    TestUtil.addNAtoms(data, 32, predicates, 5, new Random(0));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(X,Z), r(Y,Z).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
    h.setProfiler(profiler);
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
}
Also used : NFC2(fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Profiler(fr.lirmm.graphik.util.profiler.Profiler) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Random(java.util.Random) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Test(org.junit.Test)

Example 27 with AtomSet

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

the class DefaultKnowledgeBase method saturate.

@Override
public void saturate() throws KnowledgeBaseException {
    if (!this.isSaturated) {
        boolean run = this.approach == Approach.SATURATION_ONLY;
        if (!run) {
            this.analyse();
            run = this.analyse.isFES();
        }
        if (run) {
            GraphOfRuleDependencies grd = this.analysedRuleSet.getGraphOfRuleDependencies();
            ChaseWithGRD<AtomSet> chase = new ChaseWithGRD<>(grd, this.store);
            chase.setProfiler(this.getProfiler());
            try {
                chase.execute();
            } catch (ChaseException e) {
                throw new KnowledgeBaseException(e);
            }
            this.isSaturated = true;
            this.isFESSaturated = true;
            this.isSemiSaturated = true;
        } else {
            throw new KnowledgeBaseException("There is no proof for FES decidability");
        }
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) ChaseWithGRD(fr.lirmm.graphik.graal.forward_chaining.ChaseWithGRD) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 28 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet 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 29 with AtomSet

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

the class OWL2ParserTest method complexAssertionWithExistential.

@Test
public void complexAssertionWithExistential() throws OWL2ParserException {
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":A a owl:Class . " + ":B a owl:Class . " + ":p a owl:ObjectProperty . " + "_:x1 a :A. " + "_:y1 a :B. " + "_:x1 :p _:y1. " + "_:x2 a :A. " + "_:y2 a :B. " + "_:x2 :p _:y2. ");
        int nbFacts = 0;
        int nbAtoms = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof AtomSet) {
                Term a0 = null, b0 = null, p0 = null, p1 = null;
                ++nbFacts;
                CloseableIterator<Atom> it = ((AtomSet) o).iterator();
                while (it.hasNext()) {
                    Atom a = it.next();
                    ++nbAtoms;
                    if (a.getPredicate().equals(P)) {
                        p0 = a.getTerm(0);
                        p1 = a.getTerm(1);
                    } else if (a.getPredicate().equals(A)) {
                        a0 = a.getTerm(0);
                    } else if (a.getPredicate().equals(B)) {
                        b0 = a.getTerm(0);
                    }
                }
                Assert.assertEquals(p0, a0);
                Assert.assertEquals(p1, b0);
            }
        }
        parser.close();
        Assert.assertEquals("Number of facts found:", 2, nbFacts);
        Assert.assertEquals("Number of atoms found:", 6, nbAtoms);
    } catch (Throwable e) {
        Assert.fail("An exception was found: " + e);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 30 with AtomSet

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

the class OWL2ParserTest method example6.

@Test
public void example6() throws OWL2ParserException {
    OWL2Parser parser = new OWL2Parser(PREFIXES + ":A rdf:type owl:Class . " + ":B rdf:type owl:Class . " + ":C rdf:type owl:Class . " + ":D rdf:type owl:Class . " + ":p rdf:type owl:ObjectProperty . " + ":q rdf:type owl:ObjectProperty . " + ":r rdf:type owl:ObjectProperty . " + "[owl:unionOf (" + "		[owl:oneOf ( :i )] " + "		[rdf:type owl:Restriction ;" + "			owl:onProperty :p ;" + "			owl:someValuesFrom :A]" + "	)] " + "	rdfs:subClassOf " + "[owl:intersectionOf ( " + "		[rdf:type owl:Restriction ;" + "			owl:onProperty :q ;" + "			owl:someValuesFrom :B]" + "		[owl:complementOf :C ] " + "		[rdf:type owl:Restriction ;" + "			owl:onProperty :r ;" + "			owl:allValuesFrom :D]" + "	)] . ");
    int nbRules = 0;
    while (parser.hasNext()) {
        Object o = parser.next();
        if (o instanceof Rule || o instanceof AtomSet) {
            ++nbRules;
        }
    }
    parser.close();
    Assert.assertEquals("Number of assertions found:", 6, nbRules);
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) 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