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());
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations