Search in sources :

Example 6 with Pattern

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

the class JsonPrinter method build.

@Override
public Json build(Concept concept) {
    Json json = Json.object("id", concept.getId().getValue());
    if (concept.isSchemaConcept()) {
        json.set("name", concept.asSchemaConcept().getLabel().getValue());
        SchemaConcept superConcept = concept.asSchemaConcept().sup();
        if (superConcept != null)
            json.set("sub", superConcept.getLabel().getValue());
    } else if (concept.isThing()) {
        json.set("isa", concept.asThing().type().getLabel().getValue());
    } else {
        throw CommonUtil.unreachableStatement("Unrecognised concept " + concept);
    }
    if (concept.isAttribute()) {
        json.set("value", concept.asAttribute().getValue());
    }
    if (concept.isRule()) {
        Pattern when = concept.asRule().getWhen();
        if (when != null) {
            json.set("when", when.toString());
        }
        Pattern then = concept.asRule().getThen();
        if (then != null) {
            json.set("then", then.toString());
        }
    }
    return json;
}
Also used : Pattern(ai.grakn.graql.Pattern) SchemaConcept(ai.grakn.concept.SchemaConcept) Json(mjson.Json)

Example 7 with Pattern

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

the class PatternPropertyTests method theDisjunctionOfTwoPatterns_ShouldBeContainedInTheResultingPattern.

@Property
public void theDisjunctionOfTwoPatterns_ShouldBeContainedInTheResultingPattern(Pattern pattern1, Pattern pattern2) {
    Set<VarPattern> union = Sets.union(pattern1.admin().varPatterns(), pattern2.admin().varPatterns());
    Pattern disjunction = pattern1.or(pattern2);
    assertEquals(union, disjunction.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 8 with Pattern

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

the class QueryParserTest method testInsertRules.

@Test
public void testInsertRules() {
    String when = "$x isa movie;";
    String then = "id '123' isa movie;";
    Pattern whenPattern = and(var("x").isa("movie"));
    Pattern thenPattern = and(var().id(ConceptId.of("123")).isa("movie"));
    InsertQuery expected = insert(label("my-rule-thing").sub("rule"), var().isa("my-rule-thing").when(whenPattern).then(thenPattern));
    InsertQuery parsed = parse("insert 'my-rule-thing' sub rule; \n" + "isa my-rule-thing, when {" + when + "}, then {" + then + "};");
    assertEquals(expected, parsed);
}
Also used : Pattern(ai.grakn.graql.Pattern) InsertQuery(ai.grakn.graql.InsertQuery) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 9 with Pattern

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

the class DefineQueryTest method whenDefiningARule_SubRuleDeclarationsCanBeSkipped.

@Test
public void whenDefiningARule_SubRuleDeclarationsCanBeSkipped() {
    Pattern when = qb.parser().parsePattern("$x isa entity");
    Pattern then = qb.parser().parsePattern("$x isa entity");
    VarPattern vars = label("my-rule").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 10 with Pattern

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

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