Search in sources :

Example 6 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testExactUnification_BinaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes.

// only a single unifier exists
@Test
public void testExactUnification_BinaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa twoRoleEntity;$x2 isa twoRoleEntity2;($x1, $x2) isa binary;}";
    String patternString2 = "{$y1 isa twoRoleEntity;$y2 isa twoRoleEntity2;($y1, $y2) isa binary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2")));
    assertTrue(unifier.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 7 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testWhenCreatingQueryWithNonexistentType_ExceptionIsThrown.

@Test
public void testWhenCreatingQueryWithNonexistentType_ExceptionIsThrown() {
    EmbeddedGraknTx<?> graph = unificationTestSet.tx();
    String patternString = "{$x isa someType;}";
    exception.expect(GraqlQueryException.class);
    ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(patternString, graph), graph);
}
Also used : ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 8 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes.

@Test
public void testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String patternString = "{$x1 isa threeRoleEntity;$x2 isa threeRoleEntity2; $x3 isa threeRoleEntity3;($x1, $x2, $x3) isa ternary;}";
    String patternString2 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;($y2, $y3, $y1) isa ternary;}";
    String patternString3 = "{$y3 isa threeRoleEntity3;$y2 isa threeRoleEntity2;$y1 isa threeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(patternString2, graph);
    Conjunction<VarPatternAdmin> pattern3 = conjunction(patternString3, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
    Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
    Unifier unifier2 = childQuery2.getMultiUnifier(parentQuery).getUnifier();
    Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertTrue(unifier2.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 9 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes_TypeHierarchyInvolved.

// subSubThreeRoleEntity sub subThreeRoleEntity sub threeRoleEntity3
@Test
public void testExactUnification_TernaryRelationWithTypes_AllVarsHaveTypes_UnifierMatchesTypes_TypeHierarchyInvolved() {
    EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
    String parentString = "{$x1 isa threeRoleEntity;$x2 isa subThreeRoleEntity; $x3 isa subSubThreeRoleEntity;($x1, $x2, $x3) isa ternary;}";
    String childString = "{$y1 isa threeRoleEntity;$y2 isa subThreeRoleEntity;$y3 isa subSubThreeRoleEntity;($y2, $y3, $y1) isa ternary;}";
    String childString2 = "{$y1 isa threeRoleEntity;$y2 isa subThreeRoleEntity;$y3 isa subSubThreeRoleEntity;(role2: $y2, role3: $y3, role1: $y1) isa ternary;}";
    Conjunction<VarPatternAdmin> pattern = conjunction(parentString, graph);
    Conjunction<VarPatternAdmin> pattern2 = conjunction(childString, graph);
    Conjunction<VarPatternAdmin> pattern3 = conjunction(childString2, graph);
    ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(pattern, graph);
    ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(pattern2, graph);
    ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(pattern3, graph);
    MultiUnifier unifier = childQuery.getMultiUnifier(parentQuery);
    MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
    MultiUnifier correctUnifier = new MultiUnifierImpl(ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x2"), var("y3"), var("x3")), ImmutableMultimap.of(var("y1"), var("x1"), var("y2"), var("x3"), var("y3"), var("x2")), ImmutableMultimap.of(var("y1"), var("x2"), var("y2"), var("x1"), var("y3"), var("x3")), ImmutableMultimap.of(var("y1"), var("x2"), var("y2"), var("x3"), var("y3"), var("x1")), ImmutableMultimap.of(var("y1"), var("x3"), var("y2"), var("x1"), var("y3"), var("x2")), ImmutableMultimap.of(var("y1"), var("x3"), var("y2"), var("x2"), var("y3"), var("x1")));
    assertTrue(unifier.containsAll(correctUnifier));
    assertTrue(unifier2.containsAll(correctUnifier));
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Example 10 with ReasonerAtomicQuery

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

the class AtomicQueryTest method testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred.

@Test
public void testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred() {
    EmbeddedGraknTx<?> graph = materialisationTestSet.tx();
    QueryBuilder qb = graph.graql().infer(false);
    Concept firstEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity1; get;").execute()).get("x");
    Concept secondEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity2; get;").execute()).get("x");
    ReasonerAtomicQuery relationQuery = ReasonerQueries.atomic(conjunction("{" + "$r (role1: $x, role2: $y);" + "$x id " + firstEntity.getId().getValue() + ";" + "$y id " + secondEntity.getId().getValue() + ";" + "}", graph), graph);
    assertEquals(relationQuery.materialise(new QueryAnswer()).findFirst().orElse(null).get("r").asRelationship().isInferred(), true);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryBuilder(ai.grakn.graql.QueryBuilder) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Test(org.junit.Test)

Aggregations

ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)43 Test (org.junit.Test)35 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 Unifier (ai.grakn.graql.admin.Unifier)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 Answer (ai.grakn.graql.admin.Answer)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)17 QueryCache (ai.grakn.graql.internal.reasoner.cache.QueryCache)8 Concept (ai.grakn.concept.Concept)7 Conjunction (ai.grakn.graql.admin.Conjunction)7 Patterns (ai.grakn.graql.internal.pattern.Patterns)7 LazyQueryCache (ai.grakn.graql.internal.reasoner.cache.LazyQueryCache)7 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)7 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)7 Set (java.util.Set)7 Collectors.toSet (java.util.stream.Collectors.toSet)7 GetQuery (ai.grakn.graql.GetQuery)6 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)6 QueryAnswers (ai.grakn.graql.internal.reasoner.query.QueryAnswers)6 SampleKBContext (ai.grakn.test.rule.SampleKBContext)6