use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class RuleTest method whenCreatingDistinctRulesWithSimilarStringHashes_EnsureRulesDoNotClash.
@Test
public void whenCreatingDistinctRulesWithSimilarStringHashes_EnsureRulesDoNotClash() {
String productRefused = "productRefused";
Pattern when1 = graknTx.graql().parser().parsePattern("{$step has step-id 9; $e (process-case: $case) isa process-record; $case has consent false;}");
Pattern then1 = graknTx.graql().parser().parsePattern("{(record: $e, step: $step) isa record-step;}");
Rule rule1 = graknTx.putRule(productRefused, when1, then1);
String productAccepted = "productAccepted";
Pattern when2 = graknTx.graql().parser().parsePattern("{$step has step-id 7; $e (process-case: $case) isa process-record; $case has consent true;}");
Pattern then2 = graknTx.graql().parser().parsePattern("{(record: $e, step: $step) isa record-step;}");
Rule rule2 = graknTx.putRule(productAccepted, when2, then2);
assertEquals(rule1, graknTx.getRule(productRefused));
assertEquals(rule2, graknTx.getRule(productAccepted));
}
use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class CWInferenceTest method testGraphCase.
@Test
public void testGraphCase() {
EmbeddedGraknTx<?> tx = cwKB2.tx();
QueryBuilder lqb = tx.graql().infer(false);
QueryBuilder ilqb = tx.graql().infer(true);
tx.putEntityType("region");
Pattern R6_LHS = and(tx.graql().parser().parsePatterns("$x isa region;"));
Pattern R6_RHS = and(tx.graql().parser().parsePatterns("$x isa country;"));
tx.putRule("R6: If something is a region it is a country", R6_LHS, R6_RHS);
tx.commitSubmitNoLogs();
String queryString = "match $p isa criminal; get;";
String explicitQuery = "match $p isa person has name 'colonelWest'; get;";
// Reopen transaction
cwKB2.tx();
assertQueriesEqual(ilqb.parse(queryString), lqb.parse(explicitQuery));
}
use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class ConceptBuilder method tryPutConcept.
private Concept tryPutConcept() {
usedParams.clear();
Concept concept;
if (has(IS_ROLE)) {
use(IS_ROLE);
Label label = use(LABEL);
Role role = executor.tx().putRole(label);
if (has(SUPER_CONCEPT)) {
setSuper(role, use(SUPER_CONCEPT));
}
concept = role;
} else if (has(IS_RULE)) {
use(IS_RULE);
Label label = use(LABEL);
Pattern when = use(WHEN);
Pattern then = use(THEN);
Rule rule = executor.tx().putRule(label, when, then);
if (has(SUPER_CONCEPT)) {
setSuper(rule, use(SUPER_CONCEPT));
}
concept = rule;
} else if (has(SUPER_CONCEPT)) {
concept = putSchemaConcept();
} else if (has(TYPE)) {
concept = putInstance();
} else {
throw GraqlQueryException.insertUndefinedVariable(executor.printableRepresentation(var));
}
// Check for any unexpected parameters
preProvidedParams.forEach((param, value) -> {
if (!usedParams.contains(param)) {
throw GraqlQueryException.insertUnexpectedProperty(param.name(), value, concept);
}
});
return concept;
}
use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class RelationshipConverter method pattern.
public Pattern pattern(Relationship concept) {
VarPattern relationPattern = Graql.var();
Set<Pattern> idPatterns = new HashSet<>();
for (Map.Entry<Role, Set<Thing>> entry : concept.allRolePlayers().entrySet()) {
for (Thing var : entry.getValue()) {
Var rolePlayer = Graql.var();
relationPattern = relationPattern.rel(Graql.label(entry.getKey().getLabel()), rolePlayer);
idPatterns.add(rolePlayer.asUserDefined().id(var.getId()));
}
}
relationPattern = relationPattern.isa(Graql.label(concept.type().getLabel()));
Pattern pattern = relationPattern;
for (Pattern idPattern : idPatterns) {
pattern = pattern.and(idPattern);
}
return pattern;
}
use of ai.grakn.graql.Pattern in project grakn by graknlabs.
the class PatternTest method testSimpleConjunction.
@Test
public void testSimpleConjunction() {
Pattern conjunction = and();
assertFalse(conjunction.admin().isVarPattern());
assertFalse(conjunction.admin().isDisjunction());
assertTrue(conjunction.admin().isConjunction());
// noinspection AssertEqualsBetweenInconvertibleTypes
assertEquals(conjunction.admin(), conjunction.admin().asConjunction());
}
Aggregations