Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<java.enforced.version>[17,18)</java.enforced.version>
<maven.compiler.release>11</maven.compiler.release>

<rosetta.dsl.version>9.14.1</rosetta.dsl.version>
<rosetta.dsl.version>0.0.0.translate-syntax-3-SNAPSHOT</rosetta.dsl.version>
<rosetta.common.version>0.0.0.main-SNAPSHOT</rosetta.common.version>

<xtext.version>2.27.0</xtext.version>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/regnosys/testing/reports/ReportUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<ModelReportId> loadReportIdentifiers(ImmutableList<String> rosettaFo

return modelLoader.rosettaElements(models, RosettaReport.class).stream()
.map(rosettaReport -> new ModelReportId(
DottedPath.of(rosettaReport.getModel().getName()),
DottedPath.of(rosettaReport.getNamespace().getName()),
rosettaReport.getRegulatoryBody().getBody().getName(),
rosettaReport.getRegulatoryBody().getCorpusList().stream().map(RosettaNamed::getName).toArray(String[]::new)))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected PipelineModel.Serialisation getXmlOutputSerialisation(Function func) {
}

protected String formatIso20022XmlConfigPath(RosettaType type) {
String namespace = type.getModel().getName();
String namespace = type.getNamespace().getName();
String xmlConfigPath = namespace
.replace("iso20022.", "")
.replace(".", "-");
Expand All @@ -198,7 +198,7 @@ protected String formatIso20022XmlConfigPath(RosettaType type) {

protected ModelReportId toModelReportId(RosettaReport rosettaReport) {
return new ModelReportId(
DottedPath.of(rosettaReport.getModel().getName()),
DottedPath.of(rosettaReport.getNamespace().getName()),
rosettaReport.getRegulatoryBody().getBody().getName(),
rosettaReport.getRegulatoryBody().getCorpusList().stream().map(RosettaNamed::getName).toArray(String[]::new));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.regnosys.rosetta.common.util.UrlUtils;
import com.regnosys.rosetta.generator.java.types.JavaTypeTranslator;
import com.regnosys.rosetta.rosetta.RosettaModel;
import com.regnosys.rosetta.rosetta.RosettaNamespace;
import com.regnosys.rosetta.rosetta.RosettaReport;
import com.regnosys.rosetta.rosetta.RosettaType;
import com.regnosys.rosetta.rosetta.simple.AnnotationRef;
Expand Down Expand Up @@ -73,7 +74,7 @@ public List<RosettaReport> getReports(List<RosettaModel> models, String namespac
Set<String> excludedClassNames = excluded.stream().map(Class::getName).collect(Collectors.toSet());
return modelLoader.rosettaElements(models, RosettaReport.class)
.stream()
.filter(r -> filterNamespace(r.getModel(), namespaceRegex))
.filter(r -> filterNamespace(r.getNamespace(), namespaceRegex))
.filter(r -> !excludedClassNames.contains(toJavaClass(r)))
.collect(Collectors.toList());
}
Expand All @@ -82,7 +83,7 @@ public List<RosettaReport> getReports(List<RosettaModel> models, String namespac
public List<Function> getFunctionsWithAnnotation(List<RosettaModel> models, String namespaceRegex, String annotation, Collection<Class<?>> excluded) {
Set<String> excludedClassNames = excluded.stream().map(Class::getName).collect(Collectors.toSet());
return modelLoader.rosettaElements(models, Function.class).stream()
.filter(r -> filterNamespace(r.getModel(), namespaceRegex))
.filter(r -> filterNamespace(r.getNamespace(), namespaceRegex))
.filter(f -> f.getAnnotations().stream()
.map(AnnotationRef::getAnnotation)
.anyMatch(a -> annotation.equals(a.getName())))
Expand Down Expand Up @@ -122,14 +123,14 @@ public String toJavaClass(RosettaType rosettaType) {
return generatedJavaClassService.toJavaType(toModelSymbolId(rosettaType)).getCanonicalName().withDots();
}

private boolean filterNamespace(RosettaModel rosettaModel, String namespaceIncludeRegex) {
private boolean filterNamespace(RosettaNamespace rosettaNamespace, String namespaceIncludeRegex) {
return Optional.ofNullable(namespaceIncludeRegex)
.map(regex -> rosettaModel.getName().matches(regex))
.map(regex -> rosettaNamespace.getName().matches(regex))
.orElse(true);
}

private ModelSymbolId toModelSymbolId(RosettaType type) {
DottedPath namespace = DottedPath.splitOnDots(type.getModel().getName());
DottedPath namespace = DottedPath.splitOnDots(type.getNamespace().getName());
return new ModelSymbolId(namespace, type.getName());
}
}
49 changes: 27 additions & 22 deletions src/main/java/util/UnusedModelElementFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,44 @@
import com.regnosys.rosetta.rosetta.simple.Data;
import com.regnosys.rosetta.rosetta.simple.Function;
import com.regnosys.rosetta.transgest.ModelLoader;
import com.regnosys.rosetta.utils.ModelIdProvider;
import com.regnosys.testing.RosettaTestingInjectorProvider;
import com.rosetta.model.lib.ModelSymbolId;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.*;

/**
* Util needs to determine if types are used or not
*/
public class UnusedModelElementFinder {

private final ModelIdProvider modelIdProvider;

private static final Logger LOGGER = LoggerFactory.getLogger(UnusedModelElementFinder.class);
private final Set<String> listOfTypes = new HashSet<>();
private final Set<String> listOfUsedTypes = new HashSet<>();
private final Set<String> listOfOrphanedTypes = new HashSet<>();
private final Set<ModelSymbolId> listOfTypes = new HashSet<>();
private final Set<ModelSymbolId> listOfUsedTypes = new HashSet<>();
private final Set<ModelSymbolId> listOfOrphanedTypes = new HashSet<>();

private final Set<String> listOfDeprecatedTypes = new HashSet<>();
private final Set<ModelSymbolId> listOfDeprecatedTypes = new HashSet<>();
private final List<RosettaModel> models;

public UnusedModelElementFinder(List<RosettaModel> models) {
public UnusedModelElementFinder(List<RosettaModel> models, ModelIdProvider modelIdProvider) {
this.models = models;
this.modelIdProvider = modelIdProvider;
}

public static void main(String[] args) {
Injector injector = new RosettaTestingInjectorProvider().getInjector();
ModelLoader loader = injector.getInstance(ModelLoader.class);
List<RosettaModel> models = loader.loadRosettaModels(ClassPathUtils.findRosettaFilePaths().stream().map(UrlUtils::toUrl));

new UnusedModelElementFinder(models).run();
new UnusedModelElementFinder(models, injector.getInstance(ModelIdProvider.class)).run();
}

public void run() {
Expand All @@ -79,10 +85,6 @@ public void run() {

}

private String getQualifiedName(RosettaType type) {
return type.getModel().getName() + "." + type.getName();
}

private void generateTypesList() {

for (RosettaModel model : models) {
Expand All @@ -92,10 +94,11 @@ private void generateTypesList() {
.filter(Data.class::isInstance)
.map(Data.class::cast)
.forEach(dataType -> {
LOGGER.trace(" Processing data type: {}", getQualifiedName(dataType));
listOfTypes.add(getQualifiedName(dataType));
ModelSymbolId id = modelIdProvider.getSymbolId(dataType);
LOGGER.trace(" Processing data type: {}", id);
listOfTypes.add(id);
if(null!=dataType.getSuperType())
listOfUsedTypes.add(getQualifiedName(dataType.getSuperType()));
listOfUsedTypes.add(modelIdProvider.getSymbolId(dataType.getSuperType().getType()));
TreeIterator<EObject> eObjectTreeIterator = dataType.eAllContents();
updateUsedTypes(eObjectTreeIterator);

Expand All @@ -106,15 +109,17 @@ private void generateTypesList() {
.filter(RosettaEnumeration.class::isInstance)
.map(RosettaEnumeration.class::cast)
.forEach(enumeration -> {
LOGGER.trace("Processing enumeration type {}", getQualifiedName(enumeration));
listOfTypes.add(getQualifiedName(enumeration));
ModelSymbolId id = modelIdProvider.getSymbolId(enumeration);
LOGGER.trace("Processing enumeration type {}", id);
listOfTypes.add(id);
});

model.getElements().stream()
.filter(Function.class::isInstance)
.map(Function.class::cast)
.forEach(function -> {
LOGGER.trace(" Processing function types {}.{}", function.getModel().getName(), function.getName());
ModelSymbolId id = modelIdProvider.getSymbolId(function);
LOGGER.trace(" Processing function {}", id);
// listOfTypes.add((function.getModel().getName().concat(".")).concat(function.getName()));

TreeIterator<EObject> eObjectTreeIterator = function.eAllContents();
Expand All @@ -130,7 +135,7 @@ private void updateDeprecatedTypes(Data dataType) {
annotations
.forEach(annotationRef -> {
if(annotationRef.getAnnotation().getName().equals("deprecated")){
listOfDeprecatedTypes.add(getQualifiedName(dataType));
listOfDeprecatedTypes.add(modelIdProvider.getSymbolId(dataType));
}
});

Expand All @@ -142,23 +147,23 @@ private void updateUsedTypes(TreeIterator<EObject> eObjectTreeIterator) {
elements.stream()
.filter(TypeCall.class::isInstance)
.map(TypeCall.class::cast)
.forEach(typeCall -> listOfUsedTypes.add(getQualifiedName(typeCall.getType())));
.forEach(typeCall -> listOfUsedTypes.add(modelIdProvider.getSymbolId(typeCall.getType())));

}

public Set<String> getListOfTypes() {
public Set<ModelSymbolId> getListOfTypes() {
return listOfTypes;
}

public Set<String> getListOfUsedTypes() {
public Set<ModelSymbolId> getListOfUsedTypes() {
return listOfUsedTypes;
}

public Set<String> getListOfOrphanedTypes() {
public Set<ModelSymbolId> getListOfOrphanedTypes() {
return listOfOrphanedTypes;
}

public Set<String> getListOfDeprecatedTypes() {
public Set<ModelSymbolId> getListOfDeprecatedTypes() {
return listOfDeprecatedTypes;
}
}
50 changes: 30 additions & 20 deletions src/test/java/util/UnusedModelElementFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
import com.regnosys.rosetta.common.util.UrlUtils;
import com.regnosys.rosetta.rosetta.RosettaModel;
import com.regnosys.rosetta.transgest.ModelLoader;
import com.regnosys.rosetta.utils.ModelIdProvider;
import com.regnosys.testing.RosettaTestingInjectorProvider;
import com.rosetta.model.lib.ModelSymbolId;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -42,40 +46,46 @@
public class UnusedModelElementFinderTest {
@Inject
private ModelLoader modelLoader;
@Inject
private ModelIdProvider modelIdProvider;

@Test
public void getlistOfOrphanedTypes() {
public void getListOfOrphanedTypes() {

List<RosettaModel> models = modelLoader.loadRosettaModels(ClassPathUtils.findPathsFromClassPath(ImmutableList.of("model", "unused-model-element-finder"), ".*\\.rosetta", Optional.empty(), ClassPathUtils.class.getClassLoader()
).stream()
.map(UrlUtils::toUrl));
UnusedModelElementFinder unusedModelElementFinder = new UnusedModelElementFinder(models);
UnusedModelElementFinder unusedModelElementFinder = new UnusedModelElementFinder(models, modelIdProvider);

unusedModelElementFinder.run();
assertEquals(7, unusedModelElementFinder.getListOfTypes().size(), unusedModelElementFinder.getListOfTypes().toString());
Set<String> listOfTypes = unusedModelElementFinder.getListOfTypes().stream().map(ModelSymbolId::toString).collect(Collectors.toSet());
assertEquals(7, listOfTypes.size(), listOfTypes.toString());

assertTrue(unusedModelElementFinder.getListOfTypes().contains("cdm.test.Test1"), "ListOfTypes should contain cdm.test.Test1");
assertTrue(unusedModelElementFinder.getListOfTypes().contains("cdm.test.Test2"), "ListOfTypes should contain cdm.test.Test2");
assertTrue(unusedModelElementFinder.getListOfTypes().contains("cdm.test.Test3"),"ListOfTypes should contain cdm.test.Test3");
assertTrue(unusedModelElementFinder.getListOfTypes().contains("cdm.test.DirectionEnum"), "ListOfTypes should contain cdm.test.DirectionEnum");
assertTrue(unusedModelElementFinder.getListOfTypes().contains("cdm.test.TestEnum2Unused"), "ListOfTypes should contain cdm.test.TestEnum2Unused");
assertTrue(unusedModelElementFinder.getListOfTypes().contains("cdm.test.TestEnum3UsedInFuncOnly"), "ListOfTypes should contain cdm.test.TestEnum3UsedInFuncOnly");
assertTrue(listOfTypes.contains("cdm.test.Test1"), "ListOfTypes should contain cdm.test.Test1");
assertTrue(listOfTypes.contains("cdm.test.Test2"), "ListOfTypes should contain cdm.test.Test2");
assertTrue(listOfTypes.contains("cdm.test.Test3"),"ListOfTypes should contain cdm.test.Test3");
assertTrue(listOfTypes.contains("cdm.test.DirectionEnum"), "ListOfTypes should contain cdm.test.DirectionEnum");
assertTrue(listOfTypes.contains("cdm.test.TestEnum2Unused"), "ListOfTypes should contain cdm.test.TestEnum2Unused");
assertTrue(listOfTypes.contains("cdm.test.TestEnum3UsedInFuncOnly"), "ListOfTypes should contain cdm.test.TestEnum3UsedInFuncOnly");

assertEquals(5, unusedModelElementFinder.getListOfUsedTypes().size(), unusedModelElementFinder.getListOfUsedTypes().toString());
Set<String> listOfUsedTypes = unusedModelElementFinder.getListOfUsedTypes().stream().map(ModelSymbolId::toString).collect(Collectors.toSet());
assertEquals(5, listOfUsedTypes.size(), listOfUsedTypes.toString());

assertTrue(unusedModelElementFinder.getListOfUsedTypes().contains("cdm.test.Test1"), "ListOfUsedTypes should contain cdm.test.Test1");
assertTrue(unusedModelElementFinder.getListOfUsedTypes().contains("cdm.test.Test2"), "ListOfUsedTypes should contain cdm.test.Test2");
assertTrue(unusedModelElementFinder.getListOfUsedTypes().contains("cdm.test.Test3"), "ListOfUsedTypes should contain cdm.test.Test3");
assertTrue(unusedModelElementFinder.getListOfUsedTypes().contains("cdm.test.DirectionEnum"), "ListOfUsedTypes should contain cdm.test.DirectionEnum");
assertTrue(unusedModelElementFinder.getListOfUsedTypes().contains("cdm.test.TestEnum3UsedInFuncOnly"), "ListOfUsedTypes should contain cdm.test.TestEnum3UsedInFuncOnly");
assertTrue(listOfUsedTypes.contains("cdm.test.Test1"), "ListOfUsedTypes should contain cdm.test.Test1");
assertTrue(listOfUsedTypes.contains("cdm.test.Test2"), "ListOfUsedTypes should contain cdm.test.Test2");
assertTrue(listOfUsedTypes.contains("cdm.test.Test3"), "ListOfUsedTypes should contain cdm.test.Test3");
assertTrue(listOfUsedTypes.contains("cdm.test.DirectionEnum"), "ListOfUsedTypes should contain cdm.test.DirectionEnum");
assertTrue(listOfUsedTypes.contains("cdm.test.TestEnum3UsedInFuncOnly"), "ListOfUsedTypes should contain cdm.test.TestEnum3UsedInFuncOnly");


assertEquals(2, unusedModelElementFinder.getListOfOrphanedTypes().size(), unusedModelElementFinder.getListOfOrphanedTypes().toString());
assertTrue(unusedModelElementFinder.getListOfOrphanedTypes().contains("cdm.test.TestEnum2Unused"), "ListOfOrphanedTypes should contain cdm.test.TestEnum2Unused");
assertTrue(unusedModelElementFinder.getListOfOrphanedTypes().contains("cdm.test.Test4Unused"), "ListOfOrphanedTypes should contain cdm.test.Test4Unused");
Set<String> listOfOrphanedTypes = unusedModelElementFinder.getListOfOrphanedTypes().stream().map(ModelSymbolId::toString).collect(Collectors.toSet());
assertEquals(2, listOfOrphanedTypes.size(), listOfOrphanedTypes.toString());
assertTrue(listOfOrphanedTypes.contains("cdm.test.TestEnum2Unused"), "ListOfOrphanedTypes should contain cdm.test.TestEnum2Unused");
assertTrue(listOfOrphanedTypes.contains("cdm.test.Test4Unused"), "ListOfOrphanedTypes should contain cdm.test.Test4Unused");

assertEquals(1, unusedModelElementFinder.getListOfDeprecatedTypes().size(), unusedModelElementFinder.getListOfDeprecatedTypes().toString());
assertTrue(unusedModelElementFinder.getListOfDeprecatedTypes().contains("cdm.test.Test3"), "ListOfDeprecatedTypes should contain cdm.test.Test3");
Set<String> listOfDeprecatedTypes = unusedModelElementFinder.getListOfDeprecatedTypes().stream().map(ModelSymbolId::toString).collect(Collectors.toSet());
assertEquals(1, listOfDeprecatedTypes.size(), listOfDeprecatedTypes.toString());
assertTrue(listOfDeprecatedTypes.contains("cdm.test.Test3"), "ListOfDeprecatedTypes should contain cdm.test.Test3");

}
}