diff options
author | cecigg <caggsld@gmail.com> | 2013-05-11 19:17:28 (GMT) |
---|---|---|
committer | cecigg <caggsld@gmail.com> | 2013-05-11 19:17:28 (GMT) |
commit | ee226f14dd3efb6146c44e7dc31fded83d3e1c65 (patch) | |
tree | 7dd846dcb96ae658069d7ab24c3745d93b9084cf /EjercitarServer/ejbModule/beans/AdministracionBean.java | |
parent | 41a1739b4ef8e5a24ae94a0214508898097529f6 (diff) | |
parent | d330eeb391afc326ad32417601e06082ab23e0f5 (diff) |
Merge remote-tracking branch 'origin/samu' into ceci
Diffstat (limited to 'EjercitarServer/ejbModule/beans/AdministracionBean.java')
-rw-r--r-- | EjercitarServer/ejbModule/beans/AdministracionBean.java | 621 |
1 files changed, 314 insertions, 307 deletions
diff --git a/EjercitarServer/ejbModule/beans/AdministracionBean.java b/EjercitarServer/ejbModule/beans/AdministracionBean.java index 1729329..67ff358 100644 --- a/EjercitarServer/ejbModule/beans/AdministracionBean.java +++ b/EjercitarServer/ejbModule/beans/AdministracionBean.java @@ -1,307 +1,314 @@ -package beans;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.ejb.LocalBean;
-import javax.ejb.Stateless;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-
-import smile.Network;
-
-import model.Asignatura;
-import model.Concepto;
-import model.Ejercicio;
-import model.Profesor;
-import model.Tema;
-
-/**
- * Session Bean implementation class AdministracionBean
- */
-@Stateless
-@LocalBean
-public class AdministracionBean implements AdministracionBeanRemote, AdministracionBeanLocal {
-
- @PersistenceContext
- EntityManager em;
-
- /**
- * Default constructor.
- */
- public AdministracionBean() {
- // TODO Auto-generated constructor stub
- }
-
- public List getConceptoList(int idTema) {
- Tema tema = em.find(Tema.class, idTema);
- List conceptoList = tema.getConceptoList();
- System.out.println(conceptoList.toString());
- return conceptoList;
- }
-
- public void agregarEjercicio(Ejercicio ejercicio) {
- //Poner en una transaccion
- // em.getTransaction().begin();
- em.persist(ejercicio);
- agregarEjercicioRed(ejercicio);
- //em.getTransaction().commit();
- }
-
- public void agregarTema(Tema tema) {
- //Poner en una transaccion
- System.out.println("################################################################################");
- System.out.println(tema.getIdTema());
- System.out.println(tema.getDescripcion());
- System.out.println(tema.getNombre());
- System.out.println(tema.getAsignatura().getIdAsignatura());
- System.out.println(tema.getPeso());
- System.out.println(tema.getProfesor().getIdProfesor());
-
- System.out.println("################################################################################");
-
-
-
-
-
- em.persist(tema);
- // agregarTemaRed(tema);
-
- }
-
- public void agregarConcepto(Concepto concepto) {
- //Poner en una transaccion
- em.persist(concepto);
- agregarConceptoRed(concepto);
-
- }
-
- public void agregarProfesor(Profesor profesor) {
- em.persist(profesor);
- }
-
- public void calcularProbabilidades(Profesor profesor) {
- // Calculo de las probabilidades condicionales de las relaciones de agregacion
-
- String nombreRed = "red_base_curso_1.xdsl";
- System.out.println("redes/" + nombreRed);
- Network net = new Network();
- net.readFile("redes/" + nombreRed);
-
- //agregar where
- List<Tema> temaList = (List<Tema>) em.createQuery("Select e from Tema e").getResultList();
- for(Tema tema : temaList) {
- double[] temaDef = calcularProbabilidadesTema(tema);
- net.setNodeDefinition(tema.getNombre(), temaDef);
- }
-
- //agregar where
- List<Asignatura> asignaturaList = (List<Asignatura>) em.createQuery("Select e from Asignatura e").getResultList();
- for(Asignatura asignatura : asignaturaList) {
- double[] asignaturaDef = calcularProbabilidadesAsignatura(asignatura);
- net.setNodeDefinition(asignatura.getNombre(), asignaturaDef);
- }
-
- net.writeFile("redes/" + nombreRed);
- }
-
- private double[] calcularProbabilidadesAsignatura(Asignatura asignatura) {
- int dimension = (int) Math.pow(2, asignatura.getTemaList().size());
- double [] asignaturaDef = new double[dimension * 2];
-
- int j = 0;
- for(int i = 0; i < dimension; i++) {
- asignaturaDef[j] = cpAsignatura(asignatura, i);
- j++;
- asignaturaDef[j] = 1 - asignaturaDef[j-1];
- j++;
-
- }
-
- return asignaturaDef;
- }
-
- private double cpAsignatura(Asignatura asignatura, int i) {
- String comb = Integer.toBinaryString(i);
- int dimension = asignatura.getTemaList().size();
- while(comb.length() != dimension){
- comb = "0" + comb;
- }
- int k = dimension - 1;
- double valTemp = 0;
- for (int j = 0; j < dimension; j++) {
- if(comb.charAt(k) == '1') {
- valTemp = valTemp + asignatura.getTemaList().get(j).getPeso();
- }
- k--;
- }
-
- return valTemp;
- }
-
- private double [] calcularProbabilidadesEval(Ejercicio ejercicio) {
- //Calculo de las probabilidades condicionales
- int dimension = (int) Math.pow(2, ejercicio.getConceptoList().size());
- double [] ejercicioDef = new double[dimension * 2];
-
- int j = 0;
- for(int i = 0; i < dimension; i++) {
- ejercicioDef[j] = cp(ejercicio, i);
- j++;
- ejercicioDef[j] = 1 - ejercicioDef[j-1];
- j++;
-
- }
-
- return ejercicioDef;
- }
-
- private double cp(Ejercicio ejercicio, int i) {
-
- String comb = Integer.toBinaryString(i);
- while(comb.length() < ejercicio.getConceptoList().size()){
- comb = "0" + comb;
- System.out.println(comb);
- }
- int k = ejercicio.getConceptoList().size() - 1;
- double valTemp = 1;
- for (int j = 0; j < ejercicio.getConceptoList().size(); j++) {
- if(comb.charAt(k) == '0') {
- valTemp = valTemp * ejercicio.getConceptoList().get(j).getAdivinanza();
- } else {
- valTemp = valTemp * (1 -
- ejercicio.getConceptoList().get(j).getDescuido());
- }
-
- k--;
- }
-
- return valTemp;
-
- }
-
- private double [] calcularProbabilidadesTema(Tema tema) {
- //Calculo de las probabilidades condicionales
- int dimension = (int) Math.pow(2, tema.getConceptoList().size());
- double [] temaDef = new double[dimension * 2];
-
- int j = 0;
- for(int i = 0; i < dimension; i++) {
- temaDef[j] = cpTema(tema, i);
- j++;
- temaDef[j] = 1 - temaDef[j-1];
- j++;
-
- }
-
- return temaDef;
- }
-
- private double cpTema(Tema tema, int i) {
-
- String comb = Integer.toBinaryString(i);
- while(comb.length() < tema.getConceptoList().size()){
- comb = "0" + comb;
- }
- int k = tema.getConceptoList().size() - 1;
- double valTemp = 0;
- for (int j = 0; j < tema.getConceptoList().size(); j++) {
- if(comb.charAt(k) == '1') {
- valTemp = valTemp + tema.getConceptoList().get(j).getPeso();
- }
- k--;
- }
-
- return valTemp;
-
- }
-
- private void agregarEjercicioRed(Ejercicio ejercicio) {
- //operaciones sobre la red bayesiana con smile
- String nombreRed = "red_base_curso_1.xdsl";
- System.out.println("redes/" + nombreRed);
- Network net = new Network();
- net.readFile("redes/" + nombreRed);
-
- //agregar nodo ejercicio
- String titulo = "E" + Integer.toString(ejercicio.getIdEjercicio());
- net.addNode(Network.NodeType.Cpt, titulo);
- net.setOutcomeId(titulo, 0, "Incorrecto");
- net.setOutcomeId(titulo, 1, "Correcto");
-
- //agregar los arcos
- List<Concepto> conceptoList = ejercicio.getConceptoList();
- for(Concepto concepto : conceptoList) {
- net.addArc(concepto.getNombre(), titulo);
- }
-
- //definir probabilidades condicionales
- double[] ejercicioDef = calcularProbabilidadesEval(ejercicio);
- net.setNodeDefinition(titulo, ejercicioDef);
-
- net.writeFile("redes/" + nombreRed);
- }
-
- private void agregarTemaRed(Tema tema) {
- //operaciones sobre la red bayesiana con smile
- String nombreRed = "red_base_curso_1.xdsl";
- System.out.println("redes/" + nombreRed);
- Network net = new Network();
- net.readFile("redes/" + nombreRed);
-
- //agregar nodo ejercicio
- String titulo = tema.getNombre();
- net.addNode(Network.NodeType.Cpt, titulo);
- net.setOutcomeId(titulo, 0, "No_conoce");
- net.setOutcomeId(titulo, 1, "Conoce");
-
- //agregar los arcos
- List<Concepto> conceptoList = tema.getConceptoList();
- for(Concepto concepto : conceptoList) {
- net.addArc(concepto.getNombre(), titulo);
- }
-
- //definir probabilidades
- //net.setNodeDefinition(titulo, ejercicioDef);
- net.writeFile("redes/" + nombreRed);
- }
-
- private void agregarConceptoRed(Concepto concepto) {
- //operaciones sobre la red bayesiana con smile
- String nombreRed = "red_base_curso_1.xdsl";
- System.out.println("redes/" + nombreRed);
- Network net = new Network();
- net.readFile("redes/" + nombreRed);
-
- //agregar nodo concepto
- String titulo = concepto.getNombre();
- net.addNode(Network.NodeType.Cpt, titulo);
- net.setOutcomeId(titulo, 0, "No_conoce");
- net.setOutcomeId(titulo, 1, "Conoce");
-
- //agregar los arcos
-
- net.addArc(titulo, concepto.getTema().getNombre());
-
- //definir probabilidades
- double [] conceptoDef = new double [2];
- conceptoDef[1] = concepto.getApriori();
- conceptoDef[0] = 1 - conceptoDef[1];
- net.setNodeDefinition(titulo, conceptoDef);
- net.writeFile("redes/" + nombreRed);
- }
-
- @Override
- public Profesor logIn(String usuario, String password) {
- // TODO Auto-generated method stub
- Profesor profesor = (Profesor) em.createNamedQuery("Profesor.findByUsuario")
- .setParameter("usuario", usuario).getSingleResult();
-
- if(password.trim().equals(profesor.getUsuario().trim())){
- System.out.println(profesor.getAsignaturaList().toString());//No Borrar, sirve para la inicializacion
- return profesor;
- }
-
- return null;
- }
-}
+package beans; + +import java.util.ArrayList; +import java.util.List; + +import javax.ejb.LocalBean; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import smile.Network; + +import model.Asignatura; +import model.Concepto; +import model.Ejercicio; +import model.Profesor; +import model.Tema; + +/** + * Session Bean implementation class AdministracionBean + */ +@Stateless +@LocalBean +public class AdministracionBean implements AdministracionBeanRemote, AdministracionBeanLocal { + + @PersistenceContext + EntityManager em; + + /** + * Default constructor. + */ + public AdministracionBean() { + // TODO Auto-generated constructor stub + } + + public List getConceptoList(int idTema) { + Tema tema = em.find(Tema.class, idTema); + List conceptoList = tema.getConceptoList(); + System.out.println(conceptoList.toString()); + return conceptoList; + } + + public void agregarEjercicio(Ejercicio ejercicio) { + //Poner en una transaccion + // em.getTransaction().begin(); + em.persist(ejercicio); + agregarEjercicioRed(ejercicio); + //em.getTransaction().commit(); + } + + public void agregarTema(Tema tema) { + //Poner en una transaccion + System.out.println("################################################################################"); + System.out.println(tema.getIdTema()); + System.out.println(tema.getDescripcion()); + System.out.println(tema.getNombre()); + System.out.println(tema.getAsignatura().getIdAsignatura()); + System.out.println(tema.getPeso()); + System.out.println(tema.getProfesor().getIdProfesor()); + + System.out.println("################################################################################"); + + + + + + em.persist(tema); + // agregarTemaRed(tema); + + } + + public void agregarConcepto(Concepto concepto) { + //Poner en una transaccion + em.persist(concepto); + agregarConceptoRed(concepto); + + } + + public void agregarProfesor(Profesor profesor) { + em.persist(profesor); + } + public void agregarAsignatura(Asignatura asignatura) { + System.out.print("#############" + asignatura.getDescripcion()); + System.out.print("#############" +asignatura.getNombre()); + System.out.print("#############" +asignatura.getCurso().getTurno()); + System.out.print("#############" +asignatura.getProfesor().getNombre()); + em.persist(asignatura); + } + + public void calcularProbabilidades(Profesor profesor) { + // Calculo de las probabilidades condicionales de las relaciones de agregacion + + String nombreRed = "red_base_curso_1.xdsl"; + System.out.println("redes/" + nombreRed); + Network net = new Network(); + net.readFile("redes/" + nombreRed); + + //agregar where + List<Tema> temaList = (List<Tema>) em.createQuery("Select e from Tema e").getResultList(); + for(Tema tema : temaList) { + double[] temaDef = calcularProbabilidadesTema(tema); + net.setNodeDefinition(tema.getNombre(), temaDef); + } + + //agregar where + List<Asignatura> asignaturaList = (List<Asignatura>) em.createQuery("Select e from Asignatura e").getResultList(); + for(Asignatura asignatura : asignaturaList) { + double[] asignaturaDef = calcularProbabilidadesAsignatura(asignatura); + net.setNodeDefinition(asignatura.getNombre(), asignaturaDef); + } + + net.writeFile("redes/" + nombreRed); + } + + private double[] calcularProbabilidadesAsignatura(Asignatura asignatura) { + int dimension = (int) Math.pow(2, asignatura.getTemaList().size()); + double [] asignaturaDef = new double[dimension * 2]; + + int j = 0; + for(int i = 0; i < dimension; i++) { + asignaturaDef[j] = cpAsignatura(asignatura, i); + j++; + asignaturaDef[j] = 1 - asignaturaDef[j-1]; + j++; + + } + + return asignaturaDef; + } + + private double cpAsignatura(Asignatura asignatura, int i) { + String comb = Integer.toBinaryString(i); + int dimension = asignatura.getTemaList().size(); + while(comb.length() != dimension){ + comb = "0" + comb; + } + int k = dimension - 1; + double valTemp = 0; + for (int j = 0; j < dimension; j++) { + if(comb.charAt(k) == '1') { + valTemp = valTemp + asignatura.getTemaList().get(j).getPeso(); + } + k--; + } + + return valTemp; + } + + private double [] calcularProbabilidadesEval(Ejercicio ejercicio) { + //Calculo de las probabilidades condicionales + int dimension = (int) Math.pow(2, ejercicio.getConceptoList().size()); + double [] ejercicioDef = new double[dimension * 2]; + + int j = 0; + for(int i = 0; i < dimension; i++) { + ejercicioDef[j] = cp(ejercicio, i); + j++; + ejercicioDef[j] = 1 - ejercicioDef[j-1]; + j++; + + } + + return ejercicioDef; + } + + private double cp(Ejercicio ejercicio, int i) { + + String comb = Integer.toBinaryString(i); + while(comb.length() < ejercicio.getConceptoList().size()){ + comb = "0" + comb; + System.out.println(comb); + } + int k = ejercicio.getConceptoList().size() - 1; + double valTemp = 1; + for (int j = 0; j < ejercicio.getConceptoList().size(); j++) { + if(comb.charAt(k) == '0') { + valTemp = valTemp * ejercicio.getConceptoList().get(j).getAdivinanza(); + } else { + valTemp = valTemp * (1 - + ejercicio.getConceptoList().get(j).getDescuido()); + } + + k--; + } + + return valTemp; + + } + + private double [] calcularProbabilidadesTema(Tema tema) { + //Calculo de las probabilidades condicionales + int dimension = (int) Math.pow(2, tema.getConceptoList().size()); + double [] temaDef = new double[dimension * 2]; + + int j = 0; + for(int i = 0; i < dimension; i++) { + temaDef[j] = cpTema(tema, i); + j++; + temaDef[j] = 1 - temaDef[j-1]; + j++; + + } + + return temaDef; + } + + private double cpTema(Tema tema, int i) { + + String comb = Integer.toBinaryString(i); + while(comb.length() < tema.getConceptoList().size()){ + comb = "0" + comb; + } + int k = tema.getConceptoList().size() - 1; + double valTemp = 0; + for (int j = 0; j < tema.getConceptoList().size(); j++) { + if(comb.charAt(k) == '1') { + valTemp = valTemp + tema.getConceptoList().get(j).getPeso(); + } + k--; + } + + return valTemp; + + } + + private void agregarEjercicioRed(Ejercicio ejercicio) { + //operaciones sobre la red bayesiana con smile + String nombreRed = "red_base_curso_1.xdsl"; + System.out.println("redes/" + nombreRed); + Network net = new Network(); + net.readFile("redes/" + nombreRed); + + //agregar nodo ejercicio + String titulo = "E" + Integer.toString(ejercicio.getIdEjercicio()); + net.addNode(Network.NodeType.Cpt, titulo); + net.setOutcomeId(titulo, 0, "Incorrecto"); + net.setOutcomeId(titulo, 1, "Correcto"); + + //agregar los arcos + List<Concepto> conceptoList = ejercicio.getConceptoList(); + for(Concepto concepto : conceptoList) { + net.addArc(concepto.getNombre(), titulo); + } + + //definir probabilidades condicionales + double[] ejercicioDef = calcularProbabilidadesEval(ejercicio); + net.setNodeDefinition(titulo, ejercicioDef); + + net.writeFile("redes/" + nombreRed); + } + + private void agregarTemaRed(Tema tema) { + //operaciones sobre la red bayesiana con smile + String nombreRed = "red_base_curso_1.xdsl"; + System.out.println("redes/" + nombreRed); + Network net = new Network(); + net.readFile("redes/" + nombreRed); + + //agregar nodo ejercicio + String titulo = tema.getNombre(); + net.addNode(Network.NodeType.Cpt, titulo); + net.setOutcomeId(titulo, 0, "No_conoce"); + net.setOutcomeId(titulo, 1, "Conoce"); + + //agregar los arcos + List<Concepto> conceptoList = tema.getConceptoList(); + for(Concepto concepto : conceptoList) { + net.addArc(concepto.getNombre(), titulo); + } + + //definir probabilidades + //net.setNodeDefinition(titulo, ejercicioDef); + net.writeFile("redes/" + nombreRed); + } + + private void agregarConceptoRed(Concepto concepto) { + //operaciones sobre la red bayesiana con smile + String nombreRed = "red_base_curso_1.xdsl"; + System.out.println("redes/" + nombreRed); + Network net = new Network(); + net.readFile("redes/" + nombreRed); + + //agregar nodo concepto + String titulo = concepto.getNombre(); + net.addNode(Network.NodeType.Cpt, titulo); + net.setOutcomeId(titulo, 0, "No_conoce"); + net.setOutcomeId(titulo, 1, "Conoce"); + + //agregar los arcos + + net.addArc(titulo, concepto.getTema().getNombre()); + + //definir probabilidades + double [] conceptoDef = new double [2]; + conceptoDef[1] = concepto.getApriori(); + conceptoDef[0] = 1 - conceptoDef[1]; + net.setNodeDefinition(titulo, conceptoDef); + net.writeFile("redes/" + nombreRed); + } + + @Override + public Profesor logIn(String usuario, String password) { + // TODO Auto-generated method stub + Profesor profesor = (Profesor) em.createNamedQuery("Profesor.findByUsuario") + .setParameter("usuario", usuario).getSingleResult(); + + if(password.trim().equals(profesor.getUsuario().trim())){ + System.out.println(profesor.getAsignaturaList().toString());//No Borrar, sirve para la inicializacion + return profesor; + } + + return null; + } +} |