use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.
the class QueryPlannerTest method avoidImplicitTypes.
@Test
public void avoidImplicitTypes() {
Pattern pattern;
ImmutableList<Fragment> plan;
pattern = and(x.isa(thingy2), y.isa(thingy4), var().rel(x).rel(y));
plan = getPlan(pattern);
assertEquals(3L, plan.stream().filter(LabelFragment.class::isInstance).count());
String relationship = plan.get(4).start().getValue();
// should start from relationship
assertNotEquals(relationship, x.getValue());
assertNotEquals(relationship, y.getValue());
pattern = and(x.isa(resourceType), y.isa(thingy4), var().rel(x).rel(y));
plan = getPlan(pattern);
assertEquals(3L, plan.stream().filter(LabelFragment.class::isInstance).count());
relationship = plan.get(4).start().getValue();
// should start from a role player
assertTrue(relationship.equals(x.getValue()) || relationship.equals(y.getValue()));
assertTrue(plan.get(5) instanceof OutIsaFragment);
}
use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.
the class QueryPlannerTest method inferRelationshipTypeWhereAVarHasTwoTypes.
@Test
public void inferRelationshipTypeWhereAVarHasTwoTypes() {
Pattern pattern;
ImmutableList<Fragment> plan;
pattern = and(x.isa(thingy), x.isa(thingy1), y.isa(thingy4), var().rel(x).rel(y));
plan = getPlan(pattern);
// Relationship type can now be inferred, so one more relationship type label plus existing 3 labels
assertEquals(4L, plan.stream().filter(LabelFragment.class::isInstance).count());
assertEquals(4L, plan.stream().filter(fragment -> fragment instanceof OutIsaFragment || fragment instanceof InIsaFragment).count());
// Should start from the inferred relationship type, instead of role players
String relationship = plan.get(4).start().getValue();
assertNotEquals(relationship, x.getValue());
assertNotEquals(relationship, y.getValue());
}
use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.
the class QueryPlannerTest method shardCountIsUsed.
@Test
public void shardCountIsUsed() {
// force the concept to get a new shard
// shards of thing = 2 (thing = 1 and thing itself)
// thing 2 = 4, thing3 = 7
tx.shard(tx.getEntityType(thingy2).getId());
tx.shard(tx.getEntityType(thingy2).getId());
tx.shard(tx.getEntityType(thingy2).getId());
tx.shard(tx.getEntityType(thingy3).getId());
tx.shard(tx.getEntityType(thingy3).getId());
tx.shard(tx.getEntityType(thingy3).getId());
tx.shard(tx.getEntityType(thingy3).getId());
tx.shard(tx.getEntityType(thingy3).getId());
tx.shard(tx.getEntityType(thingy3).getId());
Pattern pattern;
ImmutableList<Fragment> plan;
pattern = and(x.isa(thingy1), y.isa(thingy2), z.isa(thingy3), var().rel(x).rel(y).rel(z));
plan = getPlan(pattern);
assertEquals(x, plan.get(3).end());
assertEquals(3L, plan.stream().filter(fragment -> fragment instanceof NeqFragment).count());
// TODO: should uncomment the following after updating cost of out-isa fragment
// varName = plan.get(7).end().getValue();
// assertEquals(y.getValue(), varName);
//
// varName = plan.get(11).end().getValue();
// assertEquals(y.getValue(), varName);
pattern = and(x.isa(thingy), y.isa(thingy2), var().rel(x).rel(y));
plan = getPlan(pattern);
assertEquals(x, plan.get(3).end());
pattern = and(x.isa(thingy), y.isa(thingy2), z.isa(thingy3), var().rel(x).rel(y).rel(z));
plan = getPlan(pattern);
assertEquals(x, plan.get(4).end());
pattern = and(x.isa(superType), superType.label(thingy), y.isa(thingy2), subType.sub(superType), z.isa(subType), var().rel(x).rel(y));
plan = getPlan(pattern);
assertEquals(z, plan.get(4).end());
tx.shard(tx.getEntityType(thingy1).getId());
tx.shard(tx.getEntityType(thingy1).getId());
tx.shard(tx.getEntityType(thingy).getId());
// now thing = 5, thing1 = 3
pattern = and(x.isa(thingy), y.isa(thingy2), z.isa(thingy3), var().rel(x).rel(y).rel(z));
plan = getPlan(pattern);
assertEquals(y, plan.get(3).end());
pattern = and(x.isa(thingy1), y.isa(thingy2), z.isa(thingy3), var().rel(x).rel(y).rel(z));
plan = getPlan(pattern);
assertEquals(x, plan.get(3).end());
tx.shard(tx.getEntityType(thingy1).getId());
tx.shard(tx.getEntityType(thingy1).getId());
// now thing = 7, thing1 = 5
pattern = and(x.isa(thingy), y.isa(thingy2), z.isa(thingy3), var().rel(x).rel(y).rel(z));
plan = getPlan(pattern);
assertEquals(y, plan.get(3).end());
pattern = and(x.isa(thingy1), y.isa(thingy2), z.isa(thingy3), var().rel(x).rel(y).rel(z));
plan = getPlan(pattern);
assertEquals(y, plan.get(3).end());
}
use of ai.grakn.graql.internal.gremlin.fragment.Fragment 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.internal.gremlin.fragment.Fragment in project grakn by graknlabs.
the class GraqlTraversal method fragmentListCost.
static double fragmentListCost(List<Fragment> fragments) {
Set<Var> names = new HashSet<>();
double listCost = 0;
for (Fragment fragment : fragments) {
listCost += fragmentCost(fragment, names);
names.addAll(fragment.vars());
}
return listCost;
}
Aggregations