com.gs.dmn.serialization.DMNDialectTransformer 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.serialization;
import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.runtime.DMNRuntimeException;
import com.gs.dmn.runtime.Pair;
import org.apache.commons.lang3.StringUtils;
import org.omg.spec.dmn._20180521.model.*;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import java.util.*;
import static com.gs.dmn.serialization.DMNVersion.*;
public class DMNDialectTransformer {
private static final ObjectFactory DMN_12_OBJECT_FACTORY = new ObjectFactory();
private final BuildLogger logger;
private final PrefixNamespaceMappings prefixNamespaceMappings = new PrefixNamespaceMappings();
public DMNDialectTransformer(BuildLogger logger) {
this.logger = logger;
}
public DMNModelRepository transformRepository(org.omg.spec.dmn._20151101.model.TDefinitions dmn11Definitions) {
TDefinitions definitions = transform(dmn11Definitions);
return new DMNModelRepository(definitions, prefixNamespaceMappings);
}
public Pair transformDefinitions(org.omg.spec.dmn._20151101.model.TDefinitions dmn11Definitions) {
TDefinitions definitions = transform(dmn11Definitions);
return new Pair(definitions, this.prefixNamespaceMappings);
}
private TDefinitions transform(org.omg.spec.dmn._20151101.model.TDefinitions dmn11Definitions) {
logger.info(String.format("Transforming '%s' to DMN 1.2 ...", dmn11Definitions.getName()));
TDefinitions definitions = DMN_12_OBJECT_FACTORY.createTDefinitions();
addNamedElementProperties(dmn11Definitions, definitions);
definitions.getImport().addAll(transformList(dmn11Definitions.getImport()));
definitions.getItemDefinition().addAll(transformList(dmn11Definitions.getItemDefinition()));
definitions.getDrgElement().addAll(transformList(dmn11Definitions.getDrgElement()));
definitions.getArtifact().addAll(transformList(dmn11Definitions.getArtifact()));
definitions.getElementCollection().addAll(transformList(dmn11Definitions.getElementCollection()));
definitions.getBusinessContextElement().addAll(transformList(dmn11Definitions.getBusinessContextElement()));
definitions.setExpressionLanguage(transformLanguage(dmn11Definitions.getExpressionLanguage()));
definitions.setTypeLanguage(transformLanguage(dmn11Definitions.getTypeLanguage()));
definitions.setNamespace(transformNamespace(dmn11Definitions.getNamespace()));
definitions.setExporter(dmn11Definitions.getExporter());
definitions.setExporterVersion(dmn11Definitions.getExporterVersion());
logger.info("Done");
return definitions;
}
private TDMNElement transformElement(org.omg.spec.dmn._20151101.model.TDMNElement element) {
if (element == null) {
return null;
}
if (element instanceof org.omg.spec.dmn._20151101.model.TUnaryTests) {
return transform((org.omg.spec.dmn._20151101.model.TUnaryTests) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TInputClause) {
return transform((org.omg.spec.dmn._20151101.model.TInputClause) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TArtifact) {
return transformArtifact((org.omg.spec.dmn._20151101.model.TArtifact) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TOutputClause) {
return transform((org.omg.spec.dmn._20151101.model.TOutputClause) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TDecisionRule) {
return transform((org.omg.spec.dmn._20151101.model.TDecisionRule) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TNamedElement) {
return transformNamedElement((org.omg.spec.dmn._20151101.model.TNamedElement) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TExpression) {
return transformExpression((org.omg.spec.dmn._20151101.model.TExpression) element);
} else {
throw new DMNRuntimeException(String.format("'%s' is not supported", element.getClass()));
}
}
private TNamedElement transformNamedElement(org.omg.spec.dmn._20151101.model.TNamedElement element) {
if (element == null) {
return null;
}
if (element instanceof org.omg.spec.dmn._20151101.model.TDRGElement) {
return transformDRGElement((org.omg.spec.dmn._20151101.model.TDRGElement) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TElementCollection) {
return transform((org.omg.spec.dmn._20151101.model.TElementCollection) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TDecisionService) {
return transform((org.omg.spec.dmn._20151101.model.TDecisionService) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TDefinitions) {
return transform((org.omg.spec.dmn._20151101.model.TDefinitions) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TItemDefinition) {
return transform((org.omg.spec.dmn._20151101.model.TItemDefinition) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TBusinessContextElement) {
return transformBusinessContextElement((org.omg.spec.dmn._20151101.model.TBusinessContextElement) element);
} else if (element instanceof org.omg.spec.dmn._20151101.model.TInformationItem) {
return transform((org.omg.spec.dmn._20151101.model.TInformationItem) element);
} else {
throw new DMNRuntimeException(String.format("'%s' is not supported", element.getClass()));
}
}
private void addElementProperties(org.omg.spec.dmn._20151101.model.TDMNElement source, TDMNElement target) {
target.setDescription(source.getDescription());
target.setExtensionElements(transform(source.getExtensionElements()));
target.setId(source.getId());
target.setLabel(source.getLabel());
target.getOtherAttributes().putAll(transform(source.getOtherAttributes()));
}
private void addNamedElementProperties(org.omg.spec.dmn._20151101.model.TNamedElement source, TNamedElement target) {
addElementProperties(source, target);
target.setName(source.getName());
}
private void addExpressionProperties(org.omg.spec.dmn._20151101.model.TExpression source, TExpression target) {
addElementProperties(source, target);
target.setTypeRef(transform(source.getTypeRef()));
}
private void addImportProperties(org.omg.spec.dmn._20151101.model.TImport element, TImport result) {
result.setNamespace(transformNamespace(element.getNamespace()));
result.setLocationURI(element.getLocationURI());
result.setImportType(element.getImportType());
}
private JAXBElement transformJAXBElement(JAXBElement element) {
if (element == null) {
return null;
}
Object value = element.getValue();
if (value instanceof org.omg.spec.dmn._20151101.model.TDefinitions) {
return DMN_12_OBJECT_FACTORY.createDefinitions(transform((org.omg.spec.dmn._20151101.model.TDefinitions) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TImport) {
return DMN_12_OBJECT_FACTORY.createImport(transform((org.omg.spec.dmn._20151101.model.TImport) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TElementCollection) {
return DMN_12_OBJECT_FACTORY.createElementCollection(transform((org.omg.spec.dmn._20151101.model.TElementCollection) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TDecision) {
return DMN_12_OBJECT_FACTORY.createDecision(transform((org.omg.spec.dmn._20151101.model.TDecision) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TPerformanceIndicator) {
return DMN_12_OBJECT_FACTORY.createPerformanceIndicator(transform((org.omg.spec.dmn._20151101.model.TPerformanceIndicator) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TOrganizationUnit) {
return DMN_12_OBJECT_FACTORY.createOrganizationUnit(transform((org.omg.spec.dmn._20151101.model.TOrganizationUnit) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TBusinessContextElement) {
return DMN_12_OBJECT_FACTORY.createBusinessContextElement(transformBusinessContextElement((org.omg.spec.dmn._20151101.model.TBusinessContextElement) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TBusinessKnowledgeModel) {
return DMN_12_OBJECT_FACTORY.createBusinessKnowledgeModel(transform((org.omg.spec.dmn._20151101.model.TBusinessKnowledgeModel) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TInputData) {
return DMN_12_OBJECT_FACTORY.createInputData(transform((org.omg.spec.dmn._20151101.model.TInputData) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TKnowledgeSource) {
return DMN_12_OBJECT_FACTORY.createKnowledgeSource(transform((org.omg.spec.dmn._20151101.model.TKnowledgeSource) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TInformationRequirement) {
return DMN_12_OBJECT_FACTORY.createInformationRequirement(transform((org.omg.spec.dmn._20151101.model.TInformationRequirement) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TKnowledgeRequirement) {
return DMN_12_OBJECT_FACTORY.createKnowledgeRequirement(transform((org.omg.spec.dmn._20151101.model.TKnowledgeRequirement) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TAuthorityRequirement) {
return DMN_12_OBJECT_FACTORY.createAuthorityRequirement(transform((org.omg.spec.dmn._20151101.model.TAuthorityRequirement) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TItemDefinition) {
return DMN_12_OBJECT_FACTORY.createItemDefinition(transform((org.omg.spec.dmn._20151101.model.TItemDefinition) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TLiteralExpression) {
return DMN_12_OBJECT_FACTORY.createLiteralExpression(transform((org.omg.spec.dmn._20151101.model.TLiteralExpression) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TInvocation) {
return DMN_12_OBJECT_FACTORY.createInvocation(transform((org.omg.spec.dmn._20151101.model.TInvocation) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TInformationItem) {
return DMN_12_OBJECT_FACTORY.createInformationItem(transform((org.omg.spec.dmn._20151101.model.TInformationItem) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TDecisionTable) {
return DMN_12_OBJECT_FACTORY.createDecisionTable(transform((org.omg.spec.dmn._20151101.model.TDecisionTable) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TTextAnnotation) {
return DMN_12_OBJECT_FACTORY.createTextAnnotation(transform((org.omg.spec.dmn._20151101.model.TTextAnnotation) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TAssociation) {
return DMN_12_OBJECT_FACTORY.createAssociation(transform((org.omg.spec.dmn._20151101.model.TAssociation) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TArtifact) {
return DMN_12_OBJECT_FACTORY.createArtifact(transformArtifact((org.omg.spec.dmn._20151101.model.TArtifact) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TContext) {
return DMN_12_OBJECT_FACTORY.createContext(transform((org.omg.spec.dmn._20151101.model.TContext) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TContextEntry) {
return DMN_12_OBJECT_FACTORY.createContextEntry(transform((org.omg.spec.dmn._20151101.model.TContextEntry) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TFunctionDefinition) {
return DMN_12_OBJECT_FACTORY.createFunctionDefinition(transform((org.omg.spec.dmn._20151101.model.TFunctionDefinition) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TRelation) {
return DMN_12_OBJECT_FACTORY.createRelation(transform((org.omg.spec.dmn._20151101.model.TRelation) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TList) {
return DMN_12_OBJECT_FACTORY.createList(transform((org.omg.spec.dmn._20151101.model.TList) value));
} else if (value instanceof org.omg.spec.dmn._20151101.model.TDecisionService) {
return DMN_12_OBJECT_FACTORY.createDecisionService(transform((org.omg.spec.dmn._20151101.model.TDecisionService) value));
} else {
throw new DMNRuntimeException(String.format("'%s' is not supported yet", value.getClass()));
}
}
private Collection transformList(List elements) {
List result = new ArrayList();
for(Object element: elements) {
if (element instanceof JAXBElement) {
result.add(transformJAXBElement((JAXBElement) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TDMNElement) {
result.add(transformElement((org.omg.spec.dmn._20151101.model.TDMNElement) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TImport) {
result.add(transform((org.omg.spec.dmn._20151101.model.TImport) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TKnowledgeRequirement) {
result.add(transform((org.omg.spec.dmn._20151101.model.TKnowledgeRequirement) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TAuthorityRequirement) {
result.add(transform((org.omg.spec.dmn._20151101.model.TAuthorityRequirement) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TInformationRequirement) {
result.add(transform((org.omg.spec.dmn._20151101.model.TInformationRequirement) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TDMNElementReference) {
result.add(transform((org.omg.spec.dmn._20151101.model.TDMNElementReference) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.THitPolicy) {
result.add(transform((org.omg.spec.dmn._20151101.model.THitPolicy) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TBuiltinAggregator) {
result.add(transform((org.omg.spec.dmn._20151101.model.TBuiltinAggregator) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TDecisionTableOrientation) {
result.add(transform((org.omg.spec.dmn._20151101.model.TDecisionTableOrientation) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TContextEntry) {
result.add(transform((org.omg.spec.dmn._20151101.model.TContextEntry) element));
} else if (element instanceof org.omg.spec.dmn._20151101.model.TBinding) {
result.add(transform((org.omg.spec.dmn._20151101.model.TBinding) element));
} else {
throw new DMNRuntimeException(String.format("'%s' not supported yet", element.getClass()));
}
}
return result;
}
private Map transform(Map otherAttributes) {
Map result = new LinkedHashMap<>();
for(QName key: otherAttributes.keySet()) {
result.put(new QName(key.getNamespaceURI(), key.getLocalPart(), key.getPrefix()), otherAttributes.get(key));
}
return result;
}
private TDMNElement.ExtensionElements transform(org.omg.spec.dmn._20151101.model.TDMNElement.ExtensionElements element) {
if (element == null) {
return null;
}
TDMNElement.ExtensionElements result = DMN_12_OBJECT_FACTORY.createTDMNElementExtensionElements();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy