Book a Demo

Author Topic: [Scripting] Read Object positions on a model  (Read 11111 times)

Jef Martens

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
[Scripting] Read Object positions on a model
« on: October 03, 2013, 06:32:10 pm »
Hi all, after some hard modeling time we've currently built a database with >70 BPMN models.

Now, we'd like to read the model, extract for a certain object type the vertical position and arrange the sequence in the package tree accordingly.

I've got @qwerty's book for starters and it really helps defining what to address and which elements can be read with what attributes.

Using the following function, you can:
assess a diagram,
get the objects on a diagram,
print some output to the system output screen about the diagram objects.
Code: [Select]
function listDiagramElements(diagram){
      diagramElements = diagram.DiagramObjects;
      for (i=0; i < diagramElements.Count; i++){
            diagramElement = diagramElements.GetAt (i);
            elementID = diagramElement.ElementID;
            elementName = Repository.GetElementByID (elementID).Name;
            elementType = Repository.GetElementByID (elementID).Type;
            elementYPos = diagramElement.Top;
            elementZOrder = diagramElement.Sequence;
            print("Element " + (i + 1) + " of type " + elementType + " is : " + elementName + " and Y Position is : " + elementYPos + " and Z order is : " + elementZOrder );
      }
}

My problem is that the elementYPos returns "undefined" which would mean NULL in the SQL database. How can this be? The objects should have a position though? For each EaDiagramObject coordinate (Top, Bottom, Left, Right) the objects have "undefined" values  :'(

Any hint on what's happening here?
« Last Edit: October 03, 2013, 08:48:42 pm by jef.martens »
BPM / Reference model consultant (SCOR / APQC / BIAN / TOGAF)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: [Scripting] Read Object positions on a model
« Reply #1 on: October 03, 2013, 09:07:31 pm »
Jef,

As far as I can see the column t_diagramObjects.RectTop is always filled in, so you should always have a (negative) integer in there.

Have you checked in debug mode what is stored in the diagramElement.Top?

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #2 on: October 04, 2013, 06:21:25 am »
You could run the query in the SQL builder to find out the values. I could imaging that EA has some intermediate unsaved diagram state where that could happen. But honestly I haven't seen it in reality.

q.

[edit] Just some kind of confirmation. When you create a new diagram object EA creates an entry in t_diagramobjects but it does not set correct coordinate values (seem to be random). So I still can imagine that they are sometimes NULL. Maybe especially for certain non-EAP DBs.
« Last Edit: October 04, 2013, 06:25:28 am by qwerty »

Jef Martens

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #3 on: October 07, 2013, 07:39:06 pm »
Thanks Geert and Qwerty for your replies;
@Geert: the debugging screen did not yield any information other than basic "Debugging started" and "Stopped" information - what do I need to change to get more information in the debug screen?

@Qwerty: OK I'll write my own SQL statements then and try to read the positions like that. Any pointer on to look for properties of the object in relation to the model, or on object properties?
BPM / Reference model consultant (SCOR / APQC / BIAN / TOGAF)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: [Scripting] Read Object positions on a model
« Reply #4 on: October 07, 2013, 07:54:08 pm »
Quote
Thanks Geert and Qwerty for your replies;
@Geert: the debugging screen did not yield any information other than basic "Debugging started" and "Stopped" information - what do I need to change to get more information in the debug screen?
I don't know, I've never worked with the scripting part before, but when I debug in Visual Studio, or SharpDevelop I can see the values of the attributes of all my variables.

Geert

Jef Martens

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #5 on: October 07, 2013, 07:58:29 pm »
Quote
Quote
---
I don't know, I've never worked with the scripting part before, but when I debug in Visual Studio, or SharpDevelop I can see the values of the attributes of all my variables.

Geert

That is exactly where I was searching for as I also have this in other development environments; so far I've just created many expressive printlines to do the same.  :o
« Last Edit: October 07, 2013, 07:59:30 pm by jef.martens »
BPM / Reference model consultant (SCOR / APQC / BIAN / TOGAF)

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #6 on: October 07, 2013, 08:36:26 pm »
Jef,
the answer to your questions is simply to save your diagrams before running above script. That will make the coordinates the correct values. Use Repository.SaveDiagram (diagram-id) for that purpose.

q.

[edit] Of course, if you run in a multi-user environment you need locking to be save that others don't create such invalid data while editing diagrams.
« Last Edit: October 07, 2013, 08:45:33 pm by qwerty »

Jef Martens

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #7 on: October 07, 2013, 08:47:42 pm »
Quote
Jef,
the answer to your questions is simply to save your diagrams before running above script. That will make the coordinates the correct values. Use Repository.SaveDiagram (diagram-id) for that purpose.

