use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.
the class DMNContextEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
DMNResultImpl result = (DMNResultImpl) dmnr;
// when this evaluator is executed, it should either return a Map of key/value pairs
// where keys are the name of the entries and values are the result of the evaluations
// OR if a default result is implemented, it should return the result instead
Map<String, Object> results = new HashMap<>();
DMNContext previousContext = result.getContext();
DMNContext dmnContext = previousContext.clone();
result.setContext(dmnContext);
try {
Set<String> visitedNames = new LinkedHashSet<>();
for (ContextEntryDef ed : entries) {
try {
String entryVarId = getEntryVarId(ed);
String entryExprId = getEntryExprId(ed);
DMNRuntimeEventManagerUtils.fireBeforeEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, result);
if (visitedNames.contains(ed.getName())) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.DUPLICATE_CONTEXT_ENTRY, ed.getName());
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
EvaluatorResult er = ed.getEvaluator().evaluate(eventManager, result);
if (er.getResultType() == ResultType.SUCCESS) {
Object value = er.getResult();
if (!ed.getType().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];
}
if (((DMNRuntimeImpl) eventManager.getRuntime()).performRuntimeTypeCheck(result.getModel())) {
if (!(ed.getContextEntry().getExpression() instanceof FunctionDefinition)) {
// checking directly the result type...
if (ed.getType() != null && !ed.getType().isAssignableValue(value)) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, ed.getName(), ed.getType(), value);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
} else {
// TODO ...will need calculation/inference of function return type.
}
}
results.put(ed.getName(), value);
dmnContext.set(ed.getName(), value);
visitedNames.add(ed.getName());
DMNRuntimeEventManagerUtils.fireAfterEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, value, result);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX, ed.getName(), name);
DMNRuntimeEventManagerUtils.fireAfterEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, er.getResult(), result);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
} catch (Exception e) {
logger.error("Error invoking expression for node '" + name + "'.", e);
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, e, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX_MSG, ed.getName(), name, e.getLocalizedMessage());
String entryVarId = getEntryVarId(ed);
String entryExprId = getEntryExprId(ed);
DMNRuntimeEventManagerUtils.fireAfterEvaluateContextEntry(eventManager, name, ed.getName(), entryVarId, entryExprId, null, result);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
}
} finally {
result.setContext(previousContext);
}
if (results.containsKey(RESULT_ENTRY)) {
return new EvaluatorResultImpl(results.get(RESULT_ENTRY), ResultType.SUCCESS);
} else {
return new EvaluatorResultImpl(results, ResultType.SUCCESS);
}
}
use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.
the class FunctionDefinitionConverter method writeAttributes.
@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
FunctionDefinition fd = (FunctionDefinition) parent;
writer.addAttribute(KIND, fd.getKind().value());
}
use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.
the class FunctionDefinitionConverter method assignAttributes.
@Override
protected void assignAttributes(HierarchicalStreamReader reader, Object parent) {
super.assignAttributes(reader, parent);
FunctionDefinition i = (FunctionDefinition) parent;
String kind = reader.getAttribute(KIND);
if (kind != null) {
i.setKind(FunctionKind.fromValue(kind));
}
}
use of org.kie.dmn.model.api.FunctionDefinition in project drools by kiegroup.
the class FunctionDefinitionConverter method writeAttributes.
@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
FunctionDefinition fd = (FunctionDefinition) parent;
writer.addAttribute(KIND, fd.getKind().value());
}
use of org.kie.dmn.model.api.FunctionDefinition in project kie-wb-common by kiegroup.
the class ContextPropertyConverter method wbFromDMN.
public static Context wbFromDMN(final org.kie.dmn.model.api.Context dmn, final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer) {
final Id id = new Id(dmn.getId());
final Description description = DescriptionPropertyConverter.wbFromDMN(dmn.getDescription());
final QName typeRef = QNamePropertyConverter.wbFromDMN(dmn.getTypeRef(), dmn);
final Context result = new Context(id, description, typeRef);
for (org.kie.dmn.model.api.ContextEntry ce : dmn.getContextEntry()) {
final ContextEntry ceConverted = ContextEntryPropertyConverter.wbFromDMN(ce, hasComponentWidthsConsumer);
if (ceConverted != null) {
ceConverted.setParent(result);
}
result.getContextEntry().add(ceConverted);
}
// No need to append a _default_ row if the Context is part of a JAVA or PMML FunctionDefinition
if (dmn.getParent() instanceof FunctionDefinition) {
final FunctionDefinition functionDefinition = (FunctionDefinition) dmn.getParent();
if (!functionDefinition.getKind().equals(FunctionKind.FEEL)) {
return result;
}
}
// The UI requires a ContextEntry for the _default_ result even if none has been defined
final int contextEntriesCount = result.getContextEntry().size();
if (contextEntriesCount == 0) {
result.getContextEntry().add(new ContextEntry());
} else if (!Objects.isNull(result.getContextEntry().get(contextEntriesCount - 1).getVariable())) {
result.getContextEntry().add(new ContextEntry());
}
return result;
}
Aggregations