Search in sources :

Example 6 with Fragment

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);
}
Also used : Pattern(ai.grakn.graql.Pattern) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) OutIsaFragment(ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment) OutIsaFragment(ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment) NeqFragment(ai.grakn.graql.internal.gremlin.fragment.NeqFragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Test(org.junit.Test)

Example 7 with Fragment

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());
}
Also used : InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) Pattern(ai.grakn.graql.Pattern) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) OutIsaFragment(ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment) OutIsaFragment(ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment) NeqFragment(ai.grakn.graql.internal.gremlin.fragment.NeqFragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Test(org.junit.Test)

Example 8 with Fragment

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());
}
Also used : NeqFragment(ai.grakn.graql.internal.gremlin.fragment.NeqFragment) Pattern(ai.grakn.graql.Pattern) OutIsaFragment(ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment) NeqFragment(ai.grakn.graql.internal.gremlin.fragment.NeqFragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Test(org.junit.Test)

Example 9 with Fragment

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

Example 10 with Fragment

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;
}
Also used : Var(ai.grakn.graql.Var) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) HashSet(java.util.HashSet)

Aggregations

Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)17 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)12 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)12 Test (org.junit.Test)10 Pattern (ai.grakn.graql.Pattern)9 NeqFragment (ai.grakn.graql.internal.gremlin.fragment.NeqFragment)9 OutIsaFragment (ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment)9 Var (ai.grakn.graql.Var)5 HashSet (java.util.HashSet)4 InSubFragment (ai.grakn.graql.internal.gremlin.fragment.InSubFragment)3 OutRolePlayerFragment (ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment)3 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)3 Sets (com.google.common.collect.Sets)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 ConceptId (ai.grakn.concept.ConceptId)2 Label (ai.grakn.concept.Label)2 RelationshipType (ai.grakn.concept.RelationshipType)2 Type (ai.grakn.concept.Type)2