Search in sources :

Example 11 with Pattern

use of ai.grakn.graql.Pattern 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 12 with Pattern

use of ai.grakn.graql.Pattern 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 13 with Pattern

use of ai.grakn.graql.Pattern in project grakn by graknlabs.

the class PatternPropertyTests method theConjunctionOfTwoPatterns_ShouldBeContainedInTheResultingPattern.

@Property
public void theConjunctionOfTwoPatterns_ShouldBeContainedInTheResultingPattern(Pattern pattern1, Pattern pattern2) {
    Set<VarPattern> union = Sets.union(pattern1.admin().varPatterns(), pattern2.admin().varPatterns());
    Pattern conjunction = pattern1.and(pattern2);
    assertEquals(union, conjunction.admin().varPatterns());
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) VarPattern(ai.grakn.graql.VarPattern) Property(com.pholser.junit.quickcheck.Property) VarProperty(ai.grakn.graql.admin.VarProperty)

Example 14 with Pattern

use of ai.grakn.graql.Pattern in project grakn by graknlabs.

the class DirectIsaTest method testMatchIsaAndDirectIsaReturnDifferentPlans.

@Test
public void testMatchIsaAndDirectIsaReturnDifferentPlans() {
    Pattern pattern;
    ImmutableList<Fragment> plan;
    // test type without subtypes
    pattern = x.directIsa(thingy1);
    plan = getPlan(pattern);
    assertEquals(2, plan.size());
    assertThat(plan, contains(instanceOf(LabelFragment.class), instanceOf(InIsaFragment.class)));
    pattern = and(x.directIsa(thingy1), y.directIsa(thingy1), var().rel(x).rel(y).isa(related));
    plan = getPlan(pattern);
    assertEquals(9, plan.size());
    // 3 labels: thingy1, thingy1 and related
    assertThat(plan, contains(instanceOf(LabelFragment.class), instanceOf(LabelFragment.class), instanceOf(LabelFragment.class), // start from relationship type
    instanceOf(InIsaFragment.class), // go to a role player
    instanceOf(OutRolePlayerFragment.class), // check the role player's type
    instanceOf(OutIsaFragment.class), // go to the other role player
    instanceOf(OutRolePlayerFragment.class), // check two players are different
    instanceOf(NeqFragment.class), // check the role player's type
    instanceOf(OutIsaFragment.class)));
    // test type with subtypes
    pattern = x.directIsa(thingy);
    plan = getPlan(pattern);
    assertEquals(2, plan.size());
    assertThat(plan, contains(instanceOf(LabelFragment.class), instanceOf(InIsaFragment.class)));
    pattern = x.isa(thingy);
    plan = getPlan(pattern);
    assertEquals(3, plan.size());
    assertThat(plan, contains(instanceOf(LabelFragment.class), instanceOf(InSubFragment.class), instanceOf(InIsaFragment.class)));
    pattern = and(x.directIsa(thingy), y.directIsa(thingy), var().rel(x).rel(y).isa(related));
    plan = getPlan(pattern);
    assertEquals(9, plan.size());
    // 3 labels: thingy1, thingy1 and related
    assertThat(plan, contains(instanceOf(LabelFragment.class), instanceOf(LabelFragment.class), instanceOf(LabelFragment.class), // start from relationship type
    instanceOf(InIsaFragment.class), // go to a role player
    instanceOf(OutRolePlayerFragment.class), // check the role player's type
    instanceOf(OutIsaFragment.class), // go to the other role player
    instanceOf(OutRolePlayerFragment.class), // check two players are different
    instanceOf(NeqFragment.class), // check the role player's type
    instanceOf(OutIsaFragment.class)));
    // combine isa and direct isa
    pattern = and(x.isa(thingy), y.directIsa(thingy), var().rel(x).rel(y).isa(related));
    plan = getPlan(pattern);
    assertEquals(10, plan.size());
    // 3 labels: thingy, thingy and related
    assertThat(plan, contains(instanceOf(LabelFragment.class), instanceOf(LabelFragment.class), instanceOf(LabelFragment.class), // start from relationship type
    instanceOf(InIsaFragment.class), // go to a role player
    instanceOf(OutRolePlayerFragment.class), // check the role player's type
    instanceOf(OutIsaFragment.class), // go to the other role player
    instanceOf(OutRolePlayerFragment.class), // check two players are different
    instanceOf(NeqFragment.class), // check the role player's type
    instanceOf(OutIsaFragment.class), // check the subtypes
    instanceOf(OutSubFragment.class)));
}
Also used : Pattern(ai.grakn.graql.Pattern) Test(org.junit.Test)

Example 15 with Pattern

use of ai.grakn.graql.Pattern in project grakn by graknlabs.

the class ConjunctionQueryTest method whenVarHasTwoResources_UseResourceIndexForBoth.

@Test
public void whenVarHasTwoResources_UseResourceIndexForBoth() {
    Pattern pattern = and(x.isa(resourceTypeWithoutSubTypes).val("Foo"), y.isa(resourceTypeWithoutSubTypes).val("Bar"));
    assertThat(pattern, allOf(usesResourceIndex(x, "Foo"), usesResourceIndex(y, "Bar")));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) Test(org.junit.Test)

Aggregations

Pattern (ai.grakn.graql.Pattern)35 Test (org.junit.Test)24 VarPattern (ai.grakn.graql.VarPattern)12 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)9 InIsaFragment (ai.grakn.graql.internal.gremlin.fragment.InIsaFragment)9 LabelFragment (ai.grakn.graql.internal.gremlin.fragment.LabelFragment)9 NeqFragment (ai.grakn.graql.internal.gremlin.fragment.NeqFragment)9 OutIsaFragment (ai.grakn.graql.internal.gremlin.fragment.OutIsaFragment)9 Role (ai.grakn.concept.Role)5 Rule (ai.grakn.concept.Rule)5 GraknTx (ai.grakn.GraknTx)4 Label (ai.grakn.concept.Label)4 ConceptId (ai.grakn.concept.ConceptId)3 RelationshipType (ai.grakn.concept.RelationshipType)3 SchemaConcept (ai.grakn.concept.SchemaConcept)3 InsertQuery (ai.grakn.graql.InsertQuery)3 Var (ai.grakn.graql.Var)3 Iterables (com.google.common.collect.Iterables)3 Grakn (ai.grakn.Grakn)2 GraknSession (ai.grakn.GraknSession)2