Exemplo n.º 1
0
        private void RodarArvore(int configuracaoArvoreId)
        {
            var    atributos          = db.ConfiguracaoAtributo.Where(config => config.ConfiguracaoArvoreID == configuracaoArvoreId).ToList();
            string classeMeta         = atributos[0].ClasseMeta;
            var    configuracaoArvore = db.ConfiguracaoArvore.FirstOrDefault(configArvore => configArvore.ID == configuracaoArvoreId);

            //Gerar a árvore
            List <Atributo> atrs = new List <Atributo>();

            foreach (var atr in atributos)
            {
                atrs.Add(new Atributo {
                    Nome = atr.Nome, TipoAtributo = atr.Tipo, Legenda = atr.Legenda
                });
            }

            TabelaTemporaria.PreencherAtributos(atrs, configuracaoArvoreId);

            C45 c45 = new C45();

            if (configuracaoArvore.Poda > 0)
            {
                c45.Poda = configuracaoArvore.Poda;
            }
            c45.Calcular(classeMeta, atrs);

            ArvoreGerada arvoreGerada = new ArvoreGerada();

            arvoreGerada.ConfiguracaoArvoreID = configuracaoArvoreId;
            arvoreGerada.ClasseMeta           = atributos.FirstOrDefault(x => x.Nome.Equals(classeMeta)).Legenda;
            arvoreGerada.JSON           = new JavaScriptSerializer().Serialize(c45.arvore.GerarJSON());
            arvoreGerada.UsuarioGeracao = "Agendamento";
            arvoreGerada.DataGeracao    = DateTime.Now;
            var xml = new System.Xml.Serialization.XmlSerializer(c45.arvore.GetType());

            using (StringWriter textWriter = new StringWriter())
            {
                xml.Serialize(textWriter, c45.arvore);
                arvoreGerada.XML = textWriter.ToString();
            }
            var doc = new GeradorPMML().Gerar(atrs, classeMeta, c45.arvore, c45.PercentuaisClasseMeta);

            xml = new System.Xml.Serialization.XmlSerializer(doc.GetType());
            using (StringWriter textWriter = new StringWriter())
            {
                xml.Serialize(textWriter, doc);
                arvoreGerada.XmlPmml = textWriter.ToString();
            }

            if (atributos.Count(x => x.Tipo == Tipo.Mineração_de_Texto) > 0)
            {
                arvoreGerada.JsonNuvemPalavras = new JavaScriptSerializer().Serialize(TabelaTemporaria.RetornaNuvemPalavras(atributos.FirstOrDefault(x => x.Tipo == Tipo.Mineração_de_Texto).Nivel));
            }

            db.ArvoreGerada.Add(arvoreGerada);
            db.SaveChanges();
        }
Exemplo n.º 2
0
        public ActionResult Index(IList <MineradorRH.Models.ConfiguracaoAtributo> atributos)
        {
            string classeMeta = atributos[0].ClasseMeta;

            if (string.IsNullOrEmpty(classeMeta))
            {
                ModelState.AddModelError("", "Um atributo deve ser classe meta.");
                return(View(atributos));
            }

            if (atributos.Count(x => x.Tipo == Tipo.Mineração_de_Texto) > 1)
            {
                ModelState.AddModelError("", "Árvore pode possuir apenas um atributo do tipo Mineração de texto.");
                return(View(atributos));
            }

            if (ModelState.IsValid)
            {
                foreach (var atributo in atributos.Where(atr => atr.ID > 0))
                {
                    db.Entry(atributo).State = EntityState.Modified;
                }

                foreach (var atributo in atributos.Where(atr => atr.ID <= 0))
                {
                    db.ConfiguracaoAtributo.Add(atributo);
                }

                db.SaveChanges();

                int arvoreID           = atributos[0].ConfiguracaoArvoreID;
                var configuracaoArvore = db.ConfiguracaoArvore.FirstOrDefault(configArvore => configArvore.ID == arvoreID);

                //Gerar a árvore
                List <Atributo> atrs = new List <Atributo>();
                foreach (var atr in atributos)
                {
                    atrs.Add(new Atributo {
                        Nome = atr.Nome, TipoAtributo = atr.Tipo, Legenda = atr.Legenda, Nivel = atr.Nivel
                    });
                }

                TabelaTemporaria.PreencherAtributos(atrs, arvoreID);

                C45 c45 = new C45();
                if (configuracaoArvore.Poda > 0)
                {
                    c45.Poda = configuracaoArvore.Poda;
                }
                c45.Calcular(classeMeta, atrs);

                ArvoreGerada arvoreGerada = new ArvoreGerada();
                arvoreGerada.ConfiguracaoArvoreID = arvoreID;
                arvoreGerada.ClasseMeta           = atributos.FirstOrDefault(x => x.Nome.Equals(classeMeta)).Legenda;
                arvoreGerada.JSON           = new JavaScriptSerializer().Serialize(c45.arvore.GerarJSON());
                arvoreGerada.UsuarioGeracao = User.Identity.Name;
                arvoreGerada.DataGeracao    = DateTime.Now;
                var xml = new System.Xml.Serialization.XmlSerializer(c45.arvore.GetType());
                using (StringWriter textWriter = new StringWriter())
                {
                    xml.Serialize(textWriter, c45.arvore);
                    arvoreGerada.XML = textWriter.ToString();
                }
                var doc = new GeradorPMML().Gerar(atrs, classeMeta, c45.arvore, c45.PercentuaisClasseMeta);
                xml = new System.Xml.Serialization.XmlSerializer(doc.GetType());
                using (StringWriter textWriter = new StringWriter())
                {
                    xml.Serialize(textWriter, doc);
                    arvoreGerada.XmlPmml = textWriter.ToString();
                }

                if (atributos.Count(x => x.Tipo == Tipo.Mineração_de_Texto) > 0)
                {
                    arvoreGerada.JsonNuvemPalavras = new JavaScriptSerializer().Serialize(TabelaTemporaria.RetornaNuvemPalavras(atributos.FirstOrDefault(x => x.Tipo == Tipo.Mineração_de_Texto).Nivel));
                }

                db.ArvoreGerada.Add(arvoreGerada);
                db.SaveChanges();

                return(View("Grafico", arvoreGerada));
            }

            return(View(atributos));
        }