Search in sources :

Example 1 with UnifierImpl

use of ai.grakn.graql.internal.reasoner.UnifierImpl 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 UnifierImpl

use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.

the class SNBInferenceTest method testQueryConsistency.

/**
 * Tests transitivity and Bug #7416
 */
@Test
public void testQueryConsistency() {
    QueryBuilder iqb = snbGraph.tx().graql().infer(true);
    String queryString = "match $x isa person; $y isa place; ($x, $y) isa resides;" + "$z isa person;$z has name 'Miguel Gonzalez'; ($x, $z) isa knows; get $x, $y;";
    String queryString2 = "match $x isa person; $y isa person;$y has name 'Miguel Gonzalez';" + "$z isa place; ($x, $y) isa knows; ($x, $z) isa resides; get $x, $z;";
    Unifier unifier = new UnifierImpl(ImmutableMap.of(Graql.var("z"), Graql.var("y")));
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute().stream().map(a -> a.unify(unifier)).collect(Collectors.toList());
    assertCollectionsEqual(answers, answers2);
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) ImmutableMap(com.google.common.collect.ImmutableMap) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Graql(ai.grakn.graql.Graql) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Answer(ai.grakn.graql.admin.Answer) GraqlTestUtil.assertQueriesEqual(ai.grakn.util.GraqlTestUtil.assertQueriesEqual) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Rule(org.junit.Rule) SNBKB(ai.grakn.test.kbs.SNBKB) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) Unifier(ai.grakn.graql.admin.Unifier) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) QueryBuilder(ai.grakn.graql.QueryBuilder) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 3 with UnifierImpl

use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.

the class Binary method getUnifier.

@Override
public Unifier getUnifier(Atom parentAtom) {
    if (!(parentAtom instanceof Binary)) {
        throw GraqlQueryException.unificationAtomIncompatibility();
    }
    Multimap<Var, Var> varMappings = HashMultimap.create();
    Var childVarName = this.getVarName();
    Var parentVarName = parentAtom.getVarName();
    Var childPredicateVarName = this.getPredicateVariable();
    Var parentPredicateVarName = parentAtom.getPredicateVariable();
    if (parentVarName.isUserDefinedName() && childVarName.isUserDefinedName() && !childVarName.equals(parentVarName)) {
        varMappings.put(childVarName, parentVarName);
    }
    if (parentPredicateVarName.isUserDefinedName() && childPredicateVarName.isUserDefinedName() && !childPredicateVarName.equals(parentPredicateVarName)) {
        varMappings.put(childPredicateVarName, parentPredicateVarName);
    }
    return new UnifierImpl(varMappings);
}
Also used : Var(ai.grakn.graql.Var) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl)

Example 4 with UnifierImpl

use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.

the class RelationshipAtom method getMultiUnifier.

@Override
public MultiUnifier getMultiUnifier(Atom pAtom, UnifierComparison unifierType) {
    Unifier baseUnifier = super.getUnifier(pAtom);
    Set<Unifier> unifiers = new HashSet<>();
    if (pAtom.isRelation()) {
        // This is safe due to the check above
        assert (pAtom instanceof RelationshipAtom);
        RelationshipAtom parentAtom = (RelationshipAtom) pAtom;
        // this is important for cases like unifying ($r1: $x, $r2: $y) with itself
        if (this.equals(parentAtom) && this.getPartialSubstitutions().collect(toSet()).equals(parentAtom.getPartialSubstitutions().collect(toSet())) && this.getTypeConstraints().collect(toSet()).equals(parentAtom.getTypeConstraints().collect(toSet()))) {
            return new MultiUnifierImpl();
        }
        boolean unifyRoleVariables = parentAtom.getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).anyMatch(rp -> rp.var().isUserDefinedName());
        getRelationPlayerMappings(parentAtom, unifierType).forEach(mappingList -> {
            Multimap<Var, Var> varMappings = HashMultimap.create();
            mappingList.forEach(rpm -> {
                // add role player mapping
                varMappings.put(rpm.getKey().getRolePlayer().var(), rpm.getValue().getRolePlayer().var());
                // add role var mapping if needed
                VarPattern childRolePattern = rpm.getKey().getRole().orElse(null);
                VarPattern parentRolePattern = rpm.getValue().getRole().orElse(null);
                if (parentRolePattern != null && childRolePattern != null && unifyRoleVariables) {
                    varMappings.put(childRolePattern.admin().var(), parentRolePattern.admin().var());
                }
            });
            unifiers.add(baseUnifier.merge(new UnifierImpl(varMappings)));
        });
    } else {
        unifiers.add(baseUnifier);
    }
    return new MultiUnifierImpl(unifiers);
}
Also used : Var(ai.grakn.graql.Var) VarPattern(ai.grakn.graql.VarPattern) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) HashSet(java.util.HashSet)

Example 5 with UnifierImpl

use of ai.grakn.graql.internal.reasoner.UnifierImpl in project grakn by graknlabs.

the class ResourceAtom method getUnifier.

@Override
public Unifier getUnifier(Atom parentAtom) {
    if (!(parentAtom instanceof ResourceAtom)) {
        return new UnifierImpl(ImmutableMap.of(this.getPredicateVariable(), parentAtom.getVarName()));
    }
    Unifier unifier = super.getUnifier(parentAtom);
    ResourceAtom parent = (ResourceAtom) parentAtom;
    // unify relation vars
    Var childRelationVarName = this.getRelationVariable();
    Var parentRelationVarName = parent.getRelationVariable();
    if (parentRelationVarName.isUserDefinedName() && !childRelationVarName.equals(parentRelationVarName)) {
        unifier = unifier.merge(new UnifierImpl(ImmutableMap.of(childRelationVarName, parentRelationVarName)));
    }
    return unifier;
}
Also used : Var(ai.grakn.graql.Var) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Unifier(ai.grakn.graql.admin.Unifier)

Aggregations

UnifierImpl (ai.grakn.graql.internal.reasoner.UnifierImpl)5 Var (ai.grakn.graql.Var)4 Unifier (ai.grakn.graql.admin.Unifier)4 Answer (ai.grakn.graql.admin.Answer)2 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)2 MultiUnifierImpl (ai.grakn.graql.internal.reasoner.MultiUnifierImpl)2 HashSet (java.util.HashSet)2 Concept (ai.grakn.concept.Concept)1 GraqlQueryException (ai.grakn.exception.GraqlQueryException)1 GetQuery (ai.grakn.graql.GetQuery)1 Graql (ai.grakn.graql.Graql)1 QueryBuilder (ai.grakn.graql.QueryBuilder)1 VarPattern (ai.grakn.graql.VarPattern)1 AnswerExplanation (ai.grakn.graql.admin.AnswerExplanation)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)1 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)1 UnifierComparison (ai.grakn.graql.admin.UnifierComparison)1 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)1