Search in sources :

Example 56 with InMemoryAtomSet

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

the class OWL2ParserTest method assertionWithExistential.

@Test
public void assertionWithExistential() throws OWL2ParserException {
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":A rdf:type owl:Class . " + ":p rdf:type owl:ObjectProperty . " + ":i1 :p [a :A] ." + "");
        int nbFacts = 0;
        int nbAtoms = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof InMemoryAtomSet) {
                ++nbFacts;
                CloseableIterator<Atom> it = ((AtomSet) o).iterator();
                while (it.hasNext()) {
                    it.next();
                    ++nbAtoms;
                }
            }
        }
        parser.close();
        Assert.assertEquals("Number of facts found:", 1, nbFacts);
        Assert.assertEquals("Number of atoms found:", 2, nbAtoms);
    } catch (Throwable e) {
        Assert.fail("An exception was found: " + e);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 57 with InMemoryAtomSet

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

the class JointlyAffectedPositionSet method step2.

/**
 * for each rule and for each variable x that occurs only at affected
 * positions in its body, all positions q[j] in its head where occurs x are
 * affected.
 */
protected void step2() {
    InMemoryAtomSet body;
    boolean isAffected;
    int i;
    CloseableIteratorWithoutException<Atom> atomIt;
    Iterator<Term> termIt;
    Atom a;
    Term t;
    boolean fixPoint = false;
    while (!fixPoint) {
        fixPoint = true;
        for (Rule rule : ruleSet) {
            body = rule.getBody();
            for (Variable term : rule.getBody().getVariables()) {
                isAffected = true;
                atomIt = body.iterator();
                while (atomIt.hasNext() && isAffected) {
                    i = -1;
                    a = atomIt.next();
                    termIt = a.iterator();
                    while (termIt.hasNext() && isAffected) {
                        ++i;
                        t = termIt.next();
                        if (term.equals(t)) {
                            if (!isAffected(a.getPredicate(), i)) {
                                isAffected = false;
                            }
                        }
                    }
                }
                if (isAffected) {
                    if (this.affectInHead(rule, term)) {
                        fixPoint = false;
                    }
                }
            }
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 58 with InMemoryAtomSet

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

the class AbstractRdbmsConjunctiveQueryTranslator method translate.

@Override
public Iterator<SQLQuery> translate(Rule rangeRestrictedRule) throws AtomSetException {
    Collection<SQLQuery> queries = new LinkedList<SQLQuery>();
    InMemoryAtomSet body = rangeRestrictedRule.getBody();
    CloseableIterator<Atom> it = rangeRestrictedRule.getHead().iterator();
    try {
        while (it.hasNext()) {
            Atom headAtom = it.next();
            DBTable table = this.store.createPredicateTableIfNotExist(headAtom.getPredicate());
            List<Term> terms = headAtom.getTerms();
            ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(body, terms);
            SQLQuery selectQuery = this.translate(query);
            if (!selectQuery.hasSchemaError()) {
                queries.add(new SQLQuery(store.getDriver().getInsertOrIgnoreQuery(table, selectQuery.toString())));
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return queries.iterator();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) Term(fr.lirmm.graphik.graal.api.core.Term) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 59 with InMemoryAtomSet

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

the class ConjunctiveQueryWithCompilation method issue34.

@Theory
public void issue34(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
    Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
    HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
    store.add(DlgpParser.parseAtom("<Q>(a,b)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<P>(X,Y) :- <Q>(Y,X)."));
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    InMemoryAtomSet query1 = new LinkedListAtomSet();
    query1.add(DlgpParser.parseAtom("<P>(a,Y)."));
    CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query1), store, comp);
    Assert.assertFalse(results.hasNext());
    results.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismWithCompilation(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation) Theory(org.junit.experimental.theories.Theory)

Example 60 with InMemoryAtomSet

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

the class ConjunctiveQueryFixedBugTest method GraphAtomSetQuery.

/**
 * Query using an DefaultInMemoryGraphAtomSet.
 *
 * @param h
 * @param store
 */
@Theory
public void GraphAtomSetQuery(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        InMemoryAtomSet atomset = new DefaultInMemoryGraphStore();
        atomset.add(DlgpParser.parseAtom("<P>(X)."));
        ConjunctiveQuery query = new DefaultConjunctiveQuery(atomset);
        CloseableIterator<Substitution> subReader;
        subReader = h.execute(query, store);
        Assert.assertFalse(subReader.hasNext());
        subReader.close();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Theory(org.junit.experimental.theories.Theory)

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