use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNRuntimeImpl method evaluateDecision.
private boolean evaluateDecision(DMNContext context, DMNResultImpl result, DecisionNode d, boolean typeCheck) {
DecisionNodeImpl decision = (DecisionNodeImpl) d;
String decisionId = d.getModelNamespace().equals(result.getModel().getNamespace()) ? decision.getId() : decision.getModelNamespace() + "#" + decision.getId();
if (isNodeValueDefined(result, decision, decision)) {
// already resolved
return true;
} else {
// check if the decision was already evaluated before and returned error
DMNDecisionResult.DecisionEvaluationStatus status = Optional.ofNullable(result.getDecisionResultById(decisionId)).map(DMNDecisionResult::getEvaluationStatus).orElse(// it might be an imported Decision.
DMNDecisionResult.DecisionEvaluationStatus.NOT_EVALUATED);
if (FAILED == status || SKIPPED == status || EVALUATING == status) {
return false;
}
}
BeforeEvaluateDecisionEvent beforeEvaluateDecisionEvent = null;
try {
beforeEvaluateDecisionEvent = DMNRuntimeEventManagerUtils.fireBeforeEvaluateDecision(eventManager, decision, result);
boolean missingInput = false;
DMNDecisionResultImpl dr = (DMNDecisionResultImpl) result.getDecisionResultById(decisionId);
if (dr == null) {
// an imported Decision now evaluated, requires the creation of the decision result:
String decisionResultName = d.getName();
Optional<String> importAliasFor = ((DMNModelImpl) result.getModel()).getImportAliasFor(d.getModelNamespace(), d.getModelName());
if (importAliasFor.isPresent()) {
decisionResultName = importAliasFor.get() + "." + d.getName();
}
dr = new DMNDecisionResultImpl(decisionId, decisionResultName);
if (importAliasFor.isPresent()) {
// otherwise is a transitive, skipped and not to be added to the results:
result.addDecisionResult(dr);
}
}
dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.EVALUATING);
for (DMNNode dep : decision.getDependencies().values()) {
try {
if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
missingInput = true;
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(decision), getDependencyIdentifier(decision, dep), MsgUtil.clipString(Objects.toString(result.getContext().get(dep.getName())), 50), ((DMNBaseNode) dep).getType());
reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
}
} catch (Exception e) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, e, null, Msg.ERROR_CHECKING_ALLOWED_VALUES, getDependencyIdentifier(decision, dep), e.getMessage());
}
if (!isNodeValueDefined(result, decision, dep)) {
boolean walkingIntoScope = walkIntoImportScope(result, decision, dep);
if (dep instanceof DecisionNode) {
if (!evaluateDecision(context, result, (DecisionNode) dep, typeCheck)) {
missingInput = true;
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.UNABLE_TO_EVALUATE_DECISION_REQ_DEP, getIdentifier(decision), getDependencyIdentifier(decision, dep));
reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
}
} else if (dep instanceof BusinessKnowledgeModelNode) {
evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
} else if (dep instanceof DecisionServiceNode) {
evaluateDecisionService(context, result, (DecisionServiceNode) dep, typeCheck);
} else {
missingInput = true;
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getDependencyIdentifier(decision, dep), getIdentifier(decision));
reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
}
if (walkingIntoScope) {
result.getContext().popScope();
}
}
}
if (missingInput) {
return false;
}
if (decision.getEvaluator() == null) {
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decision.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_DECISION, getIdentifier(decision));
reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
return false;
}
try {
EvaluatorResult er = decision.getEvaluator().evaluate(this, result);
if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
Object value = er.getResult();
if (!decision.getResultType().isCollection() && value instanceof Collection && ((Collection) value).size() == 1) {
// spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
// and vice-versa
value = ((Collection) value).toArray()[0];
}
try {
if (typeCheck && !d.getResultType().isAssignableValue(value)) {
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, getIdentifier(decision), decision.getResultType(), value);
reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.FAILED);
return false;
}
} catch (Exception e) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, e, null, Msg.ERROR_CHECKING_ALLOWED_VALUES, getIdentifier(decision), e.getMessage());
return false;
}
result.getContext().set(decision.getDecision().getVariable().getName(), value);
dr.setResult(value);
dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED);
} else {
dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.FAILED);
return false;
}
} catch (Throwable t) {
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, t, null, Msg.ERROR_EVAL_DECISION_NODE, getIdentifier(decision), t.getMessage());
reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.FAILED);
}
return true;
} finally {
DMNRuntimeEventManagerUtils.fireAfterEvaluateDecision(eventManager, decision, result, beforeEvaluateDecisionEvent);
}
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNTypeSafeTypeGenerator method processTypes.
public DMNTypeSafeTypeGenerator processTypes() {
Set<InputDataNode> inputs = dmnModel.getInputs();
DMNInputSetType inputSetType = new DMNInputSetType(index, codeGenConfig);
for (InputDataNode i : inputs) {
inputSetType.addField(i.getName(), i.getType());
}
inputSetType.initFields();
inputSetType.setJavadoc(postfixToJavadoc(new StringBuilder("A representation of all the InputData and other DRG Requirement of the whole DMN '").append(dmnModel.getName()).append("' inputs.").toString(), dmnModel));
types.add(inputSetType);
Set<DecisionNode> decisions = dmnModel.getDecisions();
Collection<DecisionServiceNode> decisionServices = dmnModel.getDecisionServices();
Set<BusinessKnowledgeModelNode> bkms = dmnModel.getBusinessKnowledgeModels();
DMNOutputSetType outputSetType = new DMNOutputSetType(index, codeGenConfig);
for (InputDataNode i : inputs) {
// OutputSet also contains inputs
outputSetType.addField(i.getName(), i.getType());
}
for (DecisionNode d : decisions) {
outputSetType.addField(d.getName(), d.getResultType());
}
for (DecisionServiceNode ds : decisionServices) {
outputSetType.addField(ds.getName(), dmnModel.getTypeRegistry().unknown());
}
for (BusinessKnowledgeModelNode bkm : bkms) {
outputSetType.addField(bkm.getName(), dmnModel.getTypeRegistry().unknown());
}
outputSetType.initFields();
outputSetType.setJavadoc(postfixToJavadoc(new StringBuilder("A representation of all the OutputData of the whole DMN '").append(dmnModel.getName()).append("' outputs.").toString(), dmnModel));
types.add(outputSetType);
for (DMNType type : index.typesToGenerateByNS(dmnModel.getNamespace())) {
// this generator shall only be concerned with the types belonging to this generator dmnModel.
DMNDeclaredType dmnDeclaredType = new DMNDeclaredType(index, type, codeGenConfig);
dmnDeclaredType.setJavadoc(postfixToJavadoc(new StringBuilder("A representation of the DMN defined ItemDefinition type '").append(type.getName()).append("'.").toString(), dmnModel));
types.add(dmnDeclaredType);
}
return this;
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNInputRuntimeTest method testOrdering.
@Test
public void testOrdering() {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Order.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_6318588b-c32f-4070-848b-bd8017e6b94e", "Drawing 1");
int index = 1;
for (InputDataNode node : dmnModel.getInputs()) {
assertTrue(node.getName().endsWith("" + index++));
}
index = 1;
for (DecisionNode node : dmnModel.getDecisions()) {
assertTrue(node.getName().endsWith("" + index++));
}
index = 1;
for (BusinessKnowledgeModelNode node : dmnModel.getBusinessKnowledgeModels()) {
assertTrue(node.getName().endsWith("" + index++));
}
index = 1;
for (ItemDefNode node : dmnModel.getItemDefinitions()) {
assertTrue(node.getName().endsWith("" + index++));
}
index = 1;
for (DecisionServiceNode node : dmnModel.getDecisionServices()) {
assertTrue(node.getName().endsWith("" + index++));
}
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNCompilerImpl method linkRequirements.
public void linkRequirements(DMNModelImpl model, DMNBaseNode node) {
for (InformationRequirement ir : node.getInformationRequirement()) {
if (ir.getRequiredInput() != null) {
String id = getId(ir.getRequiredInput());
InputDataNode input = model.getInputById(id);
if (input != null) {
node.addDependency(input.getName(), input);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ir.getRequiredInput(), model, null, null, Msg.REQ_INPUT_NOT_FOUND_FOR_NODE, id, node.getName());
}
} else if (ir.getRequiredDecision() != null) {
String id = getId(ir.getRequiredDecision());
DecisionNode dn = model.getDecisionById(id);
if (dn != null) {
node.addDependency(dn.getName(), dn);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ir.getRequiredDecision(), model, null, null, Msg.REQ_DECISION_NOT_FOUND_FOR_NODE, id, node.getName());
}
}
}
for (KnowledgeRequirement kr : node.getKnowledgeRequirement()) {
if (kr.getRequiredKnowledge() != null) {
String id = getId(kr.getRequiredKnowledge());
BusinessKnowledgeModelNode bkmn = model.getBusinessKnowledgeModelById(id);
DecisionServiceNode dsn = model.getDecisionServiceById(id);
if (bkmn != null) {
node.addDependency(bkmn.getName(), bkmn);
} else if (dsn != null) {
node.addDependency(dsn.getName(), dsn);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, kr.getRequiredKnowledge(), model, null, null, // TODO or a DS ?
Msg.REQ_BKM_NOT_FOUND_FOR_NODE, id, node.getName());
}
}
}
}
Aggregations