Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Jeremie0 on June 11, 2014, 11:22:56 pm

Title: Copy context references
Post by: Jeremie0 on June 11, 2014, 11:22:56 pm
Hello everyone,

I've looked everywhere but I can't find any info on how to copy context references from one usecase to another in C#

Any idea ?
Title: Re: Copy context references
Post by: qwerty on June 12, 2014, 01:01:29 am
What do you mean by "context references"?

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 12, 2014, 06:16:18 pm
the actors and systems related to the steps of a scenario

like in the screenshot
(http://s14.postimg.org/v83omphsh/Sans_titre.png)
Title: Re: Copy context references
Post by: qwerty on June 12, 2014, 06:35:28 pm
Those are just a summary of elements you find when scanning the connectors collection. So if you create connectors for the copied use case you will automatically find the references.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 12, 2014, 06:51:32 pm
i'm sorry i don't get it...

i've created an element and copied the scenario from the source element to the new one

how do you get "actor" and "CS" for example ?
Title: Re: Copy context references
Post by: qwerty on June 12, 2014, 07:31:06 pm
Have a look at the connectors of the source use case. Just copy these connectors and you will see the Context References as well in the copy.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 12, 2014, 07:59:23 pm
i get an error everytime i try to add a connector

Code: [Select]
foreach (EA.Connector ct in cts)
                                        {
                                            EA.Element tmp = Repository.GetElementByID(ct.ClientID);
                                                ctstarget = ctstarget.AddNew(tmp.Name, ct.Type);
                                        }

and when i do the following, it works but no context references appear in the scenario
Code: [Select]
foreach (EA.Connector ct in cts)
                                        {
                                            EA.Element tmp = Repository.GetElementByID(ct.ClientID);
                                            EA.Connector con = ctstarget.AddNew(tmp.Name, ct.Type);
                                            //con.Update();
                                        }
Title: Re: Copy context references
Post by: qwerty on June 12, 2014, 09:10:51 pm
As long as you comment the Update you will not create a connector.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 13, 2014, 06:00:46 pm
that's precisely where the problem is.

With the update uncommented, everytime i run the addin i get the message
"An extern component encountered an exception"
Title: Re: Copy context references
Post by: qwerty on June 13, 2014, 08:13:19 pm
Looking at it a bit more precisely you lack to supply the SupplierID. Maybe you should have a look in my Scripting book.

N.B. The awful indentation makes reading your code not an easy task.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 13, 2014, 08:49:19 pm
it still creates an exception :(
Title: Re: Copy context references
Post by: Jeremie0 on June 13, 2014, 08:49:58 pm
Code: [Select]
foreach (EA.Connector ct in cts)
                                        {
                                            EA.Element tmp = Repository.GetElementByID(ct.ClientID);
                                            EA.Connector con = ctstarget.AddNew(tmp.Name, ct.Type);
                                             con.SupplierID = ct.SupplierID;
                                                           if (!con.Update())
                                            {
                                                MessageBox.Show(con.GetLastError());
                                            }
                                        }

sorry for the double post
Title: Re: Copy context references
Post by: qwerty on June 13, 2014, 09:07:50 pm
Your code lacks quite some essentials.

Code: [Select]
useCaseOrg = Repository.GetElementByID(orgID);
useCaseCopy = package.Elements.AddNew(useCaseOrg.Name, "UseCase");
useCaseCopy.Update();
for (EA.Connector con in useCaseOrg.connectors) {
  EA.Connector conCopy = useCaseCopy.Connectors.AddNew (con.name, con.type);
  conCopy.supplierID = con.SupplierID;
  conCopy.Update();
}

I wrote this out of my head with no testing. But it should point you in the right direction.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 13, 2014, 10:02:28 pm
thanks but that's what i wrote already...
Title: Re: Copy context references
Post by: qwerty on June 13, 2014, 10:18:27 pm
Too bad I'm not given clairvoyance. So I'm out.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 13, 2014, 11:43:28 pm
the code was missing the following instruction

Code: [Select]
conCopy.ClientID = con.ClientID;
now the error is gone, but still no context references in the new scenario
Title: Re: Copy context references
Post by: qwerty on June 14, 2014, 12:40:09 am
This is because you do not create the source and client id correctly. When you're done you will see that the connectors for the new UC are not the same as that of the original one. It needs some additional if-then-else to create the connectors correctly. Try using your brain.

q.
Title: Re: Copy context references
Post by: qwerty on June 14, 2014, 12:42:43 am
Code: [Select]
my $e = $rep->GetTreeSelectedObject();
my $pk = $rep->GetPackageByID ($e->PackageID);
my $uc2 = $pk->Elements->AddNew ($e->Name . "2" , $e->Type);
$uc2->Update();
for my $con (in $e->Connectors) {
  my $con2 = $uc2->Connectors->AddNew ($con->Name, $con->Type);
  $con2->{SupplierID} = $con->SupplierID;
  $con2->Update();
}
works for connectors drawn from UC to other elements, but not vice versa.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 14, 2014, 01:00:03 am
i basically have the same code, at least i think

Code: [Select]
EA.Diagram diagram = Repository.GetDiagramByID(droppedID);
EA.Package element2 = Repository.GetPackageByID(diagram.PackageID);

EA.Element a = element2.Elements.AddNew(element.Name, element.Type);


a.Name=a.Name.Replace(a.Name, a.Name + "_tmp");
element2.Elements.Refresh();
a.Update();
                                

EA.Collection targets = a.Scenarios;
 foreach (Scenario source in element.Scenarios)
                                {
                                    EA.Scenario target = targets.AddNew(source.Name, source.Type); //on cree un nouveau scenario du meme nom
                                    
                                    var form = new Form1();
                                    var checkedListBox1 = new CheckedListBox();
                                    form.Text = target.Name;

                                    targets.Refresh();
                                    EA.Collection targetsteps = target.Steps;
                                    EA.Collection cts = element.Connectors;
                                    EA.Collection ctstarget = element2.Connectors;
                                    int i = 0;
                                    foreach (ScenarioStep aaa in source.Steps)
                                    {
                                        form.checkedListBox1.Items.Add(aaa.Name);
                                        form.checkedListBox1.SetItemChecked(i, true);
                                        foreach (EA.Connector ct in cts)
                                        {
                                            EA.Connector con = (EA.Connector)ctstarget.AddNew("", ct.Type);
                                            con.SupplierID = ct.SupplierID;
                                            con.ClientID = ct.ClientID;
                                            if(!con.Update())
                                            {
                                                MessageBox.Show(con.GetLastError());
                                            }
                                        }
                                        ctstarget.Refresh();
                                        i++;
                                        element2.Update();
                                    }

anyway thanks for all your help i guess i'll find a workaround
Title: Re: Copy context references
Post by: qwerty on June 14, 2014, 07:14:49 am
You have a similar code but as I pointed out that is not sufficient. Use a debugger to find out why you are not copying the connectors correctly.

q.
Title: Re: Copy context references
Post by: qwerty on June 14, 2014, 07:19:14 am
Furthermore: you are copying the connectors more than once if you have multiple scenarios. Move the connector copy loop outside on the same level as that for the scenarios.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 26, 2014, 05:39:56 pm
ok i figured where the problem was.
The supplierID is the elementID of the new element i created, not a copy of the old one

thanks qwerty for your help

Code: [Select]
foreach (EA.Connector ct in element.Connectors)
                                            {// ON CHERCHE A COPIER LES CONTEXT ITEMS, PAS LES CONNECTEURS
                                                EA.Connector con = (EA.Connector) element2.Connectors.AddNew("", ct.Type);
                                                con.ClientID = ct.ClientID; // <-- element2 est un package pas un element, trouver l'acteur id
                                                //string prov = Repository.GetConnectorByID(ct.ClientID).Name;

                                                con.SupplierID = a.ElementID;
                                                //prov = Repository.GetConnectorByID(ct.SupplierID).Name;

                                                try
                                                {
                                                    con.Update();
                                                }
                                                catch(Exception e)
                                                {
                                                    MessageBox.Show(e.Message);
                                                }
                                                    
                                            }
Title: Re: Copy context references
Post by: Jeremie0 on June 26, 2014, 06:06:57 pm
although it seems like there's a small problem
not all the actors in the context references are copied in the new use case
and i can't figure out why

any idea ?
Title: Re: Copy context references
Post by: qwerty on June 26, 2014, 07:41:45 pm
Compare the Element/Relationships window for the original and the copied use case.

q.
Title: Re: Copy context references
Post by: Jeremie0 on June 26, 2014, 11:03:15 pm
yeah i get it, the assocations in the newly created usecase are those linked to the source usecase in the diagram

so do you have an idea
- to create an association in the new usecase ?
- or just to add the missing ones in the context references ?
Title: Re: Copy context references
Post by: qwerty on June 27, 2014, 04:53:01 am
What I said formerly: some of the connectors are probably drawn vice versa. So you need to create them on the actor side, not on the use case.

q.