Search in sources :

Example 6 with Conjunction

use of ai.grakn.graql.admin.Conjunction in project grakn by graknlabs.

the class AtomicTest method testRuleApplicability_DerivedTypes.

@Test
public void testRuleApplicability_DerivedTypes() {
    EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
    String typeString = "{$x isa reifying-relation;}";
    String typeString2 = "{$x isa typed-relation;}";
    String typeString3 = "{$x isa description;}";
    String typeString4 = "{$x isa attribute;}";
    String typeString5 = "{$x isa relationship;}";
    Atom type = ReasonerQueries.atomic(conjunction(typeString, graph), graph).getAtom();
    Atom type2 = ReasonerQueries.atomic(conjunction(typeString2, graph), graph).getAtom();
    Atom type3 = ReasonerQueries.atomic(conjunction(typeString3, graph), graph).getAtom();
    Atom type4 = ReasonerQueries.atomic(conjunction(typeString4, graph), graph).getAtom();
    Atom type5 = ReasonerQueries.atomic(conjunction(typeString5, graph), graph).getAtom();
    List<InferenceRule> rules = RuleUtils.getRules(graph).map(r -> new InferenceRule(r, graph)).collect(Collectors.toList());
    assertEquals(2, type.getApplicableRules().count());
    assertEquals(1, type2.getApplicableRules().count());
    assertEquals(3, type3.getApplicableRules().count());
    assertEquals(rules.stream().filter(r -> r.getHead().getAtom().isResource()).count(), type4.getApplicableRules().count());
    assertEquals(rules.stream().filter(r -> r.getHead().getAtom().isRelation()).count(), type5.getApplicableRules().count());
}
Also used : Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Assert.assertThat(org.junit.Assert.assertThat) HashMultimap(com.google.common.collect.HashMultimap) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ClassRule(org.junit.ClassRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Assert.assertEquals(org.junit.Assert.assertEquals) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Test(org.junit.Test)

Example 7 with Conjunction

use of ai.grakn.graql.admin.Conjunction 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)

Aggregations

Conjunction (ai.grakn.graql.admin.Conjunction)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)6 Answer (ai.grakn.graql.admin.Answer)5 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)5 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)5 Schema (ai.grakn.util.Schema)5 Iterables (com.google.common.collect.Iterables)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Concept (ai.grakn.concept.Concept)4 GetQuery (ai.grakn.graql.GetQuery)4 Atomic (ai.grakn.graql.admin.Atomic)4 Patterns (ai.grakn.graql.internal.pattern.Patterns)4 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)4 Test (org.junit.Test)4 Graql.var (ai.grakn.graql.Graql.var)3 Var (ai.grakn.graql.Var)3 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)3 Unifier (ai.grakn.graql.admin.Unifier)3 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)3