Book a Demo

Author Topic: How to get the instance element of a classifier element  (Read 4242 times)

JiangangWang

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
How to get the instance element of a classifier element
« on: August 22, 2023, 08:14:23 pm »
I am writing the script which needs to get all the instance elements of a classifier element, but it seems there is no such attribute of an element class in the object model.
i.e. this is the script to get the classifier element ( currentElement is the Property element which is typed by classifierObject)
dim classifierObject as EA.Element
set classifierObject = Repository.GetElementByID(currentElement.PropertyType)

then this one is trying to find the instance element (i.e. a sequence element with classifierObject as its classifier)
dim sequenceObject as EA.Element
set sequenceObject = XXX

does anyone know how to achieve it?
Thanks.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to get the instance element of a classifier element
« Reply #1 on: August 22, 2023, 08:29:37 pm »
You'll have to use a query.

Something like

Code: [Select]
Repository.GetElementset("select o.Object_ID from t_object o where o.Classifier = 123", 2)
Geert

JiangangWang

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: How to get the instance element of a classifier element
« Reply #2 on: August 22, 2023, 09:22:34 pm »
Thanks a lot, Geert, it works well.

Actually I am writing the script to change the element used in sequence diagram, the reason is that SE currently uses Property defined in Architecture element directly in sequence diagram, not lifeline/sequence element.

The current script just changes the property element to the sequence element with the same classifier, but when I run it, I found the elements are changed but all the messages in between is lost.
It seems it doesn't work by purely changing the element type on the diagram.

Do you know how to get the message remained when changing element type?
Thanks

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to get the instance element of a classifier element
« Reply #3 on: August 22, 2023, 10:07:34 pm »
Changing the element type is not enough.

For each lifeline you should create a new object of type lifeline, and set it's classifier to the architecture element.
Do that for each "lifeline" on the diagram
Then put each of these lifelines on the diagram (using the same coördinates as the original diagram object)
Then you need to remap the messages on this diagram to the new lifeline objects. (source and target id)

That should do it.

Hopefully your messages are not re-used on different diagrams, because in that case you'll have to duplicate the messages as well.

Geert

JiangangWang

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: How to get the instance element of a classifier element
« Reply #4 on: August 23, 2023, 11:26:31 am »
Thanks Geert.

It seems sequence/message is not stored in t_diagramlinks table, but t_connector.
so I have to use Repository.GetConnectorByID or Repository.GetConnectorByGuid with SQL search shown as below.
dim messageConnector as EA.Connector
set messageConnector = Repository.GetConnectorByGuid("select o.ea_guid from t_connector o where o.Start_Object_ID = " & currentdiagramObject.ElementID)

but when I tried to session.output to see the result, it reports the error "lack of object 'messageConnector' ".
Do you know if something wrong here?

Thanks
Wang

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to get the instance element of a classifier element
« Reply #5 on: August 23, 2023, 02:43:10 pm »
set messageConnector = Repository.GetConnectorByGuid("select o.ea_guid from t_connector o where o.Start_Object_ID = " & currentdiagramObject.ElementID)
You can't use that method like that. It only accepts a GUID, not a query.

Use Repository.SQLQuery, and then parse the resulting xml string for the GUID.

Here how I do that:

Code: [Select]
function getConnectorsFromQuery(sqlQuery)
dim xmlResult
xmlResult = Repository.SQLQuery(sqlQuery)
dim connectorIDs
connectorIDs = convertQueryResultToArray(xmlResult)
dim connectors
set connectors = CreateObject("System.Collections.ArrayList")
dim connectorID
dim connector as EA.Connector
for each connectorID in connectorIDs
if connectorID > 0 then
set connector = Repository.GetConnectorByID(connectorID)
if not connector is nothing then
connectors.Add(connector)
end if
end if
next
set getConnectorsFromQuery = connectors
end function

'converts the query results from Repository.SQLQuery from xml format to a two dimensional array of strings
Public Function convertQueryResultToArray(xmlQueryResult)
    Dim arrayCreated
    Dim i
    i = 0
    Dim j
    j = 0
    Dim result()
    Dim xDoc
    Set xDoc = CreateObject( "MSXML2.DOMDocument" )
    'load the resultset in the xml document
    If xDoc.LoadXML(xmlQueryResult) Then       
'select the rows
Dim rowList
Set rowList = xDoc.SelectNodes("//Row")

Dim rowNode
Dim fieldNode
arrayCreated = False
'loop rows and find fields
For Each rowNode In rowList
j = 0
If (rowNode.HasChildNodes) Then
'redim array (only once)
If Not arrayCreated Then
ReDim result(rowList.Length, rowNode.ChildNodes.Length)
arrayCreated = True
End If
For Each fieldNode In rowNode.ChildNodes
'write f
result(i, j) = fieldNode.Text
j = j + 1
Next
End If
i = i + 1
Next
'make sure the array has a dimension even is we don't have any results
if not arrayCreated then
ReDim result(0, 0)
end if
end if
    convertQueryResultToArray = result
End Function

Geert