Search in sources :

Example 11 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class GraqlTraversal method applyFragments.

private GraphTraversal<Vertex, Map<String, Element>> applyFragments(EmbeddedGraknTx<?> tx, Set<Var> vars, ImmutableList<Fragment> fragmentList, GraphTraversal<Vertex, ? extends Element> traversal) {
    Set<Var> foundVars = new HashSet<>();
    // Apply fragments in order into one single traversal
    Var currentName = null;
    for (Fragment fragment : fragmentList) {
        // Apply fragment to traversal
        fragment.applyTraversal(traversal, tx, foundVars, currentName);
        currentName = fragment.end() != null ? fragment.end() : fragment.start();
    }
    // Select all the variable names
    return selectVars(traversal, Sets.intersection(vars, foundVars));
}
Also used : Var(ai.grakn.graql.Var) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) HashSet(java.util.HashSet)

Example 12 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class QueryPlannerTest method inferRelationshipTypeWithARolePlayerWithNoType.

@Test
public void inferRelationshipTypeWithARolePlayerWithNoType() {
    Pattern pattern;
    ImmutableList<Fragment> plan;
    pattern = and(x.isa(thingy1), y.isa(thingy2), var().rel(x).rel(y).rel(z));
    plan = getPlan(pattern);
    assertEquals(2L, plan.stream().filter(LabelFragment.class::isInstance).count());
    pattern = and(y.isa(thingy2), z.isa(thingy4), var().rel(x).rel(y).rel(z));
    plan = getPlan(pattern);
    // Relationship type can now be inferred, so one more relationship type label
    assertEquals(3L, plan.stream().filter(LabelFragment.class::isInstance).count());
    assertEquals(3L, plan.stream().filter(fragment -> fragment instanceof OutIsaFragment || fragment instanceof InIsaFragment).count());
}
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 13 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class QueryPlannerTest method inferUniqueRelationshipType.

@Test
public void inferUniqueRelationshipType() {
    Pattern pattern;
    ImmutableList<Fragment> plan;
    pattern = and(x.isa(thingy1), y.isa(thingy2), var().rel(x).rel(y));
    plan = getPlan(pattern);
    assertEquals(2L, plan.stream().filter(LabelFragment.class::isInstance).count());
    pattern = and(x.isa(thingy), y.isa(thingy2), var().rel(x).rel(y));
    plan = getPlan(pattern);
    assertEquals(2L, plan.stream().filter(LabelFragment.class::isInstance).count());
    pattern = and(x.isa(thingy2), y.isa(thingy4), var().rel(x).rel(y));
    plan = getPlan(pattern);
    // Relationship type can now be inferred, so one more relationship type label
    assertEquals(3L, plan.stream().filter(LabelFragment.class::isInstance).count());
    // Should start from the inferred relationship type, instead of role players
    String relationship = plan.get(3).start().getValue();
    assertNotEquals(relationship, x.getValue());
    assertNotEquals(relationship, y.getValue());
}
Also used : Pattern(ai.grakn.graql.Pattern) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) 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 14 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class QueryPlannerTest method inferRelationshipTypeWhereRolePlayedBySuperType.

@Test
public void inferRelationshipTypeWhereRolePlayedBySuperType() {
    Pattern pattern;
    ImmutableList<Fragment> plan;
    pattern = and(x.isa(thingy), y.isa(thingy4), var().rel(x).rel(y));
    plan = getPlan(pattern);
    assertEquals(2L, plan.stream().filter(LabelFragment.class::isInstance).count());
    pattern = and(x.isa(thingy), y.isa(thingy2), z.isa(thingy4), var().rel(x).rel(y).rel(z));
    plan = getPlan(pattern);
    // Relationship type can now be inferred, so one more relationship type label
    assertEquals(4L, plan.stream().filter(LabelFragment.class::isInstance).count());
    assertEquals(4L, plan.stream().filter(fragment -> fragment instanceof OutIsaFragment || fragment instanceof InIsaFragment).count());
}
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 15 with Fragment

use of ai.grakn.graql.internal.gremlin.fragment.Fragment in project grakn by graknlabs.

the class QueryPlannerTest method inferRelationshipTypeWhereAVarHasIncorrectTypes.

@Test
public void inferRelationshipTypeWhereAVarHasIncorrectTypes() {
    Pattern pattern;
    ImmutableList<Fragment> plan;
    pattern = and(x.isa(veryRelated), x.isa(thingy2), y.isa(thingy4), var().rel(x).rel(y));
    plan = getPlan(pattern);
    assertEquals(3L, plan.stream().filter(LabelFragment.class::isInstance).count());
    assertEquals(3L, plan.stream().filter(fragment -> fragment instanceof OutIsaFragment || fragment instanceof InIsaFragment).count());
    pattern = and(x.isa(veryRelated), y.isa(thingy4), var().rel(x).rel(y));
    plan = getPlan(pattern);
    assertEquals(2L, plan.stream().filter(LabelFragment.class::isInstance).count());
    assertEquals(2L, plan.stream().filter(fragment -> fragment instanceof OutIsaFragment || fragment instanceof InIsaFragment).count());
}
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)

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