Search in sources :

Example 66 with InMemoryAtomSet

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

the class ConjunctiveQueryWithFixedVariables method computeFixedQuery.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private static InMemoryAtomSet computeFixedQuery(InMemoryAtomSet atomset, Iterable<? extends Term> fixedTerms) {
    // create a Substitution for fixed query
    InMemoryAtomSet fixedQuery = DefaultAtomSetFactory.instance().create();
    Substitution fixSub = DefaultSubstitutionFactory.instance().createSubstitution();
    for (Term t : fixedTerms) {
        if (t.isVariable()) {
            fixSub.put((Variable) t, DefaultTermFactory.instance().createConstant(t.getLabel()));
        }
    }
    // apply substitution
    CloseableIteratorWithoutException<Atom> it = atomset.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        fixedQuery.add(fixSub.createImageOf(a));
    }
    return fixedQuery;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 67 with InMemoryAtomSet

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

the class PureQuery method removeAnswerPredicate.

public static void removeAnswerPredicate(ConjunctiveQuery query) {
    Term[] ans = query.getAnswerVariables().toArray(new Term[query.getAnswerVariables().size()]);
    CloseableIteratorWithoutException<Atom> ita = query.getAtomSet().iterator();
    InMemoryAtomSet toRemove = new LinkedListAtomSet();
    InMemoryAtomSet toAdd = new LinkedListAtomSet();
    while (ita.hasNext()) {
        Atom a = ita.next();
        if (a.getPredicate().equals(ansPredicate)) {
            Term ansTerm = ans[(Integer) ((Literal) a.getTerm(0)).getValue()];
            if (!ansTerm.equals(a.getTerm(1))) {
                toAdd.add(DefaultAtomFactory.instance().create(Predicate.EQUALITY, ansTerm, a.getTerm(1)));
            }
            toRemove.add(a);
        }
    }
    query.getAtomSet().removeAll(toRemove);
    query.getAtomSet().addAll(toAdd);
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 68 with InMemoryAtomSet

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

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

the class AbstractSubstitution method createImageOf.

@Override
public InMemoryAtomSet createImageOf(InMemoryAtomSet src) {
    InMemoryAtomSet dest = DefaultAtomSetFactory.instance().create();
    this.apply(src, dest);
    return dest;
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)

Example 70 with InMemoryAtomSet

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

the class DefaultConjunctiveQueryWithNegatedParts method appendTo.

@Override
public void appendTo(StringBuilder sb) {
    sb.append("ANS(");
    boolean first = true;
    for (Term t : this.responseVariables) {
        if (!first) {
            sb.append(',');
        }
        first = false;
        sb.append(t);
    }
    sb.append(") : ");
    sb.append(this.positiveAtomSet);
    for (InMemoryAtomSet atomset : this.negatedParts) {
        sb.append(", \u22A5(");
        sb.append(atomset);
        sb.append(")");
    }
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term)

Aggregations

InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)122 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)40 Atom (fr.lirmm.graphik.graal.api.core.Atom)38 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)35 Test (org.junit.Test)35 Term (fr.lirmm.graphik.graal.api.core.Term)31 Rule (fr.lirmm.graphik.graal.api.core.Rule)25 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)23 LinkedList (java.util.LinkedList)22 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)21 Variable (fr.lirmm.graphik.graal.api.core.Variable)19 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)10 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)10 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)9 Theory (org.junit.experimental.theories.Theory)9 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)8 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)8 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)8 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)7 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6