Search in sources :

Example 16 with Rule

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;
}
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) SchemaConcept(ai.grakn.concept.SchemaConcept) HashSet(java.util.HashSet)

Example 17 with Rule

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);
}
Also used : Rule(ai.grakn.concept.Rule) Test(org.junit.Test)

Example 18 with Rule

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));
}
Also used : EntityType(ai.grakn.concept.EntityType) Rule(ai.grakn.concept.Rule) Test(org.junit.Test)

Aggregations

Rule (ai.grakn.concept.Rule)18 SchemaConcept (ai.grakn.concept.SchemaConcept)7 Pattern (ai.grakn.graql.Pattern)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Test (org.junit.Test)6 Stream (java.util.stream.Stream)5 GraknTx (ai.grakn.GraknTx)4 Label (ai.grakn.concept.Label)4 Atomic (ai.grakn.graql.admin.Atomic)4 Schema (ai.grakn.util.Schema)4 Role (ai.grakn.concept.Role)3 Conjunction (ai.grakn.graql.admin.Conjunction)3 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)3 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)3 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)3 ErrorMessage (ai.grakn.util.ErrorMessage)3 Collectors (java.util.stream.Collectors)3 Attribute (ai.grakn.concept.Attribute)2 Relationship (ai.grakn.concept.Relationship)2