Book a Demo

Author Topic: Connector object through Automation Interface  (Read 10485 times)

Yves.

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Connector object through Automation Interface
« on: August 12, 2010, 01:47:04 am »
I am trying to create a Connector from a JScript file. Here is the very simple piece of code:

var myConnector as EA.Connector;
myConnector = myPackage.Connectors.addNew("connectorName", "Association");
myPackage.Connectors.Refresh();
myConnector.Update();

...where myPackage is a suitably initialized Package object.

When hitting myConnector.Update(); the scripting engine complains that myConnector is null. It looks very straight forward to me but it looks like myConnector is not added to myPackage.Connectors. Any clue?
 :-?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Connector object through Automation Interface
« Reply #1 on: August 12, 2010, 08:12:50 am »
I'm not sure about this, but for a start I would try changing it to myPackage.Element.Connectors.  I'm hoping that will get you going though if it does the original code needs to be sent in so we can correct the error you are currently seeing.
« Last Edit: August 12, 2010, 08:14:02 am by simonm »

Yves.

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Connector object through Automation Interface
« Reply #2 on: August 17, 2010, 01:37:17 am »
Thanks Simon,

First, my appologies, I posted the same request under the Automation Interface forum since I didn't find your reply first. I guess this is where my first request should have been posted.

Regarding my problem, I guess the Element.Connectors Collection is read only. Anyway, here is my code (comments in French unfortunately).
var STRING_DEBUT_REGLE = "%%%";
var STRING_FIN_REGLE = "&&&";
var nbTotalReglesCreees = 0;

function CreeRequirements(monPaquetage)
{
      var nbReglesCreees = 0;
      
      // Récupère les Elements du paquetage sélectionné
      var mesClasses as EA.Collection;
      var mesSousPaquetages as EA.Collection;
      var paquetageRegles as EA.Package;
      Session.Output(" ");
      Session.Output("Le Paquetage présentement modifié est: " + monPaquetage.Name);
      Session.Output(" ");
      mesSousPaquetages = monPaquetage.Packages;
      paquetageRegles = mesSousPaquetages.AddNew("ReglesAffaire", "Package");
      
      paquetageRegles.ApplyUserLock();
      paquetageRegles.Update();
      mesSousPaquetages.Refresh();
      Session.Output("Paquetage de dépôt des Règles: " + paquetageRegles.Name);
      mesObjets = monPaquetage.Elements;
      
      //Itération à travers les Objets du Paquetage
      Session.Output(" ");
      Session.Output( "Début de l'itération à travers les Objets du Paquetage");
      Session.Output(" ");
      for (var i = 0; i < mesObjets.Count; i++ )
            {
                  Session.Output("mesObjets.Count: " + mesObjets.Count);
                  Session.Output("variable i: " + i);
                  var objetCourant as EA.Element;
                  objetCourant = mesObjets.GetAt( i );
                  if (objetCourant.Notes != null && objetCourant.Type == "UseCase")
                  {
                        Session.Output("Objet présentement traité: " + objetCourant.Name);
                        var indexeDebutRegleCourante = objetCourant.Notes.indexOf(STRING_DEBUT_REGLE, 0);
                        var indexeFinRegleCourante = objetCourant.Notes.indexOf(STRING_FIN_REGLE, (indexeDebutRegleCourante + STRING_DEBUT_REGLE.length));
                        Session.Output("Indexe de la règle nouvelle règle: " + indexeFinRegleCourante);
                        Session.Output("Indexe de la règle courante: " + indexeDebutRegleCourante);
                        while(indexeFinRegleCourante != -1)
                        {
                              var regleCourante = objetCourant.Notes.substring(indexeDebutRegleCourante, indexeFinRegleCourante);
                              Session.Output("Règle courante: " + regleCourante);
                              
                              var myRequirement as EA.Requirement;
                              myRequirement = paquetageRegles.Elements.addNew("RegleAffaires", "Requirement");
                              paquetageRegles.Elements.Refresh();
                              myRequirement.Notes = regleCourante;
                              myRequirement.Update();
                              Session.Output("myRequirement.Name: " + myRequirement.Name);
                              
                              var myConnector as EA.Connector;
                              myConnector = paquetageRegles.Connectors.addNew("", "Association");
                              paquetageRegles.Connectors.Refresh();
                              myConnector.Update();
                              Session.Output("myRequirement.ElementID: " + myRequirement.ElementID);
                              Session.Output("objetCourant.ElementID: " + objetCourant.ElementID);
                              Session.Output("myConnector.Type: " + myConnector.Type);
                              myConnector.ClientID = myRequirement.ElementID;
                              myConnector.SupplierID = objetCourant.ElementID;
                              myConnector.Update();
                              Session.Output("myConnector.Type = " + myConnector.Type);
                              
                              Session.Output("myConnector.Type: " + myConnector.Type);
                              Session.Output("myRequirement.ElementID: " + myRequirement.ElementID);
                              Session.Output("objetCourant.ElementID= " + objetCourant.ElementID);
                              Session.Output("myConnector.ClientID: " + myConnector.ClientID);
                              Session.Output("myConnector.SupplierID: " + myConnector.SupplierID);
      
                              indexeDebutRegleCourante = objetCourant.Notes.indexOf(STRING_DEBUT_REGLE, indexeFinRegleCourante + 1 );
                              indexeFinRegleCourante = objetCourant.Notes.indexOf(STRING_FIN_REGLE, indexeFinRegleCourante + 1);
                              Session.Output("Indexe début règle courante: " + indexeDebutRegleCourante);
                              Session.Output("Indexe fin règle courante: " + indexeFinRegleCourante);
                        }                        
                  }      
            }
            
      Session.Output("Nombre de règles créées dans ce Paquetage: " + nbReglesCreees);
      return nbReglesCreees;
}
function CreeRequirementsRecursif(monPaquetage)
{
      var mesPaquetages as EA.Collection;
      mesPaquetages = monPaquetage.Packages;
      if(mesPaquetages.Count != 0)
      {
            for(var i=0; i < mesPaquetages.Count; i++)
            {
                  nbTotalReglesCreees = nbTotalReglesCreees + CreeRequirementsRecursif(mesPaquetages.GetAt(i));
            }
      }
      return CreeRequirements(monPaquetage);
}
//*****************************
Session.Output("Début du programme");
var monPaquetageSource as EA.Package;
monPaquetageSource = Repository.GetTreeSelectedPackage;
Session.Output("Le système va créer un objet Requirement pour toutes les Règles des commentaires du Paquetage **" + monPaquetageSource.Name + " ** et ses sous-paquetages");
CreeRequirementsRecursif(monPaquetageSource);
Session.Output( "Terminé!" );

