use of ai.grakn.graql.Graql in project grakn by graknlabs.
the class QueryVisitor method visitPropHas.
@Override
public UnaryOperator<VarPattern> visitPropHas(GraqlParser.PropHasContext ctx) {
Label type = visitLabel(ctx.label());
VarPattern relation = Optional.ofNullable(ctx.relation).map(this::getVariable).orElseGet(Graql::var);
VarPattern resource = Optional.ofNullable(ctx.resource).map(this::getVariable).orElseGet(Graql::var);
if (ctx.predicate() != null) {
resource = resource.val(visitPredicate(ctx.predicate()));
}
VarPattern finalResource = resource;
return var -> var.has(type, finalResource, relation);
}
use of ai.grakn.graql.Graql in project grakn by graknlabs.
the class ShortestPathTest method testMultiplePathInSubGraph.
@Test
public void testMultiplePathInSubGraph() {
Set<List<ConceptId>> correctPaths = new HashSet<>();
List<List<Concept>> allPaths;
addSchemaAndEntities();
try (GraknTx graph = session.open(GraknTxType.READ)) {
if (GraknTestUtil.usingJanus()) {
// If no path is found in the vertex program, NoResultException is thrown to skip map reduce.
// Tinker doesn't handle this well, so the next graql query would find the graph is empty.
allPaths = graph.graql().compute().paths().in(thing, anotherThing).to(entityId1).from(entityId4).execute();
assertEquals(0, allPaths.size());
}
correctPaths.add(Lists.newArrayList(entityId1, relationId12, entityId2, relationId24, entityId4));
correctPaths.add(Lists.newArrayList(entityId1, relationId13, entityId3, relationId34, entityId4));
allPaths = graph.graql().compute().paths().from(entityId1).to(entityId4).execute();
assertEquals(correctPaths.size(), allPaths.size());
Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
assertEquals(correctPaths, computedPaths);
}
}
use of ai.grakn.graql.Graql 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.Graql 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.Graql 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