Search in sources :

Example 51 with Substitution

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

the class IDConditionTest method homomorphismIssue35Test.

/**
 * Given an IDCondition[(x,y) -> (x,y,x)] then homomorphism([u,v,w], [a,b])
 * return (u->a, v->b, w->a)
 */
@Test
public void homomorphismIssue35Test() {
    // Assume.assumeTrue(false); // FIXME #35
    Term[] body = { X, Y };
    Term[] head = { X, Y, X };
    IDCondition cond = createCondition(body, head);
    Term[] query = { U, V, W };
    Term[] data = { A, B };
    Substitution h = cond.homomorphism(Arrays.asList(query), Arrays.asList(data));
    Assert.assertEquals(A, h.createImageOf(U));
    Assert.assertEquals(B, h.createImageOf(V));
    Assert.assertEquals(A, h.createImageOf(W));
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 52 with Substitution

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

the class IDConditionTest method homomorphismFailTest1.

/**
 * Given an IDCondition[(x,y) -> (x,y)] then homomorphism([x,x], [a,b])
 * return null
 */
@Test
public void homomorphismFailTest1() {
    Term[] body = { X, Y };
    Term[] head = { X, Y };
    IDCondition cond = createCondition(body, head);
    Term[] query = { U, U };
    Term[] data = { A, B };
    Substitution h = cond.homomorphism(Arrays.asList(query), Arrays.asList(data));
    Assert.assertNull(h);
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 53 with Substitution

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

the class IDConditionTest method generateBodyTest5.

/**
 * h(X,Y,X,Y) :- b(X,Y).
 *
 * generateBody([a,b,X,Y]) => [a, b].
 */
@Test
public void generateBodyTest5() {
    Term[] body1 = { X, Y };
    Term[] head1 = { X, Y, X, Y };
    IDCondition cond = createCondition(body1, head1);
    Term[] head = { A, B, X, Y };
    Term[] expected = { A, B };
    Pair<List<Term>, Substitution> ret = cond.generateBody(Arrays.asList(head));
    List<Term> computed = ret.getLeft();
    Substitution s = ret.getRight();
    Assert.assertEquals(Arrays.asList(expected), computed);
    Assert.assertEquals(A, s.createImageOf(X));
    Assert.assertEquals(B, s.createImageOf(Y));
    Assert.assertEquals(2, s.getTerms().size());
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) List(java.util.List) Term(fr.lirmm.graphik.graal.api.core.Term) Test(org.junit.Test)

Example 54 with Substitution

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

the class AbstractHomomorphism method exist.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean exist(T1 q, T2 a) throws HomomorphismException {
    CloseableIterator<Substitution> results = this.execute(q, a);
    boolean val;
    try {
        val = results.hasNext();
    } catch (IteratorException e) {
        throw new HomomorphismException(e);
    }
    results.close();
    return val;
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution)

Example 55 with Substitution

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

the class FrontierRestrictedChaseHaltingConditionTest method test.

@Test
public void test() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtom("p(a,b)."));
    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, a);
    s.put(y, atom1.getTerm(1));
    toAdd = condition.apply(rule, s, atomset);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
}
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)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)158 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)58 Test (org.junit.Test)55 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)46 Theory (org.junit.experimental.theories.Theory)44 Atom (fr.lirmm.graphik.graal.api.core.Atom)41 Term (fr.lirmm.graphik.graal.api.core.Term)36 LinkedList (java.util.LinkedList)27 Variable (fr.lirmm.graphik.graal.api.core.Variable)25 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)24 Rule (fr.lirmm.graphik.graal.api.core.Rule)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)18 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)16 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)15 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)13 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)13