Search in sources :

Example 96 with GetQuery

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

the class QueryParserTest method testVariablesEverywhereQuery.

@Test
public void testVariablesEverywhereQuery() {
    GetQuery expected = match(var().rel(var("p"), "x").rel("y"), var("x").isa(var("z")), var("y").val("crime"), var("z").sub("production"), label("has-genre").relates(var("p"))).get();
    GetQuery parsed = parse("match" + "($p: $x, $y);" + "$x isa $z;" + "$y val 'crime';" + "$z sub production;" + "has-genre relates $p; get;");
    assertEquals(expected, parsed);
}
Also used : GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 97 with GetQuery

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

the class QueryParserTest method testMoviesReleasedAfterOrAtTheSameTimeAsSpy.

@Test
public void testMoviesReleasedAfterOrAtTheSameTimeAsSpy() {
    GetQuery expected = match(var("x").has("release-date", gte(var("r"))), var().has("title", "Spy").has("release-date", var("r"))).get();
    GetQuery parsed = parse("match $x has release-date >= $r; has title 'Spy', has release-date $r; get;");
    assertEquals(expected, parsed);
}
Also used : GetQuery(ai.grakn.graql.GetQuery) Test(org.junit.Test)

Example 98 with GetQuery

use of ai.grakn.graql.GetQuery 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);
}
Also used : QueryBuilder(ai.grakn.graql.QueryBuilder) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) ImmutableMap(com.google.common.collect.ImmutableMap) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Graql(ai.grakn.graql.Graql) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Answer(ai.grakn.graql.admin.Answer) GraqlTestUtil.assertQueriesEqual(ai.grakn.util.GraqlTestUtil.assertQueriesEqual) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) List(java.util.List) Rule(org.junit.Rule) SNBKB(ai.grakn.test.kbs.SNBKB) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) Unifier(ai.grakn.graql.admin.Unifier) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) QueryBuilder(ai.grakn.graql.QueryBuilder) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 99 with GetQuery

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

the class ReasoningTests method inferrableRelationWithRolePlayersSharingResource.

// tests whether shared resources are recognised correctly
@Test
public void inferrableRelationWithRolePlayersSharingResource() {
    QueryBuilder qb = testSet29.tx().graql().infer(true);
    String queryString = "match " + "(role1: $x, role2: $y) isa binary-base;" + "$x has name $n;" + "$y has name $n;" + "get;";
    String queryString2 = "match " + "(role1: $x, role2: $y) isa binary-base;" + "$x has name $n;" + "$y has name $n;" + "$n val 'a';" + "get;";
    String queryString3 = "match " + "(role1: $x, role2: $y) isa binary-base;" + "$x has name 'a';" + "$y has name 'a';" + "get;";
    List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
    List<Answer> answers2 = qb.<GetQuery>parse(queryString2).execute();
    List<Answer> answers3 = qb.<GetQuery>parse(queryString3).execute();
    assertEquals(answers.size(), 3);
    answers.forEach(ans -> {
        assertEquals(ans.size(), 3);
        assertEquals(ans.get("x"), ans.get("y"));
    });
    assertEquals(answers2.size(), 1);
    assertEquals(answers3.size(), 1);
    answers2.stream().map(a -> a.project(Sets.newHashSet(var("x"), var("y")))).forEach(a -> assertTrue(answers3.contains(a)));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) GraknTestUtil(ai.grakn.util.GraknTestUtil) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Matchers.not(org.hamcrest.Matchers.not) Graql(ai.grakn.graql.Graql) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) CollectionUtils(org.apache.commons.collections.CollectionUtils) GraknTx(ai.grakn.GraknTx) ClassRule(org.junit.ClassRule) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Var(ai.grakn.graql.Var) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Example 100 with GetQuery

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

the class ReasoningTests method whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults.

@Test
public void whenExecutingAQueryWithImplicitTypes_InferenceHasAtLeastAsManyResults() {
    QueryBuilder withInference = testSet14.tx().graql().infer(true);
    QueryBuilder withoutInference = testSet14.tx().graql().infer(false);
    VarPattern owner = label(HAS_OWNER.getLabel("resource"));
    VarPattern value = label(HAS_VALUE.getLabel("resource"));
    VarPattern hasRes = label(HAS.getLabel("resource"));
    Function<QueryBuilder, GetQuery> query = qb -> qb.match(var().rel(owner, "x").rel(value, "y").isa(hasRes), // This pattern is added only to encourage reasoning to activate
    var("a").has("resource", var("b"))).get();
    Set<Answer> resultsWithoutInference = query.apply(withoutInference).stream().collect(toSet());
    Set<Answer> resultsWithInference = query.apply(withInference).stream().collect(toSet());
    assertThat(resultsWithoutInference, not(empty()));
    assertThat(Sets.difference(resultsWithoutInference, resultsWithInference), empty());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) GraknTestUtil(ai.grakn.util.GraknTestUtil) HAS_VALUE(ai.grakn.util.Schema.ImplicitType.HAS_VALUE) Matchers.not(org.hamcrest.Matchers.not) Graql(ai.grakn.graql.Graql) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Label(ai.grakn.concept.Label) CollectionUtils(org.apache.commons.collections.CollectionUtils) GraknTx(ai.grakn.GraknTx) ClassRule(org.junit.ClassRule) HAS_OWNER(ai.grakn.util.Schema.ImplicitType.HAS_OWNER) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) QueryBuilder(ai.grakn.graql.QueryBuilder) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) CombinatoricsUtils(org.apache.commons.math3.util.CombinatoricsUtils) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Graql.label(ai.grakn.graql.Graql.label) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Var(ai.grakn.graql.Var) SampleKBContext(ai.grakn.test.rule.SampleKBContext) HAS(ai.grakn.util.Schema.ImplicitType.HAS) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) GetQuery(ai.grakn.graql.GetQuery) VarPattern(ai.grakn.graql.VarPattern) QueryBuilder(ai.grakn.graql.QueryBuilder) Test(org.junit.Test)

Aggregations

GetQuery (ai.grakn.graql.GetQuery)163 Test (org.junit.Test)161 Answer (ai.grakn.graql.admin.Answer)65 QueryBuilder (ai.grakn.graql.QueryBuilder)61 GraknTx (ai.grakn.GraknTx)51 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)23 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 SampleKBContext (ai.grakn.test.rule.SampleKBContext)12 Concept (ai.grakn.concept.Concept)9 List (java.util.List)8 Assert.assertEquals (org.junit.Assert.assertEquals)7 Assert.assertTrue (org.junit.Assert.assertTrue)7 Set (java.util.Set)6 Label (ai.grakn.concept.Label)5 RelationshipType (ai.grakn.concept.RelationshipType)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)5 Sets (com.google.common.collect.Sets)5 Collectors (java.util.stream.Collectors)5 Collectors.toSet (java.util.stream.Collectors.toSet)5