Search in sources :

Example 31 with InMemoryAtomSet

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

the class Rules method criticalInstance.

public static InMemoryAtomSet criticalInstance(final Iterable<Rule> rules) {
    InMemoryAtomSet A = new DefaultInMemoryGraphStore();
    criticalInstance(rules, A);
    return A;
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)

Example 32 with InMemoryAtomSet

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

the class RdbmsAtomIterator method hasNext.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean hasNext() throws IteratorException {
    if (!this.hasNextCallDone) {
        this.hasNextCallDone = true;
        while (this.predicateIt.hasNext() && (this.atomIt == null || !this.atomIt.hasNext())) {
            Predicate p = predicateIt.next();
            List<Term> terms = new LinkedList<Term>();
            VariableGenerator gen = new DefaultVariableGenerator("X");
            for (int i = 0; i < p.getArity(); ++i) {
                terms.add(gen.getFreshSymbol());
            }
            InMemoryAtomSet atomSet = new LinkedListAtomSet();
            Atom atom = new DefaultAtom(p, terms);
            atomSet.add(atom);
            ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atomSet);
            SqlHomomorphism solver = SqlHomomorphism.instance();
            try {
                this.atomIt = new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this.store));
            } catch (HomomorphismException e) {
                throw new IteratorException(e);
            }
        }
    }
    return this.atomIt != null && this.atomIt.hasNext();
}
Also used : DefaultVariableGenerator(fr.lirmm.graphik.graal.core.DefaultVariableGenerator) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) SqlHomomorphism(fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism) Term(fr.lirmm.graphik.graal.api.core.Term) SubstitutionIterator2AtomIterator(fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator) LinkedList(java.util.LinkedList) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultVariableGenerator(fr.lirmm.graphik.graal.core.DefaultVariableGenerator) VariableGenerator(fr.lirmm.graphik.graal.api.core.VariableGenerator) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 33 with InMemoryAtomSet

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

the class ConjunctiveQueryTest method emptyQueryTest.

/**
 * Test an empty query that must have an empty substitution
 */
@Theory
public void emptyQueryTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        store.addAll(DlgpParser.parseAtomSet("<P>(a,b), <P>(b,c), <Q>(c,a)."));
        InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
        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 34 with InMemoryAtomSet

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

the class ConjunctiveQueryTest method emptyQueryAndEmptyAtomSetTest.

/**
 * Test an empty query with an empty atomSet that must have an empty
 * substitution
 */
@Theory
public void emptyQueryAndEmptyAtomSetTest(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        InMemoryAtomSet queryAtomSet = new LinkedListAtomSet();
        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 35 with InMemoryAtomSet

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

the class FrontierRestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    Set<Term> fixedVars = substitution.getValues();
    if (ruleIndex.get(rule) == null) {
        ruleIndex.put(rule, _currentRuleIndex++);
    }
    final int index = ruleIndex.get(rule).intValue();
    StringBuilder frontierSb = new StringBuilder();
    SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
    for (Term t : frontierSet) {
        frontierSb.append("_");
        frontierSb.append(t.getLabel());
        frontierSb.append(substitution.createImageOf(t).getLabel());
    }
    String frontier = frontierSb.toString();
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
    }
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Fixed Query:" + query);
    }
    try {
        if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
            return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
        }
    } catch (IteratorException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    return newFacts.iterator();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) TreeSet(java.util.TreeSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

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