use of ai.grakn.graql.Var in project grakn by graknlabs.
the class QueryOperationExecutor method insertAll.
private Answer insertAll(Answer results) {
concepts.putAll(results.map());
sortProperties().forEach(property -> property.executor().execute(this));
conceptBuilders.forEach(this::buildConcept);
ImmutableMap.Builder<Var, Concept> allConcepts = ImmutableMap.<Var, Concept>builder().putAll(concepts);
// Make sure to include all equivalent vars in the result
for (Var var : equivalentVars.getNodes()) {
allConcepts.put(var, concepts.get(equivalentVars.componentOf(var)));
}
Map<Var, Concept> namedConcepts = Maps.filterKeys(allConcepts.build(), Var::isUserDefinedName);
return new QueryAnswer(namedConcepts);
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class MatchBase method makeResults.
/**
* @param vars set of variables of interest
* @param tx the graph to get results from
* @param elements a map of vertices and edges where the key is the variable name
* @return a map of concepts where the key is the variable name
*/
private static Optional<Map<Var, Concept>> makeResults(Set<Var> vars, EmbeddedGraknTx<?> tx, Map<String, Element> elements) {
Map<Var, Concept> map = new HashMap<>();
for (Var var : vars) {
Element element = elements.get(var.name());
if (element == null) {
throw GraqlQueryException.unexpectedResult(var);
} else {
Concept concept = buildConcept(tx, element);
map.put(var, concept);
}
}
return Optional.of(map);
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class ComparatorPredicate method applyPredicate.
@Override
public final <S, E> GraphTraversal<S, E> applyPredicate(GraphTraversal<S, E> traversal) {
var.ifPresent(theVar -> {
// Compare to another variable
String thisVar = UUID.randomUUID().toString();
Var otherVar = theVar.var();
String otherValue = UUID.randomUUID().toString();
Traversal[] traversals = Stream.of(VALUE_PROPERTIES).map(prop -> __.values(prop).as(otherValue).select(thisVar).values(prop).where(gremlinPredicate(otherValue))).toArray(Traversal[]::new);
traversal.as(thisVar).select(otherVar.name()).or(traversals).select(thisVar);
});
persistedValue().ifPresent(theValue -> {
// Compare to a given value
DataType<?> dataType = DataType.SUPPORTED_TYPES.get(value().get().getClass().getTypeName());
Schema.VertexProperty property = dataType.getVertexProperty();
traversal.has(property.name(), gremlinPredicate(theValue));
});
return traversal;
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class GreedyTraversalPlan method inferRelationshipTypes.
private static void inferRelationshipTypes(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
Map<Var, Type> labelVarTypeMap = getLabelVarTypeMap(tx, allFragments);
if (labelVarTypeMap.isEmpty())
return;
Multimap<Var, Type> instanceVarTypeMap = getInstanceVarTypeMap(allFragments, labelVarTypeMap);
Multimap<Var, Var> relationshipRolePlayerMap = getRelationshipRolePlayerMap(allFragments, instanceVarTypeMap);
if (relationshipRolePlayerMap.isEmpty())
return;
// for each type, get all possible relationship type it could be in
Multimap<Type, RelationshipType> relationshipMap = HashMultimap.create();
labelVarTypeMap.values().stream().distinct().forEach(type -> addAllPossibleRelationships(relationshipMap, type));
// inferred labels should be kept separately, even if they are already in allFragments set
Map<Label, Var> inferredLabels = new HashMap<>();
relationshipRolePlayerMap.asMap().forEach((relationshipVar, rolePlayerVars) -> {
Set<Type> possibleRelationshipTypes = rolePlayerVars.stream().filter(instanceVarTypeMap::containsKey).map(rolePlayer -> getAllPossibleRelationshipTypes(instanceVarTypeMap.get(rolePlayer), relationshipMap)).reduce(Sets::intersection).orElse(Collections.emptySet());
// TODO: if possibleRelationshipTypes here is empty, the query will not match any data
if (possibleRelationshipTypes.size() == 1) {
Type relationshipType = possibleRelationshipTypes.iterator().next();
Label label = relationshipType.getLabel();
// add label fragment if this label has not been inferred
if (!inferredLabels.containsKey(label)) {
Var labelVar = var();
inferredLabels.put(label, labelVar);
Fragment labelFragment = Fragments.label(LabelProperty.of(label), labelVar, ImmutableSet.of(label));
allFragments.add(labelFragment);
}
// finally, add inferred isa fragments
Var labelVar = inferredLabels.get(label);
IsaProperty isaProperty = IsaProperty.of(labelVar.admin());
EquivalentFragmentSet isaEquivalentFragmentSet = EquivalentFragmentSets.isa(isaProperty, relationshipVar, labelVar, relationshipType.isImplicit());
allFragments.addAll(isaEquivalentFragmentSet.fragments());
}
});
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class GreedyTraversalPlan method getLabelVarTypeMap.
// find all vars representing types
private static Map<Var, Type> getLabelVarTypeMap(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
Map<Var, Type> labelVarTypeMap = new HashMap<>();
allFragments.stream().filter(LabelFragment.class::isInstance).forEach(fragment -> {
// TODO: labels() should return ONE label instead of a set
SchemaConcept schemaConcept = tx.getSchemaConcept(Iterators.getOnlyElement(((LabelFragment) fragment).labels().iterator()));
if (schemaConcept != null && !schemaConcept.isRole() && !schemaConcept.isRule()) {
labelVarTypeMap.put(fragment.start(), schemaConcept.asType());
}
});
return labelVarTypeMap;
}
Aggregations