use of ai.grakn.graql.internal.gremlin.fragment.LabelFragment 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());
}
});
}
Aggregations