Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: simonspaten on July 03, 2013, 07:13:56 pm

Title: generate requirements diagram automatically
Post by: simonspaten on July 03, 2013, 07:13:56 pm
Hi folks, I am new in this forum and I´m also a newbie in using the EA functions, so I wanna know if it is possible to generate a requirement diagramm automatically from imported requirements (via MDG-Link for DOORS).

Beforehand, thanks a lott for replies !
Title: Re: generate requirements diagram automatically
Post by: Helmut Ortmann on July 03, 2013, 08:35:00 pm
Hello,

if the DOORS MDG don't creates diagrams as part of the import you may write an Addin to visualize the requirements in a diagram for each package.

Kind regards,

Helmut

Title: Re: generate requirements diagram automatically
Post by: qwerty on July 03, 2013, 10:39:12 pm
FWIW: a pure requirements diagram does not mean much to readers. At that level they prefer a tabular output. However, once you have connected requirements to use cases (or whatever) these diagrams make a lot of sense. But those can't be created automatically.

q.
Title: Re: generate requirements diagram automatically
Post by: sdh on July 05, 2013, 01:59:21 pm
I am also looking for a way to create the requirement diagram after import from doors. Is there a way to connect the requirement elements using the Aggregate relationships to form a hierarchy. The imported requirements are already displayed in hierarchical manner but a matching diagram is missing.
Title: Re: generate requirements diagram automatically
Post by: Helmut Ortmann on July 05, 2013, 02:53:41 pm
Hello,

you may write an Addin to create a requirement diagram for each package containing requirements.

Best regards,

Helmut
Title: Re: generate requirements diagram automatically
Post by: Dermot on July 05, 2013, 04:02:12 pm
Like Q says, where the relationships are non existant at this stage best to view via as table: try selecting the package - right-click | Package Browser
Then use the Show Hierarchy icon to see the nesting. This will match more clearly the DOORS formating.
Title: Re: generate requirements diagram automatically
Post by: sdh on July 05, 2013, 05:25:54 pm
Quote
Hello,

you may write an Addin to create a requirement diagram for each package containing requirements.

Best regards,

Helmut

I am new to EA and not expert in addins. Is there a community created addin for the same? I am sure there must be others who would have faced the same challenge.
Title: Re: generate requirements diagram automatically
Post by: Helmut Ortmann on July 05, 2013, 05:33:15 pm
Hello,

I see the following possibilities to get quickly started:

There is also a bunch of examples in:
- Online Help
- Scripting example Code (MDG EAScriptLib) and LocalScript in View Scripting

Helmut
Title: Re: generate requirements diagram automatically
Post by: sdh on July 11, 2013, 03:25:29 pm
Thanks Helmut. I have started the VB script but I am facing some issues.
To start with, I am able to recurse through the requirement package and list all the requirements.
As a second step, I wanted to add the requirements to Requirement Diagram and create the "Aggregration" relation from the current element to elements ParentID.

I used the following code to add the current element to diagram and in the process, the requirements also became flat in the package. Any idea what is going wrong. Also how do I create the connector/relation between current element and the parent.

Code: [Select]
' Add the element to the diagram
            dim diagramObjects as EA.Collection
            set diagramObjects = currentDiagram.DiagramObjects
      
            dim currentDiagramObject as EA.DiagramObject
            set currentDiagramObject = diagramObjects.AddNew("","")
            currentDiagramObject.ElementID( currentElement.ElementID )
            CurrentDiagramObject.Update()
            CurrentDiagramObject.Refresh()
Title: Re: generate requirements diagram automatically
Post by: Helmut Ortmann on July 11, 2013, 03:56:33 pm
Hi,

