use of ai.grakn.graql.Var in project grakn by graknlabs.
the class QueryParserTest method testCustomAggregate.
@Test
public void testCustomAggregate() {
QueryBuilder qb = withoutGraph();
QueryParser parser = qb.parser();
parser.registerAggregate("get-any", args -> new GetAny((Var) args.get(0)));
AggregateQuery<Concept> expected = qb.match(var("x").isa("movie")).aggregate(new GetAny(Graql.var("x")));
AggregateQuery<Concept> parsed = qb.parse("match $x isa movie; aggregate get-any $x;");
assertEquals(expected, parsed);
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class QueryParserTest method whenParsingDeleteQuery_ResultIsSameAsJavaGraql.
@Test
public void whenParsingDeleteQuery_ResultIsSameAsJavaGraql() {
Var x = var("x");
Var y = var("y");
DeleteQuery expected = match(x.isa("movie").has("title", "The Title"), y.isa("movie")).delete(x, y);
DeleteQuery parsed = parse("match $x isa movie has title 'The Title'; $y isa movie; delete $x, $y;");
assertEquals(expected, parsed);
}
use of ai.grakn.graql.Var 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.Var 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());
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class QueryOperationExecutor method sortProperties.
/**
* Produce a valid ordering of the properties by using the given dependency information.
*
* <p>
* This method uses a topological sort (Kahn's algorithm) in order to find a valid ordering.
* </p>
*/
private ImmutableList<VarAndProperty> sortProperties() {
ImmutableList.Builder<VarAndProperty> sorted = ImmutableList.builder();
// invertedDependencies is intended to just be a 'view' on dependencies, so when dependencies is modified
// we should always also modify invertedDependencies (and vice-versa).
Multimap<VarAndProperty, VarAndProperty> dependencies = HashMultimap.create(this.dependencies);
Multimap<VarAndProperty, VarAndProperty> invertedDependencies = HashMultimap.create();
Multimaps.invertFrom(dependencies, invertedDependencies);
Queue<VarAndProperty> propertiesWithoutDependencies = new ArrayDeque<>(Sets.filter(properties, property -> dependencies.get(property).isEmpty()));
VarAndProperty property;
// Retrieve the next property without any dependencies
while ((property = propertiesWithoutDependencies.poll()) != null) {
sorted.add(property);
// We copy this into a new list because the underlying collection gets modified during iteration
Collection<VarAndProperty> dependents = Lists.newArrayList(invertedDependencies.get(property));
for (VarAndProperty dependent : dependents) {
// Because the property has been removed, the dependent no longer needs to depend on it
dependencies.remove(dependent, property);
invertedDependencies.remove(property, dependent);
boolean hasNoDependencies = dependencies.get(dependent).isEmpty();
if (hasNoDependencies) {
propertiesWithoutDependencies.add(dependent);
}
}
}
if (!dependencies.isEmpty()) {
// This means there must have been a loop. Pick an arbitrary remaining var to display
Var var = dependencies.keys().iterator().next().var();
throw GraqlQueryException.insertRecursive(printableRepresentation(var));
}
return sorted.build();
}
Aggregations