static IList <UnitSliceType> createApplicationComponentAbstractUnitSlices(IList <Tuple <string, string, EnvironmentPortType> > bindings_application)
        {
            int port_counter = 0;

            IList <UnitSliceType> slices_list = new List <UnitSliceType> ();

            foreach (Tuple <string, string, EnvironmentPortType> binding in bindings_application)
            {
                string port_name       = binding.Item2 + (port_counter++);
                string slice_unit_name = Constants.envUnitName[binding.Item3];

                UnitSliceType s = new UnitSliceType();
                s.cRef          = port_name;
                s.uRef          = slice_unit_name;
                s.sliceName     = port_name;
                s.slice_replica = 0;
                s.transitive    = true;
                slices_list.Add(s);
            }

            return(slices_list);
        }
        static void createSystemComponentAbstractUnit(IList <object> body_items,
                                                      IDictionary <string, InstanceType> component_refs,
                                                      IDictionary <ProxyComponentAttribute, string> application_attributes,
                                                      IDictionary <ProxyComponentAttribute, string> workflow_attributes,
                                                      IList <Tuple <string, string, EnvironmentPortType> > bindings_application,
                                                      IList <Tuple <string, string, EnvironmentPortType> > bindings_workflow)
        {
            IDictionary <int, SliceList> unit_mapping = new Dictionary <int, SliceList> ();

            foreach (InstanceType c in component_refs.Values)
            {
                string cRef = c.contextual_type.component_ref;
                foreach (UnitMappingType u in c.unit_mapping)
                {
                    string uRef = u.unit_id;
                    foreach (int n in u.node)
                    {
                        AbstractComponentFunctor acf  = backend.DGAC.BackEnd.acfdao.retrieve_libraryPath(cRef);
                        string   qualified_class_name = backend.DGAC.BackEnd.idao.retrieve(acf.Id_abstract, uRef, 0).Class_name;
                        string[] parts_class_name     = qualified_class_name.Split(new char[] { '.' });
                        string   iRef = parts_class_name[parts_class_name.Length - 1];

                        bool      hasNode       = unit_mapping.ContainsKey(n);
                        SliceList units_at_node = hasNode ? unit_mapping[n] : new SliceList();

                        units_at_node.Value.Add(new Tuple <string, string, string>(cRef, uRef, iRef));
                        if (!hasNode)
                        {
                            unit_mapping.Add(n, units_at_node);
                        }
                    }
                }
            }

            IDictionary <SliceList, IList <int> > slice_mapping = new Dictionary <SliceList, IList <int> > ();

            foreach (KeyValuePair <int, SliceList> r in unit_mapping)
            {
                bool        hasSlice  = slice_mapping.ContainsKey(r.Value);
                IList <int> node_list = hasSlice ? slice_mapping [r.Value] : new List <int> ();
                node_list.Add(r.Key);
                if (!hasSlice)
                {
                    slice_mapping.Add(r.Value, node_list);
                }
            }

            // PEER UNITS
            int unit_counter = 0;

            foreach (KeyValuePair <SliceList, IList <int> > r in slice_mapping)
            {
                string uname = "peer" + (unit_counter++);
                string iname = "IPeer" + (unit_counter++);

                UnitType cu = new UnitType();
                cu.facet            = 0;
                cu.iRef             = iname;
                cu.multiple         = true;
                cu.@private         = false;
                cu.replica          = 0;
                cu.uRef             = uname;
                cu.visibleInterface = true;

                InterfaceType ct = new InterfaceType();
                ct.iRef    = iname;
                ct.nArgs   = 0;
                ct.sources = null;                 //?

                IDictionary <Tuple <string, string, string>, int> slice_counter = new Dictionary <Tuple <string, string, string>, int> ();

                IList <UnitSliceType>      uslices_list = new List <UnitSliceType> ();
                IList <InterfaceSliceType> islices_list = new List <InterfaceSliceType> ();

                foreach (Tuple <string, string, string> slice in r.Key.Value)
                {
                    string cRef = component_refs [slice.Item1].instance_ref;

                    bool hasSlice      = slice_counter.ContainsKey(slice);
                    int  slice_replica = hasSlice ? slice_counter [slice] : 0;
                    if (hasSlice)
                    {
                        slice_counter.Remove(slice);
                    }
                    slice_counter.Add(slice, slice_replica + 1);

                    UnitSliceType us = new UnitSliceType();
                    us.cRef          = cRef;
                    us.uRef          = slice.Item2;
                    us.sliceName     = cRef;
                    us.slice_replica = slice_replica;
                    us.transitive    = false;
                    uslices_list.Add(us);

                    InterfaceSliceType fs = new InterfaceSliceType();
                    fs.isRef          = cRef;
                    fs.originRef      = new InterfaceRefType();
                    fs.originRef.cRef = cRef;
                    fs.originRef.iRef = slice.Item3;
                    islices_list.Add(fs);
                }

                cu.slices = new UnitSliceType[uslices_list.Count];
                uslices_list.CopyTo(cu.slices, 0);

                ct.slice = new InterfaceSliceType[islices_list.Count];
                islices_list.CopyTo(ct.slice, 0);

                body_items.Add(ct);

                body_items.Add(cu);
            }

            IList <UnitSliceType>      uslices_root_list = new List <UnitSliceType> ();
            IList <InterfaceSliceType> islices_root_list = new List <InterfaceSliceType> ();

            // ROOT UNIT
            UnitType ru = new UnitType();

            ru.facet            = 0;
            ru.iRef             = "IRoot";
            ru.multiple         = false;
            ru.@private         = false;
            ru.replica          = 0;
            ru.uRef             = "root";
            ru.visibleInterface = true;

            // APPLICATION UNIT SLICE OF THE ROOT UNIT
            UnitSliceType u_slice_app = new UnitSliceType();

            u_slice_app.cRef          = application_attributes[ProxyComponentAttribute.INNER_COMPONENT_NAME];
            u_slice_app.uRef          = application_attributes[ProxyComponentAttribute.UNIT_NAME_ABSTRACT];
            u_slice_app.sliceName     = u_slice_app.cRef;
            u_slice_app.slice_replica = 0;
            u_slice_app.transitive    = false;
            uslices_root_list.Add(u_slice_app);

            // WORKFLOW UNIT SLICE OF THE ROOT UNIT
            UnitSliceType u_slice_wrf = new UnitSliceType();

            u_slice_wrf.cRef          = workflow_attributes[ProxyComponentAttribute.INNER_COMPONENT_NAME];
            u_slice_wrf.uRef          = workflow_attributes[ProxyComponentAttribute.UNIT_NAME_ABSTRACT];
            u_slice_wrf.sliceName     = u_slice_wrf.cRef;
            u_slice_wrf.slice_replica = 0;
            u_slice_wrf.transitive    = false;
            uslices_root_list.Add(u_slice_wrf);

            // ROOT UNIT INTERFACE
            InterfaceType rt = new InterfaceType();

            rt.iRef    = "IRoot";
            rt.nArgs   = 0;
            rt.sources = null;             //??

            // APPLICATION INTERFACE SLICE OF THE ROOT UNIT
            InterfaceSliceType i_slice_app = new InterfaceSliceType();

            i_slice_app.isRef          = u_slice_app.cRef;
            i_slice_app.originRef      = new InterfaceRefType();
            i_slice_app.originRef.cRef = u_slice_app.cRef;
            i_slice_app.originRef.iRef = application_attributes[ProxyComponentAttribute.INTERFACE_NAME_ABSTRACT];
            islices_root_list.Add(i_slice_app);

            // WORKFLOW INTERFACE SLICE OF THE ROOT UNIT
            InterfaceSliceType i_slice_wrf = new InterfaceSliceType();

            i_slice_wrf.isRef          = u_slice_wrf.cRef;
            i_slice_wrf.originRef      = new InterfaceRefType();
            i_slice_wrf.originRef.cRef = u_slice_wrf.cRef;
            i_slice_wrf.originRef.iRef = workflow_attributes[ProxyComponentAttribute.INTERFACE_NAME_ABSTRACT];
            islices_root_list.Add(i_slice_wrf);

            IList <InterfacePortType> iports_root_list = new List <InterfacePortType> ();

            int counter = 0;

            // TRANSITIVE SLICES (environment ports) OF THE APPLICATION SLICE
            foreach (Tuple <string, string, EnvironmentPortType> slice in bindings_application)
            {
                string port_name = slice.Item2 + (counter++);

                // UNIT SLICES
                UnitSliceType us = new UnitSliceType();
                us.cRef          = port_name;
                us.uRef          = Constants.envUnitName[slice.Item3];
                us.sliceName     = port_name;
                us.slice_replica = 0;
                us.transitive    = true;
                uslices_root_list.Add(us);

                // INTERFACE PORTS (TRANSITIVE SLICES)
                InterfacePortType fs = new InterfacePortType();
                fs.name          = port_name;
                fs.slice         = new PortSliceType[1];
                fs.slice[0].pRef = application_attributes[ProxyComponentAttribute.INNER_COMPONENT_NAME];
                fs.slice[0].sRef = port_name;
                iports_root_list.Add(fs);
            }

            counter = 0;

            // TRANSITIVE SLICES (environment ports) OF THE WORKFLOW SLICE
            foreach (Tuple <string, string, EnvironmentPortType> slice in bindings_workflow)
            {
                string port_name = slice.Item2 + (counter++);

                // UNIT SLICES
                UnitSliceType us = new UnitSliceType();
                us.cRef          = port_name;
                us.uRef          = Constants.envUnitName[slice.Item3];
                us.sliceName     = port_name;
                us.slice_replica = 0;
                us.transitive    = true;
                uslices_root_list.Add(us);

                // INTERFACE PORTS (TRANSITIVE SLICES)
                InterfacePortType fs = new InterfacePortType();
                fs.name          = port_name;
                fs.slice         = new PortSliceType[1];
                fs.slice[0].pRef = workflow_attributes[ProxyComponentAttribute.INNER_COMPONENT_NAME];
                fs.slice[0].sRef = port_name;
                iports_root_list.Add(fs);
            }

            ru.slices = new UnitSliceType[uslices_root_list.Count];
            uslices_root_list.CopyTo(ru.slices, 0);

            rt.slice = new InterfaceSliceType[islices_root_list.Count];
            islices_root_list.CopyTo(rt.slice, 0);

            rt.port = new InterfacePortType[iports_root_list.Count];
            iports_root_list.CopyTo(rt.port, 0);
        }
        private void loadInterfaces(AbstractComponentFunctor absC)
        {
            if (unit != null)
            {
                int count = 0;
                // for each interface ...
                foreach (UnitType u in unit)
                {
                    string uRef = u.uRef;
                    string iRef = u.iRef;

                    string uRefSuper = u.super == null || u.super.Length == 0 ? "" : u.super[0].uRef;
                    if (u.super != null)
                    {
                        for (int j = 1; j < u.super.Length; j++)
                        {
                            uRefSuper += "+" + u.super[j].uRef;
                        }
                    }

                    InterfaceType ui    = lookForInterface(iRef);
                    int           nargs = ui.nArgsSpecified ? ui.nArgs : 0;

                    Console.Error.WriteLine("STEP 5.3");

                    Interface i = new Interface();
                    i.Id_abstract  = absC.Id_abstract;
                    i.Id_interface = uRef;
//					i.Unit_replica = unit_replica;
                    i.Facet = u.facet;
                    i.Id_interface_super = uRefSuper;
                    i.Class_name         = xc.header.packagePath + "." + xc.header.name + "." + iRef;
                    i.Class_nargs        = nargs;                                               // TODO
                    i.Assembly_string    = i.Class_name + ", Culture=neutral, Version=0.0.0.0"; // In the current implementation, the name of the dll is the name of the class of the unit.
                    i.Order       = ++count;
                    i.Is_parallel = u.multiple;

                    Console.Error.WriteLine("STEP 5.4");

                    if (ui.parameter != null)
                    {
                        Console.Error.WriteLine("STEP 5.4 - " + ui.parameter);

                        foreach (InterfaceParameterType ipx in ui.parameter)
                        {
                            Console.Error.WriteLine("STEP 5.4 BEGIN 1- " + ipx.parid);
                            InterfaceParameter ip = new InterfaceParameter();
                            Console.Error.WriteLine("STEP 5.4 BEGIN 2- " + ipx.parid);
                            ip.Id_abstract            = i.Id_abstract;
                            ip.Id_interface           = i.Id_interface;
                            ip.ParId                  = ipx.parid;
                            ip.VarId                  = ipx.varid;
                            ip.Id_interface_parameter = ipx.iname;
                            ip.Id_unit_parameter      = ipx.uname;
                            ip.ParOrder               = ipx.order;
                            br.ufc.pargo.hpe.backend.DGAC.BackEnd.ipdao.insert(ip);
                            Console.Error.WriteLine("STEP 5.4 END - " + ipx.parid);
                        }
                    }

                    Console.Error.WriteLine("BEFORE STEP 5.5 " + (ui == null));
                    Console.Error.WriteLine("STEP 5.5 --- " + (ui.sources[ui.sources.Length - 1].file == null));
                    int order = 0;
                    foreach (SourceFileType sft in ui.sources[ui.sources.Length - 1].file)
                    {
                        Console.Error.WriteLine("STEP 5.5.0");
                        SourceCode ss = new SourceCode();
                        ss.Type_owner         = 'i';
                        ss.Id_owner_container = i.Id_abstract;
                        ss.Id_owner           = i.Id_interface;
                        ss.Contents           = sft.contents;
                        ss.File_name          = sft.name;
                        ss.File_type          = "dll";
                        ss.Order = order++;

                        Console.Error.WriteLine("STEP 5.5.1");

                        br.ufc.pargo.hpe.backend.DGAC.BackEnd.scdao.insert(ss);
                        int size = (sft.externalDependency == null ? 0 : sft.externalDependency.Length) +
                                   (ui.externalReferences == null ? 0 : ui.externalReferences.Length);

                        Console.Error.WriteLine("STEP 5.5.2");
                        if (size > 0)
                        {
                            string[] allRefs = new string[size];
                            if (ui.externalReferences != null)
                            {
                                ui.externalReferences.CopyTo(allRefs, 0);
                            }

                            if (sft.externalDependency != null)
                            {
                                sft.externalDependency.CopyTo(allRefs, ui.externalReferences == null ? 0 : ui.externalReferences.Length);
                            }

                            Console.Error.WriteLine("STEP 5.5.3");
                            foreach (string extRef in allRefs)
                            {
                                Console.WriteLine("external reference = " + extRef);
                                SourceCodeReference ssr = new SourceCodeReference();
                                ssr.Type_owner         = ss.Type_owner;
                                ssr.Id_owner_container = ss.Id_owner_container;
                                ssr.Id_owner           = ss.Id_owner;
                                ssr.File_name          = ss.File_name;
                                ssr.Reference          = extRef;
                                if (br.ufc.pargo.hpe.backend.DGAC.BackEnd.scrdao.retrieve(ssr) == null)
                                {
                                    br.ufc.pargo.hpe.backend.DGAC.BackEnd.scrdao.insert(ssr);
                                }
                            }
                            Console.Error.WriteLine("STEP 5.5.4");
                        }
                    }

                    Console.Error.WriteLine("STEP 5.6");
                    br.ufc.pargo.hpe.backend.DGAC.BackEnd.idao.insert(i);
                    if (u.slices != null)
                    {
                        IList <string> mS = new List <string>();
                        IList <String> mP = new List <string>();
                        IDictionary <string, UnitSliceType> m = new Dictionary <string, UnitSliceType>();


                        // 1st PASS: COLLECT ALL MAPPINGS SLICE/EXPOSED SLICES
                        foreach (UnitSliceType uS in u.slices)
                        {
                            string sliceName = uS.sliceName;
                            if (uS.port != null)
                            {
                                foreach (string portName in uS.port)
                                {
                                    mS.Add(sliceName);
                                    mP.Add(portName);
                                }
                            }
                        }

                        // 2nd PASS:
                        foreach (UnitSliceType uS in u.slices)
                        {
                            string sliceName = uS.sliceName;
                            if (mP.Contains(sliceName) && !m.ContainsKey(sliceName))
                            {
                                Console.Error.WriteLine("ADDING " + sliceName + " TO m");
                                m.Add(sliceName, uS);
                            }
                        }

                        Console.Error.WriteLine("STEP 5.7");
                        // 3rd PASS:
                        foreach (UnitSliceType uS in u.slices)
                        {
                            string sname = uS.sliceName;
                            string cRefS = uS.cRef;
                            string uRefS = uS.uRef;

                            InnerComponentType innerC = lookForInnerComponent(cRefS);
                            Console.Error.WriteLine("STEP 5.8");

                            InnerComponent inner = br.ufc.pargo.hpe.backend.DGAC.BackEnd.icdao.retrieve(absC.Id_abstract, cRefS);
                            Console.Error.WriteLine("STEP 5.9 ---" + absC.Id_abstract + "," + cRefS);

                            Interface iii = br.ufc.pargo.hpe.backend.DGAC.BackEnd.idao.retrieveTop(inner.Id_abstract_inner, uRefS);
                            Console.Error.WriteLine("STEP 5.9.5 ---" + (iii == null));

                            Slice s = new Slice();
                            s.Id_abstract        = absC.Id_abstract;
                            s.Id_inner           = innerC.localRef;
                            s.Id_interface_slice = iii == null ? uRefS : iii.Id_interface;
                            s.Id_interface       = uRef;
                            s.Transitive         = mP.Contains(sname);
                            Console.Error.WriteLine("STEP 5.10");

                            string property_name = uS.sliceName;
                            string fstletter     = property_name.Substring(0, 1);
                            property_name = fstletter.ToUpper() + property_name.Substring(1, property_name.Length - 1);

                            Console.Error.WriteLine("STEP 5.11");

                            s.PortName = property_name;

                            if (!s.Transitive && uS.port != null)
                            {
                                Console.Error.WriteLine("STEP 5.12");
                                foreach (string pname in uS.port)
                                {
                                    Console.Error.WriteLine("STEP 5.12.1 -- " + pname + ", " + (m.Count));

                                    UnitSliceType usPort = null;
                                    m.TryGetValue(pname, out usPort);
                                    Console.Error.WriteLine("STEP 5.12.2 -- " + pname + ", " + (usPort == null));

                                    Console.Error.WriteLine("STEP 5.12.5 -- " + usPort.cRef);

                                    InnerComponentType innerCPort = lookForInnerComponent(usPort.cRef);

                                    Console.Error.WriteLine("STEP 5.13");

                                    InnerComponent inner2 = br.ufc.pargo.hpe.backend.DGAC.BackEnd.icdao.retrieve(absC.Id_abstract, usPort.cRef);
                                    Interface      iii2   = br.ufc.pargo.hpe.backend.DGAC.BackEnd.idao.retrieveTop(inner2.Id_abstract_inner, usPort.uRef);

                                    SliceExposed se = new SliceExposed();
                                    se.Id_abstract              = s.Id_abstract;
                                    se.Id_inner                 = innerCPort.localRef;
                                    se.Id_inner_owner           = s.Id_inner;
                                    se.Id_interface_slice_owner = s.Id_interface_slice_top; // mudado de s.Id_interface_slice em 28/06/2011
                                    se.Id_interface_slice       = iii2 == null ? usPort.uRef : iii2.Id_interface;

                                    // achar innerRenaming para cNewName = usPort.cRef e cRef = cRefS (uS.cRef) -- Id_inner_original = cOldName
                                    string id_inner_original = lookForRenamingOld(cRefS, usPort.cRef, usPort.inner_replica);
                                    se.Id_inner_original           = id_inner_original != null ? id_inner_original : usPort.cRef;
                                    se.Id_interface_slice_original = usPort.uRef; // DEVE SER O TOP !!!
                                    if (br.ufc.pargo.hpe.backend.DGAC.BackEnd.sedao.retrieve2(se.Id_inner,
                                                                                              se.Id_interface_slice,
                                                                                              se.Id_abstract,
                                                                                              se.Id_interface_slice_owner,
                                                                                              se.Id_inner_owner) == null)
                                    {
                                        br.ufc.pargo.hpe.backend.DGAC.BackEnd.sedao.insert(se);
                                    }
                                }
                            }
                            if (br.ufc.pargo.hpe.backend.DGAC.BackEnd.sdao.retrieve(s.Id_abstract, s.Id_inner, s.Id_interface_slice, s.Id_interface) == null)
                            {
                                br.ufc.pargo.hpe.backend.DGAC.BackEnd.sdao.insert(s);
                                Console.WriteLine("SLICE REGISTERED " + s.Id_abstract + "/" + s.Id_inner + "/" + s.Id_interface_slice + " --- " + s.Id_interface);
                            }
                            else
                            {
                                Console.WriteLine("SLICE NOT REGISTERED " + s.Id_abstract + "/" + s.Id_inner + "/" + s.Id_interface_slice + " --- " + s.Id_interface);
                            }
                        }
                    }

                    //if (ui.protocol != null)
                    readProtocol(i, ui);
                }
            }
        }