use of org.kie.workbench.common.dmn.api.definition.v1_1.Decision in project drools by kiegroup.
the class DMNXMLLoaderTest method testLoadingDefinitions.
@Test
public void testLoadingDefinitions() {
final DMNMarshaller DMNMarshaller = DMNMarshallerFactory.newDefaultMarshaller();
final InputStream is = this.getClass().getResourceAsStream("0001-input-data-string.dmn");
final InputStreamReader isr = new InputStreamReader(is);
final Definitions def = DMNMarshaller.unmarshal(isr);
assertThat(def, not(nullValue()));
assertThat(def.getName(), is("0001-input-data-string"));
assertThat(def.getId(), is("_0001-input-data-string"));
assertThat(def.getNamespace(), is("https://github.com/agilepro/dmn-tck"));
assertThat(def.getDrgElement().size(), is(2));
assertThat(def.getDrgElement().get(0), is(instanceOf(Decision.class)));
Decision dec = (Decision) def.getDrgElement().get(0);
assertThat(dec.getName(), is("Greeting Message"));
assertThat(dec.getId(), is("d_GreetingMessage"));
assertThat(dec.getVariable().getName(), is("Greeting Message"));
assertThat(dec.getVariable().getTypeRef().getPrefix(), is("feel"));
assertThat(dec.getVariable().getTypeRef().getLocalPart(), is("string"));
assertThat(dec.getVariable().getTypeRef().getNamespaceURI(), is(XMLConstants.NULL_NS_URI));
assertThat(dec.getInformationRequirement().size(), is(1));
assertThat(dec.getInformationRequirement().get(0).getRequiredInput().getHref(), is("#i_FullName"));
assertThat(dec.getExpression(), is(instanceOf(LiteralExpression.class)));
LiteralExpression le = (LiteralExpression) dec.getExpression();
assertThat(le.getText(), is("\"Hello \" + Full Name"));
InputData idata = (InputData) def.getDrgElement().get(1);
assertThat(idata.getId(), is("i_FullName"));
assertThat(idata.getName(), is("Full Name"));
assertThat(idata.getVariable().getName(), is("Full Name"));
assertThat(idata.getVariable().getTypeRef().getPrefix(), is("feel"));
assertThat(idata.getVariable().getTypeRef().getLocalPart(), is("string"));
assertThat(idata.getVariable().getTypeRef().getNamespaceURI(), is(XMLConstants.NULL_NS_URI));
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Decision in project drools by kiegroup.
the class DMNEvaluatorCompiler method compileDecisionTable.
private DMNExpressionEvaluator compileDecisionTable(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String dtName, DecisionTable dt) {
java.util.List<DTInputClause> inputs = new ArrayList<>();
int index = 0;
for (InputClause ic : dt.getInput()) {
index++;
String inputExpressionText = ic.getInputExpression().getText();
String inputValuesText = Optional.ofNullable(ic.getInputValues()).map(UnaryTests::getText).orElse(null);
java.util.List<UnaryTest> inputValues = null;
if (inputValuesText != null) {
inputValues = textToUnaryTestList(ctx, inputValuesText, model, ic, Msg.ERR_COMPILING_FEEL_EXPR_ON_DT_INPUT_CLAUSE_IDX, inputValuesText, node.getIdentifierString(), index);
} else if (ic.getInputExpression().getTypeRef() != null) {
QName inputExpressionTypeRef = ic.getInputExpression().getTypeRef();
BaseDMNTypeImpl typeRef = (BaseDMNTypeImpl) model.getTypeRegistry().resolveType(resolveNamespaceForTypeRef(inputExpressionTypeRef, ic.getInputExpression()), inputExpressionTypeRef.getLocalPart());
inputValues = typeRef.getAllowedValuesFEEL();
}
CompiledExpression compiledInput = feel.compileFeelExpression(ctx, inputExpressionText, model, dt, Msg.ERR_COMPILING_FEEL_EXPR_ON_DT_INPUT_CLAUSE_IDX, inputExpressionText, dtName, index);
inputs.add(new DTInputClause(inputExpressionText, inputValuesText, inputValues, compiledInput));
}
java.util.List<DTOutputClause> outputs = new ArrayList<>();
index = 0;
boolean hasOutputValues = false;
for (OutputClause oc : dt.getOutput()) {
String outputName = oc.getName();
if (outputName != null) {
DMNCompilerHelper.checkVariableName(model, node.getSource(), outputName);
}
String id = oc.getId();
String outputValuesText = Optional.ofNullable(oc.getOutputValues()).map(UnaryTests::getText).orElse(null);
String defaultValue = oc.getDefaultOutputEntry() != null ? oc.getDefaultOutputEntry().getText() : null;
BaseDMNTypeImpl typeRef = (BaseDMNTypeImpl) DMNTypeRegistry.UNKNOWN;
java.util.List<UnaryTest> outputValues = null;
if (oc.getTypeRef() != null) {
QName outputExpressionTypeRef = oc.getTypeRef();
typeRef = (BaseDMNTypeImpl) model.getTypeRegistry().resolveType(resolveNamespaceForTypeRef(outputExpressionTypeRef, oc), outputExpressionTypeRef.getLocalPart());
if (typeRef == null) {
typeRef = (BaseDMNTypeImpl) DMNTypeRegistry.UNKNOWN;
}
} else if (dt.getOutput().size() == 1 && (dt.getParent() instanceof Decision || dt.getParent() instanceof BusinessKnowledgeModel || dt.getParent() instanceof ContextEntry)) {
QName inferredTypeRef = recurseUpToInferTypeRef(model, oc, dt);
// if inferredTypeRef is null, a std err will have been reported
if (inferredTypeRef != null) {
typeRef = (BaseDMNTypeImpl) model.getTypeRegistry().resolveType(resolveNamespaceForTypeRef(inferredTypeRef, oc), inferredTypeRef.getLocalPart());
}
}
if (outputValuesText != null) {
outputValues = textToUnaryTestList(ctx, outputValuesText, model, oc, Msg.ERR_COMPILING_FEEL_EXPR_ON_DT_OUTPUT_CLAUSE_IDX, outputValuesText, node.getIdentifierString(), ++index);
} else if (typeRef != DMNTypeRegistry.UNKNOWN) {
outputValues = typeRef.getAllowedValuesFEEL();
}
if (outputValues != null && !outputValues.isEmpty()) {
hasOutputValues = true;
}
outputs.add(new DTOutputClause(outputName, id, outputValues, defaultValue, typeRef.getFeelType(), typeRef.isCollection()));
}
if (dt.getHitPolicy().equals(HitPolicy.PRIORITY) && !hasOutputValues) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, dt.getParent(), model, null, null, Msg.MISSING_OUTPUT_VALUES, dt.getParent());
}
java.util.List<DTDecisionRule> rules = new ArrayList<>();
index = 0;
for (DecisionRule dr : dt.getRule()) {
DTDecisionRule rule = new DTDecisionRule(index);
for (UnaryTests ut : dr.getInputEntry()) {
final java.util.List<UnaryTest> tests;
if (ut == null || ut.getText() == null || ut.getText().isEmpty()) {
tests = Collections.emptyList();
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ut, model, null, null, Msg.DTABLE_EMPTY_ENTRY, dt.getRule().indexOf(dr) + 1, dr.getInputEntry().indexOf(ut) + 1, dt.getParentDRDElement().getIdentifierString());
} else {
tests = textToUnaryTestList(ctx, ut.getText(), model, dr, Msg.ERR_COMPILING_FEEL_EXPR_ON_DT_RULE_IDX, ut.getText(), node.getIdentifierString(), index + 1);
}
rule.getInputEntry().add((c, x) -> tests.stream().anyMatch(t -> {
Boolean result = t.apply(c, x);
return result != null && result == true;
}));
}
for (LiteralExpression le : dr.getOutputEntry()) {
String expressionText = le.getText();
CompiledExpression compiledExpression = feel.compileFeelExpression(ctx, expressionText, model, dr, Msg.ERR_COMPILING_FEEL_EXPR_ON_DT_RULE_IDX, expressionText, dtName, index + 1);
rule.getOutputEntry().add(compiledExpression);
}
rules.add(rule);
index++;
}
String policy = dt.getHitPolicy().value() + (dt.getAggregation() != null ? " " + dt.getAggregation().value() : "");
org.kie.dmn.feel.runtime.decisiontables.HitPolicy hp = org.kie.dmn.feel.runtime.decisiontables.HitPolicy.fromString(policy);
java.util.List<String> parameterNames = new ArrayList<>();
if (node instanceof BusinessKnowledgeModelNode) {
// need to break this statement down and check for nulls
parameterNames.addAll(((BusinessKnowledgeModelNode) node).getBusinessKnowledModel().getEncapsulatedLogic().getFormalParameter().stream().map(f -> f.getName()).collect(toList()));
} else {
parameterNames.addAll(node.getDependencies().keySet());
}
// creates a FEEL instance which will be used by the invoker/impl (s)
FEEL feelInstance = feel.newFEELInstance();
DecisionTableImpl dti = new DecisionTableImpl(dtName, parameterNames, inputs, outputs, rules, hp, feelInstance);
DTInvokerFunction dtf = new DTInvokerFunction(dti);
DMNDTExpressionEvaluator dtee = new DMNDTExpressionEvaluator(node, feelInstance, dtf);
return dtee;
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Decision in project drools by kiegroup.
the class DecisionCompiler method compileNode.
@Override
public void compileNode(DRGElement de, DMNCompilerImpl compiler, DMNModelImpl model) {
Decision decision = (Decision) de;
DecisionNodeImpl dn = new DecisionNodeImpl(decision);
DMNType type = null;
if (decision.getVariable() == null) {
DMNCompilerHelper.reportMissingVariable(model, de, decision, Msg.MISSING_VARIABLE_FOR_DECISION);
return;
}
DMNCompilerHelper.checkVariableName(model, decision, decision.getName());
if (decision.getVariable() != null && decision.getVariable().getTypeRef() != null) {
type = compiler.resolveTypeRef(model, dn, decision, decision.getVariable(), decision.getVariable().getTypeRef());
} else {
type = compiler.resolveTypeRef(model, dn, decision, decision, null);
}
dn.setResultType(type);
model.addDecision(dn);
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Decision in project kie-wb-common by kiegroup.
the class DMNMarshaller method ddExtAugmentStunner.
private void ddExtAugmentStunner(Optional<org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNDiagram> dmnDDDiagram, Node currentNode) {
if (!dmnDDDiagram.isPresent()) {
return;
}
Stream<DMNShape> drgShapeStream = dmnDDDiagram.get().getAny().stream().filter(DMNShape.class::isInstance).map(DMNShape.class::cast);
View content = (View) currentNode.getContent();
if (content.getDefinition() instanceof Decision) {
Decision d = (Decision) content.getDefinition();
internalAugment(drgShapeStream, d.getId(), content.getBounds().getUpperLeft(), d.getDimensionsSet(), content.getBounds().getLowerRight(), d.getBackgroundSet(), d::setFontSet);
} else if (content.getDefinition() instanceof InputData) {
InputData d = (InputData) content.getDefinition();
internalAugment(drgShapeStream, d.getId(), content.getBounds().getUpperLeft(), d.getDimensionsSet(), content.getBounds().getLowerRight(), d.getBackgroundSet(), d::setFontSet);
} else if (content.getDefinition() instanceof BusinessKnowledgeModel) {
BusinessKnowledgeModel d = (BusinessKnowledgeModel) content.getDefinition();
internalAugment(drgShapeStream, d.getId(), content.getBounds().getUpperLeft(), d.getDimensionsSet(), content.getBounds().getLowerRight(), d.getBackgroundSet(), d::setFontSet);
} else if (content.getDefinition() instanceof KnowledgeSource) {
KnowledgeSource d = (KnowledgeSource) content.getDefinition();
internalAugment(drgShapeStream, d.getId(), content.getBounds().getUpperLeft(), d.getDimensionsSet(), content.getBounds().getLowerRight(), d.getBackgroundSet(), d::setFontSet);
} else if (content.getDefinition() instanceof TextAnnotation) {
TextAnnotation d = (TextAnnotation) content.getDefinition();
internalAugment(drgShapeStream, d.getId(), content.getBounds().getUpperLeft(), d.getDimensionsSet(), content.getBounds().getLowerRight(), d.getBackgroundSet(), d::setFontSet);
}
}
use of org.kie.workbench.common.dmn.api.definition.v1_1.Decision in project kie-wb-common by kiegroup.
the class DMNMarshaller method stunnerToDDExt.
private static DMNShape stunnerToDDExt(View<? extends DMNElement> v) {
DMNShape result = new DMNShape();
result.setId("dmnshape-" + v.getDefinition().getId().getValue());
result.setDmnElementRef(v.getDefinition().getId().getValue());
Bounds bounds = new Bounds();
result.setBounds(bounds);
bounds.setX(v.getBounds().getUpperLeft().getX());
bounds.setY(v.getBounds().getUpperLeft().getY());
if (v.getDefinition() instanceof Decision) {
Decision d = (Decision) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getBackgroundSet(), result);
result.setFontStyle(FontSetPropertyConverter.dmnFromWB(d.getFontSet()));
} else if (v.getDefinition() instanceof InputData) {
InputData d = (InputData) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getBackgroundSet(), result);
result.setFontStyle(FontSetPropertyConverter.dmnFromWB(d.getFontSet()));
} else if (v.getDefinition() instanceof BusinessKnowledgeModel) {
BusinessKnowledgeModel d = (BusinessKnowledgeModel) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getBackgroundSet(), result);
result.setFontStyle(FontSetPropertyConverter.dmnFromWB(d.getFontSet()));
} else if (v.getDefinition() instanceof KnowledgeSource) {
KnowledgeSource d = (KnowledgeSource) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getBackgroundSet(), result);
result.setFontStyle(FontSetPropertyConverter.dmnFromWB(d.getFontSet()));
} else if (v.getDefinition() instanceof TextAnnotation) {
TextAnnotation d = (TextAnnotation) v.getDefinition();
applyBounds(d.getDimensionsSet(), bounds);
applyBackgroundStyles(d.getBackgroundSet(), result);
result.setFontStyle(FontSetPropertyConverter.dmnFromWB(d.getFontSet()));
}
return result;
}
Aggregations