Search in sources :

Example 71 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class AbstractSubstitution method createImageOf.

@Override
public Rule createImageOf(Rule rule) {
    Rule substitut = DefaultRuleFactory.instance().create();
    this.apply(rule.getBody(), substitut.getBody());
    this.apply(rule.getHead(), substitut.getHead());
    return substitut;
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule)

Example 72 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class AggregAllRulesOperator method getRewritesFrom.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * Returns the rewrites compute from the given fact and the rule set of the
 * receiving object.
 *
 * @param q
 *            A fact
 * @return the ArrayList that contains the rewrites compute from the given
 *         fact and the rule set of the receiving object.
 */
@Override
public Collection<ConjunctiveQuery> getRewritesFrom(ConjunctiveQuery q, IndexedByHeadPredicatesRuleSet ruleSet, RulesCompilation compilation) {
    LinkedList<ConjunctiveQuery> rewriteSet = new LinkedList<ConjunctiveQuery>();
    List<QueryUnifier> unifiers;
    for (Rule r : getUnifiableRules(q.getAtomSet().predicatesIterator(), ruleSet, compilation)) {
        unifiers = getSinglePieceUnifiers(q, r, compilation);
        for (QueryUnifier u : unifiers) {
            rewriteSet.add(Utils.rewriteWithMark(q, u));
        }
    }
    return rewriteSet;
}
Also used : QueryUnifier(fr.lirmm.graphik.graal.core.unifier.QueryUnifier) Rule(fr.lirmm.graphik.graal.api.core.Rule) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) LinkedList(java.util.LinkedList)

Example 73 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class OWLAxiomParser method equivalentPropertiesAxiom.

private Iterable<? extends Object> equivalentPropertiesAxiom(Iterable<? extends OWLPropertyExpression> properties) {
    Collection<Rule> rules = GraalUtils.<Rule>createCollection();
    InMemoryAtomSet a1, a2;
    Iterator<? extends OWLPropertyExpression> it1, it2;
    it1 = properties.iterator();
    while (it1.hasNext()) {
        OWLPropertyExpression propExpr = (OWLPropertyExpression) it1.next();
        a1 = propExpr.accept(propertyVisitorXY);
        it1.remove();
        it2 = properties.iterator();
        while (it2.hasNext()) {
            OWLPropertyExpression next = (OWLPropertyExpression) it2.next();
            a2 = next.accept(propertyVisitorXY);
            rules.add(DefaultRuleFactory.instance().create(a1, a2));
            rules.add(DefaultRuleFactory.instance().create(a2, a1));
        }
    }
    return rules;
}
Also used : OWLPropertyExpression(org.semanticweb.owlapi.model.OWLPropertyExpression) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) SWRLRule(org.semanticweb.owlapi.model.SWRLRule)

Example 74 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule 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 75 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class SparqlRuleParser method execute.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void execute(String sparqlQuery) {
    Query sparql = QueryFactory.create(sparqlQuery);
    this.prefixes = new LinkedList<Prefix>();
    for (Map.Entry<String, String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) {
        this.prefixes.add(new Prefix(e.getKey(), e.getValue()));
    }
    Rule rule = new DefaultRule();
    if (sparql.isConstructType()) {
        Template template = sparql.getConstructTemplate();
        for (Triple triple : template.getTriples()) {
            rule.getHead().add(SparqlUtils.triple2Atom(triple));
        }
    }
    ElementVisitorImpl visitor = new ElementVisitorImpl(rule.getBody());
    sparql.getQueryPattern().visit(visitor);
    this.rule = rule;
}
Also used : DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Triple(com.hp.hpl.jena.graph.Triple) Query(com.hp.hpl.jena.query.Query) Prefix(fr.lirmm.graphik.util.Prefix) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Map(java.util.Map) Template(com.hp.hpl.jena.sparql.syntax.Template)

Aggregations

Rule (fr.lirmm.graphik.graal.api.core.Rule)141 Test (org.junit.Test)87 Atom (fr.lirmm.graphik.graal.api.core.Atom)64 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)52 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)49 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)26 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)23 LinkedList (java.util.LinkedList)19 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)16 Term (fr.lirmm.graphik.graal.api.core.Term)15 Prefix (fr.lirmm.graphik.util.Prefix)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)9 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)7 KnowledgeBase (fr.lirmm.graphik.graal.api.kb.KnowledgeBase)7 DefaultRule (fr.lirmm.graphik.graal.core.DefaultRule)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6