use of fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException in project graal by graphik-team.
the class OWL2ParserTest method disjointProperty.
@Test
public void disjointProperty() {
// ! :- q(X, Y), p(X, Y).
try {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:ObjectProperty . " + ":q rdf:type owl:ObjectProperty . " + ":p owl:propertyDisjointWith :q .");
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.assertTrue(P.equals(a1.getPredicate()) || Q.equals(a1.getPredicate()));
Assert.assertTrue(P.equals(a2.getPredicate()) || Q.equals(a2.getPredicate()));
Assert.assertTrue(P.equals(a2.getPredicate()) || P.equals(a1.getPredicate()));
Assert.assertTrue(Q.equals(a2.getPredicate()) || Q.equals(a1.getPredicate()));
Assert.assertEquals(a1.getTerm(0), a2.getTerm(0));
Assert.assertEquals(a1.getTerm(1), a2.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 inverseFunctionalObjectProperty.
@Test
public void inverseFunctionalObjectProperty() {
// Y = Z :- p(Y, X), p(Z, X).
try {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:InverseFunctionalProperty . ");
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(1), b2.getTerm(1));
Assert.assertTrue(b1.getTerm(0).equals(h.getTerm(0)) || b1.getTerm(0).equals(h.getTerm(1)));
Assert.assertTrue(b2.getTerm(0).equals(h.getTerm(0)) || b2.getTerm(0).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 reflexiveObjectPropertyOf.
@Test
public void reflexiveObjectPropertyOf() {
// p(X,X) :- Top(X).
try {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:ReflexiveProperty . ");
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 b = it.next();
Assert.assertFalse(it.hasNext());
it = r.getHead().iterator();
Atom h = it.next();
Assert.assertFalse(it.hasNext());
Assert.assertEquals(Predicate.TOP, b.getPredicate());
Assert.assertEquals(P, h.getPredicate());
Assert.assertEquals(b.getTerm(0), h.getTerm(0));
Assert.assertEquals(b.getTerm(0), 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 symetricObjectPropertyOf.
@Test
public void symetricObjectPropertyOf() {
// p(X, Y) :- p(Y, X).
try {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:SymmetricProperty . ");
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 b = (Atom) it.next();
Assert.assertFalse(it.hasNext());
it = r.getHead().iterator();
Atom h = (Atom) it.next();
Assert.assertFalse(it.hasNext());
Assert.assertEquals(P, b.getPredicate());
Assert.assertEquals(P, h.getPredicate());
Assert.assertNotEquals(b.getTerm(0), b.getTerm(1));
Assert.assertEquals(b.getTerm(0), h.getTerm(1));
Assert.assertEquals(b.getTerm(1), h.getTerm(0));
}
}
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 DefaultKnowledgeBase method query.
@Override
public CloseableIterator<Substitution> query(Query query, long timeout) throws KnowledgeBaseException, TimeoutException {
long remainingTime = timeout;
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;
if (this.approach != Approach.REWRITING_ONLY && this.approach != Approach.SATURATION_ONLY) {
this.analyse();
}
if (this.approach == Approach.REWRITING_ONLY || this.approach == Approach.SATURATION_ONLY || this.analyse.isDecidable()) {
try {
long time = System.currentTimeMillis();
this.fesSaturate(remainingTime);
if (timeout > 0) {
remainingTime -= (System.currentTimeMillis() - time);
if (remainingTime <= 0) {
throw new TimeoutException(timeout);
}
}
this.compileRule();
RuleSet fusRuleSet = this.getFUSRuleSet();
PureRewriter pure = new PureRewriter(false);
CloseableIteratorWithoutException<ConjunctiveQuery> it = pure.execute(cq, fusRuleSet, this.ruleCompilation, remainingTime);
UnionOfConjunctiveQueries ucq = new DefaultUnionOfConjunctiveQueries(cq.getAnswerVariables(), it);
CloseableIterator<Substitution> resultIt = null;
try {
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());
}
}
Aggregations