Author Topic: "Global" collection of associations  (Read 7204 times)

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
"Global" collection of associations
« on: February 01, 2013, 03:10:19 am »
Hi again all,

When I try generate a special code format I need to collect all associations in the model into a collection outside the context of classes, but how do I use the list macro to retrieve the "Linked Attributes" when in the scope of the File

(Model
         ...
      (Classes
              %list="Class" @separator="\n" @indent="            "%
      )
      (Associations
             %list="Attribute"... linkAttAccess!=""%  // <- only associations
      )
)

// Rolf Lampa
-- There's always a solution --

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: "Global" collection of associations
« Reply #1 on: February 01, 2013, 07:24:05 am »
OK, I did a dirty trick.
    1. I removed all code generation from one of the templates in the  different templates for Class.

    2. In the now "free" to use template (ClassImpl) I collected only the associations ("Linked Attributes").

    3. This means I can call for two (2) different Class templates from the top level File:

      a: one call to the template
Class (regular call from File, but stripped from all Associations) and

b: one call to the template ClassImpl which only lists Associations.
[/list]
[/list]The result is two listings of model elements collected from the same level (same level as Class):
    Model(
      (
Classes                <-- Listing 1
    (ClassA
    )
    (ClassB
    )
)
(Associations        <-- Listing 2
    (Association1
    )
    (Association2
    )
)[/list])
[/list][/color]

Mission completed.

// Rolf Lampa
« Last Edit: February 01, 2013, 07:35:05 am by Rolf_Lampa »
-- There's always a solution --

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: "Global" collection of associations
« Reply #2 on: February 01, 2013, 08:53:17 am »
I was wrong.

Everything works fine except for the fact that from the Class scope in the code template I can only get hold of the TargetRole of an association. What I cannot resolve is (as it seems):

1. the name and properties of the Association itself, which the TargetRole belongs to, nor can I get

2. the name and properties of the SourceRole.

So, my Q: Is there any other way to retrieve (from the Code Gen Templates) a collection of all Associations in the model (with all their properties and RoleInfos) other than creating an add-in-script and call it with the %EXEC_ADD_IN(...)% macro?

// Rolf Lampa
 

« Last Edit: February 01, 2013, 08:55:20 am by Rolf_Lampa »
-- There's always a solution --

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8061
  • Karma: +118/-20
    • View Profile
Re: "Global" collection of associations
« Reply #3 on: February 01, 2013, 08:53:27 am »
It's not really a dirty trick. It's named with the "Impl" suffix because it's used for C/C++/Delphi, but really it's just a secondary set of templates. Even if you didn't have that you can make a custom template applying to Class and list over that instead.

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: "Global" collection of associations
« Reply #4 on: February 01, 2013, 08:58:10 am »
OK Simon, but unfortunately it seems like I couldn't access the desired info using std template substitutions anyway. :(
-- There's always a solution --

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8061
  • Karma: +118/-20
    • View Profile
Re: "Global" collection of associations
« Reply #5 on: February 01, 2013, 09:02:39 am »
Replace
%list="Attribute"... linkAttAccess!=""%
with
%list="Connector"... connectorType="Association" and connectorSourceElemGUID=classGUID%

See http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/standard_uml_models/connector_field_substitution_m.html
« Last Edit: February 01, 2013, 09:03:04 am by simonm »

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: "Global" collection of associations
« Reply #6 on: February 01, 2013, 10:20:21 am »
Thank you very much Simon for this hint (I should have found it myself),

However, the list macro doesn't return anything (there's two Associations in my test model), not even if I remove the classGUID test (I also corrected the single '=' into '==') Like so:
    $aList=%
list="Connector" @separator="\n" @indent="      " connectorType=="Association"%
%if $aList != ""%
$aList
%else%
Nada
%endIf%
[/list]
Traverse lists? - Btw, if it had returned the expected list of associations, is it possible to iterate through the list items in order to retrieve the inner details from the individual associations? (there seems to be no dedicated templates for Connections).

The goal - I would need to get to following end result from each Association, given this relation between Person and Address:

      [Person]-0..*-residents-----homeaddress-0..1-[Address]:

     (Associations
            (Association
                  "LinkResidents"
                  "<NONE>"
                  ""
                  ""
                  "derived=False, + more Tagged Values..."
                  FALSE
                  (Roles
                        (Role
                              "homeAddress"
                              TRUE
                              FALSE
                              "Person"
                              ""
                              "0..1"
                              ""
                              0
                              2
                              0
                              ""
                              (Qualifiers
                              )
                        )
                        (Role
                              "residents"
                              TRUE
                              FALSE
                              "Address"
                              ""
                              "0..*"
                              ""
                              0
                              2
                              0
                              "Bold.Embed=False"
                              (Qualifiers
                              )
                        )
                  )
            )
            (Association
                  ... etc.
            )
      )


// Rolf Lampa
« Last Edit: February 01, 2013, 10:29:25 am by Rolf_Lampa »
-- There's always a solution --

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8061
  • Karma: +118/-20
    • View Profile
Re: "Global" collection of associations
« Reply #7 on: February 01, 2013, 11:37:32 am »
Sorry, in code generation templates you'll need to make a custom template for the connectors first. (http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/standard_uml_models/customtemplates.html)

You'll then call the list with the name generated.

eg.
$aList=%list="Connector__MyCode" @separator="\n" @indent="      " connectorType=="Association"%

