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