Search code examples
umlcode-generationacceleoocl

Generate field from association


I want to generate a field from association. The problem that I have is that I'm using:

[for (p: Property | aClass.getAssociations().ownedEnd -> select(p: Property | p.type <> aClass)) separator('\n')]
    [p.visibility/] [p.type.name/] [p.type.name.toLowerFirst()/];
[/for]

[for (p: Property | aClass.getAssociations().ownedEnd -> select(p: Property | p.type = p.getOtherEnd().type)) separator('\n')]
    [p.visibility/] [p.type.name/] [p.type.name.toLowerFirst()/];
[/for]

So that checks for all associations and if the association type isn't the same ass class type it creates a field of that type. And if the ends are the same type it creates a field of the same type as the class in which it is contained.

So basically if there is association A - B and A - A For B create A, for A create B and A. But there is a problem with second loop. It creates two fields instead of one, which is pretty logical. And here comes the question. How I can fix this? I'm not sure if I'm able to declare some kind of variable here and check %2. I can't just take one value, because there could be multiple associations. Perhaps I did all the thing wrong? Maybe there is a way to iterate over the ends that would save me all those checks and two for loops?


Solution

  •     [for (a: Association | aClass.getAssociations())]
            [if a.ownedEnd.type = a.ownedEnd.getOtherEnd().type]
                [a.visibility/] [a.endType.name/] [a.endType.name.toLowerFirst()/];
            [else]
                [for (p: Property | a.ownedEnd -> select (type <> aClass))]
                    [p.visibility/] [p.type.name/] [p.type.name.toLowerFirst()/];
                [/for]
            [/if]
        [/for]
    

    That seems to do the trick