option explicit
!INC Local Scripts.EAConstants-VBScript
' Script Name: CreateOperationFromMessage
' Author: Shimon M.
' Purpose:Adding operations based on messages in Sequence Diagram
' Date:2024-12-24
'
Repository.EnsureOutputVisible ("Running Script")
dim selectedConnector as EA.Connector
dim currentDiagram as EA.Diagram
dim msgReciever as EA.Element
dim selectedObjects as EA.Collection
dim strUser ' Used to add user name (from Model Security or Windows logon) to Notes.
dim outputMode ' change the value of outputMode to "PROMPT" in order to send all output to a user prompt
' outputMode = "PROMPT"
Function createOperationForMessage (connector)
if messageOK(connector) = TRUE then
Session.Prompt "Creating Operation for " & connector.Name &" message into " & msgReciever.Name, promptOK
CALL addOperation(msgReciever, connector)
ELSE
Session.Prompt "Can't create operation for message. See System Output window for more information.", promptOK
end if
End Function
Function addOperation (cls, connector )
Dim aConnector as EA.Connector
Set aConnector = connector
outputMessage ("Class: " & cls.Name &" Operation: " & aConnector.Name & " Note: " & aConnector.notes )
' Add a method
dim methods as EA.Collection
dim newMethod as EA.Method
dim notePrefix
dim noteSuffix
If SecurityUser.Surname = "" then
strUser = "User:: " & CreateObject ("WScript.Network").UserName
Else strUser ="User: " &SecurityUser.Surname & ", " & SecurityUser.FirstName
End IF
outputMessage (strUser)
notePrefix = "The following operation was added by: " & strUser & Chr(13) & Chr(10)
noteSuffix =Chr(13) & Chr(10) &"Added on " & Date()
set methods = cls.Methods
set newMethod = methods.AddNew(aConnector.Name,"void")
newMethod.Notes = notePrefix & aConnector.notes & noteSuffix
newMethod.Stereotype = "NewRequirement"
newMethod.Update()
methods.Refresh()
outputMessage ("Added method: " & newMethod.Name )
' Add tagged value to message, to point to created operation.
' This doesn't cause the Model to see the message as an operation, but prevents this script from creating doubles.
Dim taggedValue as EA.TaggedValue
dim taggedValueValue
taggedValueValue = newMethod.MethodGUID
outputMessage ("MethodGUID" & " "& newMethod.MethodGUID & " "& taggedValueValue )
set taggedValue = aConnector.TaggedValues.AddNew( "operation_guid", taggedValueValue )
taggedValue.Update()
End Function
' The following takes a connector and verifies that the receiving object is a Class and the connector type is Sequence.
Function messageOK (connector)
outputMessage ("Checking the message")
set msgReciever = Repository.GetElementByID(connector.SupplierID)
outputMessage ( "msgReciever Name is " & msgReciever.Name & " msgReciever Type is " & msgReciever.Type )
if (msgReciever.Type <> "Class") then
outputMessage ("RecieverType NOT Class")
elseIf (selectedConnector.Type <> "Sequence") then
outputMessage ("Wrong connector type selected")
elseIf msgIsOperation (connector) = TRUE then
outputMessage ( "Operation seems to exist")
elseIF connector.Name = "" then
outputMessage ( "Message name is empty ")
else
messageOK = TRUE
End IF
end Function
'Checks if the message is (or was) already an operation.
Function msgIsOperation (connector)
Dim aConnector as EA.Connector
Dim aTaggedValue as EA.Element
set aConnector = connector
set aTaggedValue = aConnector.TaggedValues.GetByName ("operation_guid")
outputMessage ("Checking if the message has an entry, that points to an Operation")
IF not aTaggedValue is nothing then
outputMessage ("Message has a connector tag with Name: " & aTaggedValue.Name)
msgIsOperation = TRUE
else
msgIsOperation = FALSE
end if
End Function
'The following function takes a String and outputs it to the System Output or to a user prompt, depending on the Value of outputMode.
Function outputMessage (txt1)
if outputMode = "PROMPT" then
Session.Prompt txt1 , promptOK
else
Session.Output(txt1)
end if
end Function
'The following function returns the Type of the msgReciever. It is not used in the present script.
Function msgRecieverType (msgReciever)
Session.Prompt "Checking the message reciever " & msgReciever.Name & " Type: " & msgReciever.Type, promptOK
msgRecieverType = msgReciever.Type
End Function
' Diagram Script main function
sub OnDiagramScript()
' Get a reference to the current diagram
set currentDiagram = Repository.GetCurrentDiagram()
if not currentDiagram is nothing then
' Get a reference to any selected connector/objects
set selectedConnector = currentDiagram.SelectedConnector
set selectedObjects = currentDiagram.SelectedObjects
if not selectedConnector is nothing then
' A connector is selected.
' outputMessage ( "Diagram name and type are: " & currentDiagram.Name & " " & currentDiagram.Type )
' outputMessage ( "Message name and arguments are as follows " &selectedConnector.Name & " " & selectedConnector.MessageArguments & " " &selectedConnector.ReturnValueAlias)
createOperationForMessage(selectedConnector)
else if selectedObjects.Count > 0 then
Session.Prompt selectedObjects.Count & " Objects were selected ", promptOK
else
Session.Prompt "Nothing was selected ", promptOK
end if
end if
else
Session.Prompt "This script requires a diagram to be visible", promptOK
end if
end sub
OnDiagramScript