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