some code to see how to add a connection and to estimate the position:
Code: [Select]
                       Repository.SaveDiagram(dia.DiagramID);
                        
                        EA.Element elNote = null;
                        try
                        {
                            elNote = (EA.Element)pkg.Elements.AddNew("", "Note");
                            elNote.Update();
                            pkg.Update();
                        }
                        catch { break; }

                        // add element to diagram
                        // "l=200;r=400;t=200;b=600;"

                        // get the position of the Element
                        EA.DiagramObject diaObj = null;
                        foreach (EA.DiagramObject dObj in dia.DiagramObjects) {
                            if (dObj.ElementID == el.ElementID)
                            {
                                diaObj = dObj;
                                break;
                            }
                        }
                          int left = diaObj.left + 2*(diaObj.right - diaObj.left);
                        int right = diaObj.right + 2 * (diaObj.right - diaObj.left);

                        string position = "l=" + left.ToString() +";r=" + right.ToString() + ";t=" + diaObj.top.ToString() + ";b=" + diaObj.bottom.ToString() + ";";
                        EA.DiagramObject diaObject = (EA.DiagramObject)dia.DiagramObjects.AddNew(position, "");
                        dia.Update();
                        diaObject.ElementID = elNote.ElementID;
                        diaObject.Update();
                        pkg.Elements.Refresh();
                        // make a connector
                        EA.Connector con = (EA.Connector)el.Connectors.AddNew("test", "NoteLink");
                        con.SupplierID = elNote.ElementID;
                        con.Update();
                        el.Connectors.Refresh();
                        Util.setElementHasAttchaedLink(Repository, el, elNote);
                        Repository.ReloadDiagram(dia.DiagramID);

                    }

I don't know why your requirements structure became flat. You have to ensure that the parent_id of the requirement remains unchanged. For me the code looks good. You are just creating a diagramobject and assign it to the requirement element. I would do it almost the same way.

Helmut
Title: Re: generate requirements diagram automatically
Post by: sdh on July 11, 2013, 04:17:54 pm
Thanks Helmut. One Question, when I use currentDiagramObject.ElementID( currentElement.ElementID ) command, does it add a simple link to the original object.
What I saw was that the objects were stacked one on the top of other. When I tried to drag and spearate them, the hierarchy became flat as I graged the items around. Even if I use layout tools, the diagram objests do not layout in box pattern that I selected.
If I drag and drop individual requirements on the diagram, I do not see flattening if I move the objects around in the diagram.
So there is a difference in the way the diagram is getting created by script.
Title: Re: generate requirements diagram automatically
Post by: Geert Bellekens on July 11, 2013, 04:23:41 pm
Ha, I see what is happening.

For some types of objects (I'm not sure which exactly) EA will nest/unnest the elements if you drag them onto each other, or if you drag them out of their parent.

The EA screens and gui elements work the same way.
I'm pretty sure that doesn't happen when putting elements on a diagram programatically.
You'll just have to be careful when moving elements around on the diagram manually.

There is an operation somewhere to auto-layout your diagram using the API. That might be an easy solution.

Geert
Title: Re: generate requirements diagram automatically
Post by: Helmut Ortmann on July 11, 2013, 04:36:08 pm
Hello,

I think Geert is right. I have the same experiences. You may try:

- Create the Diagrammobject in the way you want to see them
  (position them)
- After that set the parant-Id as they were before

Helmut
Title: Re: generate requirements diagram automatically
Post by: sdh on July 12, 2013, 01:36:43 pm
I am able to add all the requirements to the diagram now, they come stacked one on top of other. For now I am able to separate then using the auto layout. I will address taht later. I then tried to connect the objects in diagram and I am getting the error: Object doesn't support this property or method. Any idea what I am doing wrong.
Code: [Select]
sub addConnectors(theDiagram)
      ' Cast theDiagram to EA.Diagram so we get intellisense
      dim currentDiagram as EA.Diagram
      set currentDiagram = theDiagram
      
      ' Iterate through all objects in this diagram
      dim currentDiagramObject as EA.DiagramObject
      for each currentDiagramObject in currentDiagram.DiagramObjects
            ' Get the element that this Diagram Object represents
            dim currentElement as EA.Element
            set currentElement = Repository.GetElementByID( currentDiagramObject.ElementID )
                  
            if not currentElement is nothing then
                  ' Add the element details to the list
                  Session.Output("        -> Element: " & currentElement.Name & _
                        " (" & currentElement.Type & _
                        ", ID=" & currentElement.ElementID & _
                        ", Parent=" & currentElement.ParentID & _
                        ", Connector=" & currentElement.Connectors.Count & ")" )
                  dim connector as EA.Connector
                  if (currentElement.ParentID <>0) then
                        connector = currentElement.Connectors.AddNew("Relation","Aggregation")
                        'currentElement
                        connector.SupplierID = currentElement.ParentID
                        connector.Update
                        currentElement.Connectors.Refresh
                  end if
            end if
      next
end sub
     
Title: Re: generate requirements diagram automatically
Post by: Geert Bellekens on July 12, 2013, 03:30:48 pm
Which line is giving you the error?

Geert
Title: Re: generate requirements diagram automatically
Post by: sdh on July 12, 2013, 04:00:55 pm
sorry about that, it is:
connector = currentElement.Connectors.AddNew("Relation","Aggregation")
Title: Re: generate requirements diagram automatically
Post by: KP on July 12, 2013, 04:19:13 pm
Try
[highlight]set[/highlight] connector = currentElement.Connectors.AddNew("Relation","Aggregation")
Title: Re: generate requirements diagram automatically
Post by: sdh on July 12, 2013, 04:28:07 pm
That did the trick. Thanks KP.
Title: Re: generate requirements diagram automatically
Post by: Geert Bellekens on July 12, 2013, 04:29:44 pm
Ah, right the lovely VB syntax, if its an object you need to SET it, if its a primitive you only need to "=" it.
Almost as funny as the question "when to use braces when calling a method" ::)

