use of org.kie.dmn.model.api.LiteralExpression 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.LiteralExpression in project drools by kiegroup.
the class DMNEvaluatorCompiler method compileInvocation.
private DMNExpressionEvaluator compileInvocation(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, Invocation expression) {
Invocation invocation = expression;
// expression must be a literal text with the name of the function
if (invocation.getExpression() == null || ((LiteralExpression) invocation.getExpression()).getText().isEmpty()) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, invocation, model, null, null, Msg.MISSING_EXPRESSION_FOR_INVOCATION, node.getIdentifierString());
return null;
}
String functionName = ((LiteralExpression) invocation.getExpression()).getText();
String[] fnameParts = functionName.split("\\.");
Optional<DMNNode> findAsDep = Optional.empty();
if (fnameParts.length > 1) {
findAsDep = node.getDependencies().values().stream().filter(dmnNode -> dmnNode.getModelImportAliasFor(dmnNode.getModelNamespace(), dmnNode.getModelName()).map(alias -> Objects.equals(functionName, alias + "." + dmnNode.getName())).orElse(false)).findFirst();
} else {
findAsDep = node.getDependencies().values().stream().filter(d -> d.getName().equals(functionName)).findAny();
}
boolean findAsBuiltin = RootExecutionFrame.INSTANCE.getValue(functionName) != null;
boolean findAsCustomFunction = ctx.getFeelHelper().newCompilerContext().getFEELFunctions().stream().anyMatch(f -> f.getName().equals(functionName));
boolean findInContext = ctx.getVariables().get(functionName) != null;
if (!findAsDep.isPresent() && !findAsBuiltin && !findAsCustomFunction && !findInContext) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, invocation, model, null, null, Msg.EXPRESSION_FOR_INVOCATION_NOT_RESOLVED, functionName, node.getIdentifierString(), node.getDependencies().values().stream().map(DMNNode::getName).collect(Collectors.toList()));
}
DMNInvocationEvaluator invEval = new DMNInvocationEvaluator(node.getName(), node.getSource(), functionName, invocation, null, ctx.getFeelHelper().newFEELInstance());
for (Binding binding : invocation.getBinding()) {
if (binding.getParameter() == null) {
// error, missing binding parameter
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, binding, model, null, null, Msg.MISSING_PARAMETER_FOR_INVOCATION, node.getIdentifierString());
return null;
}
if (binding.getExpression() == null) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, binding, model, null, null, Msg.MISSING_EXPRESSION_FOR_PARAM_OF_INVOCATION, binding.getParameter().getIdentifierString(), node.getIdentifierString());
return null;
}
invEval.addParameter(binding.getParameter().getName(), compiler.resolveTypeRef(model, binding.getParameter(), binding.getParameter(), binding.getParameter().getTypeRef()), compileExpression(ctx, model, node, binding.getParameter().getName(), binding.getExpression()));
}
return invEval;
}
use of org.kie.dmn.model.api.LiteralExpression in project drools by kiegroup.
the class TableCellParser method parseCells.
public TableCells parseCells(DecisionTable decisionTable, DTQNameToTypeResolver resolver, String decisionTableName) {
List<DecisionRule> rows = decisionTable.getRule();
List<InputClause> columns = decisionTable.getInput();
TableCells tableCells = new TableCells(rows.size(), columns.size());
parseColumnDefinition(decisionTableName, columns, tableCells);
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
DecisionRule row = rows.get(rowIndex);
for (int inputColumnIndex = 0; inputColumnIndex < row.getInputEntry().size(); inputColumnIndex++) {
String input = row.getInputEntry().get(inputColumnIndex).getText();
TableIndex tableIndex = new TableIndex(rowIndex, inputColumnIndex);
InputClause column = tableIndex.getColumn(columns);
final String columnName = column.getInputExpression().getText();
final Type columnType = resolver.resolve(column.getInputExpression().getTypeRef());
TableCell cell = tableCellFactory.createInputCell(tableIndex, input, columnName, columnType);
tableCells.add(cell);
if (inputColumnIndex == row.getInputEntry().size() - 1) {
// last column
tableCells.initialiseOutputColumnsCollection(row.getOutputEntry().size());
List<LiteralExpression> outputEntry = row.getOutputEntry();
for (int outputColumnIndex = 0; outputColumnIndex < outputEntry.size(); outputColumnIndex++) {
TableIndex outputColumnTableIndex = tableIndex.outputTableIndex(outputColumnIndex);
LiteralExpression outputExpression = outputEntry.get(outputColumnIndex);
String outputRawText = outputExpression.getText();
String outputColumnName = Optional.ofNullable(decisionTable.getOutput().get(outputColumnIndex).getName()).orElse("");
TableCell outputCell = tableCellFactory.createOutputCell(outputColumnTableIndex, outputRawText, outputColumnName, columnType);
tableCells.addOutputCell(outputCell);
}
}
}
}
return tableCells;
}
use of org.kie.dmn.model.api.LiteralExpression in project drools by kiegroup.
the class LiteralExpressionConverter method writeChildren.
@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
super.writeChildren(writer, context, parent);
LiteralExpression le = (LiteralExpression) parent;
if (le.getText() != null)
writeChildrenNodeAsValue(writer, context, le.getText(), TEXT);
if (le.getImportedValues() != null)
writeChildrenNode(writer, context, le.getImportedValues(), IMPORTED_VALUES);
}
use of org.kie.dmn.model.api.LiteralExpression in project drools by kiegroup.
the class LiteralExpressionConverter method writeAttributes.
@Override
protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
super.writeAttributes(writer, parent);
LiteralExpression le = (LiteralExpression) parent;
if (le.getExpressionLanguage() != null)
writer.addAttribute(EXPR_LANGUAGE, le.getExpressionLanguage());
}
Aggregations