/* * FuzzyLogicToolBox.java * * Created on July 30, 2002, 12:20 AM */ package uk.ac.leeds.ccg.projects.MedAction.FuzzyInference; /** * * @author Jianhui Jin */ import javax.swing.*; import java.awt.*; import java.io.*; import uk.ac.leeds.ccg.andyt.grids.*; public class FuzzyLogicToolBox extends javax.swing.JFrame { private FuzzyPanel fVariablePanel=null; private FuzzyRulePanel fRulePanel=null; //private FuzzyVariable[] allFuzzyVariable=null; //private String[] allVariableFileName=null; private DataAndFunctionInputPanel dataAndFunctionInputPanel=null; private FuzzyLogicModel fuzzyLogicModel=new FuzzyLogicModel(); private FuzzyPanel getFuzzyPanel(){ return this.fVariablePanel; } /* private FuzzyVariable getFuzzyVariable(){ return this.fVariable; } */ /** Creates new form FuzzyLogicToolBox */ public FuzzyLogicToolBox() { initComponents(); this.setBounds(300,300,700,500); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents jMenuBar1 = new javax.swing.JMenuBar(); buildingMenu = new javax.swing.JMenu(); BuildFunctionMenuItem = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JSeparator(); BuildRuleModelMenuItem = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JSeparator(); SaveFunctionMenuItem = new javax.swing.JMenuItem(); jSeparator3 = new javax.swing.JSeparator(); SaveFuzzyLogicModelMenuItem = new javax.swing.JMenuItem(); inputMenu = new javax.swing.JMenu(); inputDataAndFunctionMenuItem = new javax.swing.JMenuItem(); jSeparator4 = new javax.swing.JSeparator(); inputFuzzyLogicModel = new javax.swing.JMenuItem(); runMenu = new javax.swing.JMenu(); runMenuItem = new javax.swing.JMenuItem(); jSeparator5 = new javax.swing.JSeparator(); showResultMenuItem = new javax.swing.JMenuItem(); jSeparator6 = new javax.swing.JSeparator(); compareResultwithTarget = new javax.swing.JMenuItem(); setTitle("Fuzzy Logic Model"); setBackground(new java.awt.Color(255, 255, 255)); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); buildingMenu.setText("Build"); buildingMenu.setToolTipText("Building Fuzzy Rules"); BuildFunctionMenuItem.setText("Building Membership Function"); BuildFunctionMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BuildFunctionMenuItemActionPerformed(evt); } }); buildingMenu.add(BuildFunctionMenuItem); buildingMenu.add(jSeparator1); BuildRuleModelMenuItem.setText("Build Fuzzy Rules"); BuildRuleModelMenuItem.setEnabled(false); BuildRuleModelMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BuildRuleModelMenuItemActionPerformed(evt); } }); buildingMenu.add(BuildRuleModelMenuItem); buildingMenu.add(jSeparator2); SaveFunctionMenuItem.setText("Save Function"); SaveFunctionMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SaveFunctionMenuItemActionPerformed(evt); } }); buildingMenu.add(SaveFunctionMenuItem); buildingMenu.add(jSeparator3); SaveFuzzyLogicModelMenuItem.setText("Save FL Model"); SaveFuzzyLogicModelMenuItem.setEnabled(false); SaveFuzzyLogicModelMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SaveFuzzyLogicModelMenuItemActionPerformed(evt); } }); buildingMenu.add(SaveFuzzyLogicModelMenuItem); jMenuBar1.add(buildingMenu); inputMenu.setText("Input"); inputDataAndFunctionMenuItem.setText("Input Data and Membership Function"); inputDataAndFunctionMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inputDataAndFunctionMenuItemActionPerformed(evt); } }); inputMenu.add(inputDataAndFunctionMenuItem); inputMenu.add(jSeparator4); inputFuzzyLogicModel.setText("Input FL Model"); inputFuzzyLogicModel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inputFuzzyLogicModelActionPerformed(evt); } }); inputMenu.add(inputFuzzyLogicModel); jMenuBar1.add(inputMenu); runMenu.setText("Run"); runMenuItem.setText("Run "); runMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { runMenuItemActionPerformed(evt); } }); runMenu.add(runMenuItem); runMenu.add(jSeparator5); showResultMenuItem.setText("Show a Grid"); showResultMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showResultMenuItemActionPerformed(evt); } }); runMenu.add(showResultMenuItem); runMenu.add(jSeparator6); compareResultwithTarget.setText("CompareResultWithTarget"); compareResultwithTarget.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { compareResultwithTargetActionPerformed(evt); } }); runMenu.add(compareResultwithTarget); jMenuBar1.add(runMenu); setJMenuBar(jMenuBar1); pack(); }//GEN-END:initComponents private void compareResultwithTargetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compareResultwithTargetActionPerformed double[] fire=null; double[] target=null; double[] allE=null; if(this.removeAllComp()){ try{ FileDialog fDialog=new FileDialog(this,"Load Fire Grid",FileDialog.LOAD); fDialog.show(); if(fDialog.getFile()==null) return; fire=GridDataReaderAndWriter.readGridAsciiFile(new File(fDialog.getDirectory()+fDialog.getFile())); fDialog=new FileDialog(this,"Load Target Grid",FileDialog.LOAD); fDialog.show(); if(fDialog.getFile()==null) return; target=GridDataReaderAndWriter.readGridAsciiFile(new File(fDialog.getDirectory()+fDialog.getFile())); } catch(Exception e){e.printStackTrace();} allE=GridDataReaderAndWriter.getResultStatistics(fire,target); JOptionPane.showMessageDialog(this.getContentPane(),"Min Error is " + allE[0]+"\n" +"Max Error is "+allE[1]+"\n" +"Mean Error is "+allE[2]+"\n" +"RMS Error is "+allE[3]+"\n" +"AbsErrorSumPercentage is "+allE[4]+"\n"); // Add your handling code here: } // Add your handling code here: }//GEN-LAST:event_compareResultwithTargetActionPerformed private void showResultMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showResultMenuItemActionPerformed if(this.removeAllComp()){ try{ FileDialog fDialog=new FileDialog( this, "Select a Grid File Please", FileDialog.LOAD ); fDialog.show(); if(fDialog.getFile()==null) return; GridDataReaderAndWriter.showGrid( this.getContentPane(), new File( fDialog.getDirectory() + fDialog.getFile() ) ); } catch(Exception e){e.printStackTrace();}// Add your handling code here: } // Add your handling code here: }//GEN-LAST:event_showResultMenuItemActionPerformed private void runMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runMenuItemActionPerformed Object[] ob = this.fuzzyLogicModel.toObject(); this.fuzzyLogicModel = new FuzzyLogicModel(ob); this.fuzzyLogicModel.setAllFileName(this.allVariableFileName); String[] allInputFileName = this.fuzzyLogicModel.getInputFileName(); AbstractGrid2DSquareCellDouble[] inputGrid = new AbstractGrid2DSquareCellDouble[allInputFileName.length]; Grid2DSquareCellDoubleFactory f = new Grid2DSquareCellDoubleFactory(); for ( int i = 0; i < inputGrid.length; i ++ ) { inputGrid[ i ] = f.createGrid2DSquareCellDouble( new File( allInputFileName[ i ] ) ); } try{ Grid2DSquareCellDoubleExchange.toAsciiFile( fuzzyLogicModel.runFuzzyLogicModel( inputGrid, System.getProperty( "java.io.tmpdir" ) ), new File( fuzzyLogicModel.getOutFileName() ) ); } catch ( Exception e ) { e.printStackTrace(); return; } // Add your handling code here: }//GEN-LAST:event_runMenuItemActionPerformed private void inputFuzzyLogicModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputFuzzyLogicModelActionPerformed try{ FileDialog fDialog=new FileDialog(this,"Load a Fuzzy Logic Model",FileDialog.LOAD); fDialog.show(); if(fDialog.getFile()==null) return; ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(new FileInputStream(fDialog.getDirectory()+fDialog.getFile()))); this.fuzzyLogicModel=new FuzzyLogicModel((Object[]) in.readObject()); in.close(); } catch(Exception e){ e.printStackTrace(); return; } if(removeAllComp()){ if(this.fuzzyLogicModel.getFuzzyRuleModel()!=null) { this.fRulePanel=new FuzzyRulePanel(this.fuzzyLogicModel.getFuzzyRuleModel()); } else if(this.fuzzyLogicModel.getAllFuzzyVariable()!=null&&this.fuzzyLogicModel.getAllFuzzyVariable().length!=0) { this.fuzzyLogicModel.setFuzzyRuleModel(new FuzzyRuleModel(this.fuzzyLogicModel.getAllFuzzyVariable())); this.fRulePanel=new FuzzyRulePanel(this.fuzzyLogicModel.getFuzzyRuleModel()); } this.getContentPane().add(this.fRulePanel); this.validate(); BuildRuleModelMenuItem.setEnabled(true); SaveFuzzyLogicModelMenuItem.setEnabled(true); } // Add your handling code here: }//GEN-LAST:event_inputFuzzyLogicModelActionPerformed private void inputDataAndFunctionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputDataAndFunctionMenuItemActionPerformed int totalVariableNumber=0; try{totalVariableNumber=Integer.parseInt(JOptionPane.showInputDialog(this,"how many variables including output variable you want "));} catch(Exception e){System.out.println("cancel action");return;} JScrollPane inputScroll=new JScrollPane(); if(removeAllComp()){ dataAndFunctionInputPanel=new DataAndFunctionInputPanel(totalVariableNumber); inputScroll.setViewportView(dataAndFunctionInputPanel); this.getContentPane().add(inputScroll); this.validate(); } // Add your handling code here: }//GEN-LAST:event_inputDataAndFunctionMenuItemActionPerformed private void SaveFunctionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveFunctionMenuItemActionPerformed try{ FileDialog fDialog=new FileDialog(this,"Save this Variable",FileDialog.SAVE); fDialog.show(); if(fDialog.getFile()==null) return; this.fVariablePanel.save(fVariablePanel.fVariable, new File( fDialog.getDirectory()+fDialog.getFile() ) ); } catch(Exception e){e.printStackTrace();return;}// Add your handling code here:// Add your handling code here: }//GEN-LAST:event_SaveFunctionMenuItemActionPerformed private void BuildRuleModelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuildRuleModelMenuItemActionPerformed if(removeAllComp()){this.fRulePanel=new FuzzyRulePanel(this.fuzzyLogicModel.getFuzzyRuleModel());} this.getContentPane().add(this.fRulePanel); this.validate(); // Add your handling code here: }//GEN-LAST:event_BuildRuleModelMenuItemActionPerformed private void BuildFunctionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuildFunctionMenuItemActionPerformed if(removeAllComp()){ if(JOptionPane.showInternalConfirmDialog(this.getContentPane(),(Object)"Do you what build a new one or import from file","Build or Import", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){ this.fVariablePanel=new FuzzyPanel(); this.getContentPane().add(this.fVariablePanel); this.validate();} else{ try{ FileDialog fDialog=new FileDialog(this,"Load a Variable",FileDialog.LOAD); fDialog.show(); if(fDialog.getFile()==null) return; this.fVariablePanel = new FuzzyPanel( FuzzyPanel.load( new File( fDialog.getDirectory() + fDialog.getFile() ) ) ); } catch(Exception e){e.printStackTrace();return;} } this.getContentPane().add(this.fVariablePanel); this.validate();} // Add your handling code here: }//GEN-LAST:event_BuildFunctionMenuItemActionPerformed private void SaveFuzzyLogicModelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveFuzzyLogicModelMenuItemActionPerformed try{ FileDialog fDialog=new FileDialog(this,"Save Fuzzy Logic Model",FileDialog.SAVE); fDialog.show(); if(fDialog.getFile()==null) return; this.fuzzyLogicModel.setFuzzyRuleModel(this.fRulePanel.getFuzzyRuleModel()); ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fDialog.getDirectory()+fDialog.getFile()))); out.writeObject(this.fuzzyLogicModel.toObject()); out.close(); } catch(Exception e){e.printStackTrace();} // Add your handling code here: }//GEN-LAST:event_SaveFuzzyLogicModelMenuItemActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm System.exit(0); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new FuzzyLogicToolBox().show(); } private boolean removeAllComp() { if(this.getContentPane().getComponents().length==0) {return true;} else { if(JOptionPane.showInternalConfirmDialog(this.getContentPane(),"If you already saved please ignore this message, /n or Please save this first,Or loss it","Remove Warning",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) {return false;} else { this.getContentPane().removeAll(); System.gc(); return true; } } } private class DataAndFunctionInputPanel extends javax.swing.JPanel { private JButton[] dataFileBrowseButton=null; private JButton[] functionFileBrowseButton=null; private JTextField[] dataFileNameTextField=null; private JTextField[] functionFileNameTextField=null; private int totalVariableNumber=0; private JButton constructButton=new JButton("Input and Construct"); private void addTextFieldAndBrowseButton(int totalVariableNumber){ dataFileBrowseButton=new JButton[totalVariableNumber]; functionFileBrowseButton=new JButton[totalVariableNumber]; dataFileNameTextField=new JTextField[totalVariableNumber]; functionFileNameTextField=new JTextField[totalVariableNumber]; for(int i=0;i