Geert
Title: Re: generate requirements diagram automatically
Post by: sdh on July 12, 2013, 05:36:39 pm
Thanks guys for your help. It seems to be working now, but one issue. I first dump all the elements to the diagram and then add relations for all the objects on the diagram. Looks like the addconnectors in first run does not see any objects that are added by the first function. When I run the script second time, it sees the elements and creates the relations. What should I do in between to make sure the changes from step 1 are saved and available for second step.

I do have the following code to add elements to the diagram:
Code: [Select]
' Add the element to the diagram
            set currentDiagramObject = currentDiagram.DiagramObjects.AddNew("","")
            currentDiagramObject.ElementID( currentElement.ElementID )
            CurrentDiagramObject.Update()
Title: Re: generate requirements diagram automatically
Post by: Geert Bellekens on July 12, 2013, 06:13:47 pm
Try reloading the diagram.

Geert
Title: Re: generate requirements diagram automatically
Post by: sdh on July 15, 2013, 05:52:38 pm
How do I reload a  diagram. Could not find the command.

One more issue. Before adding the requirement to diagram, I check if it already exists. My code does not look optimised and if there are large number of objects on the diagram, for every requirement, it loops through all the objects on diagram. This is very inefficient. Is there a better way to check if an element/requirement already exists on the diagram.

Code: [Select]
sub addToDiagram(theElement, theDiagram)
      ' Cast theDiagram to EA.Diagram so we get intellisense
      dim currentDiagram as EA.Diagram
      set currentDiagram = theDiagram
      ' Cast theElement to EA.Element so we get intellisense
      dim currentElement as EA.Element
      set currentElement = theElement
      
      dim elementFound
      elementFound = false
      
      ' Iterate through all objects in this diagram
      dim currentDiagramObject as EA.DiagramObject
      for each currentDiagramObject in currentDiagram.DiagramObjects
            if (currentDiagramObject.ElementID = currentElement.ElementID) then
                  elementFound = true
            end if
      next      
      
      if (elementFound) then
            Session.Output "--Element already in Diagram"
      else
            Session.Output "++Add element to Diagram"
            ' Add the element to the diagram
            set currentDiagramObject = currentDiagram.DiagramObjects.AddNew("","")
            currentDiagramObject.ElementID( currentElement.ElementID )
            CurrentDiagramObject.Update()
      end if
            
end sub
Title: Re: generate requirements diagram automatically
Post by: Geert Bellekens on July 15, 2013, 06:28:09 pm
Quote
How do I reload a  diagram. Could not find the command.
There IS a search function is the help file you know. Try search for "reloaddiagram" :-?

As for the check you need to do, if you want to optimize it you'll have to an SQL query in Repository.SQLQuery().

Another think you might want to try it to copy all diagramObjects into a regular list/array,... and iterate that instead the of the EA.Collection Diagram.DiagramObjects

Some of the EA.Collection are not "in memory" objects, and they will query the database for each "next" in an iteration.

Geert