Search in sources :

Example 6 with Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class RuleUtils method getDependentRules.

/**
 * @param query top query
 * @return all rules that are reachable from the entry types
 */
public static Set<InferenceRule> getDependentRules(ReasonerQueryImpl query) {
    final Equivalence<Atom> equivalence = new Equivalence<Atom>() {

        @Override
        protected boolean doEquivalent(Atom a1, Atom a2) {
            return a1.isAlphaEquivalent(a2);
        }

        @Override
        protected int doHash(Atom a) {
            return a.alphaEquivalenceHashCode();
        }
    };
    Set<InferenceRule> rules = new HashSet<>();
    Set<Equivalence.Wrapper<Atom>> visitedAtoms = new HashSet<>();
    Stack<Equivalence.Wrapper<Atom>> atoms = new Stack<>();
    query.selectAtoms().stream().map(equivalence::wrap).forEach(atoms::push);
    while (!atoms.isEmpty()) {
        Equivalence.Wrapper<Atom> wrappedAtom = atoms.pop();
        Atom atom = wrappedAtom.get();
        if (!visitedAtoms.contains(wrappedAtom) && atom != null) {
            atom.getApplicableRules().peek(rules::add).flatMap(rule -> rule.getBody().selectAtoms().stream()).map(equivalence::wrap).filter(at -> !visitedAtoms.contains(at)).filter(at -> !atoms.contains(at)).forEach(atoms::add);
            visitedAtoms.add(wrappedAtom);
        }
    }
    return rules;
}
Also used : HashSet(java.util.HashSet) Stream(java.util.stream.Stream) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Equivalence(com.google.common.base.Equivalence) Iterator(java.util.Iterator) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraknTx(ai.grakn.GraknTx) SchemaConcept(ai.grakn.concept.SchemaConcept) Set(java.util.Set) Schema(ai.grakn.util.Schema) Rule(ai.grakn.concept.Rule) Stack(java.util.Stack) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Stack(java.util.Stack) Equivalence(com.google.common.base.Equivalence) HashSet(java.util.HashSet)

Example 7 with Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class RuleTest method validateIllegalHead.

private void validateIllegalHead(Pattern when, Pattern then, ErrorMessage message) {
    initTx(graknTx);
    Rule rule = graknTx.putRule(UUID.randomUUID().toString(), when, then);
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(message.getMessage(then.toString(), rule.getLabel()));
    graknTx.commit();
}
Also used : Rule(ai.grakn.concept.Rule)

Example 8 with Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class RuleTest method whenCreatingRulesWithNonExistentEntityType_Throw.

@Test
public void whenCreatingRulesWithNonExistentEntityType_Throw() throws InvalidKBException {
    graknTx.putEntityType("My-Type");
    when = graknTx.graql().parser().parsePattern("$x isa Your-Type");
    then = graknTx.graql().parser().parsePattern("$x isa My-Type");
    Rule rule = graknTx.putRule("My-Sad-Rule-Type", when, then);
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(ErrorMessage.VALIDATION_RULE_MISSING_ELEMENTS.getMessage(Schema.VertexProperty.RULE_WHEN.name(), rule.getLabel(), "Your-Type"));
    graknTx.commit();
}
Also used : Rule(ai.grakn.concept.Rule) Test(org.junit.Test)

Example 9 with Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class RuleTest method whenAddingDuplicateRulesOfTheSameTypeWithDifferentPatternVariables_ReturnTheSameRule.

@Ignore("This is ignored because we currently have no way to determine if patterns with different variables name are equivalent")
@Test
public void whenAddingDuplicateRulesOfTheSameTypeWithDifferentPatternVariables_ReturnTheSameRule() {
    graknTx.putEntityType("type1");
    when = graknTx.graql().parser().parsePattern("$x isa type1");
    then = graknTx.graql().parser().parsePattern("$y 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with Rule

use of ai.grakn.concept.Rule in project grakn by graknlabs.

the class RuleTest method validateIllegalRule.

private void validateIllegalRule(Pattern when, Pattern then, ErrorMessage message) {
    initTx(graknTx);
    Rule rule = graknTx.putRule(UUID.randomUUID().toString(), when, then);
    expectedException.expect(InvalidKBException.class);
    expectedException.expectMessage(message.getMessage(rule.getLabel()));
    graknTx.commit();
}
Also used : Rule(ai.grakn.concept.Rule)

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