Search in sources :

Example 1 with IsaProperty

use of ai.grakn.graql.internal.pattern.property.IsaProperty in project grakn by graknlabs.

the class HasAttributeProperties method generate.

@Override
public HasAttributeProperty generate() {
    VarPatternAdmin varPatternAttribute;
    VarPatternAdmin varPatternRelationship;
    // `HasAttributeProperty` will implicitly attach an `IsaProperty`, so must not clash
    do {
        varPatternAttribute = gen(VarPatternAdmin.class);
    } while (varPatternAttribute.hasProperty(IsaProperty.class) || varPatternAttribute.hasProperty(DirectIsaProperty.class));
    do {
        varPatternRelationship = gen(VarPatternAdmin.class);
    } while (varPatternRelationship.hasProperty(IsaProperty.class) || varPatternRelationship.hasProperty(DirectIsaProperty.class));
    return HasAttributeProperty.of(gen(Label.class), varPatternAttribute, varPatternRelationship);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) DirectIsaProperty(ai.grakn.graql.internal.pattern.property.DirectIsaProperty) Label(ai.grakn.concept.Label) DirectIsaProperty(ai.grakn.graql.internal.pattern.property.DirectIsaProperty)

Example 2 with IsaProperty

use of ai.grakn.graql.internal.pattern.property.IsaProperty in project grakn by graknlabs.

the class QueryParserTest method whenParsingAQueryAndDefiningAllVars_AllVarsExceptLabelsAreDefined.

@Test
public void whenParsingAQueryAndDefiningAllVars_AllVarsExceptLabelsAreDefined() {
    QueryParser parser = Graql.parser();
    parser.defineAllVars(true);
    GetQuery query = parser.parseQuery("match ($x, $y) isa foo; get;");
    System.out.println(query);
    Conjunction<PatternAdmin> conjunction = query.match().admin().getPattern();
    Set<PatternAdmin> patterns = conjunction.getPatterns();
    VarPatternAdmin pattern = Iterables.getOnlyElement(patterns).asVarPattern();
    assertTrue(pattern.var().isUserDefinedName());
    IsaProperty property = pattern.getProperty(IsaProperty.class).get();
    assertFalse(property.type().var().isUserDefinedName());
}
Also used : QueryParser(ai.grakn.graql.QueryParser) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GetQuery(ai.grakn.graql.GetQuery) PatternAdmin(ai.grakn.graql.admin.PatternAdmin) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Test(org.junit.Test)

Example 3 with IsaProperty

use of ai.grakn.graql.internal.pattern.property.IsaProperty 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

IsaProperty (ai.grakn.graql.internal.pattern.property.IsaProperty)3 Label (ai.grakn.concept.Label)2 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 RelationshipType (ai.grakn.concept.RelationshipType)1 Type (ai.grakn.concept.Type)1 GetQuery (ai.grakn.graql.GetQuery)1 QueryParser (ai.grakn.graql.QueryParser)1 Var (ai.grakn.graql.Var)1 PatternAdmin (ai.grakn.graql.admin.PatternAdmin)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 DirectIsaProperty (ai.grakn.graql.internal.pattern.property.DirectIsaProperty)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1