Search in sources :

Example 56 with Substitution

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

the class IDConditionImpl method homomorphism.

@Override
public Substitution homomorphism(List<Term> head, List<Term> to) {
    if (!checkBody(to)) {
        return null;
    }
    Pair<List<Term>, Substitution> ret = this.generateBody(head);
    if (ret == null) {
        return null;
    }
    Substitution s = ret.getRight();
    Substitution homo = DefaultSubstitutionFactory.instance().createSubstitution();
    List<Term> generatedBody = ret.getLeft();
    // check for a simple homomorphism from generated body into 'to'
    Iterator<Term> itFrom = generatedBody.iterator();
    Iterator<Term> itTo = to.iterator();
    while (itFrom.hasNext() && itTo.hasNext()) {
        Term termFrom = itFrom.next();
        Term termTo = itTo.next();
        if (termFrom.isConstant()) {
            if (!termFrom.equals(termTo)) {
                return null;
            }
        } else {
            if (!homo.put((Variable) termFrom, termTo)) {
                return null;
            }
        }
    }
    if (itFrom.hasNext() || itTo.hasNext()) {
        throw new Error("Wrong term number");
    }
    // homo
    for (Variable t : s.getTerms()) {
        homo.put(t, homo.createImageOf(s.createImageOf(t)));
    }
    return homo;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) MethodNotImplementedError(fr.lirmm.graphik.util.MethodNotImplementedError) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 57 with Substitution

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

the class SubstitutionTest method aggregateTest1.

@Theory
public void aggregateTest1(Substitution s1, Substitution s2) {
    s1.put(Y, A);
    s1.put(Z, A);
    s2.put(X, Y);
    s2.put(Y, Z);
    Substitution aggregation = s1.aggregate(s2);
    Assert.assertNotNull(aggregation);
    Assert.assertEquals(A, aggregation.createImageOf(X));
    Assert.assertEquals(A, aggregation.createImageOf(Y));
    Assert.assertEquals(A, aggregation.createImageOf(Z));
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Theory(org.junit.experimental.theories.Theory)

Example 58 with Substitution

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

the class SubstitutionTest method aggregateImpossibleTest2.

@Theory
public void aggregateImpossibleTest2(Substitution s1, Substitution s2) {
    s1.put(X, Z);
    s1.put(Y, Z);
    s2.put(X, A);
    s2.put(Y, B);
    Substitution aggregation = s1.aggregate(s2);
    Assert.assertNull(aggregation);
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Theory(org.junit.experimental.theories.Theory)

Example 59 with Substitution

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

the class SubstitutionTest method compositionTest1.

@Theory
public void compositionTest1(Substitution s1, Substitution s2) {
    s1.put(X, Y);
    s1.put(S, T);
    s2.put(Y, Z);
    s2.put(U, V);
    Substitution comp = s2.compose(s1);
    System.out.println(comp);
    Assert.assertEquals(Z, comp.createImageOf(X));
    Assert.assertEquals(Z, comp.createImageOf(Y));
    Assert.assertEquals(Z, comp.createImageOf(Z));
    Assert.assertEquals(T, comp.createImageOf(S));
    Assert.assertEquals(V, comp.createImageOf(U));
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Theory(org.junit.experimental.theories.Theory)

Example 60 with Substitution

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

the class SubstitutionTest method aggregateImpossibleTest1.

@Theory
public void aggregateImpossibleTest1(Substitution s1, Substitution s2) {
    s1.put(X, A);
    s2.put(X, B);
    Substitution aggregation = s1.aggregate(s2);
    Assert.assertNull(aggregation);
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Theory(org.junit.experimental.theories.Theory)

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