use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.
the class DMNDTAnalyser method compileTableRules.
private void compileTableRules(DecisionTable dt, DDTATable ddtaTable) {
for (int jRowIdx = 0; jRowIdx < dt.getRule().size(); jRowIdx++) {
DecisionRule r = dt.getRule().get(jRowIdx);
DDTARule ddtaRule = new DDTARule();
int jColIdx = 0;
for (UnaryTests ie : r.getInputEntry()) {
ProcessedUnaryTest compileUnaryTests = (ProcessedUnaryTest) FEEL.compileUnaryTests(ie.getText(), FEEL.newCompilerContext());
UnaryTestInterpretedExecutableExpression interpreted = compileUnaryTests.getInterpreted();
UnaryTestListNode utln = (UnaryTestListNode) interpreted.getASTNode();
DDTAInputClause ddtaInputClause = ddtaTable.getInputs().get(jColIdx);
DDTAInputEntry ddtaInputEntry = new DDTAInputEntry(utln.getElements(), toIntervals(utln.getElements(), utln.isNegated(), ddtaInputClause.getDomainMinMax(), ddtaInputClause.getDiscreteValues(), jRowIdx + 1, jColIdx + 1));
for (Interval interval : ddtaInputEntry.getIntervals()) {
Interval domainMinMax = ddtaTable.getInputs().get(jColIdx).getDomainMinMax();
if (!domainMinMax.includes(interval)) {
throw new IllegalStateException(MsgUtil.createMessage(Msg.DTANALYSIS_ERROR_RULE_OUTSIDE_DOMAIN, jRowIdx + 1, interval, domainMinMax, jColIdx + 1));
}
}
ddtaRule.getInputEntry().add(ddtaInputEntry);
jColIdx++;
}
for (LiteralExpression oe : r.getOutputEntry()) {
ProcessedExpression compile = (ProcessedExpression) FEEL.compile(oe.getText(), FEEL.newCompilerContext());
InterpretedExecutableExpression interpreted = compile.getInterpreted();
BaseNode outputEntryNode = (BaseNode) interpreted.getASTNode();
Comparable<?> value = valueFromNode(outputEntryNode, outputClauseVisitor);
ddtaRule.getOutputEntry().add(value);
jColIdx++;
}
ddtaTable.addRule(ddtaRule);
}
}
use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.
the class MCDCAnalyser method calculateElseRuleIdx.
private void calculateElseRuleIdx() {
if (dt.getHitPolicy() == HitPolicy.PRIORITY) {
// calculate "else" rule if present.
for (int ruleIdx = ddtaTable.getRule().size() - 1; ruleIdx >= 0 && !elseRuleIdx.isPresent(); ruleIdx--) {
DDTARule rule = ddtaTable.getRule().get(ruleIdx);
List<DDTAInputEntry> ie = rule.getInputEntry();
boolean checkAll = true;
for (int colIdx = 0; colIdx < ie.size() && checkAll; colIdx++) {
DDTAInputEntry ieIDX = ie.get(colIdx);
boolean idIDXsize1 = ieIDX.getIntervals().size() == 1;
Interval ieIDXint0 = ieIDX.getIntervals().get(0);
Interval domainMinMax = ddtaTable.getInputs().get(colIdx).getDomainMinMax();
boolean equals = ieIDXint0.equals(domainMinMax);
checkAll &= idIDXsize1 && equals;
}
if (checkAll) {
LOG.debug("I believe P table with else rule: {}", ruleIdx + 1);
elseRuleIdx = Optional.of(ruleIdx);
}
}
}
}
use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.
the class MCDCAnalyser method calculatePosNegBlock.
private Optional<PosNegBlock> calculatePosNegBlock(Integer idx, Object value, Record posCandidate, List<List<?>> allEnumValues) {
List<Comparable<?>> posOutput = posCandidate.output;
List<?> enumValues = allEnumValues.get(idx);
List<?> allOtherEnumValues = new ArrayList<>(enumValues);
allOtherEnumValues.remove(value);
List<Record> negativeRecords = new ArrayList<>();
for (Object otherEnumValue : allOtherEnumValues) {
Object[] negCandidate = Arrays.copyOf(posCandidate.enums, posCandidate.enums.length);
negCandidate[idx] = otherEnumValue;
Record negRecordForNegCandidate = null;
for (int i = 0; negRecordForNegCandidate == null && i < ddtaTable.getRule().size(); i++) {
DDTARule rule = ddtaTable.getRule().get(i);
boolean ruleMatches = ruleMatches(rule, negCandidate);
if (ruleMatches) {
negRecordForNegCandidate = new Record(i, negCandidate, rule.getOutputEntry());
}
}
if (negRecordForNegCandidate != null) {
negativeRecords.add(negRecordForNegCandidate);
}
}
boolean allNegValuesDiffer = true;
for (Record record : negativeRecords) {
allNegValuesDiffer &= !record.output.equals(posOutput);
}
if (allNegValuesDiffer) {
PosNegBlock posNegBlock = new PosNegBlock(idx, posCandidate, negativeRecords);
return Optional.of(posNegBlock);
}
LOG.trace("For In{}={} and candidate positive of {}, it cannot be a matching rule because some negative case had SAME output {}", idx + 1, value, posCandidate, negativeRecords);
return Optional.empty();
}
use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.
the class MCDCAnalyser method ruleIndexesMatchingValues.
private List<Integer> ruleIndexesMatchingValues(Object[] values) {
List<Integer> ruleIndexes = new ArrayList<>();
for (int i = 0; i < ddtaTable.getRule().size(); i++) {
DDTARule rule = ddtaTable.getRule().get(i);
if (ruleMatches(rule, values)) {
ruleIndexes.add(i);
}
}
if (dt.getHitPolicy() == HitPolicy.PRIORITY) {
List<List<Comparable<?>>> outputs = new ArrayList<>();
for (Integer ruleIdx : ruleIndexes) {
DDTARule rule = ddtaTable.getRule().get(ruleIdx);
List<Comparable<?>> ruleOutput = rule.getOutputEntry();
outputs.add(ruleOutput);
}
List<Comparable<?>> computedOutput = new ArrayList<>();
for (int i = 0; i < ddtaTable.getOutputs().size(); i++) {
List outputOrder = ddtaTable.getOutputs().get(i).getOutputOrder();
int outputCursor = Integer.MAX_VALUE;
for (List<Comparable<?>> outs : outputs) {
Comparable<?> out = outs.get(i);
if (outputOrder.indexOf(out) < outputCursor) {
outputCursor = outputOrder.indexOf(out);
}
}
computedOutput.add((Comparable<?>) outputOrder.get(outputCursor));
}
List<Integer> pIndexes = new ArrayList<>();
for (Integer ruleIdx : ruleIndexes) {
DDTARule rule = ddtaTable.getRule().get(ruleIdx);
List<Comparable<?>> ruleOutput = rule.getOutputEntry();
if (ruleOutput.equals(computedOutput)) {
pIndexes.add(ruleIdx);
}
}
return pIndexes;
}
return ruleIndexes;
}
Aggregations