Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata for standard dialect #686

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@
*/
package com.gs.dmn.transformation;

import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.ast.TDefinitions;
import com.gs.dmn.context.DMNContext;
import com.gs.dmn.dialect.DMNDialectDefinition;
import com.gs.dmn.el.analysis.semantics.type.Type;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.runtime.DMNRuntimeException;
import com.gs.dmn.runtime.metadata.DMNMetadata;
import com.gs.dmn.serialization.JsonSerializer;
import com.gs.dmn.serialization.TypeDeserializationConfigurer;
import com.gs.dmn.tck.ast.TestCases;
import com.gs.dmn.transformation.basic.BasicDMNToNativeTransformer;
import com.gs.dmn.transformation.lazy.LazyEvaluationDetector;
import com.gs.dmn.transformation.template.TemplateProvider;
import com.gs.dmn.validation.DMNValidator;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

public class DMNToJavaTransformer<NUMBER, DATE, TIME, DATE_TIME, DURATION> extends AbstractDMNToNativeTransformer<NUMBER, DATE, TIME, DATE_TIME, DURATION, TestCases> {
public static final String DMN_METADATA_FILE_NAME = "DMNMetadata";

public DMNToJavaTransformer(DMNDialectDefinition<NUMBER, DATE, TIME, DATE_TIME, DURATION, TestCases> dialectDefinition, DMNValidator dmnValidator, DMNTransformer<TestCases> dmnTransformer, TemplateProvider templateProvider, LazyEvaluationDetector lazyEvaluationDetector, TypeDeserializationConfigurer typeDeserializationConfigurer, InputParameters inputParameters, BuildLogger logger) {
super(dialectDefinition, dmnValidator, dmnTransformer, templateProvider, lazyEvaluationDetector, typeDeserializationConfigurer, inputParameters, logger);
}
Expand All @@ -29,4 +44,40 @@ public DMNToJavaTransformer(DMNDialectDefinition<NUMBER, DATE, TIME, DATE_TIME,
protected String getFileExtension() {
return ".java";
}

@Override
protected void transform(BasicDMNToNativeTransformer<Type, DMNContext> dmnTransformer, DMNModelRepository dmnModelRepository, Path outputPath) {
super.transform(dmnTransformer, dmnModelRepository, outputPath);

// Generate metadata
processManifest(dmnTransformer, DMN_METADATA_FILE_NAME, outputPath);
}

private void processManifest(BasicDMNToNativeTransformer<Type, DMNContext> dmnTransformer, String jsonFileName, Path outputPath) {
String javaPackageName = dmnTransformer.nativeRootPackageName();
String filePath = javaPackageName.replace('.', '/');
String fileExtension = ".json";

try {
DMNToManifestTransformer dmnToManifestTransformer = new DMNToManifestTransformer(dmnTransformer, logger);
List<String> dmnNamespaces = getNamespaces(dmnTransformer.getDMNModelRepository().getAllDefinitions());
String nativeNamespace = dmnTransformer.nativeRootPackageName();
String dmnVersion = this.inputParameters.getDmnVersion();
String modelVersion = this.inputParameters.getModelVersion();
String platformVersion = this.inputParameters.getPlatformVersion();
DMNMetadata manifest = dmnToManifestTransformer.toManifest(dmnNamespaces, nativeNamespace, dmnVersion, modelVersion, platformVersion);
File resultFile = this.templateProcessor.makeOutputFile(outputPath, filePath, jsonFileName, fileExtension);
JsonSerializer.OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(resultFile, manifest);
} catch (Exception e) {
throw new DMNRuntimeException("Cannot process manifest file", e);
}
}

private List<String> getNamespaces(List<TDefinitions> allDefinitions) {
if (allDefinitions == null) {
return null;
} else {
return allDefinitions.stream().map(TDefinitions::getNamespace).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.gs.dmn.transformation.template.TemplateProvider;
import com.gs.dmn.validation.DMNValidator;

public class DMNToKotlinTransformer<NUMBER, DATE, TIME, DATE_TIME, DURATION> extends AbstractDMNToNativeTransformer<NUMBER, DATE, TIME, DATE_TIME, DURATION, TestCases> {
public class DMNToKotlinTransformer<NUMBER, DATE, TIME, DATE_TIME, DURATION> extends DMNToJavaTransformer<NUMBER, DATE, TIME, DATE_TIME, DURATION> {
public DMNToKotlinTransformer(DMNDialectDefinition<NUMBER, DATE, TIME, DATE_TIME, DURATION, TestCases> dialectDefinition, DMNValidator dmnValidator, DMNTransformer<TestCases> dmnTransformer, TemplateProvider templateProvider, LazyEvaluationDetector lazyEvaluationDetector, TypeDeserializationConfigurer typeDeserializationConfigurer, InputParameters inputParameters, BuildLogger logger) {
super(dialectDefinition, dmnValidator, dmnTransformer, templateProvider, lazyEvaluationDetector, typeDeserializationConfigurer, inputParameters, logger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
* "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;
package com.gs.dmn.transformation;

import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.QualifiedName;
import com.gs.dmn.ast.*;
import com.gs.dmn.context.DMNContext;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.runtime.DMNRuntimeException;
import com.gs.dmn.runtime.metadata.*;
import com.gs.dmn.signavio.SignavioDMNModelRepository;
import com.gs.dmn.signavio.transformation.basic.BasicSignavioDMNToJavaTransformer;
import com.gs.dmn.transformation.basic.BasicDMNToNativeTransformer;

import java.util.ArrayList;
Expand All @@ -29,20 +28,22 @@
import static com.gs.dmn.serialization.DMNVersion.DMN_11;

public class DMNToManifestTransformer {
private final BasicDMNToNativeTransformer<com.gs.dmn.el.analysis.semantics.type.Type, DMNContext> dmnTransformer;
private final DMNModelRepository dmnModelRepository;
protected final BasicDMNToNativeTransformer<com.gs.dmn.el.analysis.semantics.type.Type, DMNContext> dmnTransformer;
protected final DMNModelRepository dmnModelRepository;
private final BuildLogger logger;

public DMNToManifestTransformer(BasicDMNToNativeTransformer<com.gs.dmn.el.analysis.semantics.type.Type, DMNContext> dmnTransformer) {
public DMNToManifestTransformer(BasicDMNToNativeTransformer<com.gs.dmn.el.analysis.semantics.type.Type, DMNContext> dmnTransformer, BuildLogger logger) {
this.dmnModelRepository = dmnTransformer.getDMNModelRepository();
this.dmnTransformer = dmnTransformer;
this.logger = logger;
}

public DMNMetadata toManifest(String dmnNamespace, String nativeNamespace, String dmnVersion, String modelVersion, String platformVersion) {
DMNMetadata manifest = new DMNMetadata(dmnNamespace, nativeNamespace, dmnVersion, modelVersion, platformVersion);
public DMNMetadata toManifest(List<String> dmnNamespaces, String nativeNamespace, String dmnVersion, String modelVersion, String platformVersion) {
DMNMetadata manifest = new DMNMetadata(dmnNamespaces, nativeNamespace, dmnVersion, modelVersion, platformVersion);
for (TDefinitions definitions: this.dmnModelRepository.getAllDefinitions()) {
// Add types
for (TItemDefinition itemDefinition : this.dmnModelRepository.findItemDefinitions(definitions)) {
com.gs.dmn.runtime.metadata.Type type = makeMetadataType(definitions, itemDefinition);
Type type = makeMetadataType(definitions, itemDefinition);
manifest.addType(type);
}
// Add elements
Expand All @@ -51,11 +52,11 @@ public DMNMetadata toManifest(String dmnNamespace, String nativeNamespace, Strin
manifest.addElement(element);
}
for (TBusinessKnowledgeModel bkm : this.dmnModelRepository.findBKMs(definitions)) {
com.gs.dmn.runtime.metadata.BKM element = makeMetadataBKM(definitions, bkm);
BKM element = makeMetadataBKM(definitions, bkm);
manifest.addElement(element);
}
for (TDecision decision : this.dmnModelRepository.findDecisions(definitions)) {
com.gs.dmn.runtime.metadata.Decision element = makeMetadataDecision(definitions, decision);
Decision element = makeMetadataDecision(definitions, decision);
manifest.addElement(element);
}
}
Expand All @@ -65,20 +66,20 @@ public DMNMetadata toManifest(String dmnNamespace, String nativeNamespace, Strin
//
// Types
//
private com.gs.dmn.runtime.metadata.Type makeMetadataType(TDefinitions model, TItemDefinition itemDefinition) {
private Type makeMetadataType(TDefinitions model, TItemDefinition itemDefinition) {
String id = itemDefinition.getId();
String name = itemDefinition.getName();
String label = itemDefinition.getLabel();
boolean isCollection = itemDefinition.isIsCollection();
com.gs.dmn.runtime.metadata.QName typeRef = makeMetadataTypeRef(model, QualifiedName.toQualifiedName(model, itemDefinition.getTypeRef()));
QName typeRef = makeMetadataTypeRef(model, QualifiedName.toQualifiedName(model, itemDefinition.getTypeRef()));
String allowedValues = makeMetadataAllowedValues(itemDefinition.getAllowedValues());

List<TItemDefinition> children = itemDefinition.getItemComponent();
com.gs.dmn.runtime.metadata.Type type;
Type type;
if (children == null || children.isEmpty()) {
type = new TypeReference(id, name, label, isCollection, typeRef, allowedValues);
} else {
List<com.gs.dmn.runtime.metadata.Type> subTypes = new ArrayList<>();
List<Type> subTypes = new ArrayList<>();
for (TItemDefinition child : children) {
subTypes.add(makeMetadataType(model, child));
}
Expand All @@ -87,13 +88,13 @@ private com.gs.dmn.runtime.metadata.Type makeMetadataType(TDefinitions model, TI
return type;
}

private com.gs.dmn.runtime.metadata.QName makeMetadataTypeRef(TDefinitions model, QualifiedName typeRef) {
protected QName makeMetadataTypeRef(TDefinitions model, QualifiedName typeRef) {
if (this.dmnModelRepository.isNull(typeRef)) {
return null;
}
String importName = typeRef.getNamespace();
String namespace = findNamespace(model, importName);
return new com.gs.dmn.runtime.metadata.QName(namespace, typeRef.getLocalPart());
return new QName(namespace, typeRef.getLocalPart());
}

private String makeMetadataAllowedValues(TUnaryTests allowedValues) {
Expand Down Expand Up @@ -124,56 +125,72 @@ private InputData makeMetadataInputData(com.gs.dmn.DRGElementReference<TInputDat
return makeMetadataInputData(definitions, inputData);
}

private InputData makeMetadataInputData(TDefinitions definitions, TInputData inputData) {
protected InputData makeMetadataInputData(TDefinitions definitions, TInputData inputData) {
String id = inputData.getId();
String name = inputData.getName();
String label = inputData.getLabel();
String diagramId = ((SignavioDMNModelRepository) this.dmnModelRepository).getDiagramId(inputData);
String shapeId = ((SignavioDMNModelRepository) this.dmnModelRepository).getShapeId(inputData);
String diagramId = getDiagramId(inputData);
String shapeId = getShapeId(inputData);
String javaParameterName = this.dmnTransformer.namedElementVariableName(inputData);
String javaTypeName = this.dmnTransformer.drgElementOutputType(inputData);
com.gs.dmn.runtime.metadata.QName typeRef = makeMetadataTypeRef(definitions, QualifiedName.toQualifiedName(definitions, inputData.getVariable().getTypeRef()));
String javaTypeName = getJavaTypeName(inputData);
QName typeRef = makeMetadataTypeRef(definitions, QualifiedName.toQualifiedName(definitions, inputData.getVariable().getTypeRef()));
return new InputData(id, name, label, diagramId, shapeId, javaParameterName, javaTypeName, typeRef);
}

private BKM makeMetadataBKM(TDefinitions definitions, TBusinessKnowledgeModel bkm) {
protected BKM makeMetadataBKM(TDefinitions definitions, TBusinessKnowledgeModel bkm) {
String id = bkm.getId();
String name = bkm.getName();
String label = bkm.getLabel();
String diagramId = ((SignavioDMNModelRepository) this.dmnModelRepository).getDiagramId(bkm);
String shapeId = ((SignavioDMNModelRepository) this.dmnModelRepository).getShapeId(bkm);
String diagramId = getDiagramId(bkm);
String shapeId = getShapeId(bkm);
String javaParameterName = this.dmnTransformer.namedElementVariableName(bkm);
String javaTypeName = this.dmnTransformer.qualifiedName(this.dmnTransformer.nativeModelPackageName(definitions.getName()), this.dmnTransformer.drgElementClassName(bkm));
String javaOutputTypeName = this.dmnTransformer.drgElementOutputType(bkm);
com.gs.dmn.runtime.metadata.QName typeRef = makeMetadataTypeRef(definitions, this.dmnTransformer.drgElementOutputTypeRef(bkm));
String javaOutputTypeName = getJavaTypeName(bkm);
QName typeRef = makeMetadataTypeRef(definitions, QualifiedName.toQualifiedName(definitions, bkm.getVariable().getTypeRef()));
List<DRGElementReference> knowledgeReferences = makeMetadataKnowledgeReferences(bkm.getKnowledgeRequirement());
return new BKM(id, name, label, diagramId, shapeId, javaParameterName, javaTypeName, javaOutputTypeName, typeRef, knowledgeReferences);
}

private Decision makeMetadataDecision(TDefinitions definitions, TDecision decision) {
protected Decision makeMetadataDecision(TDefinitions definitions, TDecision decision) {
String id = decision.getId();
String diagramId = ((SignavioDMNModelRepository) this.dmnModelRepository).getDiagramId(decision);
String shapeId = ((SignavioDMNModelRepository) this.dmnModelRepository).getShapeId(decision);
String diagramId = getDiagramId(decision);
String shapeId = getShapeId(decision);
String name = decision.getName();
String label = decision.getLabel();
String nativeParameterName = this.dmnTransformer.namedElementVariableName(decision);
String nativeTypeName = this.dmnTransformer.qualifiedName(this.dmnTransformer.nativeModelPackageName(definitions.getName()), this.dmnTransformer.drgElementClassName(decision));
String nativeOutputTypeName = this.dmnTransformer.drgElementOutputType(decision);
com.gs.dmn.runtime.metadata.QName typeRef = makeMetadataTypeRef(definitions, QualifiedName.toQualifiedName(definitions, decision.getVariable().getTypeRef()));
String nativeOutputTypeName = getJavaTypeName(decision);
QName typeRef = makeMetadataTypeRef(definitions, QualifiedName.toQualifiedName(definitions, decision.getVariable().getTypeRef()));
List<DRGElementReference> references = makeMetadataInformationReferences(decision);
List<DRGElementReference> knowledgeReferences = makeMetadataKnowledgeReferences(decision.getKnowledgeRequirement());
List<ExtensionElement> extensions = ((BasicSignavioDMNToJavaTransformer) this.dmnTransformer).makeMetadataExtensions(decision);
List<ExtensionElement> extensions = null;
List<InputData> transitiveRequiredInputs = makeTransitiveRequiredInputs(decision);
String protoRequestName = this.dmnTransformer.qualifiedRequestMessageName(decision);
String protoResponseName = this.dmnTransformer.qualifiedResponseMessageName(decision);
return new Decision(id, name, label, diagramId, shapeId, nativeParameterName, nativeTypeName, nativeOutputTypeName, typeRef, references, knowledgeReferences, extensions, transitiveRequiredInputs, protoRequestName, protoResponseName);
}

protected String getDiagramId(TDRGElement element) {
return null;
}

protected String getShapeId(TDRGElement element) {
return null;
}

private String getJavaTypeName(TDRGElement element) {
try {
return this.dmnTransformer.drgElementOutputType(element);
} catch (Exception e) {
logger.error(e.getMessage());
return Object.class.getName();
}
}

//
// References
//
private List<DRGElementReference> makeMetadataKnowledgeReferences(List<TKnowledgeRequirement> knowledgeRequirementList) {
protected List<DRGElementReference> makeMetadataKnowledgeReferences(List<TKnowledgeRequirement> knowledgeRequirementList) {
List<DRGElementReference> references = new ArrayList<>();
for (TKnowledgeRequirement knowledgeRequirement : knowledgeRequirementList) {
addMetadataReference(references, knowledgeRequirement.getRequiredKnowledge());
Expand All @@ -182,7 +199,7 @@ private List<DRGElementReference> makeMetadataKnowledgeReferences(List<TKnowledg
return references;
}

private List<DRGElementReference> makeMetadataInformationReferences(TDecision decision) {
protected List<DRGElementReference> makeMetadataInformationReferences(TDecision decision) {
List<DRGElementReference> references = new ArrayList<>();
List<TInformationRequirement> informationRequirementList = decision.getInformationRequirement();
for (TInformationRequirement informationRequirement : informationRequirementList) {
Expand All @@ -192,7 +209,7 @@ private List<DRGElementReference> makeMetadataInformationReferences(TDecision de
return references;
}

private List<InputData> makeTransitiveRequiredInputs(TDecision element) {
protected List<InputData> makeTransitiveRequiredInputs(TDecision element) {
com.gs.dmn.DRGElementReference<TDecision> reference = this.dmnModelRepository.makeDRGElementReference(element);
List<com.gs.dmn.DRGElementReference<TInputData>> drgElementReferences = this.dmnTransformer.inputDataClosure(reference);
return drgElementReferences.stream().map(this::makeMetadataInputData).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import java.util.List;
import java.util.stream.Collectors;

@JsonPropertyOrder(value = {"dmnVersion", "modelVersion", "platformVersion", "dmnNamespace", "nativeNamespace", "types", "elements"})
@JsonPropertyOrder(value = {"dmnVersion", "modelVersion", "platformVersion", "dmnNamespaces", "nativeNamespace", "types", "elements"})
public class DMNMetadata {
private String dmnVersion;
private String modelVersion;
private String platformVersion;
private String dmnNamespace;
private List<String> dmnNamespaces;
private String nativeNamespace;
private final List<Type> types = new ArrayList<>();
private final List<DRGElement> elements = new ArrayList<>();
Expand All @@ -35,17 +35,17 @@ public class DMNMetadata {
public DMNMetadata() {
}

public DMNMetadata(String dmnNamespace, String nativeNamespace, String dmnVersion, String modelVersion, String platformVersion) {
public DMNMetadata(List<String> dmnNamespaces, String nativeNamespace, String dmnVersion, String modelVersion, String platformVersion) {
this.dmnVersion = dmnVersion;
this.modelVersion = modelVersion;
this.platformVersion = platformVersion;
this.dmnNamespace = dmnNamespace;
this.dmnNamespaces = dmnNamespaces;
this.nativeNamespace = nativeNamespace;
}

@JsonGetter("dmnNamespace")
public String getDmnNamespace() {
return dmnNamespace;
@JsonGetter("dmnNamespaces")
public List<String> getDmnNamespaces() {
return dmnNamespaces;
}

@JsonGetter("nativeNamespace")
Expand Down
Loading
Loading