use of org.kie.dmn.model.api.InformationItem 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.InformationItem in project drools by kiegroup.
the class DecisionServiceCompiler method checkFnConsistency.
private void checkFnConsistency(DMNModelImpl model, DecisionServiceNodeImpl ni, DMNType type, List<DecisionNode> outputDecisions) {
SimpleFnTypeImpl fnType = ((SimpleFnTypeImpl) type);
FunctionItem fi = fnType.getFunctionItem();
if (fi.getParameters().size() != ni.getInputParameters().size()) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.PARAMETER_COUNT_MISMATCH_COMPILING, ni.getName(), fi.getParameters().size(), ni.getInputParameters().size());
return;
}
for (int i = 0; i < fi.getParameters().size(); i++) {
InformationItem fiII = fi.getParameters().get(i);
String fpName = ni.getInputParameters().keySet().stream().skip(i).findFirst().orElse(null);
if (!fiII.getName().equals(fpName)) {
List<String> fiParamNames = fi.getParameters().stream().map(InformationItem::getName).collect(Collectors.toList());
List<String> funcDefParamNames = ni.getInputParameters().keySet().stream().collect(Collectors.toList());
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.PARAMETER_NAMES_MISMATCH_COMPILING, ni.getName(), fiParamNames, funcDefParamNames);
return;
}
QName fiQname = fiII.getTypeRef();
QName fdQname = null;
DMNNode fpDMNNode = ni.getInputParameters().get(fpName);
if (fpDMNNode instanceof InputDataNodeImpl) {
fdQname = ((InputDataNodeImpl) fpDMNNode).getInputData().getVariable().getTypeRef();
} else if (fpDMNNode instanceof DecisionNodeImpl) {
fdQname = ((DecisionNodeImpl) fpDMNNode).getDecision().getVariable().getTypeRef();
}
if (fiQname != null && fdQname != null && !fiQname.equals(fdQname)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.PARAMETER_TYPEREF_MISMATCH_COMPILING, ni.getName(), fiII.getName(), fiQname, fdQname);
}
}
QName fiReturnType = fi.getOutputTypeRef();
if (ni.getDecisionService().getOutputDecision().size() == 1) {
QName fdReturnType = outputDecisions.get(0).getDecision().getVariable().getTypeRef();
if (fiReturnType != null && fdReturnType != null && !fiReturnType.equals(fdReturnType)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, ni.getName(), fiReturnType, fdReturnType);
}
} else if (ni.getDecisionService().getOutputDecision().size() > 1) {
final Function<QName, QName> lookupFn = (in) -> DMNCompilerImpl.getNamespaceAndName(ni.getDecisionService(), model.getImportAliasesForNS(), in, model.getNamespace());
LinkedHashMap<String, QName> fdComposite = new LinkedHashMap<>();
for (DecisionNode dn : outputDecisions) {
fdComposite.put(dn.getName(), lookupFn.apply(dn.getDecision().getVariable().getTypeRef()));
}
final QName lookup = lookupFn.apply(fiReturnType);
Optional<ItemDefNodeImpl> composite = model.getItemDefinitions().stream().filter(id -> id.getModelNamespace().equals(lookup.getNamespaceURI()) && id.getName().equals(lookup.getLocalPart())).map(ItemDefNodeImpl.class::cast).findFirst();
if (!composite.isPresent()) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, ni.getName(), lookup, fdComposite);
return;
}
LinkedHashMap<String, QName> fiComposite = new LinkedHashMap<>();
for (ItemDefinition ic : composite.get().getItemDef().getItemComponent()) {
fiComposite.put(ic.getName(), lookupFn.apply(ic.getTypeRef()));
}
if (!fiComposite.equals(fdComposite)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, ni.getName(), fiComposite, fdComposite);
}
}
}
use of org.kie.dmn.model.api.InformationItem 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.model.api.InformationItem in project drools by kiegroup.
the class XLS2DMNParser method appendInputData.
private void appendInputData(Definitions definitions, Map<String, DTHeaderInfo> headerInfos) {
Set<String> usedRI = new LinkedHashSet<>();
for (DTHeaderInfo hi : headerInfos.values()) {
for (String ri : hi.getRequiredInput()) {
if (!usedRI.contains(ri)) {
InputData id = new TInputData();
id.setName(ri);
id.setId("id_" + CodegenStringUtil.escapeIdentifier(ri));
InformationItem variable = new TInformationItem();
variable.setName(ri);
variable.setId("idvar_" + CodegenStringUtil.escapeIdentifier(ri));
variable.setTypeRef(new QName("Any"));
id.setVariable(variable);
definitions.getDrgElement().add(id);
}
usedRI.add(ri);
}
}
}
use of org.kie.dmn.model.api.InformationItem in project drools by kiegroup.
the class XLS2DMNParser method appendDecisionDT.
private void appendDecisionDT(Definitions definitions, Map<String, DTHeaderInfo> headerInfos) {
for (DTHeaderInfo hi : headerInfos.values()) {
Decision decision = new TDecision();
decision.setName(hi.getSheetName());
decision.setId("d_" + CodegenStringUtil.escapeIdentifier(hi.getSheetName()));
InformationItem variable = new TInformationItem();
variable.setName(hi.getSheetName());
variable.setId("dvar_" + CodegenStringUtil.escapeIdentifier(hi.getSheetName()));
variable.setTypeRef(new QName("Any"));
decision.setVariable(variable);
for (String ri : hi.getRequiredInput()) {
InformationRequirement ir = new TInformationRequirement();
DMNElementReference er = new TDMNElementReference();
er.setHref("#id_" + CodegenStringUtil.escapeIdentifier(ri));
ir.setRequiredInput(er);
decision.getInformationRequirement().add(ir);
}
for (String ri : hi.getRequiredDecision()) {
InformationRequirement ir = new TInformationRequirement();
DMNElementReference er = new TDMNElementReference();
er.setHref("#d_" + CodegenStringUtil.escapeIdentifier(ri));
ir.setRequiredDecision(er);
decision.getInformationRequirement().add(ir);
}
DecisionTable dt = new TDecisionTable();
dt.setOutputLabel(hi.getSheetName());
dt.setId("ddt_" + CodegenStringUtil.escapeIdentifier(hi.getSheetName()));
dt.setHitPolicy(HitPolicy.ANY);
for (String ri : hi.getRequiredInput()) {
InputClause ic = new TInputClause();
ic.setLabel(ri);
LiteralExpression le = new TLiteralExpression();
le.setText(ri);
ic.setInputExpression(le);
dt.getInput().add(ic);
}
for (String rd : hi.getRequiredDecision()) {
InputClause ic = new TInputClause();
ic.setLabel(rd);
LiteralExpression le = new TLiteralExpression();
le.setText(rd);
ic.setInputExpression(le);
dt.getInput().add(ic);
}
OutputClause oc = new TOutputClause();
dt.getOutput().add(oc);
decision.setExpression(dt);
definitions.getDrgElement().add(decision);
}
}
Aggregations