Book a Demo

Author Topic: Automatic method addition  (Read 11359 times)

neuron

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Automatic method addition
« on: March 19, 2008, 06:46:30 pm »
Has EA feature like this ? ->

When I create method m() in sequence diagram, m() method will be automaticaly added to class Class2

A: Class1  B: Class2
--             --
|      m()    |
|   -------> |
|               |

Luis J. Lobo

  • EA User
  • **
  • Posts: 252
  • Karma: +0/-0
  • IT Consultant
    • View Profile
Re: Automatic method addition
« Reply #1 on: March 19, 2008, 08:32:26 pm »
When you trace a message from:

Object1:Class1  ----> Object2:Class2

...in the message properties window, exists a button called "Operations..." that gives you to the parent class operations window, where you can create the method.

When method is created, you can select it from the combobox of the message window.

shimon

  • EA User
  • **
  • Posts: 172
  • Karma: +6/-0
    • View Profile
Re: Automatic method addition
« Reply #2 on: December 06, 2024, 01:30:20 am »
Hi,
As this seems to be deprecated in version 15.2 ( I think it got dropped in version 12), did anyone make a simple script to add the operation to the receiving Class?
Shimon

shimon

  • EA User
  • **
  • Posts: 172
  • Karma: +6/-0
    • View Profile
Re: Automatic method addition
« Reply #3 on: December 23, 2024, 05:20:33 pm »
Hi,
As the saying (Pirkey Avot) goes:
If I am not (there) for myself, who is there?
And when I am all to myself, what am I?
And if not now ( i.e. if you don't get around to it now) , then when?

I started writing a script that adds an operation to the receiving class on a sequence diagram, based on the message chosen. I might extend it to run through an entire diagram.

I just wrote the outline of checks that I do, to verify that I have the right kind of connector, and that it is not already an operation. I now added the operation with the notes of the message, but even after adding a tagged value to the message, when I rename the operation, the message does not get renamed.
This means that you have to manually change the message to the operation that you just created. This is not the most elegant solution.
I'll try to share this script.
Sincerely,
Shimon
P.S. Meanwhile this works only on Classes, not Objects (instances).

« Last Edit: December 24, 2024, 03:28:51 am by shimon »

shimon

  • EA User
  • **
  • Posts: 172
  • Karma: +6/-0
    • View Profile
Re: Automatic method addition
« Reply #4 on: February 20, 2025, 03:38:39 am »
Hi,
As Sparx didn't understand the problem I described that the message didn't point to the newly created operation, I uploaded two screen-recordings to youtube.
I'll add the script here, later on.

https://youtu.be/2aIX1_bzHTU
https://youtu.be/9yYU63Ms02Y





shimon

  • EA User
  • **
  • Posts: 172
  • Karma: +6/-0
    • View Profile
Re: Automatic method addition
« Reply #5 on: February 20, 2025, 03:39:35 am »
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