//**************************************************************** // TRABAJO ISBC : INTEGRACION JAVA-JESS. CASO DEL ELEVADOR //**************************************************************** // // Fichero: ControladorDisplay.java // Autor: Daniel Urieta Lopez // Modificado por: // Proposito: Contiene la clase ControladorDisplay y ControladorPanel // que enlazan un motor de inferencia Rete (Jess) // con display de salida por pantalla, implementando // el interfaz ReteDisplay. // // //**************************************************************** package ISBC_JESS; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import jess.*; import java.io.*; import java.util.*; import ISBC_JESS.*; public class ControladorDisplay extends Observable implements ReteDisplay { ReteISBC reteISBC; //Panel donde pondremos un area de texto para la salida estandar del // motor de inferencia. private Panel m_panel,m_panel2; //Panel de checkbox para visualizar watches private Panel cb_panel; private Panel b_panel; //Objetos necesarios para un area de texto de salida y un boton de // borrado de pantalla. private TextArea ta; private PrintStream ps; private Button m_bClear; private Button m_bRules; private Button m_bFacts; private Label H; private Checkbox Hechos; private Label R; private Checkbox Reglas; private Label D; private Checkbox Disparos; private Label C; private Checkbox Compilaciones; private Label M; private Checkbox Mensajes; public ControladorDisplay(ReteISBC r) { reteISBC = r; ta = new TextArea(20, 80); ta.setEditable(false); H=new Label("Hechos"); Hechos=new Checkbox(); R=new Label("Reglas"); Reglas=new Checkbox(); D=new Label("Activaciones"); Disparos=new Checkbox(); C=new Label("Definiciones"); Compilaciones=new Checkbox(); M=new Label("Mensajes"); Mensajes=new Checkbox(); cb_panel = new Panel(); cb_panel.setLayout(new GridLayout(3,4)); cb_panel.add(H); cb_panel.add(Hechos); cb_panel.add(R); cb_panel.add(Reglas); cb_panel.add(D); cb_panel.add(Disparos); cb_panel.add(C); cb_panel.add(Compilaciones); cb_panel.add(M); cb_panel.add(Mensajes); m_bClear = new Button("Borrar ventana"); m_bRules = new Button("Reglas..."); m_bFacts = new Button("Hechos..."); Panel b_panel = new Panel(); b_panel.setLayout(new BorderLayout()); b_panel.add("East",m_bClear); b_panel.add("Center",m_bRules); b_panel.add("West",m_bFacts); Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add("South", b_panel); TextAreaOutputStream taos = new TextAreaOutputStream(ta); ps = new PrintStream(taos, true); //Situamos el boton y el area de texto en un panel de la clase // Controlador panel. m_panel2 = new ControladorPanel2(reteISBC, Hechos, Reglas, Disparos ,Compilaciones ,Mensajes); m_panel2.setLayout(new BorderLayout()); m_panel2.add("South", cb_panel); //Situamos el boton y el area de texto en un panel de la clase // Controlador panel. m_panel = new ControladorPanel(reteISBC, ta, ps, m_bClear, m_bRules, m_bFacts); m_panel.setLayout(new BorderLayout()); m_panel.add("Center", ta); m_panel.add("North", m_panel2); m_panel.add("South", p); } //IMPLEMENTACION DE LOS METODOS DEL INTERFAZ RETEDISPLAY final public Panel panel() { return m_panel; } public PrintStream stdout() { return ps;} public PrintStream stderr() { return ps;} public InputStream stdin() { return System.in; } public Applet applet() {return null; } public void assertFact(ValueVector fact) { setChanged(); notifyObservers("FACT"); } public void retractFact(ValueVector fact) { setChanged(); notifyObservers("FACT"); } public void addDeffacts(Deffacts df) {} public void addDeftemplate(Deftemplate dt) {} public void addDefrule(Defrule rule) { setChanged(); notifyObservers("RULE"); } public void activateRule(Defrule rule) {} public void deactivateRule(Defrule rule) {} public void fireRule(Defrule rule) { setChanged(); notifyObservers("RULE"); System.gc(); } } class ControladorPanel2 extends Panel { ReteISBC reteISBC; private Checkbox Hechos; private Checkbox Reglas; private Checkbox Disparos; private Checkbox Compilaciones; private Checkbox Mensajes; ControladorPanel2(ReteISBC r ,Checkbox H ,Checkbox R ,Checkbox D , Checkbox C ,Checkbox M) { reteISBC = r; Hechos = H; Reglas = R; Disparos = D; Compilaciones = C; Mensajes = M; Hechos.setState(false); Reglas.setState(false); Disparos.setState(false); Compilaciones.setState(false); Mensajes.setState(false); } //Escucha para accionar el boton public boolean action(Event e, Object O) { System.err.println("Estoy tratando una accion"); if (e.target == Hechos) { reteISBC.setHechos(Hechos.getState()); return true; } else if (e.target == Reglas) { reteISBC.setReglas(Reglas.getState()); return true; } else if (e.target == Disparos) { reteISBC.setDisparos(Disparos.getState()); return true; } else if (e.target == Compilaciones) { reteISBC.setCompilaciones(Compilaciones.getState()); return true; } else if (e.target == Mensajes) { reteISBC.setVerbose(Mensajes.getState()); return true; } return false; } } //CLASE CONTROLADOR PANEL CON UN AREA DE TEXTO Y UN BOTON class ControladorPanel extends Panel { ReteISBC reteISBC; private TextArea m_ta; private PrintStream m_out; private Button m_bClear; private Button m_bRules; private Button m_bFacts; ControladorPanel(ReteISBC r, TextArea ta, PrintStream out, Button bClear , Button bRules ,Button bFacts) { reteISBC = r; m_ta = ta; m_out = out; m_bClear = bClear; m_bRules = bRules; m_bFacts = bFacts; } //Escucha para accionar el boton public boolean action(Event e, Object o) { if (e.target == m_bClear) { m_ta.setText(""); return true; } else { if (e.target == m_bFacts) { reteISBC.executeFacts(); return true; } else { if (e.target == m_bFacts) { reteISBC.executeRules(); return true; } } } return false; } }