Search in sources :

Example 36 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class GeoInferenceTest method testTransitiveQuery_withSpecificResource_noRoles.

@Test
public void testTransitiveQuery_withSpecificResource_noRoles() {
    GraknTx graph = geoKB.tx();
    QueryBuilder iqb = graph.graql().infer(true);
    Concept masovia = getConcept(graph, "name", "Masovia");
    String queryString = "match " + "($x, $y) isa is-located-in;" + "$y has name 'Masovia'; get;";
    String queryString2 = "match " + "{(geo-entity: $x, entity-location: $y) isa is-located-in or " + "(geo-entity: $y, entity-location: $x) isa is-located-in;};" + "$y has name 'Masovia'; get;";
    List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
    answers.forEach(ans -> assertEquals(ans.size(), 2));
    answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
    assertEquals(answers.size(), 5);
    List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
    assertCollectionsEqual(answers, answers2);
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 37 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class ReasoningTests method reasoningWithLimitHigherThanNumberOfResults_ReturnsConsistentResults.

// Expected result: The same set of results is always returned
@Test
public void reasoningWithLimitHigherThanNumberOfResults_ReturnsConsistentResults() {
    QueryBuilder qb = testSet23.tx().graql().infer(true);
    String queryString = "match (friend1:$x1, friend2:$x2) isa knows-trans;limit 60; get;";
    List<Answer> oldAnswers = qb.<GetQuery>parse(queryString).execute();
    for (int i = 0; i < 5; i++) {
        List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
        assertEquals(answers.size(), 6);
        assertTrue(answers.containsAll(oldAnswers));
        assertTrue(oldAnswers.containsAll(answers));
    }
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 38 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class ReasoningTests method reasoningWithReifiedRelations.

// tests if partial substitutions are propagated correctly - atom disjointness may lead to variable loss (bug #15476)
// Expected result: 2 relations obtained by correctly finding reified relations
@Test
public void reasoningWithReifiedRelations() {
    QueryBuilder qb = testSet26.tx().graql().infer(true);
    String queryString = "match (role1: $x1, role2: $x2) isa relation2; get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    assertEquals(answers.size(), 2);
    String queryString2 = "match " + "$b isa entity2;" + "$b has res1 'value';" + "$rel1 has res2 'value1';" + "$rel1 (role1: $p, role2: $b) isa relation1;" + "$rel2 has res2 'value2';" + "$rel2 (role1: $c, role2: $b) isa relation1; get;";
    List<Answer> answers2 = qb.<GetQuery>parse(queryString2).execute();
    assertEquals(answers2.size(), 2);
    Set<Var> vars = Sets.newHashSet(var("b"), var("p"), var("c"), var("rel1"), var("rel2"));
    answers2.forEach(ans -> assertTrue(ans.vars().containsAll(vars)));
}
Also used : Answer(ai.grakn.graql.admin.Answer) Var(ai.grakn.graql.Var) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 39 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class ReasoningTests method duplicatesNotProducedWhenResolvingNonResolvableConjunctionsWithoutType.

// Expected result: number of answers equal to specified limit (no duplicates produced)
@Test
public void duplicatesNotProducedWhenResolvingNonResolvableConjunctionsWithoutType() {
    QueryBuilder qb = testSet28.tx().graql().infer(true);
    String queryString = "match " + "(role1: $x, role2: $y);" + "(role1: $y, role2: $z);" + "(role3: $z, role4: $w) isa relation3;" + "limit 3; get;";
    assertEquals(qb.<GetQuery>parse(queryString).execute().size(), 3);
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 40 with QueryBuilder

use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.

the class ReasoningTests method instanceTypeHierarchyRespected_queryOverwritesTypes_recursiveRule.

// Expected result: Single answer obtained only if the rule query containing super type is correctly executed.
@Test
public void instanceTypeHierarchyRespected_queryOverwritesTypes_recursiveRule() {
    QueryBuilder qb = testSet19recursive.tx().graql().infer(true);
    String queryString = "match " + "$x isa subEntity1;" + "$y isa entity1;" + "(role1: $x, role2: $y) isa relation1;";
    String queryString2 = queryString + "$y has name 'a';";
    List<Answer> answers = qb.<GetQuery>parse(queryString + " get;").execute();
    assertEquals(answers.size(), 2);
    List<Answer> answers2 = qb.<GetQuery>parse(queryString2 + " get;").execute();
    assertEquals(answers2.size(), 2);
}
Also used : Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

QueryBuilder (ai.grakn.graql.QueryBuilder)208 Test (org.junit.Test)202 Answer (ai.grakn.graql.admin.Answer)101 GetQuery (ai.grakn.graql.GetQuery)60 GraknTx (ai.grakn.GraknTx)51 SampleKBContext (ai.grakn.test.rule.SampleKBContext)20 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)18 Concept (ai.grakn.concept.Concept)16 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)9 Ignore (org.junit.Ignore)8 Var (ai.grakn.graql.Var)5 VarPattern (ai.grakn.graql.VarPattern)5 List (java.util.List)5 Label (ai.grakn.concept.Label)4 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)4 Sets (com.google.common.collect.Sets)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 RelationshipType (ai.grakn.concept.RelationshipType)3 Graql (ai.grakn.graql.Graql)3 Graql.var (ai.grakn.graql.Graql.var)3