use of org.kie.dmn.model.api.Context in project drools by kiegroup.
the class ValidatorContextTest method testCONTEXT_DUP_ENTRY_ReaderInput.
@Test
public void testCONTEXT_DUP_ENTRY_ReaderInput() throws IOException {
try (final Reader reader = getReader("context/CONTEXT_DUP_ENTRY.dmn")) {
final List<DMNMessage> validate = validator.validate(reader, VALIDATE_SCHEMA, VALIDATE_MODEL, VALIDATE_COMPILATION);
assertThat(ValidatorUtil.formatMessages(validate), validate.size(), is(2));
assertTrue(validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.DUPLICATE_NAME)));
}
}
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 compileFunctionDefinitionPMML.
private DMNExpressionEvaluator compileFunctionDefinitionPMML(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String functionName, FunctionDefinition funcDef) {
if (funcDef.getExpression() instanceof Context) {
Context context = (Context) funcDef.getExpression();
String pmmlDocument = null;
String pmmlModel = null;
for (ContextEntry ce : context.getContextEntry()) {
if (ce.getVariable() != null && ce.getVariable().getName() != null && ce.getExpression() instanceof LiteralExpression) {
LiteralExpression ceLitExpr = (LiteralExpression) ce.getExpression();
if (ce.getVariable().getName().equals("document")) {
if (ceLitExpr.getText() != null) {
pmmlDocument = stripQuotes(ceLitExpr.getText().trim());
}
} else if (ce.getVariable().getName().equals("model")) {
if (ceLitExpr.getText() != null) {
pmmlModel = stripQuotes(ceLitExpr.getText().trim());
}
}
}
}
final String nameLookup = pmmlDocument;
Optional<Import> lookupImport = model.getDefinitions().getImport().stream().filter(x -> x.getName().equals(nameLookup)).findFirst();
if (lookupImport.isPresent()) {
Import theImport = lookupImport.get();
logger.trace("theImport: {}", theImport);
Resource pmmlResource = DMNCompilerImpl.resolveRelativeResource(getRootClassLoader(), model, theImport, funcDef, ctx.getRelativeResolver());
logger.trace("pmmlResource: {}", pmmlResource);
DMNImportPMMLInfo pmmlInfo = model.getPmmlImportInfo().get(pmmlDocument);
logger.trace("pmmlInfo: {}", pmmlInfo);
if (pmmlModel == null || pmmlModel.isEmpty()) {
List<String> pmmlModelNames = pmmlInfo.getModels().stream().map(PMMLModelInfo::getName).filter(x -> x != null).collect(Collectors.toList());
if (pmmlModelNames.size() > 0) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, funcDef, model, null, null, Msg.FUNC_DEF_PMML_MISSING_MODEL_NAME, pmmlModelNames.stream().collect(Collectors.joining(",")));
}
}
AbstractPMMLInvocationEvaluator invoker = PMMLInvocationEvaluatorFactory.newInstance(model, getRootClassLoader(), funcDef, pmmlResource, pmmlModel, pmmlInfo);
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);
}
func.setEvaluator(invoker);
return func;
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_PMML_MISSING_ENTRY, functionName, node.getIdentifierString());
}
} else {
// error, PMML 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 DMNEvaluatorCompiler method compileContext.
private DMNExpressionEvaluator compileContext(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String contextName, Context expression) {
Context ctxDef = expression;
DMNContextEvaluator ctxEval = new DMNContextEvaluator(node.getName(), ctxDef);
ctx.enterFrame();
try {
for (ContextEntry ce : ctxDef.getContextEntry()) {
if (ce.getVariable() != null) {
String entryName = ce.getVariable().getName();
DMNCompilerHelper.checkVariableName(model, node.getSource(), entryName);
DMNType entryType = compiler.resolveTypeRef(model, ce.getVariable(), ce.getVariable(), ce.getVariable().getTypeRef());
// add context entry to the list of available variables for the following entries
ctx.setVariable(entryName, entryType);
DMNExpressionEvaluator evaluator = compileExpression(ctx, model, node, entryName, ce.getExpression());
ctxEval.addEntry(entryName, entryType, evaluator, ce);
} else {
// if the variable is not defined, then it should be the last
// entry in the context and the result of this context evaluation is the
// result of this expression itself
// TODO: if it is not the last entry, raise error message
DMNType type = null;
if (ctxDef.getParent() instanceof ContextEntry && ((ContextEntry) ctxDef.getParent()).getVariable() != null) {
ContextEntry parentEntry = (ContextEntry) ctxDef.getParent();
type = compiler.resolveTypeRef(model, parentEntry.getVariable(), parentEntry.getVariable(), parentEntry.getVariable().getTypeRef());
} else if (node instanceof BusinessKnowledgeModelNode) {
type = ((BusinessKnowledgeModelNode) node).getResultType();
} else if (node instanceof DecisionNode) {
type = ((DecisionNode) node).getResultType();
}
ctxEval.addEntry(DMNContextEvaluator.RESULT_ENTRY, type, compileExpression(ctx, model, node, contextName, ce.getExpression()), ce);
}
}
} finally {
ctx.exitFrame();
}
return ctxEval;
}
Aggregations