use of ai.grakn.concept.Rule in project grakn by graknlabs.
the class ValidateGlobalRules method checkRuleSideInvalid.
/**
* @param graph The graph to query against
* @param rule The rule the pattern was extracted from
* @param side The side from which the pattern was extracted
* @param pattern The pattern from which we will extract the types in the pattern
* @return A list of errors if the pattern refers to any non-existent types in the graph
*/
private static Set<String> checkRuleSideInvalid(GraknTx graph, Rule rule, Schema.VertexProperty side, Pattern pattern) {
Set<String> errors = new HashSet<>();
pattern.admin().varPatterns().stream().flatMap(v -> v.innerVarPatterns().stream()).flatMap(v -> v.getTypeLabels().stream()).forEach(typeLabel -> {
SchemaConcept schemaConcept = graph.getSchemaConcept(typeLabel);
if (schemaConcept == null) {
errors.add(ErrorMessage.VALIDATION_RULE_MISSING_ELEMENTS.getMessage(side, rule.getLabel(), typeLabel));
} else {
if (Schema.VertexProperty.RULE_WHEN.equals(side)) {
if (schemaConcept.isType()) {
RuleImpl.from(rule).addHypothesis(schemaConcept.asType());
}
} else if (Schema.VertexProperty.RULE_THEN.equals(side)) {
if (schemaConcept.isType()) {
RuleImpl.from(rule).addConclusion(schemaConcept.asType());
}
} else {
throw GraknTxOperationException.invalidPropertyUse(rule, side);
}
}
});
return errors;
}
use of ai.grakn.concept.Rule in project grakn by graknlabs.
the class RuleTest method whenAddingDuplicateRulesOfTheSameTypeWithTheSamePattern_ReturnTheSameRule.
@Test
public void whenAddingDuplicateRulesOfTheSameTypeWithTheSamePattern_ReturnTheSameRule() {
graknTx.putEntityType("type1");
when = graknTx.graql().parser().parsePattern("$x isa type1");
then = graknTx.graql().parser().parsePattern("$x isa type1");
Rule rule1 = graknTx.putRule("My-Angry-Rule", when, then);
Rule rule2 = graknTx.putRule("My-Angry-Rule", when, then);
assertEquals(rule1, rule2);
}
use of ai.grakn.concept.Rule in project grakn by graknlabs.
the class RuleTest method whenCreatingRules_EnsureHypothesisAndConclusionTypesAreFilledOnCommit.
@Test
public void whenCreatingRules_EnsureHypothesisAndConclusionTypesAreFilledOnCommit() throws InvalidKBException {
EntityType t1 = graknTx.putEntityType("type1");
EntityType t2 = graknTx.putEntityType("type2");
when = graknTx.graql().parser().parsePattern("$x isa type1");
then = graknTx.graql().parser().parsePattern("$x isa type2");
Rule rule = graknTx.putRule("My-Happy-Rule", when, then);
assertThat(rule.getHypothesisTypes().collect(Collectors.toSet()), empty());
assertThat(rule.getConclusionTypes().collect(Collectors.toSet()), empty());
graknTx.commit();
assertThat(rule.getHypothesisTypes().collect(Collectors.toSet()), containsInAnyOrder(t1));
assertThat(rule.getConclusionTypes().collect(Collectors.toSet()), containsInAnyOrder(t2));
}
Aggregations