Search in sources :

Example 51 with Predicate

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);
    }
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) ParseException(fr.lirmm.graphik.graal.api.io.ParseException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 52 with Predicate

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);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) PureRewriter(fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Term(fr.lirmm.graphik.graal.api.core.Term) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 53 with Predicate

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));
    }
}
Also used : TreeSet(java.util.TreeSet) Term(fr.lirmm.graphik.graal.api.core.Term) Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 54 with Predicate

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;
}
Also used : LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLDataAllValuesFrom(org.semanticweb.owlapi.model.OWLDataAllValuesFrom) OWLObjectMaxCardinality(org.semanticweb.owlapi.model.OWLObjectMaxCardinality) OWLDataRange(org.semanticweb.owlapi.model.OWLDataRange) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) OWLDataMaxCardinality(org.semanticweb.owlapi.model.OWLDataMaxCardinality) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) Pair(org.apache.commons.lang3.tuple.Pair)

Example 55 with Predicate

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;
}
Also used : Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Aggregations

Predicate (fr.lirmm.graphik.graal.api.core.Predicate)77 Atom (fr.lirmm.graphik.graal.api.core.Atom)35 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)28 Term (fr.lirmm.graphik.graal.api.core.Term)27 Test (org.junit.Test)25 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)16 LinkedList (java.util.LinkedList)16 Theory (org.junit.experimental.theories.Theory)14 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)9 Rule (fr.lirmm.graphik.graal.api.core.Rule)9 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)9 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)7 DefaultURI (fr.lirmm.graphik.util.DefaultURI)7 TreeSet (java.util.TreeSet)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)6 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 Pair (org.apache.commons.lang3.tuple.Pair)6