use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNRuntimeImpl method evaluateBKM.
private void evaluateBKM(DMNContext context, DMNResultImpl result, BusinessKnowledgeModelNode b, boolean typeCheck) {
BusinessKnowledgeModelNodeImpl bkm = (BusinessKnowledgeModelNodeImpl) b;
if (isNodeValueDefined(result, bkm, bkm)) {
// TODO: do we need to check if the defined variable is a function as it should?
return;
}
// TODO: do we need to check/resolve dependencies?
if (bkm.getEvaluator() == null) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, bkm.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_BKM, getIdentifier(bkm));
return;
}
try {
DMNRuntimeEventManagerUtils.fireBeforeEvaluateBKM(eventManager, bkm, result);
for (DMNNode dep : bkm.getDependencies().values()) {
if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(bkm), getDependencyIdentifier(bkm, dep), MsgUtil.clipString(Objects.toString(result.getContext().get(dep.getName())), 50), ((DMNBaseNode) dep).getType());
return;
}
if (!isNodeValueDefined(result, bkm, dep)) {
boolean walkingIntoScope = walkIntoImportScope(result, bkm, dep);
if (dep instanceof BusinessKnowledgeModelNode) {
evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
} else if (dep instanceof DecisionServiceNode) {
evaluateDecisionService(context, result, (DecisionServiceNode) dep, typeCheck);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getDependencyIdentifier(bkm, dep), getIdentifier(bkm));
return;
}
if (walkingIntoScope) {
result.getContext().popScope();
}
}
}
EvaluatorResult er = bkm.getEvaluator().evaluate(this, result);
if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
final FEELFunction original_fn = (FEELFunction) er.getResult();
FEELFunction resultFn = original_fn;
if (typeCheck) {
DMNType resultType = b.getResultType();
resultFn = new DMNFunctionWithReturnType(original_fn, resultType, result, b);
}
result.getContext().set(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
}
} catch (Throwable t) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, t, null, Msg.ERROR_EVAL_BKM_NODE, getIdentifier(bkm), t.getMessage());
} finally {
DMNRuntimeEventManagerUtils.fireAfterEvaluateBKM(eventManager, bkm, result);
}
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNRuntimeImpl method evaluateDecisionService.
@Override
public DMNResult evaluateDecisionService(DMNModel model, DMNContext context, String decisionServiceName) {
Objects.requireNonNull(model, () -> MsgUtil.createMessage(Msg.PARAM_CANNOT_BE_NULL, "model"));
Objects.requireNonNull(context, () -> MsgUtil.createMessage(Msg.PARAM_CANNOT_BE_NULL, "context"));
Objects.requireNonNull(decisionServiceName, () -> MsgUtil.createMessage(Msg.PARAM_CANNOT_BE_NULL, "decisionServiceName"));
boolean typeCheck = performRuntimeTypeCheck(model);
DMNResultImpl result = createResultImpl(model, context);
// the engine should evaluate all belonging to the "local" model namespace, not imported nodes explicitly.
Optional<DecisionServiceNode> lookupDS = ((DMNModelImpl) model).getDecisionServices().stream().filter(d -> d.getModelNamespace().equals(model.getNamespace())).filter(ds -> ds.getName().equals(decisionServiceName)).findFirst();
if (lookupDS.isPresent()) {
DecisionServiceNodeImpl decisionService = (DecisionServiceNodeImpl) lookupDS.get();
for (DMNNode dep : decisionService.getInputParameters().values()) {
if (!isNodeValueDefined(result, decisionService, dep)) {
DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decisionService.getSource(), result, null, null, Msg.REQ_INPUT_NOT_FOUND_FOR_DS, getDependencyIdentifier(decisionService, dep), getIdentifier(decisionService));
final boolean walkingIntoScope = walkIntoImportScope(result, decisionService, dep);
result.getContext().set(dep.getName(), null);
if (walkingIntoScope) {
result.getContext().popScope();
}
} else {
final boolean walkingIntoScope = walkIntoImportScope(result, decisionService, dep);
final Object originalValue = result.getContext().get(dep.getName());
DMNType depType = ((DMNModelImpl) model).getTypeRegistry().unknown();
if (dep instanceof InputDataNode) {
depType = ((InputDataNode) dep).getType();
} else if (dep instanceof DecisionNode) {
depType = ((DecisionNode) dep).getResultType();
}
Object c = coerceUsingType(originalValue, depType, typeCheck, (r, t) -> MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decisionService.getDecisionService(), result, null, null, Msg.PARAMETER_TYPE_MISMATCH_DS, dep.getName(), t, MsgUtil.clipString(r.toString(), 50)));
if (c != originalValue) {
// intentional by-reference
result.getContext().set(dep.getName(), c);
}
if (walkingIntoScope) {
result.getContext().popScope();
}
}
}
// please note singleton output coercion does not influence anyway when invoked DS on a model.
EvaluatorResult evaluate = new DMNDecisionServiceEvaluator(decisionService, true, false).evaluate(this, result);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, null, result, null, null, Msg.DECISION_SERVICE_NOT_FOUND_FOR_NAME, decisionServiceName);
}
return result;
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DynamicDMNContextBuilder method populateContextForDecisionServiceWith.
public DMNContext populateContextForDecisionServiceWith(String decisionServiceName, Map<String, Object> json) {
DecisionServiceNode dsNode = model.getDecisionServices().stream().filter(ds -> ds.getName().equals(decisionServiceName)).findFirst().orElseThrow(IllegalArgumentException::new);
for (Entry<String, Object> kv : json.entrySet()) {
DecisionServiceNodeImpl dsNodeImpl = (DecisionServiceNodeImpl) dsNode;
DMNNode node = dsNodeImpl.getInputParameters().get(kv.getKey());
if (node instanceof InputDataNode) {
processInputDataNode(kv, (InputDataNode) node);
} else if (node instanceof DecisionNode) {
processDecisionNode(kv, (DecisionNode) node);
} else {
LOG.debug("The key {} was not a RequiredInput nor a RequiredDecision for the DecisionService, setting it as-is.", kv.getKey());
context.set(kv.getKey(), kv.getValue());
}
}
return context;
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DMNCompilerImpl method processDrgElements.
private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Definitions dmndefs) {
for (DRGElement e : dmndefs.getDrgElement()) {
boolean foundIt = false;
for (DRGElementCompiler dc : drgCompilers) {
if (dc.accept(e)) {
foundIt = true;
dc.compileNode(e, this, model);
continue;
}
}
if (!foundIt) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, e, model, null, null, Msg.UNSUPPORTED_ELEMENT, e.getClass().getSimpleName(), e.getId());
}
}
// in DMN v1.1 the DecisionService is not on the DRGElement but as an extension
if (dmndefs.getExtensionElements() != null) {
List<DecisionServices> decisionServices = dmndefs.getExtensionElements().getAny().stream().filter(DecisionServices.class::isInstance).map(DecisionServices.class::cast).collect(Collectors.toList());
for (DecisionServices dss : decisionServices) {
for (DecisionService ds : dss.getDecisionService()) {
// compatibility with DMN v1.1, create Decision Service's variable:
if (ds.getVariable() == null) {
InformationItem variable = new TInformationItem();
variable.setId(UUID.randomUUID().toString());
variable.setName(ds.getName());
variable.setParent(ds);
// the introduction of an on-the-fly ItemDefinition has been removed. The variable type will be evaluated as feel:any, or in v1.2 will receive the (user-defined, explicit) ItemDefinition type.
ds.setVariable(variable);
}
// continuing with normal compilation of Decision Service:
boolean foundIt = false;
for (DRGElementCompiler dc : drgCompilers) {
if (dc.accept(ds)) {
foundIt = true;
dc.compileNode(ds, this, model);
continue;
}
}
}
}
}
for (DecisionServiceNode ds : model.getDecisionServices()) {
DecisionServiceNodeImpl dsi = (DecisionServiceNodeImpl) ds;
dsi.addModelImportAliases(model.getImportAliasesForNS());
for (DRGElementCompiler dc : drgCompilers) {
if (dsi.getEvaluator() == null && dc.accept(dsi)) {
// will compile in fact all DS belonging to this model (not the imported ones).
dc.compileEvaluator(dsi, this, ctx, model);
}
}
}
for (BusinessKnowledgeModelNode bkm : model.getBusinessKnowledgeModels()) {
BusinessKnowledgeModelNodeImpl bkmi = (BusinessKnowledgeModelNodeImpl) bkm;
bkmi.addModelImportAliases(model.getImportAliasesForNS());
for (DRGElementCompiler dc : drgCompilers) {
if (bkmi.getEvaluator() == null && dc.accept(bkm)) {
dc.compileEvaluator(bkm, this, ctx, model);
}
}
}
for (DecisionNode d : model.getDecisions()) {
DecisionNodeImpl di = (DecisionNodeImpl) d;
di.addModelImportAliases(model.getImportAliasesForNS());
for (DRGElementCompiler dc : drgCompilers) {
if (di.getEvaluator() == null && dc.accept(d)) {
dc.compileEvaluator(d, this, ctx, model);
}
}
}
for (AfterProcessDrgElements callback : afterDRGcallbacks) {
logger.debug("About to invoke callback: {}", callback);
callback.callback(this, ctx, model);
}
detectCycles(model);
}
use of org.kie.dmn.api.core.ast.DecisionServiceNode in project drools by kiegroup.
the class DecisionCompiler method loadInCtx.
public static void loadInCtx(DMNBaseNode node, DMNCompilerContext ctx, DMNModelImpl model) {
Map<String, DMNType> importedTypes = new HashMap<>();
for (DMNNode dep : node.getDependencies().values()) {
DMNType depType = null;
if (dep instanceof DecisionNode) {
depType = ((DecisionNode) dep).getResultType();
} else if (dep instanceof InputDataNode) {
depType = ((InputDataNode) dep).getType();
} else if (dep instanceof BusinessKnowledgeModelNode) {
depType = ((BusinessKnowledgeModelNode) dep).getResultType();
} else if (dep instanceof DecisionServiceNode) {
depType = ((DecisionServiceNode) dep).getResultType();
}
if (dep.getModelNamespace().equals(model.getNamespace())) {
// for BKMs might need to create a DMNType for "functions" and replace the type here by that
ctx.setVariable(dep.getName(), depType);
} else {
// then the dependency is an imported dependency.
Optional<String> alias = model.getImportAliasFor(dep.getModelNamespace(), dep.getModelName());
if (alias.isPresent()) {
CompositeTypeImpl importedComposite = (CompositeTypeImpl) importedTypes.computeIfAbsent(alias.get(), a -> new CompositeTypeImpl());
importedComposite.addField(dep.getName(), depType);
}
}
}
for (Entry<String, DMNType> importedType : importedTypes.entrySet()) {
ctx.setVariable(importedType.getKey(), importedType.getValue());
}
}
Aggregations