Skip to content

Commit

Permalink
Added overridden syntax to allow rosetta files to flag when they fork… (
Browse files Browse the repository at this point in the history
#761)

* Added overridden syntax to allow rosetta files to flag when they fork a parent model file

* Moved the overridden syntax to before the model description

* Moved the override keyword

* Moved the override keyword

* Review comments
  • Loading branch information
minesh-s-patel authored May 28, 2024
1 parent c975bf0 commit 3bda3d1
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 54 deletions.
11 changes: 11 additions & 0 deletions rosetta-ide/rosetta.tmLanguage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,24 @@ patterns:
repository:
model:
patterns:
- include: '#override'
- include: '#namespace'
- include: '#version'
- include: '#import'
- include: '#configuration'
- include: '#rootElement'

repository:
override:
name: meta.override.rosetta
begin: '{{wordStart}}override{{wordEnd}}'
beginCaptures:
0: { name: keyword.other.override.rosetta }
end: '{{rootEnd}}'
patterns:
- include: '#comment'
- include: '#string'

namespace:
name: meta.namespace.rosetta
begin: '{{wordStart}}namespace{{wordEnd}}'
Expand Down
1 change: 1 addition & 0 deletions rosetta-lang/model/Rosetta.xcore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.regnosys.rosetta.rosetta.expression.RosettaExpression
class RosettaModel extends RosettaDefinable {
String name
String version = "0.0.0"
boolean overridden
contains Import[] imports
contains RosettaQualifiableConfiguration[] configurations opposite model
contains RosettaRootElement[] elements opposite model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "http://www.eclipse.org/emf/2002/Ecore" as ecore
// - [RosettaSynonymSource|QualifiedName]

RosettaModel:
'namespace' name=(QualifiedName | STRING) (':' RosettaDefinable)? // TODO do we need STRING here? It is not supported for highlighting.
(overridden?='override')? 'namespace' name=(QualifiedName | STRING) (':' RosettaDefinable)? // TODO do we need STRING here? It is not supported for highlighting.
// TODO colon is inconsistent with other documentation

('version' version=STRING)? // TODO: could do better than STRING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected RosettaConfiguration readConfigFromFile() {
if (file != null) {
return mapper.readValue(file, RosettaConfiguration.class);
}
LOGGER.info("No configuration file was found. Falling back to the default configuration.");
LOGGER.debug("No configuration file was found. Falling back to the default configuration.");
return null;
} catch (IOException e) {
LOGGER.error("Could not read Rosetta configuration.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ class RosettaFormatter extends AbstractRosettaFormatter2 {

def dispatch void format(RosettaModel rosettaModel, extension IFormattableDocument document) {
val extension modelGrammarAccess = rosettaModelAccess

rosettaModel.regionFor.keyword(namespaceKeyword_0)
.prepend[noSpace]
if (rosettaModel.regionFor.keyword(overriddenOverrideKeyword_0_0) !== null) {
rosettaModel.regionFor.keyword(overriddenOverrideKeyword_0_0)
.prepend[noSpace]
.append[oneSpace]

} else {
rosettaModel.regionFor.keyword(namespaceKeyword_1)
.prepend[noSpace]
}
rosettaModel.regionFor.keyword(namespaceKeyword_1)
.append[oneSpace]
rosettaModel.regionFor.keyword(versionKeyword_3_0)

rosettaModel.regionFor.keyword(versionKeyword_4_0)
.prepend[newLine]
.append[oneSpace]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,24 @@ class RosettaGenerator implements IGenerator2 {

@Inject
ResourceAwareFSAFactory fsaFactory;

@Inject
RosettaGeneratorsConfiguration config;

// For files that are
val ignoredFiles = #{'model-no-code-gen.rosetta', 'basictypes.rosetta', 'annotations.rosetta'}

val Map<ResourceSet, DemandableLock> locks = newHashMap

def void beforeAllGenerate(ResourceSet resourceSet, IFileSystemAccess2 fsa2, IGeneratorContext context) {
LOGGER.trace("Starting the before all generate method")
val lock = locks.computeIfAbsent(resourceSet, [new DemandableLock]);
try {
lock.getWriteLock(true);
val models = resourceSet.resources
.filter[!ignoredFiles.contains(URI.segments.last)]
.map[contents.head as RosettaModel]
.filter[config.namespaceFilter.test(it.name)]
.toList
val models = resourceSet.resources.filter[!ignoredFiles.contains(URI.segments.last)].map [
contents.head as RosettaModel
].filter[it.shouldGenerate].toList
val version = models.head?.version // TODO: find a way to access the version of a project directly

externalGenerators.forEach [ generator |
generator.beforeAllGenerate(resourceSet, models, version, [ map |
map.entrySet.forEach[fsa2.generateFile(key, generator.outputConfiguration.getName, value)]
Expand All @@ -95,38 +92,40 @@ class RosettaGenerator implements IGenerator2 {
} catch (CancellationException e) {
LOGGER.trace("Code generation cancelled, this is expected")
} catch (Exception e) {
LOGGER.warn("Unexpected calling before all generate for rosetta -" + e.message + " - see debug logging for more")
LOGGER.warn("Unexpected calling before all generate for rosetta -" + e.message +
" - see debug logging for more")
LOGGER.debug("Unexpected calling before all generate for rosetta", e);
} finally {
lock.releaseWriteLock
}
}

override void beforeGenerate(Resource resource, IFileSystemAccess2 fsa2, IGeneratorContext context) {
if (!ignoredFiles.contains(resource.URI.segments.last)) {
LOGGER.trace("Starting the before generate method for " + resource.URI.toString)
val lock = locks.computeIfAbsent(resource.resourceSet, [new DemandableLock]);
val fsa = fsaFactory.resourceAwareFSA(resource, fsa2, true)
try {
lock.getWriteLock(true);

fsaFactory.beforeGenerate(resource)

val model = resource.contents.head as RosettaModel
if (!config.namespaceFilter.test(model.name)) {
if (!model.shouldGenerate) {
return
}
val version = model.version

externalGenerators.forEach [ generator |
generator.beforeGenerate(resource, model, version, [ map |
map.entrySet.forEach[fsa.generateFile(key, generator.outputConfiguration.getName, value)]
], lock)
]
generator.beforeGenerate(resource, model, version, [ map |
map.entrySet.forEach[fsa.generateFile(key, generator.outputConfiguration.getName, value)]
], lock)
]
} catch (CancellationException e) {
LOGGER.trace("Code generation cancelled, this is expected")
} catch (Exception e) {
LOGGER.warn("Unexpected calling before generate for rosetta -" + e.message + " - see debug logging for more")
LOGGER.warn("Unexpected calling before generate for rosetta -" + e.message +
" - see debug logging for more")
LOGGER.debug("Unexpected calling before generate for rosetta", e);
} finally {
lock.releaseWriteLock
Expand All @@ -141,13 +140,13 @@ class RosettaGenerator implements IGenerator2 {
val lock = locks.computeIfAbsent(resource.resourceSet, [new DemandableLock]);
try {
lock.getWriteLock(true);

val model = resource.contents.head as RosettaModel
if (!config.namespaceFilter.test(model.name)) {
if (!model.shouldGenerate) {
return
}
val version = model.version

// generate
val packages = new RootPackage(model)

Expand All @@ -159,13 +158,13 @@ class RosettaGenerator implements IGenerator2 {
Data: {
dataGenerator.generate(packages, fsa, it, version)
metaGenerator.generate(packages, fsa, it, version)
//Legacy
// Legacy
validatorsGenerator.generate(packages, fsa, it, version)
it.conditions.forEach [ cond |
conditionGenerator.generate(packages, fsa, it, cond, version)
]
//new
//validatorGenerator.generate(packages, fsa, it, version)
// new
// validatorGenerator.generate(packages, fsa, it, version)
tabulatorGenerator.generate(fsa, it, Optional.empty)
}
Function: {
Expand Down Expand Up @@ -218,51 +217,47 @@ class RosettaGenerator implements IGenerator2 {
val fsa = fsaFactory.resourceAwareFSA(resource, fsa2, true)
try {
lock.getWriteLock(true)

val model = resource.contents.head as RosettaModel
if (!config.namespaceFilter.test(model.name)) {
if (!model.shouldGenerate) {
return
}
val version = model.version

externalGenerators.forEach [ generator |
generator.afterGenerate(resource, model, version, [ map |
map.entrySet.forEach[fsa.generateFile(key, generator.outputConfiguration.getName, value)]
], lock)
]
fsaFactory.afterGenerate(resource)

// TODO: move this over to `afterAllGenerate` once the language supports that method as well.
val models = resource.resourceSet.resources
.filter[!ignoredFiles.contains(URI.segments.last)]
.map[contents.head as RosettaModel]
.filter[config.namespaceFilter.test(it.name)]
.toList
javaPackageInfoGenerator.generatePackageInfoClasses(fsa2, models)
val models = resource.resourceSet.resources.filter[!ignoredFiles.contains(URI.segments.last)].map [
contents.head as RosettaModel
].filter[shouldGenerate].toList
javaPackageInfoGenerator.generatePackageInfoClasses(fsa2, models)
} catch (CancellationException e) {
LOGGER.trace("Code generation cancelled, this is expected")
} catch (Exception e) {
LOGGER.warn("Unexpected calling after generate for rosetta -" + e.message + " - see debug logging for more")
LOGGER.warn("Unexpected calling after generate for rosetta -" + e.message +
" - see debug logging for more")
LOGGER.debug("Unexpected calling after generate for rosetta", e);
} finally {
lock.releaseWriteLock
}
}
}
def void afterAllGenerate(ResourceSet resourceSet, IFileSystemAccess2 fsa2, IGeneratorContext context) {

def void afterAllGenerate(ResourceSet resourceSet, IFileSystemAccess2 fsa2, IGeneratorContext context) {
LOGGER.trace("Starting the after all generate method")
val lock = locks.computeIfAbsent(resourceSet, [new DemandableLock]);
try {
lock.getWriteLock(true)

val models = resourceSet.resources
.filter[!ignoredFiles.contains(URI.segments.last)]
.map[contents.head as RosettaModel]
.filter[config.namespaceFilter.test(it.name)]
.toList

val models = resourceSet.resources.filter[!ignoredFiles.contains(URI.segments.last)].map [
contents.head as RosettaModel
].filter[shouldGenerate].toList
val version = models.head?.version // TODO: find a way to access the version of a project directly

externalGenerators.forEach [ generator |
generator.afterAllGenerate(resourceSet, models, version, [ map |
map.entrySet.forEach[fsa2.generateFile(key, generator.outputConfiguration.getName, value)]
Expand All @@ -271,10 +266,16 @@ class RosettaGenerator implements IGenerator2 {
} catch (CancellationException e) {
LOGGER.trace("Code generation cancelled, this is expected")
} catch (Exception e) {
LOGGER.warn("Unexpected calling after all generate for rosetta -" + e.message + " - see debug logging for more")
LOGGER.warn("Unexpected calling after all generate for rosetta -" + e.message +
" - see debug logging for more")
LOGGER.debug("Unexpected calling after all generate for rosetta", e);
} finally {
lock.releaseWriteLock
}
}

private def boolean shouldGenerate(RosettaModel model) {
config.namespaceFilter.test(model.name) || model.overridden
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MetaFieldGenerator {

for (ref:refs) {
val targetModel = ref.type.model
if (config.namespaceFilter.test(targetModel.name)) {
if (targetModel.shouldGenerate) {
val targetPackage = new RootPackage(targetModel)
val metaJt = ref.toMetaJavaType

Expand All @@ -106,7 +106,7 @@ class MetaFieldGenerator {
val metas = nsc.value.filter(Data).flatMap[expandedAttributes].filter[hasMetas && !metas.exists[name=="reference" || name=="address"]].toSet
for (meta:metas) {
val targetModel = meta.type.model
if (config.namespaceFilter.test(targetModel.name)) {
if (targetModel.shouldGenerate) {
val targetPackage = new RootPackage(targetModel)
val metaJt = meta.toMetaJavaType

Expand Down Expand Up @@ -306,6 +306,10 @@ class MetaFieldGenerator {
return rc.model.name
}

private def boolean shouldGenerate(RosettaModel model) {
config.namespaceFilter.test(model.name) || model.overridden
}

/** generate once per resource marker */
static class MarkerAdapterFactory extends AdapterFactoryImpl {

Expand Down

0 comments on commit 3bda3d1

Please sign in to comment.