Generate multiple targets from single source


as stated into the section "How to generate multiple targets from single source?" of the Generator cookbook, I would like to use the second option: have no generator into my language plus one language for each desired target, each one with its generator.

I would like to have also a solution for each target for example to add a parameter to each generation target. So we have this model:

  • language MyLang (no generator)
  • solution MySol using MyLang

and this generators

  • language toJava, with generator to generator java
  • solution using toJava with details for Java (import MySol).
  • language toXml, with generator to generator xml
  • solution using toXml with details for XML (import MySol).
  • ecc.

The question is: I would like each target generator be able "to see" models from MySol while generating its solution, for example the java solution, without the need for the java solution to reference each nodes of the MySol solution somewhere. It's possible?

Obviously a simple solution is to have a container concept into the java solution that references all root nodes of MySol. But this way I have to change the java solution each time a root node is addeded/removed from MySol.

And if using "Language engaged on generation" I should put everything into one single solution.

Maybe I can reformulate my question:

can a generator while generating SolutionB see nodes from SolutionA just because SolutionB is importing SolutionA? I mean without having nodes from SolutionB referencing nodes of SolutionA?

Short answer would be "No". Generator takes a model, and transforms a model. If your model references nodes from another models, you indeed can access them, although usually just to get corresponding generated artefacts (e.g. Component1 from model A references Component2 from model B, and while generating classes for Component1 you'd like to invoke classes generated from model B, you could use export labels to find out which class was generated for Component2).

Longer answer would be "It's possible", and depends how far you'd like to go down the road of internal and non=stable API ;). genContext.originalModel gives you model object which you could cast to openapi.SModel, and then internal API j.m.smodel.SModelOperations.getImportedModelUIDs() gives you list of model imports, which you'd need to resolve to get to SModel/model object again.
I got it!
However I did not need to go that deep: with your hint about genContext.originalModel I saw it has the method rootsIncludingImported() and this is actually returning roots from the imported solution.

I created reference nodes into a transient input model using a pre-precessing script, indeed my first pre-processing script.
Don't know if it's the best implementation but it works. Thx!
Indeed, I forgot to mention rootsIncludingImported, as it's quite rarely used. However, rootsIncludedImported is much better approach than the one I've described with getImportedModelUIDs.

Please sign in to leave a comment.