Search in sources :

Example 16 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable in project drools by kiegroup.

the class DMNCompilerImpl method resolveTypeRef.

/**
 * Resolve the QName typeRef accordingly to definition of builtin (FEEL) types, local model ItemDef or imported definitions.
 * If the typeRef cannot be resolved, (FEEL) UNKNOWN is returned and an error logged using standard DMN message logging.
 */
public DMNType resolveTypeRef(DMNModelImpl dmnModel, NamedElement model, DMNModelInstrumentedBase localElement, QName typeRef) {
    if (typeRef != null) {
        QName nsAndName = getNamespaceAndName(localElement, dmnModel.getImportAliasesForNS(), typeRef, dmnModel.getNamespace());
        DMNType type = dmnModel.getTypeRegistry().resolveType(nsAndName.getNamespaceURI(), nsAndName.getLocalPart());
        if (type == null && localElement.getURIFEEL().equals(nsAndName.getNamespaceURI())) {
            if (model instanceof Decision && ((Decision) model).getExpression() instanceof DecisionTable) {
                DecisionTable dt = (DecisionTable) ((Decision) model).getExpression();
                if (dt.getOutput().size() > 1) {
                    // implicitly define a type for the decision table result
                    CompositeTypeImpl compType = new CompositeTypeImpl(dmnModel.getNamespace(), model.getName() + "_Type", model.getId(), dt.getHitPolicy().isMultiHit());
                    for (OutputClause oc : dt.getOutput()) {
                        DMNType fieldType = resolveTypeRef(dmnModel, model, oc, oc.getTypeRef());
                        compType.addField(oc.getName(), fieldType);
                    }
                    dmnModel.getTypeRegistry().registerType(compType);
                    return compType;
                } else if (dt.getOutput().size() == 1) {
                    return resolveTypeRef(dmnModel, model, dt.getOutput().get(0), dt.getOutput().get(0).getTypeRef());
                }
            }
        } else if (type == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, localElement, dmnModel, null, null, Msg.UNKNOWN_TYPE_REF_ON_NODE, typeRef.toString(), localElement.getParentDRDElement().getIdentifierString());
            type = dmnModel.getTypeRegistry().unknown();
        }
        return type;
    }
    return dmnModel.getTypeRegistry().unknown();
}
Also used : OutputClause(org.kie.dmn.model.api.OutputClause) DecisionTable(org.kie.dmn.model.api.DecisionTable) QName(javax.xml.namespace.QName) Decision(org.kie.dmn.model.api.Decision) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 17 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable in project drools by kiegroup.

the class DMNDTAnalyser method analyse.

@Override
public List<DTAnalysis> analyse(DMNModel model, Set<DMNValidator.Validation> flags) {
    if (!flags.contains(Validation.ANALYZE_DECISION_TABLE)) {
        throw new IllegalArgumentException();
    }
    List<DTAnalysis> results = new ArrayList<>();
    List<? extends DecisionTable> decisionTables = model.getDefinitions().findAllChildren(DecisionTable.class);
    for (DecisionTable dt : decisionTables) {
        try {
            DTAnalysis result = dmnDTAnalysis(model, dt, flags);
            results.add(result);
        } catch (Throwable t) {
            LOG.debug("Skipped dmnDTAnalysis for table: {}", dt.getId(), t);
            DTAnalysis result = DTAnalysis.ofError(dt, t);
            results.add(result);
        }
    }
    return results;
}
Also used : DecisionTable(org.kie.dmn.model.api.DecisionTable) ArrayList(java.util.ArrayList) DTAnalysis(org.kie.dmn.validation.dtanalysis.model.DTAnalysis)

Example 18 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable in project drools by kiegroup.

the class DecisionTableConverter method writeAttributes.

@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
    super.writeAttributes(writer, parent);
    DecisionTable dt = (DecisionTable) parent;
    if (dt.getHitPolicy() != null)
        writer.addAttribute(HIT_POLICY, dt.getHitPolicy().value());
    if (dt.getAggregation() != null)
        writer.addAttribute(AGGREGATION, dt.getAggregation().value());
    if (dt.getPreferredOrientation() != null)
        writer.addAttribute(PREFERRED_ORIENTATION, dt.getPreferredOrientation().value());
    if (dt.getOutputLabel() != null)
        writer.addAttribute(OUTPUT_LABEL, dt.getOutputLabel());
}
Also used : DecisionTable(org.kie.dmn.model.api.DecisionTable) TDecisionTable(org.kie.dmn.model.v1_1.TDecisionTable)

