use of ai.grakn.graql.admin.ReasonerQuery in project grakn by graknlabs.
the class ValidateGlobalRules method validateRuleHead.
/**
* @param graph graph used to ensure the rule head is valid
* @param rule the rule to be validated
* @return Error messages if the rule head is invalid - is not a single-atom conjunction, doesn't contain illegal atomics and is ontologically valid
*/
private static Set<String> validateRuleHead(GraknTx graph, Rule rule) {
Set<String> errors = new HashSet<>();
Set<Conjunction<VarPatternAdmin>> headPatterns = rule.getThen().admin().getDisjunctiveNormalForm().getPatterns();
if (headPatterns.size() != 1) {
errors.add(ErrorMessage.VALIDATION_RULE_DISJUNCTION_IN_HEAD.getMessage(rule.getLabel()));
} else {
ReasonerQuery bodyQuery = Iterables.getOnlyElement(rule.getWhen().admin().getDisjunctiveNormalForm().getPatterns()).toReasonerQuery(graph);
ReasonerQuery headQuery = Iterables.getOnlyElement(headPatterns).toReasonerQuery(graph);
ReasonerQuery combinedQuery = headQuery.conjunction(bodyQuery);
Set<Atomic> headAtoms = headQuery.getAtoms();
combinedQuery.getAtoms().stream().filter(headAtoms::contains).map(at -> at.validateAsRuleHead(rule)).forEach(errors::addAll);
Set<Atomic> selectableHeadAtoms = headAtoms.stream().filter(Atomic::isAtom).filter(Atomic::isSelectable).collect(Collectors.toSet());
if (selectableHeadAtoms.size() > 1) {
errors.add(ErrorMessage.VALIDATION_RULE_HEAD_NON_ATOMIC.getMessage(rule.getLabel()));
}
}
return errors;
}
use of ai.grakn.graql.admin.ReasonerQuery in project grakn by graknlabs.
the class ValidateGlobalRules method validateRuleOntologically.
/**
* NB: this only gets checked if the rule obeys the Horn clause form
* @param graph graph used to ensure the rule is a valid Horn clause
* @param rule the rule to be validated ontologically
* @return Error messages if the rule has ontological inconsistencies
*/
static Set<String> validateRuleOntologically(GraknTx graph, Rule rule) {
Set<String> errors = new HashSet<>();
// both body and head refer to the same graph and have to be valid with respect to the schema that governs it
// as a result the rule can be ontologically validated by combining them into a conjunction
// this additionally allows to cross check body-head references
ReasonerQuery combinedQuery = combinedRuleQuery(graph, rule);
errors.addAll(combinedQuery.validateOntologically());
return errors;
}
Aggregations