Search in sources :

Example 6 with CloseableIteratorWithoutException

use of fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException in project graal by graphik-team.

the class DefaultKnowledgeBase method query.

@Override
public CloseableIterator<Substitution> query(Query query) throws KnowledgeBaseException {
    if (this.isSaturated) {
        try {
            return SmartHomomorphism.instance().execute(query, this.store);
        } catch (HomomorphismException e) {
            throw new KnowledgeBaseException(e);
        }
    } else if (query instanceof ConjunctiveQuery) {
        ConjunctiveQuery cq = (ConjunctiveQuery) query;
        this.analyse();
        if (this.analyse.isDecidable() && (!this.getApproach().equals(Approach.REWRITING_ONLY) || this.getFESRuleSet().isEmpty()) && (!this.getApproach().equals(Approach.SATURATION_ONLY) || this.getFUSRuleSet().isEmpty())) {
            try {
                this.fesSaturate();
                this.compileRule();
                RuleSet fusRuleSet = this.getFUSRuleSet();
                PureRewriter pure = new PureRewriter(false);
                CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation);
                UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                CloseableIterator<Substitution> resultIt = null;
                try {
                    if (this.isSemiSaturated) {
                        resultIt = SmartHomomorphism.instance().execute(query, this.store);
                    } else {
                        resultIt = SmartHomomorphism.instance().execute(ucq, this.store, this.ruleCompilation);
                    }
                } catch (HomomorphismException e) {
                    if (this.getApproach().equals(Approach.REWRITING_FIRST)) {
                        it = PureRewriter.unfold(ucq, this.ruleCompilation);
                        ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
                    } else {
                        this.semiSaturate();
                    }
                    resultIt = SmartHomomorphism.instance().execute(ucq, this.store);
                }
                return new FilterIterator<Substitution, Substitution>(resultIt, new UniqFilter<Substitution>());
            } catch (ChaseException e) {
                throw new KnowledgeBaseException(e);
            } catch (HomomorphismException e) {
                throw new KnowledgeBaseException(e);
            }
        } else {
            throw new KnowledgeBaseException("No decidable combinaison found with the defined approach: " + this.getApproach());
        }
    } else {
        throw new KnowledgeBaseException("No implementation found for this kind of query: " + query.getClass());
    }
}
Also used : DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) KnowledgeBaseException(fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException) UniqFilter(fr.lirmm.graphik.util.stream.filter.UniqFilter) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) DefaultUnionOfConjunctiveQueries(fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries) UnionOfConjunctiveQueries(fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 7 with CloseableIteratorWithoutException

use of fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException in project graal by graphik-team.

the class OWL2ParserTest method irreflexiveObjectPropertyOf.

@Test
public void irreflexiveObjectPropertyOf() {
    // ! :- p(X,X) .
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:IrreflexiveProperty . ");
        int nbRules = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof DefaultNegativeConstraint) {
                ++nbRules;
                Rule r = (Rule) o;
                CloseableIteratorWithoutException<Atom> it = r.getBody().iterator();
                Atom b = it.next();
                Assert.assertFalse(it.hasNext());
                Assert.assertEquals(P, b.getPredicate());
                Assert.assertEquals(b.getTerm(0), b.getTerm(1));
            }
        }
        parser.close();
        Assert.assertEquals("Number of assertions found:", 1, nbRules);
    } catch (Exception e) {
        Assert.assertFalse(e.getMessage(), true);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) OWL2ParserException(fr.lirmm.graphik.graal.io.owl.OWL2ParserException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Test(org.junit.Test)

Example 8 with CloseableIteratorWithoutException

use of fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException in project graal by graphik-team.

the class OWL2ParserTest method transitiveObjectPropertyOf.

@Test
public void transitiveObjectPropertyOf() {
    // p(X, Z) :- p(X, Y), p(Y, Z).
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:TransitiveProperty . ");
        int nbRules = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof Rule) {
                ++nbRules;
                Rule r = (Rule) o;
                CloseableIteratorWithoutException<Atom> it = r.getBody().iterator();
                Atom b1 = (Atom) it.next();
                Atom b2 = (Atom) it.next();
                Assert.assertFalse(it.hasNext());
                it = r.getHead().iterator();
                Atom h = (Atom) it.next();
                Assert.assertFalse(it.hasNext());
                Assert.assertEquals(P, b1.getPredicate());
                Assert.assertEquals(P, b2.getPredicate());
                Assert.assertEquals(P, h.getPredicate());
                Assert.assertTrue(b1.getTerm(0).equals(b2.getTerm(1)) || b1.getTerm(1).equals(b2.getTerm(0)));
                Assert.assertTrue(h.getTerm(0).equals(b1.getTerm(0)) || h.getTerm(0).equals(b2.getTerm(0)));
                Assert.assertTrue(h.getTerm(1).equals(b1.getTerm(1)) || h.getTerm(1).equals(b2.getTerm(1)));
            }
        }
        parser.close();
        Assert.assertEquals("Number of assertions found:", 1, nbRules);
    } catch (Exception e) {
        Assert.assertFalse(e.getMessage(), true);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) OWL2ParserException(fr.lirmm.graphik.graal.io.owl.OWL2ParserException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Test(org.junit.Test)

Example 9 with CloseableIteratorWithoutException

use of fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException in project graal by graphik-team.

the class OWL2ParserTest method functionalObjectProperty.

@Test
public void functionalObjectProperty() {
    // Y = Z :- p(X, Y), p(X, Z).
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:ObjectProperty ; rdf:type owl:FunctionalProperty . ");
        int nbRules = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof Rule) {
                ++nbRules;
                Rule r = (Rule) o;
                CloseableIteratorWithoutException<Atom> it = r.getBody().iterator();
                Atom b1 = it.next();
                Atom b2 = it.next();
                Assert.assertFalse(it.hasNext());
                it = r.getHead().iterator();
                Atom h = it.next();
                Assert.assertFalse(it.hasNext());
                Assert.assertEquals(P, b1.getPredicate());
                Assert.assertEquals(P, b2.getPredicate());
                Assert.assertEquals(Predicate.EQUALITY, h.getPredicate());
                Assert.assertEquals(b1.getTerm(0), b2.getTerm(0));
                Assert.assertTrue(b1.getTerm(1).equals(h.getTerm(0)) || b1.getTerm(1).equals(h.getTerm(1)));
                Assert.assertTrue(b2.getTerm(1).equals(h.getTerm(0)) || b2.getTerm(1).equals(h.getTerm(1)));
            }
        }
        parser.close();
        Assert.assertEquals("Number of assertions found:", 1, nbRules);
    } catch (Exception e) {
        Assert.assertFalse(e.getMessage(), true);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) OWL2ParserException(fr.lirmm.graphik.graal.io.owl.OWL2ParserException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Test(org.junit.Test)

Example 10 with CloseableIteratorWithoutException

use of fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException in project graal by graphik-team.

the class OWL2ParserTest method asymetricObjectPropertyOf.

@Test
public void asymetricObjectPropertyOf() {
    // ! :- q(X, Y), q(X, Y).
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:AsymmetricProperty . ");
        int nbRules = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof Rule) {
                ++nbRules;
                Rule r = (Rule) o;
                CloseableIteratorWithoutException<Atom> it = r.getBody().iterator();
                Atom a1 = (Atom) it.next();
                Atom a2 = (Atom) it.next();
                Assert.assertFalse(it.hasNext());
                Assert.assertEquals(P, a1.getPredicate());
                Assert.assertEquals(P, a2.getPredicate());
                Assert.assertEquals(a1.getTerm(0), a2.getTerm(1));
                Assert.assertEquals(a1.getTerm(1), a2.getTerm(0));
            }
        }
        parser.close();
        Assert.assertEquals("Number of assertions found:", 1, nbRules);
    } catch (Exception e) {
        Assert.assertFalse(e.getMessage(), true);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) OWL2ParserException(fr.lirmm.graphik.graal.io.owl.OWL2ParserException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Test(org.junit.Test)

Aggregations

CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)10 Atom (fr.lirmm.graphik.graal.api.core.Atom)8 Rule (fr.lirmm.graphik.graal.api.core.Rule)8 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)8 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)8 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)8 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)8 Test (org.junit.Test)8 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)2 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)2 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)2 UnionOfConjunctiveQueries (fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries)2 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)2 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)2 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)2 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)2 DefaultUnionOfConjunctiveQueries (fr.lirmm.graphik.graal.core.DefaultUnionOfConjunctiveQueries)2 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)2 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)2 CloseableIterator (fr.lirmm.graphik.util.stream.CloseableIterator)2