Esempio n. 1
0
        public void Detect()
        {
            if (this.AllCommunities.Count > 0)
            {
                return;
            }

            if (this.graph.Nodes.Count() > 0)
            {
                Dictionary <int, int> partition = Community.BestPartition(this.graph);
                foreach (var kvp in partition)
                {
                    int id = kvp.Value + 1;

                    List <int> nodeset;
                    if (!this.AllCommunities.TryGetValue(id, out nodeset))
                    {
                        nodeset = new List <int>();
                        this.AllCommunities.Add(id, nodeset);
                    }

                    nodeset.Add(kvp.Key);
                }

                this.modularity = Community.Modularity(this.graph, partition);
            }
            else
            {
                this.modularity = 0;
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Graph g = new Graph();
            //test network
            int edgecounter = 0;

            using (StreamReader sr = new StreamReader("testnetwork.csv"))
            {
                string line = null;
                while ((line = sr.ReadLine()) != null)
                {
                    line = line.Trim();
                    if (line == "")
                    {
                        continue;
                    }
                    string[] d     = line.Split(',');
                    int      agent = Int32.Parse(d[0]);
                    for (int i = 1; i < d.Length; i++)
                    {
                        int friends = Int32.Parse(d[i]);
                        g.AddEdge(agent, friends, 1);
                        edgecounter++;
                    }
                }
            }
            Console.WriteLine("{0} edges added", edgecounter);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Restart();
            Dictionary <int, int> partition = Community.BestPartition(g);

            Console.WriteLine("BestPartition: {0}", stopwatch.Elapsed);
            var communities = new Dictionary <int, List <int> >();

            foreach (var kvp in partition)
            {
                List <int> nodeset;
                if (!communities.TryGetValue(kvp.Value, out nodeset))
                {
                    nodeset = communities[kvp.Value] = new List <int>();
                }
                nodeset.Add(kvp.Key);
            }
            Console.WriteLine("{0} communities found", communities.Count);
            int counter = 0;

            foreach (var kvp in communities)
            {
                Console.WriteLine("community {0}: {1} people", counter, kvp.Value.Count);
                counter++;
            }
            Console.ReadLine();
        }
Esempio n. 3
0
        private static void GenerateKBObjectEdgesTxt(string name, string fileName)
        {
            IKBService kbserv = UIServices.KB;
            KBModel    model  = kbserv.CurrentModel;
            Graph      g      = new Graph();

            Module rootModule = kbserv.CurrentModel.GetDesignModel().RootModule;

            IOutputService   output  = CommonServices.Output;
            StringCollection aristas = new StringCollection();

            output.AddLine("Generating " + name);

            Dictionary <string, Tuple <int, string> > dictionary = new Dictionary <string, Tuple <int, string> >();
            Dictionary <int, int> initialpartition = new Dictionary <int, int>();

            // Dictionary<string, Tuple<int,string>> dictionary = new Dictionary<string, Tuple<int,string>>();

            Dictionary <string, int>    NameToId     = new Dictionary <string, int>();
            Dictionary <string, string> NameToModule = new Dictionary <string, string>();
            Dictionary <int, string>    IdToName     = new Dictionary <int, string>();
            Dictionary <int, string>    IdToModule   = new Dictionary <int, string>();
            Dictionary <int, EntityKey> IdToKey      = new Dictionary <int, EntityKey>();

            int objId = 0;

            foreach (KBObject obj in model.Objects.GetAll())
            {
                if (Functions.hasModule(obj) || (obj is Module))
                {
                    string objName    = NombreNodo(obj);
                    string modulename = ModulesHelper.ObjectModuleName(obj);

                    try
                    {
                        objId += 1;
                        NameToId.Add(objName, objId);
                        NameToModule.Add(objName, modulename);
                        IdToName.Add(objId, objName);
                        IdToModule.Add(objId, modulename);
                        IdToKey.Add(objId, obj.Key);
                    }
                    catch (Exception e)
                    {// output.AddWarningLine("Can't add : " + objName + " Exception: " + e.Message + " " + e.InnerException);
                    };
                }
            }


            foreach (KBObject obj in model.Objects.GetAll())
            {
                string objName = "";

                if (Functions.hasModule(obj) || (obj is Module)) //((Functions.isRunable(obj) && ObjectsHelper.isGenerated(obj)) || (obj is Table))
                {
                    /*
                     * objName = NombreNodo(obj);
                     * string modulename = ModulesHelper.ObjectModuleName(obj);
                     *
                     * try
                     * {
                     *  objId += 1;
                     *  NameToId.Add(objName, objId);
                     *  NameToModule.Add(objName, modulename);
                     *  IdToName.Add(objId, objName);
                     *  IdToModule.Add(objId, modulename);
                     *  IdToKey.Add(objId, obj.Key);
                     * }
                     * catch (Exception e) { //output.AddWarningLine("Can't add : " + objName);
                     * };
                     */

                    //Tomo las referencias que no sean tablas.
                    foreach (EntityReference r in obj.GetReferencesTo())
                    {
                        KBObject objRef = KBObject.Get(obj.Model, r.From);
                        if ((objRef != null) && (Functions.isRunable(objRef)) || (objRef is Table))

                        {
                            string objRefName = NombreNodo(objRef);
                            if (objName != objRefName)
                            {
                                int    weight = ReferenceWeight(objRef, obj);
                                String edge   = objRefName + " " + objName;

                                if (!aristas.Contains(edge))
                                {
                                    aristas.Add(edge);
                                    GraboArista(g, NameToId, objRefName, objName, weight);
                                }
                            }
                        }
                    }
                }
            }
            ;


            foreach (int node in g.Nodes)
            {
                string moduleName = IdToModule[node];
                int    moduleId   = NameToId[moduleName];
                initialpartition.Add(node, moduleId);
            }

            output.AddLine("Before automatic modularization. TurboMQ = " + TurboMQ(g, initialpartition).ToString());

            //Empiezo modularizacion
            Stopwatch stopwatch = new Stopwatch();
            // stopwatch.Restart();
            Dictionary <int, int> partition = Community.BestPartition(g);

            output.AddLine("BestPartition: " + stopwatch.Elapsed);
            var communities = new Dictionary <int, List <int> >();

            foreach (var kvp in partition)
            {
                List <int> nodeset;
                if (!communities.TryGetValue(kvp.Value, out nodeset))
                {
                    nodeset = communities[kvp.Value] = new List <int>();
                }
                nodeset.Add(kvp.Key);
                //    output.AddLine(kvp.Key.ToString() +"  "+kvp.Value);
            }
            output.AddLine(communities.Count + " modules found");
            Dictionary <string, int> modu = new Dictionary <string, int>();
            int counter = 0;

            foreach (var kvp in communities)
            {
                output.AddLine(String.Format("module {0}: {1} objects", counter, kvp.Value.Count));
                foreach (var objid in kvp.Value)
                {
                    var objname  = IdToName[objid];
                    int cantidad = 0;
                    // output.AddLine("Module :" + counter.ToString() + " " + objname);
                    string pareja = IdToModule[objid] + " " + counter.ToString();
                    if (modu.ContainsKey(pareja))
                    {
                        modu[pareja] = modu[pareja] + 1;
                    }
                    else
                    {
                        modu.Add(pareja, 1);
                    }
                }


                var sortedDict = from entry in modu orderby entry.Value descending select entry;

                //Cantidad de modulo nuevo y modulo viejo.
                foreach (KeyValuePair <string, int> entry in sortedDict)
                {
                    //   output.AddLine(entry.Key + " " + entry.Value.ToString());
                    Module m = new Module(model);
                    m.Name = entry.Key.Replace(" ", "_") + string.Format("_{0:yyyy_MM_dd_hh_mm_ss}", DateTime.Now);
                    output.AddLine(m.Name);
                    m.Module = kbserv.CurrentModel.GetDesignModel().RootModule;
                    m.Save();

                    foreach (var objid in kvp.Value)
                    {
                        KBObject objToChange = KBObject.Get(model, IdToKey[objid]);
                        if (objToChange != null)
                        {
                            if (objToChange is Table)
                            {
                                try
                                {
                                    KBObject trnBest = GenexusBLServices.Tables.GetBestAssociatedTransaction(model, objToChange.Key);
                                    trnBest.Module = m;
                                    trnBest.Save();
                                }
                                catch (Exception e) { output.AddErrorLine(objToChange.Name + e.Message); }
                            }
                            else
                            {
                                try
                                {
                                    objToChange.Module = m;
                                    objToChange.Save();
                                }
                                catch (Exception e) { output.AddErrorLine(objToChange.Name + e.Message); }
                            }
                        }
                    }

                    break;
                }
                counter++;
                modu.Clear();
            }
        }