//Title: gramatica.java //Version: 1.0 //Copyright: //Author: ELENA ARREGUI // MARIANO MEDIAVILLA // RAQUEL MIGUEL //Asignatura: ISBC //Description: ventana de la aplicación que realiza la interacción con jess package isbc; import java.awt.*; import com.borland.jbcl.control.*; import com.borland.jbcl.layout.*; import java.awt.event.*; import jess.*; import java.lang.*; import javax.swing.*; import java.util.*; public class ventana extends DecoratedFrame { XYLayout xYLayout1 = new XYLayout(); ButtonControl buttonControl1 = new ButtonControl(); ButtonControl buttonControl2 = new ButtonControl(); gramatica gramatica1 = new gramatica(); Rete rete = new Rete(); TextAreaControl resultado = new TextAreaControl(); TextFieldControl sentencia = new TextFieldControl(); TextControl textControl1 = new TextControl(); TextControl textControl2 = new TextControl(); ButtonControl buttonControl3 = new ButtonControl(); TextControl mal = new TextControl(); public ventana() { try { jbInit(); this.resize(410,500); this.show(); rete.executeCommand("(batch jess/misbloques.clp)"); rete.reset(); mal.setVisible(false); //ocultación del indicador de petición incorrecta mal.setText("Sentencia no reconocida"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ventana ventana1 = new ventana(); } private void jbInit() throws Exception { this.setLayout(xYLayout1); buttonControl1.setFont(new java.awt.Font("Dialog", 1, 12)); buttonControl1.setLabel("Aceptar"); buttonControl2.setFont(new java.awt.Font("Dialog", 1, 12)); buttonControl2.setLabel("Salir"); buttonControl2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonControl2_actionPerformed(e); } }); buttonControl1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonControl1_actionPerformed(e); } }); xYLayout1.setHeight(498); xYLayout1.setWidth(400); textControl1.setFont(new java.awt.Font("Dialog", 1, 12)); textControl1.setText("Resultado:"); textControl2.setFont(new java.awt.Font("Dialog", 1, 12)); textControl2.setText("Sentencia:"); buttonControl3.setFont(new java.awt.Font("Dialog", 1, 12)); buttonControl3.setLabel("Reset"); buttonControl3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonControl3_actionPerformed(e); } }); mal.setFont(new java.awt.Font("Dialog", 1, 12)); mal.setForeground(Color.red); mal.setText(""); this.add(sentencia, new XYConstraints(35, 31, 333, -1)); this.add(resultado, new XYConstraints(37, 120, 333, 261)); this.add(textControl1, new XYConstraints(20, 91, -1, -1)); this.add(buttonControl1, new XYConstraints(36, 407, 73, 34)); this.add(buttonControl2, new XYConstraints(292, 409, 75, 31)); this.add(buttonControl3, new XYConstraints(162, 407, 76, 34)); this.add(textControl2, new XYConstraints(15, 4, 80, 21)); this.add(mal, new XYConstraints(32, 69, 191, 18)); } void buttonControl1_actionPerformed(ActionEvent e) { mal.setVisible(false); //ocultación de frase de petición incorrecta String objetivo = new String(this.gramatica1.get_sentencia(sentencia.getText())); if (objetivo.equals("")){ // petición incorrecta mal.setVisible(true); //muestra el indicador } else{ //petición aceptada try{ int indiceprin; rete.executeCommand(objetivo); //inserción del objetivo correspondiente a la petición rete.store("CAMINO",new java.lang.String("")); //inicialización de la solución rete.run(); // obtenemos la solución. Cada paso separado por ; String cad_resultado = new String(rete.fetch("CAMINO").toString().substring(1,rete.fetch("CAMINO").toString().length()-1).toString()); int indice = cad_resultado.indexOf(';'); if (indice >= 0) { //algún movimiento //se añade el primer paso en el recuadro resultado.setText(resultado.getText().concat(cad_resultado.substring(0,indice))); while (indice < cad_resultado.length()-1) {// se añaden los pasos sucesivos resultado.setText(resultado.getText().concat("\n")); indiceprin = indice + 1; indice = cad_resultado.indexOf(';',indiceprin); resultado.setText(resultado.getText().concat(cad_resultado.substring(indiceprin,indice))); } } resultado.setText(resultado.getText().concat("\n-----------------------------------------------\n")); } catch (JessException e1){ System.out.println("Error jess: "+ e1.getMessage()); } } } void buttonControl2_actionPerformed(ActionEvent e) { System.exit(0); } void buttonControl3_actionPerformed(ActionEvent e) { try{ rete.reset(); mal.setVisible(false); } catch(JessException e1){ System.out.println("Error jess: " + e1.getMessage()); } sentencia.setText(""); resultado.setText(""); } }