Search in sources :

Example 11 with Unifier

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

the class InferenceRule method propagateConstraints.

/**
 * @param parentAtom atom containing constraints (parent)
 * @param unifier unifier unifying parent with the rule
 * @return rule with propagated constraints from parent
 */
public InferenceRule propagateConstraints(Atom parentAtom, Unifier unifier) {
    if (!parentAtom.isRelation() && !parentAtom.isResource())
        return this;
    Atom headAtom = head.getAtom();
    Set<Atomic> bodyAtoms = new HashSet<>(body.getAtoms());
    // transfer value predicates
    parentAtom.getPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).forEach(bodyAtoms::add);
    // if head is a resource merge vps into head
    if (headAtom.isResource() && ((ResourceAtom) headAtom).getMultiPredicate().isEmpty()) {
        ResourceAtom resourceHead = (ResourceAtom) headAtom;
        Set<ValuePredicate> innerVps = parentAtom.getInnerPredicates(ValuePredicate.class).flatMap(vp -> vp.unify(unifier).stream()).peek(bodyAtoms::add).collect(toSet());
        headAtom = ResourceAtom.create(headAtom.getPattern(), headAtom.getPredicateVariable(), resourceHead.getRelationVariable(), resourceHead.getTypeId(), innerVps, headAtom.getParentQuery());
    }
    Set<TypeAtom> unifiedTypes = parentAtom.getTypeConstraints().flatMap(type -> type.unify(unifier).stream()).collect(toSet());
    // set rule body types to sub types of combined query+rule types
    Set<TypeAtom> ruleTypes = body.getAtoms(TypeAtom.class).filter(t -> !t.isRelation()).collect(toSet());
    Set<TypeAtom> allTypes = Sets.union(unifiedTypes, ruleTypes);
    allTypes.stream().filter(ta -> {
        SchemaConcept schemaConcept = ta.getSchemaConcept();
        SchemaConcept subType = allTypes.stream().map(Atom::getSchemaConcept).filter(Objects::nonNull).filter(t -> ReasonerUtils.supers(t).contains(schemaConcept)).findFirst().orElse(null);
        return schemaConcept == null || subType == null;
    }).forEach(t -> bodyAtoms.add(t.copy(body)));
    return new InferenceRule(ReasonerQueries.atomic(headAtom), ReasonerQueries.create(bodyAtoms, tx), ruleId, tx);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryStateBase(ai.grakn.graql.internal.reasoner.state.QueryStateBase) Rule(ai.grakn.concept.Rule) ResolutionState(ai.grakn.graql.internal.reasoner.state.ResolutionState) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) QueryCache(ai.grakn.graql.internal.reasoner.cache.QueryCache) Patterns(ai.grakn.graql.internal.pattern.Patterns) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Conjunction(ai.grakn.graql.admin.Conjunction) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) RuleState(ai.grakn.graql.internal.reasoner.state.RuleState) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) Atomic(ai.grakn.graql.admin.Atomic) SchemaConcept(ai.grakn.concept.SchemaConcept) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Objects(java.util.Objects) HashSet(java.util.HashSet)

Example 12 with Unifier

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

the class AtomicTest method testUnification_MatchAllParentAtom.

@Test
public void testUnification_MatchAllParentAtom() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String parentString = "{$r($a, $x);}";
    String childString = "{$rel (role1: $z, role2: $b) isa binary;}";
    Atom parent = ReasonerQueries.atomic(conjunction(parentString, graph), graph).getAtom();
    Atom child = ReasonerQueries.atomic(conjunction(childString, graph), graph).getAtom();
    MultiUnifier multiUnifier = child.getMultiUnifier(parent, UnifierType.RULE);
    Unifier correctUnifier = new UnifierImpl(ImmutableMap.of(var("z"), var("a"), var("b"), var("x"), child.getVarName(), parent.getVarName()));
    Unifier correctUnifier2 = new UnifierImpl(ImmutableMap.of(var("z"), var("x"), var("b"), var("a"), child.getVarName(), parent.getVarName()));
    assertEquals(multiUnifier.size(), 2);
    multiUnifier.forEach(u -> assertTrue(u.containsAll(correctUnifier) || u.containsAll(correctUnifier2)));
}
Also used : MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 13 with Unifier

