use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class BackwardChainingTest method issue35.
@Theory
public void issue35(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
try {
RuleSet rules = new LinkedListRuleSet();
Predicate p = new Predicate("p", 2);
Predicate q = new Predicate("q", 3);
Predicate r = new Predicate("r", 2);
rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- q(X,Y,Y), r(X,Y).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
int i = 0;
while (it.hasNext()) {
ConjunctiveQuery next = it.next();
InMemoryAtomSet atomSet = next.getAtomSet();
Set<Predicate> predicates = atomSet.getPredicates();
Assert.assertTrue(predicates.contains(r));
if (predicates.contains(p)) {
Assert.assertEquals(1, atomSet.getTerms().size());
} else if (predicates.contains(q)) {
Assert.assertEquals(query, next);
} else {
Assert.assertFalse(true);
}
++i;
}
Assert.assertEquals(2, i);
} catch (Exception e) {
Assert.assertFalse("There is an error.", true);
}
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class BackwardChainingTest method getBody1.
/**
* c(X) :- b(X,Y,Y).
*
* getBody([X]) => b(X, Y, Y).
*
* @throws ParseException
*/
@Theory
public void getBody1(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X) :- q(X,Y,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
boolean isFound = false;
while (rewIt.hasNext()) {
ConjunctiveQuery rew = rewIt.next();
CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
if (it.hasNext()) {
Atom a = it.next();
if (a.getPredicate().equals(new Predicate("q", 3))) {
isFound = true;
List<Term> terms = a.getTerms();
Assert.assertEquals(terms.get(1), terms.get(2));
}
}
}
Assert.assertTrue("Rewrite not found", isFound);
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class Rules method criticalInstance.
/**
* The skolem chase on the critical instance of R halts iff the skolem chase of R halts
* universally.
* The critical instance for a set of rules R contains all facts that can be constructed
* using all predicates occuring in R, all constants occurring in the body of a rule in R, and one
* special fresh constant.
*/
public static void criticalInstance(final Iterable<Rule> rules, InMemoryAtomSet A) {
Set<Term> terms = new TreeSet<Term>();
terms.add(GraalConstant.freshConstant());
Set<Predicate> predicates = new TreeSet<Predicate>();
for (Rule r : rules) {
CloseableIteratorWithoutException<Atom> it = r.getBody().iterator();
while (it.hasNext()) {
Atom b = it.next();
predicates.add(b.getPredicate());
for (Term t : b.getTerms()) if (t.isConstant())
terms.add(t);
}
}
for (Predicate p : predicates) {
generateCriticalInstance(A, terms, p, 0, new DefaultAtom(p));
}
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class OWLAxiomParser method mainProcess.
private Iterable<? extends Rule> mainProcess(OWLSubClassOfAxiom arg) {
Collection<Rule> objects = new LinkedList<Rule>();
InMemoryAtomSet body = null;
try {
body = arg.getSubClass().accept(this.classVisitorX);
} catch (UnsupportedConstructor e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("[ " + arg.getSubClass() + "] is not supported as subClass. This axioms was skipped : " + arg);
}
return Collections.emptyList();
}
// RULES
InMemoryAtomSet head = null;
try {
if (arg.getSuperClass() instanceof OWLObjectMaxCardinality) {
OWLObjectMaxCardinality maxCard = (OWLObjectMaxCardinality) arg.getSuperClass();
body.addAll(maxCard.getProperty().accept(this.propertyVisitorXY));
body.addAll(maxCard.getProperty().accept(this.propertyVisitorXZ));
InMemoryAtomSet bodyTemplate = body;
head = GraalUtils.createAtomSet(DefaultAtomFactory.instance().create(Predicate.EQUALITY, glueVarY, glueVarZ));
OWLClassExpression expr = OWLAPIUtils.classExpressionDisjunctiveNormalForm(maxCard.getFiller());
for (Pair<OWLClassExpression, OWLClassExpression> pair : MathUtils.selfCartesianProduct(OWLAPIUtils.getObjectUnionOperands(expr))) {
body = new LinkedListAtomSet(bodyTemplate);
body.addAll(pair.getLeft().accept(classVisitorY));
body.addAll(pair.getRight().accept(classVisitorZ));
objects.add(DefaultRuleFactory.instance().create(body, head));
}
} else if (arg.getSuperClass() instanceof OWLDataMaxCardinality) {
OWLDataMaxCardinality maxCard = (OWLDataMaxCardinality) arg.getSuperClass();
Predicate p = GraalUtils.createPredicate(maxCard.getProperty());
body.add(DefaultAtomFactory.instance().create(p, glueVarX, glueVarY));
body.add(DefaultAtomFactory.instance().create(p, glueVarX, glueVarZ));
InMemoryAtomSet bodyTemplate = body;
head = GraalUtils.createAtomSet(DefaultAtomFactory.instance().create(Predicate.EQUALITY, glueVarY, glueVarZ));
OWLDataRange expr = OWLAPIUtils.dataRangeDisjunctiveNormalForm(maxCard.getFiller());
for (Pair<OWLDataRange, OWLDataRange> pair : MathUtils.selfCartesianProduct(OWLAPIUtils.getDataUnionOperands(expr))) {
body = new LinkedListAtomSet(bodyTemplate);
body.addAll(pair.getLeft().accept(dataRangeVisitorY));
body.addAll(pair.getRight().accept(dataRangeVisitorZ));
objects.add(DefaultRuleFactory.instance().create(body, head));
}
} else if (arg.getSuperClass() instanceof OWLDataAllValuesFrom) {
OWLDataAllValuesFrom allvalues = (OWLDataAllValuesFrom) arg.getSuperClass();
Predicate p = GraalUtils.createPredicate(allvalues.getProperty());
body.add(DefaultAtomFactory.instance().create(p, glueVarX, glueVarY));
head = allvalues.getFiller().accept(dataRangeVisitorY);
objects.add(DefaultRuleFactory.instance().create(body, head));
} else {
head = arg.getSuperClass().accept(this.classVisitorX);
objects.add(DefaultRuleFactory.instance().create(body, head));
}
} catch (UnsupportedConstructor e) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("[ " + e.getConstructor() + "] is not supported here. This axioms was skipped : " + arg);
}
objects = Collections.emptyList();
}
return objects;
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class DlgpWriter method write.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
public DlgpWriter write(Directive d) throws IOException {
switch(d.getType()) {
case UNA:
this.write("@una");
break;
case BASE:
if (!d.getValue().toString().isEmpty()) {
this.base = d.getValue().toString();
this.write("@base ");
this.write('<');
this.write(encode(this.base));
this.write('>');
}
break;
case TOP:
if (d.getValue() instanceof Predicate) {
this.top = (Predicate) d.getValue();
} else {
this.top = new Predicate(d.getValue().toString(), 1);
}
this.write("@top ");
this.writePredicate(this.top);
break;
case COMMENT:
this.write("%% ");
this.write(d.getValue());
break;
}
this.write("\n");
return this;
}
Aggregations