Yves.

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Connector object through Automation Interface
« Reply #3 on: August 17, 2010, 01:47:32 am »
Just a few explanations regarding the purpose of this code to help you figuring out what's going on.

I am parsing the Notes from the Elements that are contained in the Browser selected Package (monPaquetage) to locate portions of comments that are defining business rules. These portions of Notes are starting by STRING_DEBUT_REGLE and ending by STRING_FIN_REGLE. Once I locate one of them, I am creating a Requirement objetct that I am adding to monPaquetage. So far everything works fine.

Now, I want to connect to the original object (objetCourant) to the new Requirement object. This is when I am creating this Connector that I got the bug.

If you need any further explanation, don't hesitate to let me know.
Thanks again,
Yves.

fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.&lt;Pogo, 1970&gt;
    • View Profile
Re: Connector object through Automation Interface
« Reply #4 on: August 17, 2010, 03:39:21 am »
Yves,

I had a similar problem with a C# add-in and found that I needed to set the SupplierID to that of the element at the other end of the connector before calling Update and Refresh. Code before:
Code: [Select]
Connector conn = sub.Connectors.AddNew(String.Empty, ctype);
bool ok = conn.Update();
sub.Connectors.Refresh();
conn.SupplierID = super.ElementID;
This threw an exception.

Code after:
Code: [Select]
Connector conn = sub.Connectors.AddNew(String.Empty, ctype);
conn.SupplierID = super.ElementID; // Note: Both connector ends must be set before calling update!
bool ok = conn.Update();
sub.Connectors.Refresh();
This works like a charm!

In both code snippets, sub and super are both elements, while ctype is the string defining the connector type.

HTH,
Fred
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.


Yves.

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Connector object through Automation Interface
« Reply #5 on: August 20, 2010, 12:46:19 am »
Thanks Fred,

As a matter of fact, I already tried this before and didn't work But your post made me aware that it may be related to the type of objects I am trying to connect, which are a Requirement and a UseCase object. Following you advice, I retried it to connect two UseCase objetcts and it did work as expected. To be short, the problem was related to the fact that a Requirement object is also an Element object and I was using the RequirementID as the SupplierID instead of its ElementID. Changed it and it does work good now.

Thanks again,
Yves.

fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.&lt;Pogo, 1970&gt;
    • View Profile
Re: Connector object through Automation Interface
« Reply #6 on: August 20, 2010, 01:03:55 am »
Yves,

No worries. You do bring up a good point, though - what does the Automation Interface do if you attempt to connect two elements with a connector type not supported by UML? Will EA allow this or will an exception be thrown? That a question for the Sparxians, obviously...

Best Regards,
Fred
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Connector object through Automation Interface
« Reply #7 on: August 23, 2010, 03:35:16 pm »
Fred,

That's pretty obvious, EA will allow you to create any type of connector between any type of elements, regardless of the "strict UML" setting.
(wich is a GOOD thing in my mind)

Geert

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Connector object through Automation Interface
« Reply #8 on: August 23, 2010, 04:57:28 pm »
Just from memory - I'm pretty sure the Strict UML Syntax option did have an effect of the types of Connectors you could create via automation, much like it does in Enterprise Architect.  I think trying to create an illegal relationship when Strict UML Syntax was enabled would raise an Exception.  Haven't tested this in a while, but I assume it still applies...

beginner

  • Guest
Re: Connector object through Automation Interface
« Reply #9 on: August 24, 2010, 05:54:44 pm »
Right and wrong. EA does not raise an exception. It silently ignores creation of the connector. That is Update returns false. You need to poll connector.GetLatestError to see the cause.

b.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Connector object through Automation Interface
« Reply #10 on: August 24, 2010, 06:14:34 pm »
Nice  :-X

Geert