Skip to content

Commit

Permalink
Rule source label fix (#923)
Browse files Browse the repository at this point in the history
* Rule source label fix

* Removed rule references for functions
  • Loading branch information
SimonCockx authored Feb 14, 2025
1 parent 8e15729 commit cdbd17d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,68 @@ other int (1..1)
);
}

@Test
void testReportLabelOverridesExternalRuleSourceRuleReferenceLabel() throws IOException {
RosettaTestModel model = loadModel("""
namespace test
body Authority Body
corpus Regulation "Description" Corpus
report Body Corpus in T+1
from int
when IsEligible
with type Foo
with source Source
eligibility rule IsEligible from int:
item
type Foo:
attr string (1..1)
[label as "My attribute"]
bar Bar (1..1)
[label barAttr1 as "Bar Attribute 1 label"]
type Bar:
barAttr1 int (1..1)
barAttr2 int (1..1)
reporting rule FooAttr from int:
to-string
as "My attribute from rule"
reporting rule BarAttr1 from int:
item
as "My Bar Attribute 1 from rule"
reporting rule BarAttr2 from int:
item
as "My Bar Attribute 2 from rule"
rule source Source {
Foo:
+ attr
[ruleReference FooAttr]
Bar:
+ barAttr1
[ruleReference BarAttr1]
+ barAttr2
[ruleReference BarAttr2]
}
""");

generateLabelProviderForReport(model, "Body", "Corpus");

assertSingleGeneratedFile("report-with-external-source/BodyCorpusLabelProvider.java", "/test/labels/BodyCorpusLabelProvider.java");
assertLabels(
"attr:My attribute",
"bar.barAttr1:Bar Attribute 1 label",
"bar.barAttr2:My Bar Attribute 2 from rule"
);
}

@Test
void testComplexReportLabels() throws IOException {
RosettaTestModel model = loadModel("""
Expand Down Expand Up @@ -276,7 +338,7 @@ opt2Attribute int (1..1)
}

@Test
void testCircularReferenceInTypesAreSupported() throws IOException {
void testCircularReferencesInTypesAreSupported() throws IOException {
RosettaTestModel model = loadModel("""
namespace test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test.labels;

import com.regnosys.rosetta.lib.labelprovider.GraphBasedLabelProvider;
import com.regnosys.rosetta.lib.labelprovider.LabelNode;
import java.util.Arrays;


public class BodyCorpusLabelProvider extends GraphBasedLabelProvider {
public BodyCorpusLabelProvider() {
super(new LabelNode());

startNode.addLabel(Arrays.asList("attr"), "My attribute");
startNode.addLabel(Arrays.asList("bar", "barAttr1"), "Bar Attribute 1 label");

LabelNode barNode = new LabelNode();
barNode.addLabel(Arrays.asList("barAttr1"), "My Bar Attribute 1 from rule");
barNode.addLabel(Arrays.asList("barAttr2"), "My Bar Attribute 2 from rule");

startNode.addOutgoingEdge("bar", barNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import com.regnosys.rosetta.types.RChoiceType
import org.apache.commons.text.StringEscapeUtils
import com.regnosys.rosetta.lib.labelprovider.GraphBasedLabelProvider
import com.regnosys.rosetta.lib.labelprovider.LabelNode
import java.util.Set
import java.util.Arrays
import java.util.stream.Collectors
import java.util.HashSet
import com.regnosys.rosetta.utils.ExternalAnnotationUtil
import com.regnosys.rosetta.types.RAttribute

class LabelProviderGenerator {
@Inject extension ImportManagerExtension
Expand All @@ -41,30 +42,36 @@ class LabelProviderGenerator {
@Inject JavaTypeTranslator typeTranslator
@Inject DeepFeatureCallUtil deepPathUtil
@Inject LabelProviderGeneratorUtil util
@Inject ExternalAnnotationUtil externalAnnotationUtil

def void generateForFunctionIfApplicable(IFileSystemAccess2 fsa, Function func) {
if (util.shouldGenerateLabelProvider(func)) {
val rFunction = rObjectFactory.buildRFunction(func)
generate(fsa, rFunction)
generate(fsa, rFunction, emptyMap)
}
}
def void generateForReport(IFileSystemAccess2 fsa, RosettaReport report) {
val attributeToRuleMap = externalAnnotationUtil.getAllReportingRules(report)
.entrySet()
.stream()
.collect(Collectors.toMap([e| e.getKey().getAttr()], [e| e.getValue()]));
val rFunction = rObjectFactory.buildRFunction(report)
generate(fsa, rFunction)
generate(fsa, rFunction, attributeToRuleMap)
}
private def void generate(IFileSystemAccess2 fsa, RFunction f) {
private def void generate(IFileSystemAccess2 fsa, RFunction f, Map<RAttribute, RosettaRule> attributeToRuleMap) {
val javaClass = typeTranslator.toLabelProviderJavaClass(f)
val fileName = javaClass.canonicalName.withForwardSlashes + '.java'

val topScope = new JavaScope(javaClass.packageName)
val StringConcatenationClient classBody = classBody(f, javaClass, topScope)
val StringConcatenationClient classBody = classBody(f, attributeToRuleMap, javaClass, topScope)

val content = buildClass(javaClass.packageName, classBody, topScope)
fsa.generateFile(fileName, content)
}

private def StringConcatenationClient classBody(
RFunction function,
Map<RAttribute, RosettaRule> attributeToRuleMap,
JavaClass<LabelProvider> javaClass,
JavaScope topScope
) {
Expand All @@ -81,7 +88,7 @@ class LabelProviderGenerator {
outputType
}
if (startNode instanceof RDataType) {
buildLabelGraph(startNode, labelsPerNode, edgesPerNode)
buildLabelGraph(startNode, labelsPerNode, edgesPerNode, attributeToRuleMap)
pruneLabelGraph(labelsPerNode, edgesPerNode)
}
constructorScope.createIdentifier(startNode, "startNode")
Expand Down Expand Up @@ -125,7 +132,7 @@ class LabelProviderGenerator {
'''«Arrays».asList(«path.stream.map[StringEscapeUtils.escapeJava(it)].collect(Collectors.joining("\", \"", "\"", "\""))»)'''
}

private def void buildLabelGraph(RDataType currentNode, Map<RDataType, Map<DottedPath, String>> labelsPerNode, Map<RDataType, Map<String, RDataType>> edgesPerNode) {
private def void buildLabelGraph(RDataType currentNode, Map<RDataType, Map<DottedPath, String>> labelsPerNode, Map<RDataType, Map<String, RDataType>> edgesPerNode, Map<RAttribute, RosettaRule> attributeToRuleMap) {
if (labelsPerNode.containsKey(currentNode)) {
// Circular reference: we already computed this node.
return
Expand All @@ -146,12 +153,13 @@ class LabelProviderGenerator {
}
if (t instanceof RDataType) {
edges.put(attr.name, t)
buildLabelGraph(t, labelsPerNode, edgesPerNode)
buildLabelGraph(t, labelsPerNode, edgesPerNode, attributeToRuleMap)
}

// 2. Register legacy `as` annotations from rule references
if (attr.ruleReference !== null) {
registerLegacyRuleAsLabel(attr.ruleReference, attrPath, labels)
val ruleRef = attributeToRuleMap.get(attr)
if (ruleRef !== null) {
registerLegacyRuleAsLabel(ruleRef, attrPath, labels)
}

// 3. Register label annotations
Expand Down

0 comments on commit cdbd17d

Please sign in to comment.