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);
}
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)));
}
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);
}
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")));
}
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"));
}
Aggregations