Example 19 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable in project drools by kiegroup.

the class DMNDTAnalysisExceptionTest method smokeTest.

@Test
public void smokeTest() {
    DecisionTable dtRef = new TDecisionTable();
    DMNDTAnalysisException ut = new DMNDTAnalysisException("smoke test", dtRef);
    Assertions.assertThat(ut.getDt()).isEqualTo(dtRef);
}
Also used : DecisionTable(org.kie.dmn.model.api.DecisionTable) TDecisionTable(org.kie.dmn.model.v1_3.TDecisionTable) TDecisionTable(org.kie.dmn.model.v1_3.TDecisionTable) Test(org.junit.Test)

Example 20 with DecisionTable

use of org.kie.dmn.model.api.DecisionTable in project drools by kiegroup.

the class OverlapHitPolicyTest method testOverlapHitPolicy.

@Test
public void testOverlapHitPolicy() {
    Definitions definitions = getDefinitions("OverlapHitPolicy.dmn", "https://github.com/kiegroup/drools/kie-dmn/_3010653A-DD3F-4C88-89DA-3FDD845F6604", "OverlapHitPolicy");
    // mutates XML file in the Hit Policy, accordingly to this test parameter.
    ((DecisionTable) ((Decision) definitions.getDrgElement().get(0)).getExpression()).setHitPolicy(hp);
    List<DMNMessage> validate = validator.validate(definitions, VALIDATE_COMPILATION, ANALYZE_DECISION_TABLE);
    checkAnalysis(validate);
    if (hp == HitPolicy.UNIQUE) {
        assertTrue("It should contain at least 1 DMNMessage for the type", validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_UNIQUE)));
    } else if (hp == HitPolicy.ANY) {
        assertTrue("It should contain at least 1 DMNMessage for the type", validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_ANY)));
    } else {
        LOG.debug("Testing for {} I am expecting there is NOT DMNMessage pertaining to Overlaps", hp);
        assertTrue(validate.stream().noneMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_UNIQUE)) && validate.stream().noneMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP_HITPOLICY_ANY)) && validate.stream().noneMatch(p -> p.getMessageType().equals(DMNMessageType.DECISION_TABLE_OVERLAP)));
    }
}
Also used : Arrays(java.util.Arrays) RangeBoundary(org.kie.dmn.feel.runtime.Range.RangeBoundary) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNMessage(org.kie.dmn.api.core.DMNMessage) Bound(org.kie.dmn.validation.dtanalysis.model.Bound) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) VALIDATE_COMPILATION(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION) RunWith(org.junit.runner.RunWith) Definitions(org.kie.dmn.model.api.Definitions) BigDecimal(java.math.BigDecimal) Hyperrectangle(org.kie.dmn.validation.dtanalysis.model.Hyperrectangle) Decision(org.kie.dmn.model.api.Decision) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Parameterized(org.junit.runners.Parameterized) Interval(org.kie.dmn.validation.dtanalysis.model.Interval) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) HitPolicy(org.kie.dmn.model.api.HitPolicy) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) DTAnalysis(org.kie.dmn.validation.dtanalysis.model.DTAnalysis) Overlap(org.kie.dmn.validation.dtanalysis.model.Overlap) ANALYZE_DECISION_TABLE(org.kie.dmn.validation.DMNValidator.Validation.ANALYZE_DECISION_TABLE) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNMessage(org.kie.dmn.api.core.DMNMessage) Definitions(org.kie.dmn.model.api.Definitions) Test(org.junit.Test)

Aggregations

DecisionTable (org.kie.dmn.model.api.DecisionTable)24 OutputClause (org.kie.dmn.model.api.OutputClause)7 Definitions (org.kie.dmn.model.api.Definitions)6 InputClause (org.kie.dmn.model.api.InputClause)6 DecisionRule (org.kie.dmn.model.api.DecisionRule)5 TDecisionTable (org.kie.dmn.model.v1_2.TDecisionTable)5 DTAnalysis (org.kie.dmn.validation.dtanalysis.model.DTAnalysis)5 DMNMessage (org.kie.dmn.api.core.DMNMessage)4 Decision (org.kie.dmn.model.api.Decision)4 TDecisionTable (org.kie.dmn.model.v1_3.TDecisionTable)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 QName (javax.xml.namespace.QName)3 Test (org.junit.Test)3 BigDecimal (java.math.BigDecimal)2 Collectors (java.util.stream.Collectors)2 DMNContext (org.kie.dmn.api.core.DMNContext)2 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNResult (org.kie.dmn.api.core.DMNResult)2 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)2