Search in sources :

Example 6 with RelationshipAtom

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());
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Test(org.junit.Test)

Example 7 with RelationshipAtom

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());
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Test(org.junit.Test)

Example 8 with RelationshipAtom

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()));
}
Also used : Role(ai.grakn.concept.Role) Var(ai.grakn.graql.Var) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Test(org.junit.Test)

Example 9 with RelationshipAtom

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);
}
Also used : Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)

Example 10 with RelationshipAtom

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

RelationshipAtom (ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom)15 Test (org.junit.Test)13 Role (ai.grakn.concept.Role)10 Var (ai.grakn.graql.Var)10 RelationshipType (ai.grakn.concept.RelationshipType)1 Type (ai.grakn.concept.Type)1 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)1 Unifier (ai.grakn.graql.admin.Unifier)1 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)1 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)1 ResourceAtom (ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom)1 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)1 InferenceRule (ai.grakn.graql.internal.reasoner.rule.InferenceRule)1