Note: The help is wrong about the name. It uses a single underscore, it requires a double.

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: "Global" collection of associations
« Reply #8 on: February 01, 2013, 07:51:42 pm »
OK, thank you very much for this Simon, I'll try this today, It's exactly what I needed to know.

Best Regards,
// Rolf Lampa
-- There's always a solution --

RIL

  • EA User
  • **
  • Posts: 164
  • Karma: +3/-0
  • -- There's always a solution --
    • View Profile
Re: "Global" collection of associations
« Reply #9 on: February 02, 2013, 04:45:28 am »
First I want to say that your instruction works very well. I can now get (almost) all the details I'm looking for. But one little detail escapes me:

Q: How can I get hold of the LinkClassName of a link class association?

I only found the following substitutions for the Association itself named "LinkWithAssociationClass". Is there a way to get hold of the link class (in my test model it's named "AssociationClass" but it doesn't show up in any existing substitutions that I could find (also looking for substitutions with IntelliSense)):

    (Association
          connectorName= "LinkWithAssociationClass"
          connectorAlias=
          connectorDirection= Source -> Destination
          connectorEffect=
          connectorGuard=
          connectorGUID= {512F91F7-F611-412c-ABD7-903EE5F5474F}
          connectorNotes=
          connectorStereotype=
          connectorTrigger=
          connectorType= Association
          connectorWeight=
          connectorTag=
          (Roles
                ...
          )
    )

    [/list]



    Documentation Hint: There are missing substitution items in the (web) help file:

    http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/standard_uml_models/connector_field_substitution_m.html

    In the list of substitutions for Connectors (at least) the following substitution names are missing:
      connectorSourceElemGUID
      connectorSourceElemName
      connectorSourceTag

      [/list]
      and ditto for Dest:
        connectorDestElemGUID
        connectorDestElemName
        connectorDestTag

        [/list]
        // Rolf Lampa
        « Last Edit: February 02, 2013, 04:50:46 am by Rolf_Lampa »
        -- There's always a solution --

        Eve

        • EA Administrator
        • EA Guru
        • *****
        • Posts: 8061
        • Karma: +118/-20
          • View Profile
        Re: "Global" collection of associations
        « Reply #10 on: February 04, 2013, 08:26:53 am »
        Last I knew there was no way to get any information about an association class from the code templates. ie. You couldn't tell if an association was an association class or the other way around.

        RIL

        • EA User
        • **
        • Posts: 164
        • Karma: +3/-0
        • -- There's always a solution --
          • View Profile
        Re: "Global" collection of associations
        « Reply #11 on: February 04, 2013, 09:35:38 pm »
        Quote
        Last I knew there was no way to get any information about an association class from the code templates.
        Ouch. :o

        Finding a workaround

        But OK, then there's the option to try to look for a work around instead.

        One approach would be to make use of the fact that the code platform I'm generating code for (Bold Architecture in this case) actually has the modelling rule that the name of the Link Class Association (§2) would be spelled with lowe-case initial letter, and the corresponding Link Class itself (§1) be spelled with upper-case initial letter as usual, like so:
          1:               [
        AssociationClass]
                                       |
        2: <--role1--[associationClass]---role2-->[/list]
        Q: With the above in mind, is there an neat way to determine a lower-case first char in the association name (without too much performance penalty), using only the code gen syntax?

        (If such an association is found I could simply try to retrieve a Class with the same name (spelled with an uppercase initial letter) and if found I can conclude that that very class is the modelled link class).

        This is perhaps not a generic solution to the problem, but in the case of Bold Architecture the model names must follow this rule anyway.

        Regards,

        // Rolf Lampa
        « Last Edit: February 04, 2013, 09:41:06 pm by Rolf_Lampa »
        -- There's always a solution --

        RIL

        • EA User
        • **
        • Posts: 164
        • Karma: +3/-0
        • -- There's always a solution --
          • View Profile
        Re: "Global" collection of associations
        « Reply #12 on: February 04, 2013, 09:54:18 pm »
        if the FIND(<src>, <substring>) macro is case sensitive then it should be possible to solve the problem.

        EDIT: Yes, the FIND macro is case sensitive, so the following code works (the scope is Connector, so I can't "confirm" the existence of the LinkClass here though). I only assume that the corresponding LinkClass exist:

          $COMMENT = "// Get first char from association name            //"

          $LinkClassName = ""
          $LinkName = %connectorName%
          $FirstChar = %LEFT($LinkName, 1)%

          $COMMENT = "// Determine if lower-case                               //"

          $Pos = %FIND("abcdefghijklmnopqrstuvwxyz", $FirstChar)%
          %if $Pos != "-1"%
          $COMMENT = "// Yes, so make a Class name with upper-case initial //"
          $FirstChar = %TO_UPPER($FirstChar)%
          $_lassName = %MID($LinkName, 1)%
          $LinkClassName = $FirstChar + $_lassName
          %endIf%

          $COMMENT = "// Build result                                                 //"

          (Association
          "$LinkName"
          "$LinkClassName"    <-- Empty for regular non-linkclass associations
          ...
          )[/list]

          Perhaps there's a more efficient way to accomplish the same thing but at least this works.

          // Rolf Lampa
          « Last Edit: February 05, 2013, 01:59:24 am by Rolf_Lampa »
          -- There's always a solution --