Skip to content

Commit

Permalink
Merge pull request #668 from goldmansachs/prefix-transformer
Browse files Browse the repository at this point in the history
Extend the prefix transformer to cover annotations
  • Loading branch information
opatrascoiu authored May 29, 2024
2 parents e5d2f3a + 357d3ef commit 2934378
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ private void transform(DMNModelRepository repository, TDecision decision, TDecis
for (TLiteralExpression exp: rule.getOutputEntry()) {
addMissingPrefix(exp, names);
}
// Annotation
for (TRuleAnnotation annotation: rule.getAnnotationEntry()) {
addMissingPrefix(annotation, names);
}
}
}

Expand All @@ -132,6 +136,13 @@ private void addMissingPrefix(TUnaryTests test, Set<String> names) {
}
}

private void addMissingPrefix(TRuleAnnotation annotation, Set<String> names) {
if (annotation != null) {
String newText = addMissingPrefix(annotation.getText(), names);
annotation.setText(newText);
}
}

String addMissingPrefix(String text, Set<String> names) {
if (StringUtils.isEmpty(text)) {
return text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package com.gs.dmn.transformation;

import com.gs.dmn.DMNModelRepository;
import com.gs.dmn.ast.*;
import com.gs.dmn.runtime.Pair;
import org.junit.jupiter.api.Test;

Expand All @@ -26,6 +28,79 @@ public class AddMissingImportPrefixInDTTransformerTest {
private final AddMissingImportPrefixInDTTransformer transformer = new AddMissingImportPrefixInDTTransformer();
private final Set<String> names = new LinkedHashSet<>(Arrays.asList("ident1", "ident2"));

@Test
public void testModel() {
String literalExpressionText = "person.age";
String unaryTestsText = "> person.age";
String annotationText = "\"rule triggered\" + person.age";

ObjectFactory objectFactory = new ObjectFactory();
TDefinitions definitions = objectFactory.createTDefinitions();

// Create input
TInputData inputData = objectFactory.createTInputData();
inputData.setName("person");
inputData.setId("person");
definitions.getDrgElement().add(inputData);

// Create child decision
TDecision child = objectFactory.createTDecision();
child.setName("child");
child.setId("child");
definitions.getDrgElement().add(child);

// Create decision
TDecision decision = objectFactory.createTDecision();
decision.setName("decision");
decision.setId("decision");
// Create IRs
TInformationRequirement inputDataRequirement = objectFactory.createTInformationRequirement();
TDMNElementReference inputDataReference = objectFactory.createTDMNElementReference();
inputDataReference.setHref("#person");
inputDataRequirement.setRequiredInput(inputDataReference);
decision.getInformationRequirement().add(inputDataRequirement);
TInformationRequirement childRequirement = objectFactory.createTInformationRequirement();
TDMNElementReference decisionReference = objectFactory.createTDMNElementReference();
decisionReference.setHref("#child");
childRequirement.setRequiredDecision(decisionReference);
decision.getInformationRequirement().add(childRequirement);
// Create decision table
TDecisionTable decisionTable = objectFactory.createTDecisionTable();
// Create input clause
TInputClause inputClause = objectFactory.createTInputClause();
TLiteralExpression inputExpression = objectFactory.createTLiteralExpression();
inputExpression.setText(literalExpressionText);
inputClause.setInputExpression(inputExpression);
decisionTable.getInput().add(inputClause);
// Create output clause
TOutputClause outputClause = objectFactory.createTOutputClause();
TUnaryTests outputClauseTest = objectFactory.createTUnaryTests();
outputClauseTest.setText(unaryTestsText);
outputClause.setOutputValues(outputClauseTest);
decisionTable.getOutput().add(outputClause);
// Create rule
TDecisionRule rule = objectFactory.createTDecisionRule();
TUnaryTests inputEntryTests = objectFactory.createTUnaryTests();
rule.getInputEntry().add(inputEntryTests);
TLiteralExpression ruleOutputExpression = objectFactory.createTLiteralExpression();
ruleOutputExpression.setText(literalExpressionText);
rule.getOutputEntry().add(ruleOutputExpression);
TRuleAnnotation ruleAnnotation = objectFactory.createTRuleAnnotation();
ruleAnnotation.setText(annotationText);
rule.getAnnotationEntry().add(ruleAnnotation);
decisionTable.getRule().add(rule);
decision.setExpression(decisionTable);
definitions.getDrgElement().add(decision);

transformer.transform(new DMNModelRepository(definitions));

// Check replacements in
assertEquals("person.person . age", inputClause.getInputExpression().getText());
assertEquals("> person.person . age", outputClause.getOutputValues().getText());
assertEquals("person.person . age", rule.getOutputEntry().get(0).getText());
assertEquals("\"rule triggered\" + person.person . age", rule.getAnnotationEntry().get(0).getText());
}

@Test
public void testAddMissingPrefix() {
List<Pair<String, String>> testCases = Arrays.asList(
Expand All @@ -49,5 +124,4 @@ public void testAddMissingPrefix() {
assertEquals(pair.getRight(), newText);
}
}

}

0 comments on commit 2934378

Please sign in to comment.