Book a Demo

Author Topic: EA-Matic : auto update diagram depending of stereotype  (Read 4917 times)

sylvain68

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
EA-Matic : auto update diagram depending of stereotype
« on: January 31, 2018, 03:10:54 am »
Hi,

I'm trying to specialize the auto-updating diagrams script.
The idea is to automate the update based on the stereotype of the related element.

If a diagram is named "auto_map2principles", and if after the underscore, the word starts with "map2" then this diagram is only updated if a relationship with an element with stereotype "principle" is created.
Principle is not a stereotype of course, therefore I would like to create a mapping with Archimate stereotype like this :

Case keyword = "principles" then the expected stereotype for the element is Archimate_Principles.
Case keyword = "actors", then the expected stereotype for the element  is Archimate_BusinessActors.

Here is the code based on the auto-updating diagrams script :

Code: [Select]
'adds the related element to the auto_updatediagrams if any
function addRelatedElementoAutoDiagram(element,relatedElement, model)

' Show the script output window
Repository.EnsureOutputVisible "Script"

'get the diagram owned by this element
dim ownedDiagrams
set ownedDiagrams = model.toArrayList(element.ownedDiagrams)
for each diagram In ownedDiagrams
'check the name of the diagram
if Left(diagram.name,LEN("AUTO_")) = "AUTO_" then
'Check if it is a map...
if Left(lcase(right(diagram.name,(LEN(diagram.name) - LEN("AUTO_")))),LEN("map2")) = "map2" then
'It is a map, searching the stereotype
select case Right(lcase(right(diagram.name,(LEN(diagram.name) - LEN("AUTO_")))),(LEN(lcase(right(diagram.name,(LEN(diagram.name) - LEN("AUTO_"))))) - LEN("map2")))
Case "principle"
myStereotype = "ArchiMate_Principle"
Case "goals"
myStereotype = "ArchiMate_Goals"
Case "actors"
myStereotype = "ArchiMate_BusinessActor"
Case Else
'Problem, keyword is not matching !
exit function
End Select

if relatedElement.Stereotype <> myStereotype Then
'Debug.Print "Element stereotype is not matching, exiting."
exit function
end if
'Session.Output "Adding related element to the diagram"
'add the related element to the diagram
diagram.addToDiagram(relatedElement)
end if
end if
next
end function

I have some issues :

1) because
Code: [Select]
relatedElement.Stereotype is not a valid property neither a valid method. I guess it's because "model" itself a type of the EAAddinFramework ...
2) an element can have many stereotypes, therefore I will have to control a list... : has someone an idea how to do that ?
3) Session.Ouput seems not working : any idea how to get message in the system ouput window ?

Could someone give me some help ?

Sylvain

Nizam

  • Prolab Moderator
  • EA User
  • *
  • Posts: 320
  • Karma: +15/-2
  • Model Sharing - Simplified
    • View Profile
    • Professional Model Collaboration
Re: EA-Matic : auto update diagram depending of stereotype
« Reply #1 on: January 31, 2018, 04:20:59 am »
1. Stereotype is a valid property (refer to http://sparxsystems.com/enterprise_architect_user_guide/13.0/automation/element2.html) . You need to check if 'RelatedElement' is not null, you could be relying on a  EA call (like Context Item, etc) which could be NULL.
2. You need to look at StereotypeEx field
3. Are you doing the 'Debug'  (If I'm not wrong Run wouldn't  write to  Output console). You can enable 'System Output' from Windows button in ribbon. or searching for 'System Output'

HTH
Nizam

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: EA-Matic : auto update diagram depending of stereotype
« Reply #2 on: January 31, 2018, 06:42:55 am »

3. you can add Repository.EnsureOutputVisible ("System")

not sure about that VB syntax here...

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: EA-Matic : auto update diagram depending of stereotype
« Reply #3 on: January 31, 2018, 05:57:14 pm »
Hi Silvain,

The script you are starting from uses the EAAddinframework as you noticed.
So that means you are no longer programming against the EA API, but against the library of wrappers I wrote.

The relatedElement you get as a parameter is coming from connector.relatedElements, which returns hashmap of UML.Classes.Kernel.Element (see ConnectorWrapper)

The Element interface does not define a property Stereotype but it does have a property called stereotypes, a hashmap of Stereotypes. So before using that in VBScript you'll have to convert that to an ArrayList like we did with the RelatedElements (generics cannot be used in VBScript)

Then you can iterate the list of stereotypes and check the .name property.

Alternatively you can rely on the properties and operations of the Element class, which is the parent class of almost all classes in the EAAddin framework.
That class defines an operation to test for a specific stereotype:
bool HasStereotype(string stereotype)
and also a list of stereotype names
List<string> stereotypeNames which you have to convert to an ArrayList again before being able to use it.

Hmm, explaining it like that makes it look very complicated doesn't it :-[ I didn't really intend the library for usage in scripts. It is just a side effect of installing the EA Toolpack. The library is much richer then the standard EA API, and often allows you to write denser and more functional code. But it also has somewhat of a learning curve I guess.

Geert

PS. You don't have to use the EAAddinLibrary when writing EA-Matic scripts. You can also simply use the raw EA API.