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);
}
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());
}
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());
}
});
}
Aggregations