Search in sources :

Example 61 with InMemoryAtomSet

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

the class ConjunctiveQueryTest method booleanQueryTest.

/**
 * Test a boolean query
 */
@Theory
public void booleanQueryTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        store.addAll(DlgpParser.parseAtomSet("<P>(a,b).<P>(b,c).<Q>(a,c).<Q>(d,c)."));
        InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
        queryAtomSet.add(DlgpParser.parseAtom("<Q>(a,c)."));
        ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(queryAtomSet);
        CloseableIterator<Substitution> subReader;
        Substitution sub;
        subReader = h.execute(query, store);
        Assert.assertTrue(subReader.hasNext());
        sub = subReader.next();
        Assert.assertEquals(0, sub.getTerms().size());
        Assert.assertFalse(subReader.hasNext());
        subReader.close();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 62 with InMemoryAtomSet

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

the class ConjunctiveQueryTest method booleanQueryWithoutAnswerTest.

/**
 * Test a boolean query
 */
@Theory
public void booleanQueryWithoutAnswerTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
        queryAtomSet.add(DlgpParser.parseAtom("<Q>(a,c)."));
        ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(queryAtomSet);
        CloseableIterator<Substitution> subReader;
        subReader = h.execute(query, store);
        Assert.assertFalse(subReader.hasNext());
        subReader.close();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Example 63 with InMemoryAtomSet

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

the class StoreTest method bugConcurrentModificationException.

@Theory
public void bugConcurrentModificationException(InMemoryAtomSet store) throws IteratorException, RuleApplicationException, AtomSetException {
    Assume.assumeTrue(store instanceof Store);
    Rule r = DlgpParser.parseRule("<T>(Z,W), <P>(Y,W) :- <P>(X,Y).");
    store.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
    try {
        DefaultRuleApplier<InMemoryAtomSet> applier = new DefaultRuleApplier<InMemoryAtomSet>();
        applier.apply(r, store);
        applier.apply(r, store);
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) RdbmsStore(fr.lirmm.graphik.graal.store.rdbms.RdbmsStore) Store(fr.lirmm.graphik.graal.api.store.Store) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) Rule(fr.lirmm.graphik.graal.api.core.Rule) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) SQLException(java.sql.SQLException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) ParseException(fr.lirmm.graphik.graal.api.io.ParseException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) DefaultRuleApplier(fr.lirmm.graphik.graal.forward_chaining.rule_applier.DefaultRuleApplier) Theory(org.junit.experimental.theories.Theory)

Example 64 with InMemoryAtomSet

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

the class Utils method getSafeCopy.

public static Rule getSafeCopy(Rule rule) {
    Substitution substitution = new TreeMapSubstitution();
    for (Variable t : rule.getVariables()) {
        substitution.put(t, varGen.getFreshSymbol());
    }
    InMemoryAtomSet body = rule.getBody();
    InMemoryAtomSet head = rule.getHead();
    InMemoryAtomSet safeBody = new LinkedListAtomSet();
    InMemoryAtomSet safeHead = new LinkedListAtomSet();
    substitution.apply(body, safeBody);
    substitution.apply(head, safeHead);
    return DefaultRuleFactory.instance().create(safeBody, safeHead);
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 65 with InMemoryAtomSet

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

the class Utils method developpRewriting.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
// /////////////////////////////////////////////////////////////////////////
/**
 * Add in the given rewriting set the rewrites that can be entailed from the
 * predicate order ex: the rewrite A(x) can be entailed from the rewrite
 * B(x) and the predicate order A > B
 *
 * @return a Collection of unfolded rewritings.
 */
private static Collection<ConjunctiveQuery> developpRewriting(Iterable<ConjunctiveQuery> rewritingSet, RulesCompilation compilation) {
    Collection<ConjunctiveQuery> unfoldingRewritingSet = new LinkedList<ConjunctiveQuery>();
    LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesBefore = new LinkedList<Pair<InMemoryAtomSet, Substitution>>();
    LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesAfter = new LinkedList<Pair<InMemoryAtomSet, Substitution>>();
    LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesTmp;
    Iterable<Pair<Atom, Substitution>> atomsRewritings;
    InMemoryAtomSet copy;
    for (ConjunctiveQuery originalQuery : rewritingSet) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        // q = query.getIrredondant(compilation);
        // for all atom of the query we will build a list of all the
        // rewriting
        newQueriesBefore.clear();
        newQueriesBefore.add(new ImmutablePair<InMemoryAtomSet, Substitution>(new LinkedListAtomSet(), DefaultSubstitutionFactory.instance().createSubstitution()));
        // we will build all the possible fact from the rewriting of the
        // atoms
        CloseableIteratorWithoutException<Atom> it = originalQuery.iterator();
        while (!Thread.currentThread().isInterrupted() && it.hasNext()) {
            Atom a = it.next();
            atomsRewritings = compilation.getRewritingOf(a);
            for (Pair<InMemoryAtomSet, Substitution> before : newQueriesBefore) {
                // query and add the atom
                for (Pair<Atom, Substitution> rew : atomsRewritings) {
                    // 
                    copy = new LinkedListAtomSet(before.getLeft());
                    copy.add(rew.getLeft());
                    Substitution newSub = Substitutions.aggregate(before.getRight(), rew.getRight());
                    if (newSub != null) {
                        newQueriesAfter.add(new ImmutablePair<InMemoryAtomSet, Substitution>(copy, newSub));
                    }
                }
            }
            // switch list
            newQueriesTmp = newQueriesBefore;
            newQueriesBefore = newQueriesAfter;
            newQueriesAfter = newQueriesTmp;
            newQueriesAfter.clear();
        }
        for (Pair<InMemoryAtomSet, Substitution> before : newQueriesBefore) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            Substitution s = before.getRight();
            InMemoryAtomSet atomset = before.getLeft();
            atomset = s.createImageOf(atomset);
            List<Term> ans = s.createImageOf(originalQuery.getAnswerVariables());
            unfoldingRewritingSet.add(DefaultConjunctiveQueryFactory.instance().create(atomset, ans));
        }
    }
    return unfoldingRewritingSet;
}
Also used : LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

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