Search in sources :

Example 6 with ReasonerQuery

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;
}
Also used : RuleImpl(ai.grakn.kb.internal.concept.RuleImpl) Iterables(com.google.common.collect.Iterables) VALIDATION_CASTING(ai.grakn.util.ErrorMessage.VALIDATION_CASTING) VALIDATION_REQUIRED_RELATION(ai.grakn.util.ErrorMessage.VALIDATION_REQUIRED_RELATION) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Type(ai.grakn.concept.Type) Rule(ai.grakn.concept.Rule) Attribute(ai.grakn.concept.Attribute) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Relationship(ai.grakn.concept.Relationship) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) VALIDATION_RELATION_TYPES_ROLES_SCHEMA(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPES_ROLES_SCHEMA) SchemaConceptImpl(ai.grakn.kb.internal.concept.SchemaConceptImpl) Conjunction(ai.grakn.graql.admin.Conjunction) VALIDATION_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPE) Collection(java.util.Collection) Set(java.util.Set) VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE) Collectors(java.util.stream.Collectors) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) VALIDATION_RELATION_CASTING_LOOP_FAIL(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL) Casting(ai.grakn.kb.internal.structure.Casting) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Collections(java.util.Collections) VALIDATION_NOT_EXACTLY_ONE_KEY(ai.grakn.util.ErrorMessage.VALIDATION_NOT_EXACTLY_ONE_KEY) Conjunction(ai.grakn.graql.admin.Conjunction) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) HashSet(java.util.HashSet)

Example 7 with ReasonerQuery

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;
}
Also used : ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) HashSet(java.util.HashSet)

Aggregations

ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)4 HashSet (java.util.HashSet)4 GraqlQueryException (ai.grakn.exception.GraqlQueryException)3 Var (ai.grakn.graql.Var)3 Atomic (ai.grakn.graql.admin.Atomic)3 Conjunction (ai.grakn.graql.admin.Conjunction)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 GraknTx (ai.grakn.GraknTx)2 Concept (ai.grakn.concept.Concept)2 Label (ai.grakn.concept.Label)2 Relationship (ai.grakn.concept.Relationship)2 Role (ai.grakn.concept.Role)2 SchemaConcept (ai.grakn.concept.SchemaConcept)2 Thing (ai.grakn.concept.Thing)2 Answer (ai.grakn.graql.admin.Answer)2 CommonUtil (ai.grakn.util.CommonUtil)2 Collection (java.util.Collection)2 Set (java.util.Set)2