Search in sources :

Example 31 with Pattern

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

the class QueryPlannerTest method inferRelationshipTypeWithMoreThan2Roles.

@Test
public void inferRelationshipTypeWithMoreThan2Roles() {
    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(3L, plan.stream().filter(LabelFragment.class::isInstance).count());
    pattern = and(x.isa(thingy1), 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 32 with Pattern

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

the class InsertQueryTest method testInsertRelation.

@Test
public void testInsertRelation() {
    VarPattern rel = var("r").isa("has-genre").rel("genre-of-production", "x").rel("production-with-genre", "y");
    VarPattern x = var("x").has("title", "Godfather").isa("movie");
    VarPattern y = var("y").has("name", "comedy").isa("genre");
    VarPattern[] vars = new VarPattern[] { rel, x, y };
    Pattern[] patterns = new Pattern[] { rel, x, y };
    assertNotExists(qb.match(patterns));
    qb.insert(vars).execute();
    assertExists(qb, patterns);
    qb.match(patterns).delete("r").execute();
    assertNotExists(qb, patterns);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 33 with Pattern

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

the class DefineQueryTest method whenDefiningARule_TheRuleIsInTheKB.

@Test
public void whenDefiningARule_TheRuleIsInTheKB() {
    Pattern when = qb.parser().parsePattern("$x isa entity");
    Pattern then = qb.parser().parsePattern("$x isa entity");
    VarPattern vars = label("my-rule").sub(label(RULE.getLabel())).when(when).then(then);
    qb.define(vars).execute();
    assertNotNull(movies.tx().getRule("my-rule"));
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Pattern(ai.grakn.graql.Pattern) VarPattern(ai.grakn.graql.VarPattern) Test(org.junit.Test)

Example 34 with Pattern

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

the class GeoKB method buildRules.

@Override
public void buildRules(GraknTx tx) {
    Pattern transitivity_LHS = tx.graql().parser().parsePattern("{(geo-entity: $x, entity-location: $y) isa is-located-in;" + "(geo-entity: $y, entity-location: $z) isa is-located-in;}");
    Pattern transitivity_RHS = tx.graql().parser().parsePattern("{(geo-entity: $x, entity-location: $z) isa is-located-in;}");
    tx.putRule("Geo Rule", transitivity_LHS, transitivity_RHS);
}
Also used : Pattern(ai.grakn.graql.Pattern)

Example 35 with Pattern

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

the class MovieKB method buildRules.

@Override
protected void buildRules(GraknTx tx) {
    // These rules are totally made up for testing purposes and don't work!
    Pattern when = tx.graql().parser().parsePattern("$x has name 'expectation-when'");
    Pattern then = tx.graql().parser().parsePattern("$x has name 'expectation-then'");
    tx.putRule("expectation-rule", when, then);
    when = tx.graql().parser().parsePattern("$x has name 'materialize-when'");
    then = tx.graql().parser().parsePattern("$x has name 'materialize-then'");
    tx.putRule("materialize-rule", when, then);
}
Also used : Pattern(ai.grakn.graql.Pattern)

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