com.gs.dmn.signavio.transformation.basic.BasicSignavioDMNToJavaTransformer Maven / Gradle / Ivy
/*
* Copyright 2016 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.gs.dmn.signavio.transformation.basic;
import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.DRGElementReference;
import com.gs.dmn.QualifiedName;
import com.gs.dmn.ast.*;
import com.gs.dmn.context.DMNContext;
import com.gs.dmn.context.environment.EnvironmentFactory;
import com.gs.dmn.dialect.DMNDialectDefinition;
import com.gs.dmn.el.analysis.semantics.type.Type;
import com.gs.dmn.el.analysis.syntax.ast.expression.Expression;
import com.gs.dmn.feel.analysis.semantics.type.FEELFunctionType;
import com.gs.dmn.feel.analysis.syntax.ast.expression.context.Context;
import com.gs.dmn.feel.analysis.syntax.ast.expression.function.FormalParameter;
import com.gs.dmn.feel.analysis.syntax.ast.expression.function.FunctionDefinition;
import com.gs.dmn.feel.analysis.syntax.ast.expression.literal.StringLiteral;
import com.gs.dmn.feel.lib.StringEscapeUtil;
import com.gs.dmn.feel.synthesis.type.NativeTypeFactory;
import com.gs.dmn.runtime.DMNRuntimeException;
import com.gs.dmn.runtime.Pair;
import com.gs.dmn.runtime.metadata.ExtensionElement;
import com.gs.dmn.signavio.SignavioDMNModelRepository;
import com.gs.dmn.signavio.extension.MultiInstanceDecisionLogic;
import com.gs.dmn.transformation.InputParameters;
import com.gs.dmn.transformation.basic.BasicDMNToJavaTransformer;
import com.gs.dmn.transformation.basic.BasicDMNToNativeTransformer;
import com.gs.dmn.transformation.lazy.LazyEvaluationDetector;
import com.gs.dmn.transformation.native_.statement.Statement;
import com.gs.dmn.transformation.proto.ProtoBufferJavaFactory;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class BasicSignavioDMNToJavaTransformer extends BasicDMNToJavaTransformer {
private static final String DECISION_OUTPUT_FIELD_NAME = "value";
private final SignavioDMNModelRepository dmnModelRepository;
public BasicSignavioDMNToJavaTransformer(DMNDialectDefinition, ?, ?, ?, ?, ?> dialect, DMNModelRepository dmnModelRepository, EnvironmentFactory environmentFactory, NativeTypeFactory feelTypeTranslator, LazyEvaluationDetector lazyEvaluationDetector, InputParameters inputParameters) {
super(dialect, dmnModelRepository, environmentFactory, feelTypeTranslator, lazyEvaluationDetector, inputParameters);
this.dmnModelRepository = (SignavioDMNModelRepository) super.getDMNModelRepository();
}
@Override
protected void setProtoBufferFactory(BasicDMNToJavaTransformer transformer) {
this.protoFactory = new ProtoBufferJavaFactory(this);
}
@Override
protected void setDMNEnvironmentFactory(BasicDMNToNativeTransformer transformer) {
this.dmnEnvironmentFactory = new SignavioDMNEnvironmentFactory(transformer);
}
@Override
protected void setExpressionToNativeTransformer(BasicDMNToNativeTransformer transformer) {
this.expressionToNativeTransformer = new SignavioDMNExpressionToNativeTransformer(transformer);
}
//
// BKM
//
@Override
public String drgElementOutputType(TDRGElement element) {
String outputType;
if (this.dmnModelRepository.isBKMLinkedToDecision(element)) {
TDecision outputDecision = this.dmnModelRepository.getOutputDecision((TBusinessKnowledgeModel) element);
outputType = super.drgElementOutputType(outputDecision);
} else if (element instanceof TDecision && this.dmnModelRepository.isFreeTextLiteralExpression(element)) {
Type type = drgElementOutputFEELType(element);
outputType = toNativeType(type);
} else {
outputType = super.drgElementOutputType(element);
}
return this.nativeTypeFactory.nullableType(outputType);
}
public String drgElementOutputFieldName(TDRGElement element, int outputIndex) {
if (this.dmnModelRepository.isDecisionTableExpression(element)) {
TDecisionTable decisionTable = (TDecisionTable) this.dmnModelRepository.expression(element);
return nativeFriendlyVariableName(this.dmnModelRepository.outputClauseName(element, decisionTable.getOutput().get(outputIndex)));
} else if (this.dmnModelRepository.isLiteralExpression(element)) {
return DECISION_OUTPUT_FIELD_NAME;
} else {
TExpression value = this.dmnModelRepository.expression(element);
throw new UnsupportedOperationException(String.format("'%s' is not supported yet ", value.getClass().getSimpleName()));
}
}
public String externalFunctionClassName(Expression body) {
if (body instanceof Context) {
Expression javaExpression = ((Context) body).entry("java").getExpression();
if (javaExpression instanceof Context) {
Expression returnTypeExp = ((Context) javaExpression).entry("class").getExpression();
if (returnTypeExp instanceof StringLiteral) {
String lexeme = ((StringLiteral) returnTypeExp).getLexeme();
return StringEscapeUtil.stripQuotes(lexeme);
}
}
}
throw new DMNRuntimeException(String.format("Missing class in '%s'", body));
}
public String externalFunctionMethodName(Expression body) {
if (body instanceof Context) {
Expression javaExpression = ((Context) body).entry("java").getExpression();
if (javaExpression instanceof Context) {
Expression returnTypeExp = ((Context) javaExpression).entry("methodSignature").getExpression();
if (returnTypeExp instanceof StringLiteral) {
// Signature should be methodName(arg1, arg2, ..., argN)
String lexeme = ((StringLiteral) returnTypeExp).getLexeme();
String signature = StringEscapeUtil.stripQuotes(lexeme);
int index = signature.indexOf('(');
if (index != -1) {
signature = signature.substring(0, index);
}
return signature;
}
}
}
throw new DMNRuntimeException(String.format("Missing methodName in '%s'", body));
}
@Override
public String drgElementSignature(DRGElementReference extends TDRGElement> reference) {
TDRGElement element = reference.getElement();
if (this.dmnModelRepository.isBKMLinkedToDecision(element)) {
TDecision outputDecision = this.dmnModelRepository.getOutputDecision((TBusinessKnowledgeModel) element);
DRGElementReference outputReference = this.dmnModelRepository.makeDRGElementReference(outputDecision);
List> parameters = inputDataParametersClosure(outputReference);
String decisionSignature = parameters.stream().map(p -> this.nativeFactory.nullableParameter(toNativeType(p.getRight()), p.getLeft())).collect(Collectors.joining(", "));
return augmentSignature(decisionSignature);
} else {
return super.drgElementSignature(reference);
}
}
@Override
protected List> bkmParameters(DRGElementReference reference, Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy