Search in sources :

Example 1 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class ReasonerAtomicQuery method getMultiUnifier.

/**
 * @throws IllegalArgumentException if passed a {@link ReasonerQuery} that is not a {@link ReasonerAtomicQuery}.
 */
@Override
public MultiUnifier getMultiUnifier(ReasonerQuery p, UnifierComparison unifierType) {
    if (p == this)
        return new MultiUnifierImpl();
    Preconditions.checkArgument(p instanceof ReasonerAtomicQuery);
    ReasonerAtomicQuery parent = (ReasonerAtomicQuery) p;
    MultiUnifier multiUnifier = this.getAtom().getMultiUnifier(parent.getAtom(), unifierType);
    Set<TypeAtom> childTypes = this.getAtom().getTypeConstraints().collect(Collectors.toSet());
    if (childTypes.isEmpty())
        return multiUnifier;
    // get corresponding type unifiers
    Set<TypeAtom> parentTypes = parent.getAtom().getTypeConstraints().collect(Collectors.toSet());
    if (multiUnifier.isEmpty())
        return new MultiUnifierImpl(typeUnifier(childTypes, parentTypes, new UnifierImpl()));
    Set<Unifier> unifiers = multiUnifier.unifiers().stream().map(unifier -> typeUnifier(childTypes, parentTypes, unifier)).collect(Collectors.toSet());
    return new MultiUnifierImpl(unifiers);
}
Also used : QueryAnswerStream.entityTypeFilter(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.entityTypeFilter) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Concept(ai.grakn.concept.Concept) LoggerFactory(org.slf4j.LoggerFactory) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) Iterators(com.google.common.collect.Iterators) HashSet(java.util.HashSet) ReasonerQueryIterator(ai.grakn.graql.internal.reasoner.iterator.ReasonerQueryIterator) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Logger(org.slf4j.Logger) NeqPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.NeqPredicate) Iterator(java.util.Iterator) Conjunction(ai.grakn.graql.admin.Conjunction) AnswerState(ai.grakn.graql.internal.reasoner.state.AnswerState) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) LazyQueryCache(ai.grakn.graql.internal.reasoner.cache.LazyQueryCache) Sets(com.google.common.collect.Sets) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Cache(ai.grakn.graql.internal.reasoner.cache.Cache) Stream(java.util.stream.Stream) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) QueryAnswerStream.knownFilterWithInverse(ai.grakn.graql.internal.reasoner.query.QueryAnswerStream.knownFilterWithInverse) RuleExplanation(ai.grakn.graql.internal.reasoner.explanation.RuleExplanation) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Preconditions(com.google.common.base.Preconditions) AtomicStateProducer(ai.grakn.graql.internal.reasoner.state.AtomicStateProducer) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerUtils.typeUnifier(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typeUnifier) Unifier(ai.grakn.graql.admin.Unifier)

Example 2 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_RepeatingRelationPlayers_withMetaRoles.

@Test
public void testExactUnification_TernaryRelationWithTypes_RepeatingRelationPlayers_withMetaRoles() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa threeRoleEntity;$x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
    String patternString2 = "{$y3 isa threeRoleEntity3;$y1 isa threeRoleEntity;($y2, $y3, $y1) isa ternary;}";
    String patternString3 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier unifier2 = childQuery2.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertTrue(unifier2.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 3 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class AtomicQueryTest method testWhenUnifiyingAtomWithItself_UnifierIsIdentity.

@Test
public void testWhenUnifiyingAtomWithItself_UnifierIsIdentity() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa twoRoleEntity;$x2 isa twoRoleEntity2;($x1, $x2) isa binary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier unifier2 = parentQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier unifier3 = childQuery.getMultiUnifier(childQuery).getUnifier();
    assertTrue(Sets.intersection(unifier.keySet(), Sets.newHashSet(var("x"), var("y"))).isEmpty());
    assertTrue(unifier2.isEmpty());
    assertTrue(unifier3.isEmpty());
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 4 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes.

@Test
public void testExactUnification_TernaryRelationWithTypes_SomeVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa threeRoleEntity;$x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
    String patternString2 = "{$y3 isa threeRoleEntity3;$y1 isa threeRoleEntity;($y2, $y3, $y1) isa ternary;}";
    String patternString3 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier unifier2 = childQuery2.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertTrue(unifier2.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 5 with Unifier

use of ai.grakn.graql.admin.Unifier in project grakn by graknlabs.

the class AtomicQueryTest method testExactUnification_BinaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes.

// only a single unifier exists
@Test
public void testExactUnification_BinaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa twoRoleEntity;$x2 isa twoRoleEntity2;($x1, $x2) isa binary;}";
    String patternString2 = "{$y1 isa twoRoleEntity;$y2 isa twoRoleEntity2;($y1, $y2) isa binary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Aggregations

Unifier (ai.grakn.graql.admin.Unifier)28 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)23 Test (org.junit.Test)18 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)16 Answer (ai.grakn.graql.admin.Answer)13 Var (ai.grakn.graql.Var)11 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)9 InferenceRule (ai.grakn.graql.internal.reasoner.rule.InferenceRule)7 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)7 Concept (ai.grakn.concept.Concept)6 Atomic (ai.grakn.graql.admin.Atomic)6 Conjunction (ai.grakn.graql.admin.Conjunction)6 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)6 RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)6 ResourceAtom (ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom)6 Sets (com.google.common.collect.Sets)6 List (java.util.List)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6