Skip to content

Commit

Permalink
Merge pull request #554 from opatrascoiu/master
Browse files Browse the repository at this point in the history
Add transformers for DT
  • Loading branch information
opatrascoiu authored Jun 27, 2022
2 parents 94aeb92 + d3691cb commit 129161e
Show file tree
Hide file tree
Showing 12 changed files with 1,892 additions and 12 deletions.
12 changes: 12 additions & 0 deletions dmn-core/src/main/java/com/gs/dmn/DMNModelRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public List<TDefinitions> getAllDefinitions() {
return this.allDefinitions;
}

// Model name might not be unique
public List<TDefinitions> findDefinitionByName(String modelName) {
List<TDefinitions> result = new ArrayList<>();
for (TDefinitions definitions : this.allDefinitions) {
if (modelName.equals(definitions.getName())) {
result.add(definitions);
}
}
return result;
}

public void addElementMap(TDRGElement element, TDefinitions definitions) {
this.elementToDefinitions.put(element, definitions);
}
Expand Down Expand Up @@ -1225,4 +1236,5 @@ private void collectCompositeItemDefinitions(List<TItemDefinition> itemDefinitio
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.transformation;

import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.ast.*;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.log.Slf4jBuildLogger;
import com.gs.dmn.runtime.Pair;
import com.gs.dmn.tck.ast.InputNode;
import com.gs.dmn.tck.ast.ResultNode;
import com.gs.dmn.tck.ast.TestCase;
import com.gs.dmn.tck.ast.TestCases;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class AddMissingNamespaceInTestCasesTransformer extends SimpleDMNTransformer<TestCases> {
private static final Logger LOGGER = LoggerFactory.getLogger(AddMissingNamespaceInTestCasesTransformer.class);

private final BuildLogger logger;
private boolean transformRepository = true;

public AddMissingNamespaceInTestCasesTransformer() {
this(new Slf4jBuildLogger(LOGGER));
}

public AddMissingNamespaceInTestCasesTransformer(BuildLogger logger) {
this.logger = logger;
}

@Override
public DMNModelRepository transform(DMNModelRepository repository) {
if (isEmpty(repository)) {
logger.warn("DMN repository is empty; transformer will not run");
return repository;
}

this.transformRepository = false;
return repository;
}

@Override
public Pair<DMNModelRepository, List<TestCases>> transform(DMNModelRepository repository, List<TestCases> testCasesList) {
if (isEmpty(repository, testCasesList)) {
logger.warn("DMN repository or test cases list is empty; transformer will not run");
return new Pair<>(repository, testCasesList);
}

// Transform model
if (transformRepository) {
transform(repository);
}

for (TestCases testCases : testCasesList) {
// Search model by name
if (StringUtils.isBlank(testCases.getNamespace())) {
String modelName = testCases.getModelName();
List<TDefinitions> definitionsList = repository.findDefinitionByName(modelName);
if (definitionsList.size() == 1) {
testCases.setNamespace(definitionsList.get(0).getNamespace());
}
}

// Set namespace for nodes
for (TestCase testCase : testCases.getTestCase()) {
for (InputNode node : testCase.getInputNode()) {
if (StringUtils.isBlank(node.getNamespace())) {
String name = node.getName();
TDRGElement drgElement = repository.findDRGElementByName(name);
if (drgElement != null) {
node.setNamespace(repository.getNamespace(drgElement));
}
}
}
for (ResultNode node : testCase.getResultNode()) {
if (StringUtils.isBlank(node.getNamespace())) {
String name = node.getName();
TDRGElement drgElement = repository.findDRGElementByName(name);
if (drgElement != null) {
node.setNamespace(repository.getNamespace(drgElement));
}
}
}
}
}

return new Pair<>(repository, testCasesList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.transformation;

import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.ast.*;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.log.Slf4jBuildLogger;
import com.gs.dmn.runtime.Pair;
import com.gs.dmn.tck.ast.TestCases;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.namespace.QName;
import java.util.List;

public class MixedItemDefinitionsTransformer extends SimpleDMNTransformer<TestCases> {
private static final Logger LOGGER = LoggerFactory.getLogger(MixedItemDefinitionsTransformer.class);

private final BuildLogger logger;
private boolean transformRepository = true;
private final Visitor visitor = new MixedItemDefinitionsVisitor();

public MixedItemDefinitionsTransformer() {
this(new Slf4jBuildLogger(LOGGER));
}

public MixedItemDefinitionsTransformer(BuildLogger logger) {
this.logger = logger;
}

@Override
public DMNModelRepository transform(DMNModelRepository repository) {
if (isEmpty(repository)) {
logger.warn("DMN repository is empty; transformer will not run");
return repository;
}

for (TDefinitions definitions : repository.getAllDefinitions()) {
definitions.accept(visitor, null);
}

this.transformRepository = false;
return repository;
}

@Override
public Pair<DMNModelRepository, List<TestCases>> transform(DMNModelRepository repository, List<TestCases> testCasesList) {
if (isEmpty(repository, testCasesList)) {
logger.warn("DMN repository or test cases list is empty; transformer will not run");
return new Pair<>(repository, testCasesList);
}

// Transform model
if (transformRepository) {
transform(repository);
}

return new Pair<>(repository, testCasesList);
}
}

class MixedItemDefinitionsVisitor extends DefaultDMNVisitor {
@Override
protected <C> QName visitTypeRef(QName typeRef, C context) {
if (typeRef != null) {
String localPart = typeRef.getLocalPart();
if (localPart != null) {
localPart = localPart.trim();
if (localPart.startsWith("sig.")) {
return new QName(localPart.substring(4));
}
}
}
return typeRef;
}

@Override
public <C> Object visit(TItemDefinition element, C context) {
super.visit(element, context);

if (isMixed(element) && isEmpty(element)) {
QName typeRef = new QName("feel.Any");
element.setTypeRef(typeRef);
}

return element;
}

boolean isMixed(TItemDefinition element) {
if (element == null) {
return false;
}
TDMNElement.ExtensionElements extensionElements = element.getExtensionElements();
if (extensionElements == null) {
return false;
}
for (Object any : extensionElements.getAny()) {
if ("mixed".equals(any)) {
return true;
}
}
return false;
}

boolean isEmpty(TItemDefinition element) {
if (element == null) {
return false;
}
return element.getTypeRef() == null
&& element.getItemComponent().isEmpty()
&& element.getFunctionItem() == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.transformation;

import com.gs.dmn.runtime.Pair;
import com.gs.dmn.tck.ast.TestCases;
import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedHashMap;

import static com.gs.dmn.serialization.DMNConstants.XSI_NS;
import static com.gs.dmn.serialization.DMNConstants.XSI_PREFIX;

public class AddMissingNamespaceInTestCasesTransformerTest extends SimpleDMNTransformerTest {
@Test
public void testTransform() throws Exception {
doTest("1.1", Arrays.asList("0004-lending.dmn"),
"0004-lending-test-01.xml", new LinkedHashMap<String, Pair<String, String>>() {{
put("0004-lending.dmn", new Pair<>("http://www.trisotech.com/definitions/_4e0f0b70-d31c-471c-bd52-5ca709ed362b", "tns"));
put("0004-lending-test-01.xml", new Pair<>(XSI_NS, XSI_PREFIX));
}}
);
}

@Override
protected DMNTransformer<TestCases> getTransformer() {
return new AddMissingNamespaceInTestCasesTransformer();
}

@Override
protected String getInputPath() {
return "dmn/input/";
}

@Override
protected String getTargetPath() {
return "target/missing-namespace/";
}

@Override
protected String getExpectedPath() {
return "dmn/expected/missing-namespace/";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.transformation;

import com.gs.dmn.ast.TDMNElement;
import com.gs.dmn.ast.TItemDefinition;
import org.junit.Test;

import javax.xml.namespace.QName;

import static org.junit.Assert.*;

public class MixedItemDefinitionsVisitorTest {
MixedItemDefinitionsVisitor visitor = new MixedItemDefinitionsVisitor();

@Test
public void testVisitTypeRef() {
visitor.visitTypeRef(null, null);
assertEquals(new QName("correct"), visitor.visitTypeRef(new QName("correct"), null));
assertEquals(new QName(" XXX"), visitor.visitTypeRef(new QName("sig. XXX"), null));
assertEquals(new QName(" XXX"), visitor.visitTypeRef(new QName(" sig. XXX"), null));
}

@Test
public void testVisitItemDefinition() {
TItemDefinition itemDefinition = makeItemDefinition("mixed");
assertNull(itemDefinition.getTypeRef());
itemDefinition.accept(visitor, null);
assertEquals(new QName("feel.Any"), itemDefinition.getTypeRef());
}

@Test
public void testIsMixed() {
assertFalse(visitor.isMixed(null));
assertFalse(visitor.isMixed(new TItemDefinition()));
assertTrue(visitor.isMixed(makeItemDefinition("mixed")));
assertFalse(visitor.isMixed(makeItemDefinition("other")));
}

@Test
public void testIsEmpty() {
assertFalse(visitor.isEmpty(null));
assertTrue(visitor.isEmpty(new TItemDefinition()));
}

private TItemDefinition makeItemDefinition(String any) {
TItemDefinition element = new TItemDefinition();
element.setExtensionElements(new TDMNElement.ExtensionElements());
element.getExtensionElements().getAny().add(any);
return element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import java.util.List;
import java.util.Map;

public abstract class NameTransformerTest extends AbstractFileTransformerTest {
protected static final ClassLoader CLASS_LOADER = NameTransformerTest.class.getClassLoader();
public abstract class SimpleDMNTransformerTest extends AbstractFileTransformerTest {
protected static final ClassLoader CLASS_LOADER = SimpleDMNTransformerTest.class.getClassLoader();

protected final DMNSerializer dmnSerializer = new XMLDMNSerializer(LOGGER, true);
protected final TCKSerializer tckSerializer = new XMLTCKSerializer(LOGGER, true);
Expand All @@ -43,9 +43,9 @@ protected void doTest(String dmmVersion, List<String> dmnFileNames, String tests

// Read DMN files
List<TDefinitions> definitionsList = readModels(path, dmnFileNames);
// Defintions are normalized in Repository
// Definitions are normalized in Repository
Map<String, TDefinitions> definitionsMap = new LinkedHashMap<>();
for (int i = 0; i < dmnFileNames.size(); i++) {
for (int i=0; i < dmnFileNames.size(); i++) {
definitionsMap.put(dmnFileNames.get(i), definitionsList.get(i));
}
DMNModelRepository repository = new DMNModelRepository(definitionsList);
Expand Down
Loading

0 comments on commit 129161e

Please sign in to comment.