q.

Hmm, what happens when the diagram is not even opened? The Object state should still be 'saved' right?
Or did I 'open' the model the moment that I accessed the Diagram Object and should it be closed the moment I've retrieved all objects in the diagram? (just like many SQL related topics in PHP and Java, to save memory?)

I'll give it a shot and will post the results. Maybe it has something to do with the fact that it is a BPMN model...
BPM / Reference model consultant (SCOR / APQC / BIAN / TOGAF)

Jef Martens

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #8 on: October 07, 2013, 11:42:52 pm »
OK, got it working:
using the raw sql lookup solution:
Code: [Select]
// This function prints the objects on a diagram
// input: a diagram object
// output: printed statements: elementType, elementName and the Y position of the element
function listDiagramElements(diagram){
      diagramElements = diagram.DiagramObjects;
      // none = Repository.SaveDiagram (diagram.elementID);
      diagramElements = sortPos(diagramElements);
      // diagramElements = sortPos(diagramElements);
      for (i=0; i < diagramElements.Count; i++){
            diagramElement = diagramElements.GetAt (i);
            elementID = diagramElement.ElementID;
            elementName = Repository.GetElementByID (elementID).Name;
            elementType = Repository.GetElementByID (elementID).Type;
            // Retrieve the object element Y posistion manually:
            sql = "SELECT RectTop FROM DATABASE.dbo.t_diagramobjects WHERE Diagram_ID = '" + diagram.DiagramID + "' AND Object_ID = '" + elementID + "'";
            intermediate = Repository.SQLQuery (sql);
            elementYPos = readXML(intermediate, "RectTop");
            print(
                  "Element " + (i + 1) +
                  " of type " + elementType +
                  " is : " + elementName +
                  " and Y position is : " + elementYPos
                  );
      }
}

Using the helper function to read XML from the database query results:

Code: [Select]
// This function returns the value for an XML input
// input: a XML string, the key to look for "name" in the tag. i.e. <name>
// output: the value of the XML entry
function readXML(xml, tag) {
      // Define the resulting XML Value
      xmlValue = null;
      // Setup the regular expression
      start = "<" + tag;
      end = "</" + tag;
      re = new RegExp(start);
      re2 = new RegExp(end);
      // Execute the regular expression
      m = re.exec(xml); // Look for the starting position
      n = re2.exec(xml); // Look for the ending position
      
      startpos = m.index + tag.length + 2; // take the position where looked (i.e. |<tag> and this will become <tag>|
      endpos = n.index; //this position is right where it should be, we do not want any XML tags to be outputted.
      // Read the value of the xml stream, looking for the tag
      xmlValue = xml.substring(startpos,endpos);
      // return the value for the XML tag
      return xmlValue;
}

The problem for me inbetween was that the SQL execute statement cannot be used for SELECT statements; the returned values is therefore also not a single value, but an XML answer.

With my little helper function, the tag can be sent and then used to search for the right content.
 ;)
« Last Edit: October 07, 2013, 11:45:51 pm by jef.martens »
BPM / Reference model consultant (SCOR / APQC / BIAN / TOGAF)

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #9 on: October 08, 2013, 07:12:22 am »
Quote
Quote
Jef,
the answer to your questions is simply to save your diagrams before running above script. That will make the coordinates the correct values. Use Repository.SaveDiagram (diagram-id) for that purpose.

q.

Hmm, what happens when the diagram is not even opened? The Object state should still be 'saved' right?
Or did I 'open' the model the moment that I accessed the Diagram Object and should it be closed the moment I've retrieved all objects in the diagram? (just like many SQL related topics in PHP and Java, to save memory?)

I'll give it a shot and will post the results. Maybe it has something to do with the fact that it is a BPMN model...
What I saw is that adding new elements to a diagram results in bad position parameters. I have not seen NULL yet, but could imagine that other DBs would produce that (or strange circumstances). Just opening a diagram does not harm. Maybe moving invalidates positions (not unlikely, but I have not tested that). Generally, before fiddling around with diagram objects (either via API or in the table) you should ensure all diagrams are saved with the mentioned SaveDiagram.

q.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: [Scripting] Read Object positions on a model
« Reply #10 on: October 08, 2013, 07:14:13 am »
Quote
The problem for me inbetween was that the SQL execute statement cannot be used for SELECT statements; the returned values is therefore also not a single value, but an XML answer.
To execute a query use Repository.SQLQuery (sql) instead of Execute. The latter is only for non-select statements.

q.