Search in sources :

Example 51 with Variable

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

the class AtomicQueryHomomorphismWithNegatedParts method execute.

// /////////////////////////////////////////////////////////////////////////
// HOMOMORPHISM METHODS
// /////////////////////////////////////////////////////////////////////////
public CloseableIterator<Substitution> execute(ConjunctiveQueryWithNegatedParts query, AtomSet data, RulesCompilation compilation) throws HomomorphismException {
    try {
        Atom atom = query.getPositivePart().iterator().next();
        List<Term> ans = query.getAnswerVariables();
        List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
        for (Pair<Atom, Substitution> im : compilation.getRewritingOf(atom)) {
            iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(data.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), ans, im.getRight())));
        }
        CloseableIterator<Substitution> subIt = new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
        // manage negative parts
        Set<Variable> variables = query.getPositivePart().getVariables();
        @SuppressWarnings("rawtypes") Filter[] filters = new Filter[query.getNegatedParts().size()];
        int i = 0;
        for (InMemoryAtomSet negPart : query.getNegatedParts()) {
            Set<Variable> frontier = SetUtils.intersection(variables, negPart.getVariables());
            filters[i++] = new NegFilter(negPart, frontier, data, compilation);
        }
        @SuppressWarnings("unchecked") Filter<Substitution> filter = new AndFilter<Substitution>(filters);
        return new FilterIterator<Substitution, Substitution>(subIt, filter);
    } catch (AtomSetException e) {
        throw new HomomorphismException(e);
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) CloseableIteratorAggregator(fr.lirmm.graphik.util.stream.CloseableIteratorAggregator) FilterIterator(fr.lirmm.graphik.util.stream.filter.FilterIterator) ConverterCloseableIterator(fr.lirmm.graphik.util.stream.converter.ConverterCloseableIterator) CloseableIterator(fr.lirmm.graphik.util.stream.CloseableIterator) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) AndFilter(fr.lirmm.graphik.util.stream.filter.AndFilter) Filter(fr.lirmm.graphik.util.stream.filter.Filter) AndFilter(fr.lirmm.graphik.util.stream.filter.AndFilter) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)

Example 52 with Variable

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

the class FrontierRestrictedChaseHaltingConditionTest method test2.

@Test
public void test2() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtomSet("p(a,b), p(b,a)."));
    Rule rule = DlgpParser.parseRule("p(X,Z):-p(X,Y).");
    Variable x = DefaultTermFactory.instance().createVariable("X");
    Variable y = DefaultTermFactory.instance().createVariable("Y");
    Constant a = DefaultTermFactory.instance().createConstant("a");
    Constant b = DefaultTermFactory.instance().createConstant("b");
    Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
    s.put(x, a);
    s.put(y, b);
    FrontierRestrictedChaseHaltingCondition condition = new FrontierRestrictedChaseHaltingCondition();
    CloseableIterator<Atom> toAdd = condition.apply(rule, s, atomset);
    Assert.assertTrue(toAdd.hasNext());
    Atom atom1 = toAdd.next();
    atomset.add(atom1);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
    s = DefaultSubstitutionFactory.instance().createSubstitution();
    s.put(x, b);
    s.put(y, a);
    toAdd = condition.apply(rule, s, atomset);
    Assert.assertTrue(toAdd.hasNext());
    Atom atom2 = toAdd.next();
    atomset.add(atom2);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
    Assert.assertNotEquals(atom1.getTerm(1), atom2.getTerm(1));
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 53 with Variable

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

the class BacktrackIterator method solutionFound.

private Substitution solutionFound(List<Term> ans) {
    Substitution s = new HashMapSubstitution();
    for (Term t : ans) {
        if (t instanceof Variable) {
            Integer idx = this.data.index.get((Variable) t);
            Var v = this.vars[idx];
            s.put(v.shared.value, v.image);
        }
    }
    return s;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) Term(fr.lirmm.graphik.graal.api.core.Term) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution)

Example 54 with Variable

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

the class RecursiveBacktrackHomomorphism method exist.

@Override
public boolean exist(ConjunctiveQuery query, AtomSet data) throws HomomorphismException {
    try {
        InMemoryAtomSet atomSet1 = query.getAtomSet();
        List<Variable> orderedVars = order(atomSet1.getVariables());
        Collection<Atom>[] queryAtomRanked = getAtomRank(atomSet1, orderedVars);
        if (isHomomorphism(queryAtomRanked[0], data, new HashMapSubstitution())) {
            return existHomomorphism(atomSet1, queryAtomRanked, data, new HashMapSubstitution(), orderedVars, 1);
        } else {
            return false;
        }
    } catch (Exception e) {
        throw new HomomorphismException(e.getMessage(), e);
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Collection(java.util.Collection) HashMapSubstitution(fr.lirmm.graphik.graal.core.HashMapSubstitution) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) CloseableIterableWithoutException(fr.lirmm.graphik.util.stream.CloseableIterableWithoutException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)

Example 55 with Variable

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

the class RestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, substitution.createImageOf(rule.getFrontier()));
    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);
    } catch (BacktrackException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    // replace variables by fresh symbol
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, data.getFreshSymbolGenerator().getFreshSymbol());
    }
    CloseableIteratorWithoutException<Atom> it = substitution.createImageOf(rule.getHead()).iterator();
    return it;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Variable(fr.lirmm.graphik.graal.api.core.Variable) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException)

Aggregations

Variable (fr.lirmm.graphik.graal.api.core.Variable)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)33 Term (fr.lirmm.graphik.graal.api.core.Term)32 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)25 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)19 LinkedList (java.util.LinkedList)15 Test (org.junit.Test)15 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)8 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)8 Constant (fr.lirmm.graphik.graal.api.core.Constant)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)4 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)4 HashMapSubstitution (fr.lirmm.graphik.graal.core.HashMapSubstitution)4 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)4