Book a Demo

Author Topic: C# client & supplier enterprise architect  (Read 4374 times)

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
C# client & supplier enterprise architect
« on: June 30, 2012, 02:08:10 am »
Hi! My goal is to create an object Client for each client of every connection in the diagram and insert the supplier in a list inside the Client object. To have as a result: client -ID- : [list of suppliers of this client].

The following code do what i want:
Code: [Select]
public Metrics(EA.Repository repository)
        {
                      
            this.modelRepository = repository;
            

            using (StreamWriter writer = new StreamWriter("C:\\Users\\Martina\\Desktop\\metrics.txt"))
            {
                Console.SetOut(writer);

                List<String> supplierList = new List<String>();
                List<int> clientList = new List<int>();
                var clientLookup = new Dictionary<int,Client>();


                foreach (EA.Package package in modelRepository.Models)
                {
                    foreach (EA.Package inpack in package.Packages)
                    {
                        foreach (EA.Element element in inpack.Elements)
                        {
                            foreach (EA.Connector link in element.Connectors)
                            {
                                int supplier = link.SupplierID; // target
                                int client = link.ClientID; // source

                                EA.Element classNome = modelRepository.GetElementByID(supplier);

                              

                                if (!clientLookup.ContainsKey(client))
                                {
                                    clientLookup.Add(client, new Client(client));
                                    
                                    Client node = clientLookup[client];
                                    node.addSupplier(classNome.Name);
                                }
                                else
                                {
                                    Client node = clientLookup[client];
                                    node.addSupplier(classNome.Name);
                                }

                                /*
                                ArrayList connectionList = new ArrayList();
                                connectionList.Add(link.Type);

                              
                                foreach (String type in connectionList)
                                { Console.WriteLine(type); }
                                Console.WriteLine("");*/
                            }                    
                        }
                    }
                }
                foreach (KeyValuePair<int, Client> item in clientLookup)
                {
                    int clientID = item.Key;
                    Client clientNode = item.Value;

                    supplierList = clientNode.getSupplierList();

                    Console.WriteLine("the client is:" + clientID);
                    Console.WriteLine("the suppliers are: ");
                    foreach (String number in supplierList)
                    { Console.WriteLine(number); }
                    Console.WriteLine("");
                }


the code of .getSupplierList is:
Code: [Select]
public List<String> getSupplierList()
        {
            return supplierListObj;
        }

I made a List of String just to see what nodes it will print out in the txt., so i can understand by the name.

but as a result i get double supplier. In fact i noticed that every connection is considered in both ways source-destination and destination-source. How can i fix it?

The .txt that i get as a result is the following:

the client is:14
the suppliers are:
Interface1
B
B
Interface1

the client is:15
the suppliers are:
C
C

the client is:16
the suppliers are:
PB
Co1
Co1

the client is:21
the suppliers are:
Co2
Co2
« Last Edit: July 02, 2012, 10:23:25 pm by defi »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: C# client & supplier enterprise architect
« Reply #1 on: June 30, 2012, 08:13:57 am »
Not sure, but it might be that you simply evaluate both client and supplier in your nested loop. In which case you get the link to the opposite. So you have it doubled.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# client & supplier enterprise architect
« Reply #2 on: July 02, 2012, 03:38:44 pm »
Yep,

I't quite logical. You will see each connection twice since it appears in both the client.Connectors as the suppliers.Connectors collection.

But you haven't posted the code of getSupplierList()
That might be the one containing the error.

Geert

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: C# client & supplier enterprise architect
« Reply #3 on: July 02, 2012, 10:24:57 pm »
I modified the question and inserted the code of the procedure .getSupplierList. But it is a simple return of the list where I add every time the supplier.

Can you suggest me how can i fix it?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: C# client & supplier enterprise architect
« Reply #4 on: July 02, 2012, 11:18:10 pm »
Are you sure this is wrong?
It is perfectly possible that for some reason you have double relationships. If everything would be doubled then you would not be able to have :
Quote
the client is:16
the suppliers are:
[highlight]PB[/highlight]
Co1
Co1

Maybe try to add the id of the connector iso the name of the supplier, then see if you still have duplicates

Geert

Geert

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: C# client & supplier enterprise architect
« Reply #5 on: July 02, 2012, 11:48:34 pm »
I noticed that the connection were analyzed twice, so i find a solution: I made a dictionary of the connectors so every time i search in this dictionary if the connector with the specific ID has been evaluated yet, if is not, then i proceed, and if it is then i pass to another connector. And so i get the result that i wanted:
the client is:14
the suppliers are:
Interface1
B

the client is:15
the suppliers are:
C

the client is:16
the suppliers are:
PB
Co1

the client is:21
the suppliers are:
Co2

the client is:22
the suppliers are:
Co2


What do you think about this solution, is it ok? The code is:
Code: [Select]
List<String> supplierList = new List<String>(); // nomi dei nodi supplier di ogni connessione di un dato client
                List<int> clientList = new List<int>();  // lista dei clientID di ogni client presente nel diagramma
                var clientLookup = new Dictionary<int,Client>();
               [highlight] var connectionLookup = new Dictionary<int,String>();
[/highlight]

                foreach (EA.Package package in modelRepository.Models)
                {
                    foreach (EA.Package inpack in package.Packages)
                    {
                        foreach (EA.Element element in inpack.Elements)
                        {
                            foreach (EA.Connector link in element.Connectors)
                            {

                                int connection = link.ConnectorID;
                                int supplier = link.SupplierID; // target
                                int client = link.ClientID; // source

                                EA.Element classNome = modelRepository.GetElementByID(supplier);
                                String connectionName = link.Name;
                                
                              [highlight]  if (!connectionLookup.ContainsKey(connection))
                                    {
                                    connectionLookup.Add(connection,link.Name);[/highlight]
                                

                                if (!clientLookup.ContainsKey(client))
                                {
                                    clientLookup.Add(client, new Client(client));
                                    
                                    Client node = clientLookup[client];
                                    node.addSupplier(classNome.Name);
                                }
                                else
                                {
                                    Client node = clientLookup[client];
                                    node.addSupplier(classNome.Name);
                                }
« Last Edit: July 02, 2012, 11:51:22 pm by defi »