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