use of ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom in project grakn by graknlabs.
the class AtomicTest method testRuleApplicability_AmbiguousRoleMapping_RolePlayerTypeMismatch.
@Test
public void testRuleApplicability_AmbiguousRoleMapping_RolePlayerTypeMismatch() {
EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
// although singleRoleEntity plays only one role it can also play an implicit role of the resource so mapping ambiguous
String relationString = "{($x, $y, $z);$x isa singleRoleEntity; $y isa twoRoleEntity; $z isa threeRoleEntity;}";
RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role"), var("x"), graph.getRole("role"), var("y"), graph.getRole("role"), var("z"));
assertEquals(roleMap, roleSetMap(relation.getRoleVarMap()));
assertEquals(2, relation.getApplicableRules().count());
}
use of ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom in project grakn by graknlabs.
the class AtomicTest method testRuleApplicability_WithWildcard_MissingMappings.
@Test
public void testRuleApplicability_WithWildcard_MissingMappings() {
EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
// although singleRoleEntity plays only one role it can also play an implicit role of the resource so mapping ambiguous
String relationString = "{($x, $y, $z);$y isa singleRoleEntity; $z isa singleRoleEntity;}";
RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role"), var("x"), graph.getRole("role"), var("y"), graph.getRole("role"), var("z"));
assertEquals(roleMap, roleSetMap(relation.getRoleVarMap()));
assertThat(relation.getApplicableRules().collect(toSet()), empty());
}
use of ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom in project grakn by graknlabs.
the class AtomicTest method testRoleInference_WithMetaType.
// entity1 plays role1 but entity2 plays roles role1, role2 hence ambiguous and metarole has to be assigned, EXPECTED TO CHANGE WITH CARDINALITY CONSTRAINTS
@Test
public void testRoleInference_WithMetaType() {
EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
String relationString = "{($x, $y, $z) isa ternary;$x isa singleRoleEntity; $y isa twoRoleEntity; $z isa entity;}";
RelationshipAtom relation = (RelationshipAtom) ReasonerQueries.atomic(conjunction(relationString, graph), graph).getAtom();
ImmutableSetMultimap<Role, Var> roleMap = ImmutableSetMultimap.of(graph.getRole("role1"), var("x"), graph.getRole("role"), var("y"), graph.getRole("role"), var("z"));
assertEquals(roleMap, roleSetMap(relation.getRoleVarMap()));
}
use of ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom in project grakn by graknlabs.
the class TypeInferenceQueryTest method typeInference.
private void typeInference(List<RelationshipType> possibleTypes, String pattern, String subbedPattern, EmbeddedGraknTx<?> graph) {
ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
ReasonerAtomicQuery subbedQuery = ReasonerQueries.atomic(conjunction(subbedPattern, graph), graph);
RelationshipAtom atom = (RelationshipAtom) query.getAtom();
RelationshipAtom subbedAtom = (RelationshipAtom) subbedQuery.getAtom();
List<Type> relationshipTypes = atom.inferPossibleTypes(new QueryAnswer());
List<Type> subbedRelationshipTypes = subbedAtom.inferPossibleTypes(new QueryAnswer());
if (possibleTypes.size() == 1) {
assertEquals(possibleTypes, relationshipTypes);
assertEquals(relationshipTypes, subbedRelationshipTypes);
assertEquals(atom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
assertEquals(subbedAtom.getSchemaConcept(), Iterables.getOnlyElement(possibleTypes));
} else {
assertTrue(CollectionUtils.isEqualCollection(possibleTypes, relationshipTypes));
assertTrue(CollectionUtils.isEqualCollection(relationshipTypes, subbedRelationshipTypes));
assertEquals(atom.getSchemaConcept(), null);
assertEquals(subbedAtom.getSchemaConcept(), null);
}
typeInferenceQueries(possibleTypes, pattern, graph);
typeInferenceQueries(possibleTypes, subbedPattern, graph);
}
use of ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom 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