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;
}
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();
}
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();
}
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);
}
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();
}
Aggregations