Search in sources :

Example 41 with LinkedListAtomSet

use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.

the class HomomorphismTest method test5.

@Test
public void test5() throws HomomorphismException, IteratorException, AtomSetException {
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p(a,b), r(b,b), r(c,c), q(c), q(b)."));
    Variable x = DefaultTermFactory.instance().createVariable("X");
    InMemoryAtomSet positivePart = new LinkedListAtomSet();
    positivePart.addAll(DlgpParser.parseAtomSet("p(a,X),r(W,Y),q(Y)."));
    positivePart.iterator().next().setTerm(1, x);
    InMemoryAtomSet negatedPart = new LinkedListAtomSet();
    negatedPart.addAll(DlgpParser.parseAtomSet("q(X)."));
    negatedPart.iterator().next().setTerm(0, x);
    DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, Collections.singletonList(negatedPart), Collections.<Term>emptyList());
    BCC bcc = new BCC(new GraphBaseBackJumping(), true);
    BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts(bcc.getBCCScheduler(), StarBootstrapper.instance(), new NFC2(), bcc.getBCCBackJumping());
    CloseableIterator<Substitution> res = h.execute(query, data);
    Assert.assertFalse(res.hasNext());
    res.close();
}
Also used : DefaultConjunctiveQueryWithNegatedParts(fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts) Variable(fr.lirmm.graphik.graal.api.core.Variable) BCC(fr.lirmm.graphik.graal.homomorphism.bbc.BCC) NFC2(fr.lirmm.graphik.graal.homomorphism.forward_checking.NFC2) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) GraphBaseBackJumping(fr.lirmm.graphik.graal.homomorphism.backjumping.GraphBaseBackJumping) Test(org.junit.Test)

Example 42 with LinkedListAtomSet

use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.

the class QueryUnifier method aggregate.

/**
 * Returns the aggregation of the given unifier and the receiving unifier
 *
 * @param u
 *            an unifier
 * @return unifier
 */
public QueryUnifier aggregate(QueryUnifier u) {
    // we create a piece that is the union of the two pieces
    InMemoryAtomSet pieces = new LinkedListAtomSet();
    pieces.addAll(getPiece());
    pieces.addAll(u.getPiece());
    // we create a rule that is the aggregation of the two rules
    InMemoryAtomSet b = new LinkedListAtomSet();
    InMemoryAtomSet h = new LinkedListAtomSet();
    CloseableIteratorWithoutException<Atom> it = getRule().getBody().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        b.add(a);
    }
    it = getRule().getHead().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        h.add(a);
    }
    it = u.getRule().getBody().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        b.add(a);
    }
    it = u.getRule().getHead().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        h.add(a);
    }
    Rule rule = DefaultRuleFactory.instance().create(b, h);
    // we create the partition which is the join of the two partitions
    Partition<Term> part = getPartition().join(u.getPartition());
    return new QueryUnifier(pieces, part, rule, getQuery());
}
Also used : InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 43 with LinkedListAtomSet

use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.

the class UnifierUtils method getSinglePieceUnifiersAHR.

/**
 * Returns the list of all single-piece unifier between the given query and
 * the given atomic-head rule cannot work with IDCompilation ( have to
 * conserve the fact that an atom of the query can only been associated by a
 * single unification with an atom of the head
 *
 * <br/>
 * AHR: Atomic Header Rule
 *
 * @param q
 *            the query that we want unify
 * @param r
 *            the atomic-head rule that we want unify
 * @return the ArrayList of all single-piece unifier between the query of
 *         the receiving object and R an atomic-head rule
 */
