-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #554 from opatrascoiu/master
Add transformers for DT
- Loading branch information
Showing
12 changed files
with
1,892 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...re/src/main/java/com/gs/dmn/transformation/AddMissingNamespaceInTestCasesTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
dmn-core/src/main/java/com/gs/dmn/transformation/MixedItemDefinitionsTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rc/test/java/com/gs/dmn/transformation/AddMissingNamespaceInTestCasesTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
dmn-core/src/test/java/com/gs/dmn/transformation/MixedItemDefinitionsVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.