yellicode / elements

The meta model API for Yellicode - an extensible code generator.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get Association relationships for a target class?

medleykupps opened this issue · comments

commented

I can add an Association to a entity using the Yellicode Modeller tool, however, I can't seem to work out how to get access to this information from the model object inside a template.

Generator.getModel().then((model: elements.Model) => {
  model.getAllClasses().forEach((targetClass) => {
    // access the Associations relevant to this targetClass object
  });
});

Any help appreciated?

You can access the associations as follows (you will need to iterate elements at the root of the model). Depending on how you want to use them, you might want to build a map that you can use for looking up the associations for a particular class.

 model.packagedElements.forEach(pe => {
        if (elements.isAssociation(pe)) {
            // An association has 2 ends, but can have more more in UML.
            // The owner of each association end is an Element, typically a Class or Interface in your model.
            const owner1 = pe.memberEnds[0].owner as elements.Class; 
            const owner2 = pe.memberEnds[1].owner as elements.Class; 
            // TODO: e.g. add the class to a Map<elements.Class, elements.Assocation[]> to lookup later by class...
        }
    });

The reason for this approach is that technically (in UML), an Association is never "owned" by a Class (or any other type), but always by the model itself.

commented

Thanks for your help and your quick response! That was exactly what I was looking for.

I found the class types I was looking for at the following properties:

pe.memberEnds[0].type.name
and
pe.memberEnds[1].type.name

Thanks again.