use of org.kie.dmn.core.impl.SimpleFnTypeImpl 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.core.impl.SimpleFnTypeImpl in project drools by kiegroup.
the class DecisionServiceCompiler method compileNode.
/**
* backport of DMN v1.1
*/
public void compileNode(DecisionService ds, DMNCompilerImpl compiler, DMNModelImpl model) {
DMNType type = null;
DMNType fnType = null;
if (ds.getVariable() == null) {
// even for the v1.1 backport, variable creation is taken care in DMNCompiler.
DMNCompilerHelper.reportMissingVariable(model, ds, ds, Msg.MISSING_VARIABLE_FOR_DS);
return;
}
DMNCompilerHelper.checkVariableName(model, ds, ds.getName());
if (ds.getVariable() != null && ds.getVariable().getTypeRef() != null) {
type = compiler.resolveTypeRef(model, ds, ds.getVariable(), ds.getVariable().getTypeRef());
if (type instanceof SimpleFnTypeImpl) {
fnType = type;
type = ((SimpleFnTypeImpl) type).getReturnType();
}
} else {
// for now the call bellow will return type UNKNOWN
type = compiler.resolveTypeRef(model, ds, ds, null);
}
DecisionServiceNodeImpl bkmn = new DecisionServiceNodeImpl(ds, fnType, type);
model.addDecisionService(bkmn);
}
use of org.kie.dmn.core.impl.SimpleFnTypeImpl in project drools by kiegroup.
the class BusinessKnowledgeModelCompiler method compileNode.
@Override
public void compileNode(DRGElement de, DMNCompilerImpl compiler, DMNModelImpl model) {
BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) de;
BusinessKnowledgeModelNodeImpl bkmn = new BusinessKnowledgeModelNodeImpl(bkm);
DMNType type = null;
DMNType fnType = null;
if (bkm.getVariable() == null) {
DMNCompilerHelper.reportMissingVariable(model, de, bkm, Msg.MISSING_VARIABLE_FOR_BKM);
return;
}
DMNCompilerHelper.checkVariableName(model, bkm, bkm.getName());
if (bkm.getVariable().getTypeRef() != null) {
// variable must be present, otherwise error was already reported above.
type = compiler.resolveTypeRef(model, bkm, bkm.getVariable(), bkm.getVariable().getTypeRef());
if (type instanceof SimpleFnTypeImpl) {
fnType = type;
type = ((SimpleFnTypeImpl) type).getReturnType();
}
// consistency checks
if (bkm.getEncapsulatedLogic() != null && bkm.getEncapsulatedLogic().getTypeRef() != null) {
DMNType bkmELType = compiler.resolveTypeRef(model, bkm, bkm.getEncapsulatedLogic(), bkm.getEncapsulatedLogic().getTypeRef());
if (!areSameDMNType(fnType != null ? fnType : type, bkmELType)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.WARN, bkm.getEncapsulatedLogic(), model, null, null, Msg.VARIABLE_TYPE_MISMATCH_FOR_BKM_EL, bkm.getEncapsulatedLogic().getTypeRef(), bkm.getVariable().getTypeRef());
}
}
if (fnType == null && bkm.getEncapsulatedLogic() != null && bkm.getEncapsulatedLogic().getExpression() != null && bkm.getEncapsulatedLogic().getExpression().getTypeRef() != null) {
// if fnType != null, this is already taken care of DROOLS-6488 in method checkFnConsistency, further below.
DMNType elExprType = compiler.resolveTypeRef(model, bkm, bkm.getEncapsulatedLogic().getExpression(), bkm.getEncapsulatedLogic().getExpression().getTypeRef());
if (!areSameDMNType(type, elExprType)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.WARN, bkm.getEncapsulatedLogic(), model, null, null, Msg.VARIABLE_TYPE_MISMATCH_FOR_BKM_EL_BODY, bkm.getEncapsulatedLogic().getExpression().getTypeRef(), type.getName());
}
}
} else if (bkm.getVariable().getTypeRef() == null && bkm.getEncapsulatedLogic().getExpression() != null && bkm.getEncapsulatedLogic().getExpression().getTypeRef() != null) {
type = compiler.resolveTypeRef(model, bkm, bkm.getEncapsulatedLogic().getExpression(), bkm.getEncapsulatedLogic().getExpression().getTypeRef());
} else {
// for now the call bellow will return type UNKNOWN
type = compiler.resolveTypeRef(model, bkm, bkm, null);
}
bkmn.setType(fnType);
bkmn.setResultType(type);
model.addBusinessKnowledgeModel(bkmn);
}
use of org.kie.dmn.core.impl.SimpleFnTypeImpl in project drools by kiegroup.
the class DMNCompilerImpl method buildTypeDef.
/**
* @param topLevel null if it is a top level ItemDefinition
*/
private DMNType buildTypeDef(DMNCompilerContext ctx, DMNModelImpl dmnModel, DMNNode node, ItemDefinition itemDef, DMNType topLevel) {
BaseDMNTypeImpl type = null;
if (itemDef.getTypeRef() != null) {
// this is a reference to an existing type, so resolve the reference
type = (BaseDMNTypeImpl) resolveTypeRef(dmnModel, itemDef, itemDef, itemDef.getTypeRef());
if (type != null) {
UnaryTests allowedValuesStr = itemDef.getAllowedValues();
// or if it changes the metadata for the base type
if (topLevel == null || allowedValuesStr != null || itemDef.isIsCollection() != type.isCollection()) {
// we have to clone this type definition into a new one
String name = itemDef.getName();
String namespace = dmnModel.getNamespace();
String id = itemDef.getId();
BaseDMNTypeImpl baseType = type;
Type baseFEELType = type.getFeelType();
if (baseFEELType instanceof BuiltInType) {
// Then it is an ItemDefinition in place for "aliasing" a base FEEL type, for having type(itemDefname) I need to define its SimpleType.
baseFEELType = new AliasFEELType(itemDef.getName(), (BuiltInType) baseFEELType);
}
List<UnaryTest> av = null;
if (allowedValuesStr != null) {
av = ctx.getFeelHelper().evaluateUnaryTests(ctx, allowedValuesStr.getText(), dmnModel, itemDef, Msg.ERR_COMPILING_ALLOWED_VALUES_LIST_ON_ITEM_DEF, allowedValuesStr.getText(), node.getName());
}
boolean isCollection = itemDef.isIsCollection();
if (isCollection) {
baseFEELType = new GenListType(baseFEELType);
}
if (type instanceof CompositeTypeImpl) {
CompositeTypeImpl compositeTypeImpl = (CompositeTypeImpl) type;
type = new CompositeTypeImpl(namespace, name, id, isCollection, compositeTypeImpl.getFields(), baseType, baseFEELType);
} else if (type instanceof SimpleTypeImpl) {
type = new SimpleTypeImpl(namespace, name, id, isCollection, av, baseType, baseFEELType);
}
if (topLevel != null) {
((BaseDMNTypeImpl) type).setBelongingType(topLevel);
}
}
if (topLevel == null) {
DMNType registered = dmnModel.getTypeRegistry().registerType(type);
if (registered != type) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
}
}
}
} else if (itemDef.getItemComponent() != null && itemDef.getItemComponent().size() > 0) {
// first, locate preregistered or create anonymous inner composite
if (topLevel == null) {
type = (CompositeTypeImpl) dmnModel.getTypeRegistry().resolveType(dmnModel.getNamespace(), itemDef.getName());
} else {
DMNCompilerHelper.checkVariableName(dmnModel, itemDef, itemDef.getName());
type = new CompositeTypeImpl(dmnModel.getNamespace(), itemDef.getName(), itemDef.getId(), itemDef.isIsCollection());
((BaseDMNTypeImpl) type).setBelongingType(topLevel);
}
// second, add fields to located composite
for (ItemDefinition fieldDef : itemDef.getItemComponent()) {
DMNCompilerHelper.checkVariableName(dmnModel, fieldDef, fieldDef.getName());
DMNType fieldType = buildTypeDef(ctx, dmnModel, node, fieldDef, type);
fieldType = fieldType != null ? fieldType : dmnModel.getTypeRegistry().unknown();
((CompositeTypeImpl) type).addField(fieldDef.getName(), fieldType);
}
} else if (isFunctionItem(itemDef)) {
FunctionItem fi = itemDef.getFunctionItem();
String name = itemDef.getName();
String namespace = dmnModel.getNamespace();
String id = itemDef.getId();
Map<String, DMNType> params = new HashMap<>();
for (InformationItem p : fi.getParameters()) {
DMNType resolveTypeRef = resolveTypeRef(dmnModel, itemDef, itemDef, p.getTypeRef());
params.put(p.getName(), resolveTypeRef);
}
DMNType returnType = resolveTypeRef(dmnModel, itemDef, itemDef, fi.getOutputTypeRef());
List<Type> feelPs = fi.getParameters().stream().map(InformationItem::getName).map(n -> ((BaseDMNTypeImpl) params.get(n)).getFeelType()).collect(Collectors.toList());
GenFnType feeltype = new GenFnType(feelPs, ((BaseDMNTypeImpl) returnType).getFeelType());
type = new SimpleFnTypeImpl(namespace, name, id, feeltype, params, returnType, fi);
DMNType registered = dmnModel.getTypeRegistry().registerType(type);
if (registered != type) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
}
} else {
DMNType unknown = (BaseDMNTypeImpl) resolveTypeRef(dmnModel, itemDef, itemDef, null);
type = new SimpleTypeImpl(dmnModel.getNamespace(), itemDef.getName(), itemDef.getId(), itemDef.isIsCollection(), null, unknown, ((BaseDMNTypeImpl) unknown).getFeelType());
if (topLevel == null) {
DMNType registered = dmnModel.getTypeRegistry().registerType(type);
if (registered != type) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
}
} else {
((BaseDMNTypeImpl) type).setBelongingType(topLevel);
}
}
return type;
}
use of org.kie.dmn.core.impl.SimpleFnTypeImpl in project drools by kiegroup.
the class BusinessKnowledgeModelCompiler method checkFnConsistency.
private void checkFnConsistency(DMNModelImpl model, BusinessKnowledgeModelNodeImpl bkmi, DMNType type, FunctionDefinition funcDef) {
SimpleFnTypeImpl fnType = ((SimpleFnTypeImpl) type);
FunctionItem fi = fnType.getFunctionItem();
if (fi.getParameters().size() != funcDef.getFormalParameter().size()) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, bkmi.getBusinessKnowledModel(), model, null, null, Msg.PARAMETER_COUNT_MISMATCH_COMPILING, bkmi.getName(), fi.getParameters().size(), funcDef.getFormalParameter().size());
return;
}
for (int i = 0; i < fi.getParameters().size(); i++) {
InformationItem fiII = fi.getParameters().get(i);
InformationItem fdII = funcDef.getFormalParameter().get(i);
if (!fiII.getName().equals(fdII.getName())) {
List<String> fiParamNames = fi.getParameters().stream().map(InformationItem::getName).collect(Collectors.toList());
List<String> funcDefParamNames = funcDef.getFormalParameter().stream().map(InformationItem::getName).collect(Collectors.toList());
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, bkmi.getBusinessKnowledModel(), model, null, null, Msg.PARAMETER_NAMES_MISMATCH_COMPILING, bkmi.getName(), fiParamNames, funcDefParamNames);
return;
}
QName fiQname = fiII.getTypeRef();
QName fdQname = fdII.getTypeRef();
if (fiQname != null && fdQname != null && !fiQname.equals(fdQname)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, bkmi.getBusinessKnowledModel(), model, null, null, Msg.PARAMETER_TYPEREF_MISMATCH_COMPILING, bkmi.getName(), fiII.getName(), fiQname, fdQname);
}
}
QName fiReturnType = fi.getOutputTypeRef();
QName fdReturnType = funcDef.getExpression().getTypeRef();
if (fiReturnType != null && fdReturnType != null && !fiReturnType.equals(fdReturnType)) {
MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, bkmi.getBusinessKnowledModel(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, bkmi.getName(), fiReturnType, fdReturnType);
}
}
Aggregations