use of ai.grakn.graql.Var in project grakn by graknlabs.
the class MatchModifierTest method testVoteCountOrderedQuery.
@Test
public void testVoteCountOrderedQuery() {
Var z = var("z");
Match match = qb.match(z.isa("movie").has("tmdb-vote-count", var("v"))).orderBy("v", desc);
// Make sure movies are in the correct order
assertThat(match, variable(z, contains(godfather, hocusPocus, apocalypseNow, theMuppets, chineseCoffee)));
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class RelationshipAtom method materialise.
@Override
public Stream<Answer> materialise() {
RelationshipType relationType = getSchemaConcept().asRelationshipType();
Multimap<Role, Var> roleVarMap = getRoleVarMap();
Answer substitution = getParentQuery().getSubstitution();
Relationship relationship = RelationshipTypeImpl.from(relationType).addRelationshipInferred();
roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.addRolePlayer(key, substitution.get(var).asThing())));
Answer relationSub = getRoleSubstitution().merge(getVarName().isUserDefinedName() ? new QueryAnswer(ImmutableMap.of(getVarName(), relationship)) : new QueryAnswer());
return Stream.of(substitution.merge(relationSub));
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class ResolutionPlan method isValid.
/**
* @return true if the plan doesn't lead to any non-ground neq predicate
*/
private boolean isValid() {
// check for neq groundness
Set<NeqPredicate> nonGroundPredicates = new HashSet<>();
Set<Var> mappedVars = new HashSet<>();
for (Atom atom : plan) {
mappedVars.addAll(atom.getVarNames());
atom.getPredicates(NeqPredicate.class).forEach(neq -> {
// look for non-local non-ground predicates
if (!mappedVars.containsAll(neq.getVarNames()) && !atom.getVarNames().containsAll(neq.getVarNames())) {
nonGroundPredicates.add(neq);
} else {
// if this is ground for this atom but non-ground for another it is ground
if (nonGroundPredicates.contains(neq))
nonGroundPredicates.remove(neq);
}
});
}
return nonGroundPredicates.isEmpty();
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class QueryOperationExecutor method create.
private static QueryOperationExecutor create(Collection<VarPatternAdmin> patterns, GraknTx graph, ExecutionType executionType) {
ImmutableSet<VarAndProperty> properties = patterns.stream().flatMap(pattern -> VarAndProperty.fromPattern(pattern, executionType)).collect(toImmutableSet());
/*
We build several many-to-many relations, indicated by a `Multimap<X, Y>`. These are used to represent
the dependencies between properties and variables.
`propDependencies.containsEntry(prop, var)` indicates that the property `prop` cannot be inserted until
the concept represented by the variable `var` is created.
For example, the property `$x isa $y` depends on the existence of the concept represented by `$y`.
*/
Multimap<VarAndProperty, Var> propDependencies = HashMultimap.create();
for (VarAndProperty property : properties) {
for (Var requiredVar : property.executor().requiredVars()) {
propDependencies.put(property, requiredVar);
}
}
/*
`varDependencies.containsEntry(var, prop)` indicates that the concept represented by the variable `var`
cannot be created until the property `prop` is inserted.
For example, the concept represented by `$x` will not exist before the property `$x isa $y` is inserted.
*/
Multimap<Var, VarAndProperty> varDependencies = HashMultimap.create();
for (VarAndProperty property : properties) {
for (Var producedVar : property.executor().producedVars()) {
varDependencies.put(producedVar, property);
}
}
/*
Equivalent vars are variables that must represent the same concept as another var.
$X label movie, sub entity;
$Y label movie;
$z isa $Y;
In this example, `$z isa $Y` must not be inserted before `$Y` is. However, `$Y` does not have enough
information to insert on its own. It also needs a super type!
We know `$Y` must represent the same concept as `$X`, because they both share the same label property.
Therefore, we can share their dependencies, such that:
varDependencies.containsEntry($X, prop) <=> varDependencies.containsEntry($Y, prop)
Therefore:
varDependencies.containsEntry($X, `$X sub entity`) => varDependencies.containsEntry($Y, `$X sub entity`)
Now we know that `$Y` depends on `$X sub entity` as well as `$X label movie`, which is enough information to
insert the type!
*/
Partition<Var> equivalentVars = Partition.singletons(Collections.emptyList());
equivalentProperties(properties).asMap().values().forEach(vars -> {
// These vars must refer to the same concept, so share their dependencies
Collection<VarAndProperty> producers = vars.stream().flatMap(var -> varDependencies.get(var).stream()).collect(toList());
Var first = vars.iterator().next();
vars.forEach(var -> {
varDependencies.replaceValues(var, producers);
equivalentVars.merge(first, var);
});
});
/*
Together, `propDependencies` and `varDependencies` can be composed into a single many-to-many relation:
dependencies = propDependencies ∘ varDependencies
By doing so, we map _directly_ between properties, skipping the vars. For example, if we previously had:
propDependencies.containsEntry(`$x isa $y`, `$y`); // `$x isa $y` depends on `$y`
varDependencies.containsEntry(`$y`, `$y label movie`); // `$y` depends on `$y label movie`
Then it follows that:
dependencies.containsEntry(`$x isa $y`, `$y label movie`); // `$x isa $y` depends on `$y label movie`
The `dependencies` relation contains all the information to decide what order to execute the properties.
*/
Multimap<VarAndProperty, VarAndProperty> dependencies = composeMultimaps(propDependencies, varDependencies);
return new QueryOperationExecutor(graph, properties, equivalentVars, ImmutableMultimap.copyOf(dependencies));
}
use of ai.grakn.graql.Var 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);
}
Aggregations