use of ai.grakn.graql.admin.Unifier 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 14 with Unifier

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

the class AtomicTest method testUnification_ResourceWithIndirectValuePredicate.

@Test
public void testUnification_ResourceWithIndirectValuePredicate() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String resource = "{$x has resource $r;$r val 'f';}";
    String resource2 = "{$r has resource $x;$x val 'f';}";
    String resource3 = "{$r has resource 'f';}";
    ReasonerAtomicQuery resourceQuery = ReasonerQueries.atomic(conjunction(resource, graph), graph);
    ReasonerAtomicQuery resourceQuery2 = ReasonerQueries.atomic(conjunction(resource2, graph), graph);
    ReasonerAtomicQuery resourceQuery3 = ReasonerQueries.atomic(conjunction(resource3, graph), graph);
    String type = "{$x isa resource;$x id '" + resourceQuery.getQuery().execute().iterator().next().get("r").getId().getValue() + "';}";
    ReasonerAtomicQuery typeQuery = ReasonerQueries.atomic(conjunction(type, graph), graph);
    Atom typeAtom = typeQuery.getAtom();
    Atom resourceAtom = resourceQuery.getAtom();
    Atom resourceAtom2 = resourceQuery2.getAtom();
    Atom resourceAtom3 = resourceQuery3.getAtom();
    Unifier unifier = resourceAtom.getUnifier(typeAtom);
    Unifier unifier2 = resourceAtom2.getUnifier(typeAtom);
    Unifier unifier3 = resourceAtom3.getUnifier(typeAtom);
    Answer typeAnswer = typeQuery.getQuery().execute().iterator().next();
    Answer resourceAnswer = resourceQuery.getQuery().execute().iterator().next();
    Answer resourceAnswer2 = resourceQuery2.getQuery().execute().iterator().next();
    Answer resourceAnswer3 = resourceQuery3.getQuery().execute().iterator().next();
    assertEquals(typeAnswer.get(var("x")), resourceAnswer.unify(unifier).get(var("x")));
    assertEquals(typeAnswer.get(var("x")), resourceAnswer2.unify(unifier2).get(var("x")));
    assertEquals(typeAnswer.get(var("x")), resourceAnswer3.unify(unifier3).get(var("x")));
}
Also used : Answer(ai.grakn.graql.admin.Answer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 15 with Unifier

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

the class AtomicTest method testRewriteAndUnification.

@Test
public void testRewriteAndUnification() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String parentString = "{$r (subRole1: $x) isa binary;}";
    Atom parentAtom = ReasonerQueries.atomic(conjunction(parentString, graph), graph).getAtom();
    Var parentVarName = parentAtom.getVarName();
    String childPatternString = "(subRole1: $x, subRole2: $y) isa binary";
    InferenceRule testRule = new InferenceRule(graph.putRule("Checking Rewrite & Unification", graph.graql().parser().parsePattern(childPatternString), graph.graql().parser().parsePattern(childPatternString)), graph).rewrite(parentAtom);
    RelationshipAtom headAtom = (RelationshipAtom) testRule.getHead().getAtom();
    Var headVarName = headAtom.getVarName();
    Unifier unifier = Iterables.getOnlyElement(testRule.getMultiUnifier(parentAtom));
    Unifier correctUnifier = new UnifierImpl(ImmutableMap.of(var("x"), var("x"), headVarName, parentVarName));
    assertTrue(unifier.containsAll(correctUnifier));
    Multimap<Role, Var> roleMap = roleSetMap(headAtom.getRoleVarMap());
    Collection<Var> wifeEntry = roleMap.get(graph.getRole("subRole1"));
    assertEquals(wifeEntry.size(), 1);
    assertEquals(wifeEntry.iterator().next(), var("x"));
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) 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