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);
}
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);
}
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);
}
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)));
}
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());
}
Aggregations