Search in sources :

Example 1 with LabelFragment

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());
        }
    });
}
Also used : HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) RelationshipType(ai.grakn.concept.RelationshipType) Label(ai.grakn.concept.Label) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType)

Aggregations

Label (ai.grakn.concept.Label)1 RelationshipType (ai.grakn.concept.RelationshipType)1 Type (ai.grakn.concept.Type)1 Var (ai.grakn.graql.Var)1 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)1 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)1 InSubFragment (ai.grakn.graql.internal.gremlin.fragment.InSubFragment)1 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)1 OutRolePlayerFragment (ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment)1 IsaProperty (ai.grakn.graql.internal.pattern.property.IsaProperty)1 HashMap (java.util.HashMap)1