public static LinkedList<QueryUnifier> getSinglePieceUnifiersAHR(ConjunctiveQuery q, AtomicHeadRule r, RulesCompilation compilation) {
    LinkedList<Atom> unifiableAtoms = getUnifiableAtoms(q, r, compilation);
    LinkedList<QueryUnifier> unifiers = new LinkedList<QueryUnifier>();
    Iterator<Atom> i = unifiableAtoms.iterator();
    while (i.hasNext()) {
        InMemoryAtomSet p = new LinkedListAtomSet();
        Rule tmpRule = getSafeCopy(r);
        AtomicHeadRule copy = new AtomicHeadRule(tmpRule.getBody(), tmpRule.getHead().iterator().next());
        Atom toUnif = i.next();
        p.add(toUnif);
        Partition<Term> partition = new Partition<Term>(toUnif.getTerms(), copy.getHead().getAtom().getTerms());
        // compute separating variable
        LinkedList<Term> sep = AtomSetUtils.sep(p, q.getAtomSet());
        // compute sticky variable
        LinkedList<Term> sticky = TermPartitionUtils.getStickyVariable(partition, sep, copy);
        InMemoryAtomSet pBar = AtomSetUtils.minus(q.getAtomSet(), p);
        while (partition != null && !sticky.isEmpty()) {
            CloseableIteratorWithoutException<Atom> ia = pBar.iterator();
            while (partition != null && ia.hasNext()) {
                Atom a = ia.next();
                Iterator<Term> ix = sticky.iterator();
                while (partition != null && ix.hasNext()) {
                    Term x = ix.next();
                    // all the atoms of Q/P which contain x must be add to P
                    if (a.getTerms().contains(x)) {
                        // isMappable
                        if (compilation.isMappable(a.getPredicate(), copy.getHead().getAtom().getPredicate())) {
                            p.add(a);
                            Partition<Term> part = partition.join(new Partition<Term>(a.getTerms(), copy.getHead().getAtom().getTerms()));
                            if (TermPartitionUtils.isAdmissible(part, copy)) {
                                partition = part;
                            } else
                                partition = null;
                        } else
                            partition = null;
                    }
                }
            }
            if (partition != null) {
                sep = AtomSetUtils.sep(p, q.getAtomSet());
                pBar = AtomSetUtils.minus(q.getAtomSet(), p);
                sticky = TermPartitionUtils.getStickyVariable(partition, sep, copy);
            }
        }
        i.remove();
        if (partition != null) {
            QueryUnifier u = new QueryUnifier(p, partition, copy, q);
            unifiers.add(u);
        }
    }
    return unifiers;
}
Also used : Partition(fr.lirmm.graphik.util.Partition) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule)

Example 44 with LinkedListAtomSet

use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.

the class UnifierUtils method getSinglePieceUnifiersNAHR.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
public static List<QueryUnifier> getSinglePieceUnifiersNAHR(ConjunctiveQuery q, Rule r, RulesCompilation compilation) {
    LinkedList<QueryUnifier> u = new LinkedList<QueryUnifier>();
    Rule ruleCopy = getSafeCopy(r);
    HashMap<Atom, LinkedList<Partition<Term>>> possibleUnification = new HashMap<Atom, LinkedList<Partition<Term>>>();
    // compute possible unification between atoms of Q and head(R)
    CloseableIteratorWithoutException<Atom> it = q.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        CloseableIteratorWithoutException<Atom> it2 = ruleCopy.getHead().iterator();
        while (it2.hasNext()) {
            Atom b = it2.next();
            if (compilation.isMappable(a.getPredicate(), b.getPredicate())) {
                Collection<Partition<Term>> unification = compilation.getUnification(a, b);
                for (Partition<Term> partition : unification) {
                    if (TermPartitionUtils.isAdmissible(partition, ruleCopy)) {
                        if (possibleUnification.get(a) == null)
                            possibleUnification.put(a, new LinkedList<Partition<Term>>());
                        possibleUnification.get(a).add(partition);
                    }
                }
            }
        }
    }
    LinkedList<Atom> atoms = getUnifiableAtoms(q, r, compilation);
    for (Atom a : atoms) {
        LinkedList<Partition<Term>> partitionList = possibleUnification.get(a);
        if (partitionList != null) {
            Iterator<Partition<Term>> i = partitionList.iterator();
            while (i.hasNext()) {
                Partition<Term> unif = i.next();
                InMemoryAtomSet p = new LinkedListAtomSet();
                p.add(a);
                u.addAll(extend(p, unif, possibleUnification, q, ruleCopy));
                i.remove();
            }
        }
    }
    return u;
}
Also used : Partition(fr.lirmm.graphik.util.Partition) HashMap(java.util.HashMap) 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) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule)

Example 45 with LinkedListAtomSet

use of fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet in project graal by graphik-team.

the class UnifierUtils method getSafeCopy.

public static Rule getSafeCopy(Rule rule) {
    Substitution substitution = new FreshVarSubstitution(varGen);
    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 : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) FreshVarSubstitution(fr.lirmm.graphik.graal.core.FreshVarSubstitution) FreshVarSubstitution(fr.lirmm.graphik.graal.core.FreshVarSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)

Aggregations

LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)52 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)35 Atom (fr.lirmm.graphik.graal.api.core.Atom)28 LinkedList (java.util.LinkedList)19 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)17 Term (fr.lirmm.graphik.graal.api.core.Term)17 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)11 Theory (org.junit.experimental.theories.Theory)11 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 Test (org.junit.Test)9 Variable (fr.lirmm.graphik.graal.api.core.Variable)8 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)8 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)7 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)5 SubstitutionIterator2AtomIterator (fr.lirmm.graphik.graal.core.stream.SubstitutionIterator2AtomIterator)5 SqlHomomorphism (fr.lirmm.graphik.graal.store.rdbms.homomorphism.SqlHomomorphism)5 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)4 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)4