use of org.kie.dmn.model.api.Context in project drools by kiegroup.
the class ContextConverter method writeChildren.
@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
super.writeChildren(writer, context, parent);
Context c = (Context) parent;
for (ContextEntry ce : c.getContextEntry()) {
writeChildrenNode(writer, context, ce, CONTEXT_ENTRY);
}
}
use of org.kie.dmn.model.api.Context in project drools by kiegroup.
the class ContextConverter method writeChildren.
@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
super.writeChildren(writer, context, parent);
Context c = (Context) parent;
for (ContextEntry ce : c.getContextEntry()) {
writeChildrenNode(writer, context, ce, CONTEXT_ENTRY);
}
}
use of org.kie.dmn.model.api.Context in project drools by kiegroup.
the class DMNEvaluatorCompiler method compileFunctionDefinitionJAVA.
private DMNExpressionEvaluator compileFunctionDefinitionJAVA(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String functionName, FunctionDefinition funcDef) {
if (funcDef.getExpression() instanceof Context) {
// proceed
Context context = (Context) funcDef.getExpression();
String clazz = null;
String method = null;
for (ContextEntry ce : context.getContextEntry()) {
if (ce.getVariable() != null && ce.getVariable().getName() != null && ce.getExpression() != null && ce.getExpression() instanceof LiteralExpression) {
if (ce.getVariable().getName().equals("class")) {
clazz = stripQuotes(((LiteralExpression) ce.getExpression()).getText().trim());
} else if (ce.getVariable().getName().equals("method signature")) {
method = stripQuotes(((LiteralExpression) ce.getExpression()).getText().trim());
}
}
}
if (clazz != null && method != null) {
String params = funcDef.getFormalParameter().stream().map(p -> p.getName()).collect(Collectors.joining(","));
String expr = String.format("function(%s) external { java: { class: \"%s\", method signature: \"%s\" }}", params, clazz, method);
try {
FEELFunction feelFunction = ctx.getFeelHelper().evaluateFunctionDef(ctx, expr, model, funcDef, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString());
if (feelFunction != null) {
((BaseFEELFunction) feelFunction).setName(functionName);
}
DMNInvocationEvaluator invoker = new DMNInvocationEvaluator(node.getName(), node.getSource(), functionName, null, (fctx, fname) -> feelFunction, // feel can be null as anyway is hardcoded to `feelFunction`
null);
DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node, funcDef);
for (InformationItem p : funcDef.getFormalParameter()) {
DMNCompilerHelper.checkVariableName(model, p, p.getName());
DMNType dmnType = compiler.resolveTypeRef(model, p, p, p.getTypeRef());
func.addParameter(p.getName(), dmnType);
invoker.addParameter(p.getName(), dmnType, (em, dr) -> new EvaluatorResultImpl(dr.getContext().get(p.getName()), EvaluatorResult.ResultType.SUCCESS));
}
func.setEvaluator(invoker);
return func;
} catch (Throwable e) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, e, null, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString(), "Exception raised: " + e.getClass().getSimpleName());
}
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_MISSING_ENTRY, functionName, node.getIdentifierString());
}
} else {
// error, java function definitions require a context
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_BODY_NOT_CONTEXT, node.getIdentifierString());
}
return new DMNFunctionDefinitionEvaluator(node, funcDef);
}
use of org.kie.dmn.model.api.Context in project drools by kiegroup.
the class ValidatorContextTest method testCONTEXT_MISSING_ENTRIES_ReaderInput.
@Test
public void testCONTEXT_MISSING_ENTRIES_ReaderInput() throws IOException {
try (final Reader reader = getReader("context/CONTEXT_MISSING_ENTRIES.dmn")) {
final List<DMNMessage> validate = validator.validate(reader, VALIDATE_SCHEMA, VALIDATE_MODEL, VALIDATE_COMPILATION);
assertThat(ValidatorUtil.formatMessages(validate), validate.size(), is(1));
assertTrue(validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.MISSING_EXPRESSION)));
}
}
use of org.kie.dmn.model.api.Context in project drools by kiegroup.
the class ValidatorContextTest method testCONTEXT_ENTRY_MISSING_VARIABLE_ReaderInput.
@Test
public void testCONTEXT_ENTRY_MISSING_VARIABLE_ReaderInput() throws IOException {
try (final Reader reader = getReader("context/CONTEXT_ENTRY_MISSING_VARIABLE.dmn")) {
final List<DMNMessage> validate = validator.validate(reader, VALIDATE_SCHEMA, VALIDATE_MODEL, VALIDATE_COMPILATION);
assertThat(ValidatorUtil.formatMessages(validate), validate.size(), is(1));
assertTrue(validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.MISSING_VARIABLE)));
// check that it reports and error for the second context entry, but not for the last one
final ContextEntry ce = (ContextEntry) validate.get(0).getSourceReference();
assertThat(((Context) ce.getParent()).getContextEntry().indexOf(ce), is(1));
}
}
Aggregations