NMFCode / NMF

This repository contains the entire code for the .NET Modeling Framework
BSD 3-Clause "New" or "Revised" License
36 stars 15 forks source link

Ecore2Code does not handle multiple namespaces #44

Closed ChrisH07 closed 5 years ago

ChrisH07 commented 5 years ago

I'm using Ecore2Code to generate classes from a nmf file that contains references to types in another metamodel. Since I use a different namespace when generating code for the referenced metamodel, the output when using resolve creates the wrong namespace for the referenced metamodel. The root cause is Namespace2Namespace.GetName using the DefaultNamespace from the transformation since it has no other information. To workaround this issue I added a Uri to base namespace map in Meta2ClassesTransformation. I modified the syntax of the resolve string to be of the form: uri<@baseNamespace>=file where the bracketed part is optional then populate this map. Namespace2Namespace.GetName then tests if the model Uri is in the map and uses that baseName instead. If this is a reasonable fix this I can submit a pull request.

georghinkel commented 5 years ago

Hi,

to be honest, I havent't used the code generator with types split across multiple assemblies that much, so thoughtful contributions here are generally welcomed.

If I understood you correctly, you made the <@basenamepace> optional, is it? My first idea was that the code generator should load the entire assembly in such a case, but I think your solution is a more lightweight alternative. Therefore, I would be happy to merge a PR.

Best regards,

Georg

ChrisH07 commented 5 years ago

Hi Georg,

Yes. That part is optional, e.g. you would write http://some.com/metamodel/@some=metamodel.nmf if you wanted to override the base namespace and get some.metamodel as the complete namespace. Otherwise you drop the @ part and it works like it does now. I'm not sure what you mean by load the entire assembly but the code generator still generates the code for the reference models. I actually needed a way to suppress generating the reference model code which was easy enough but still needed to generate the namespace reference correctly. I'll cleanup what I have and submit a PR.

Chris

georghinkel commented 5 years ago

If you load an assembly that has a metamodel as an embedded resource, NMF automatically loads the metamodel into the meta repository which all model repositories reference. In addition, NMF scans the assembly for classes that have a ModelRepresentationClassAttribute and attach a stereotype to the loaded metamodels that contains a reference to the actual type. The code generator recognizes this stereotype and uses the correct namespace. However, this requires the code generator to load the assembly (and not only in pure reflection mode since the attributes are actually loaded). Therefore, this causes considerably more overhead, in addition to the fact that I think there is currently no option to load additional assemblies in Ecore2Code.

ChrisH07 commented 5 years ago

That makes a lot of sense. Ultimately, I will be creating and loading metamodels at runtime so that approach solves my longer term problem. I might just look at adding assembly loading to Ecore2Code. Until then I've submitted #45 to solve this problem.

ChrisH07 commented 5 years ago

Closing due to PR merge.