GuiHittingSets.java
Go to the documentation of this file.
00001 
00002 
00003 package dfengine;
00004 
00005 import java.util.*;
00006 import java.text.*;
00007 import java.io.*;
00008 import javax.swing.*;
00009 import javax.swing.text.*;
00010 import javax.swing.border.*;
00011 import java.awt.*;
00012 import java.awt.event.*;
00013 
00014 
00015 import hittingsetalg.*;
00016 import theoremprover.*;
00017 import utils.*;
00018 
00019 public class GuiHittingSets  
00020     implements ActionListener, ItemListener, 
00021     WindowListener, FocusListener {
00022 
00023     private JFrame frame;
00024 
00025     private JTabbedPane tabbedPane;
00026 
00027     private JLabel abLabel;
00028     private JLabel nabLabel;
00029     private JLabel dfLabel;
00030     private JTextArea propText;
00031     private JTextArea sdText;
00032     private JTextArea obsText;
00033     private JSpinner explSizeSpinner;
00034     private JTextField numExplText;
00035     private JCheckBox useFaultModelsCB;
00036     private JCheckBox depFaultsCB;
00037     private JCheckBox useProbabilitiesCB;
00038     private JTextField assABText;
00039     private JTextField assNABText;
00040     private JTextField assDFText;
00041     private JTextField negPrefixText;
00042     private JButton checkConsBtn;
00043     private JButton computeMinHSBtn;
00044     private JButton computeMoreBtn;
00045     private JButton computeMinDiagEnvBtn;
00046     //private JButton computeMergedDEBtn;
00047     private TitledBorder resultsBorder;
00048     private JPanel resultsPanel;
00049     private JTextArea results1Text;
00050     private JTextArea results2Text;
00051     private JButton clearPropBtn;
00052     private JButton clearSdBtn;
00053     private JButton clearObsBtn;
00054     private JButton commentBtn;
00055     private JButton uncommentBtn;
00056     private JTextField searchText;
00057     private JButton searchFirstBtn;
00058     private JButton searchNextBtn;
00059     private JLabel searchResultLabel;
00060     private JTextArea fdgText;
00061     private JTextArea sddText;
00062     private JPanel dfSettingsPanel;
00063     private JCheckBox betaEnvCB;
00064     private JCheckBox mergeDEsCB;
00065     private JCheckBox discardOrderPermsCB;
00066     private JSpinner maxDFChainSpinner;
00067 
00068     private JMenuItem savePropAsMenu;
00069     private JMenuItem saveSdAsMenu;
00070     private JMenuItem saveObsAsMenu;
00071     private JMenuItem saveFdgAsMenu;
00072     private JMenuItem saveSddAsMenu;
00073 
00074     private JMenuItem savePropMenu;
00075     private JMenuItem saveSdMenu;
00076     private JMenuItem saveObsMenu;
00077     private JMenuItem saveFdgMenu;
00078     private JMenuItem saveSddMenu;
00079     private JMenuItem saveAllMenu;
00080 
00081     private JMenuItem openPropMenu;
00082     private JMenuItem openSdMenu;
00083     private JMenuItem openObsMenu;
00084     private JMenuItem openFdgMenu;
00085     private JMenuItem openSddMenu;
00086     private JMenuItem reloadPropMenu;
00087     private JMenuItem reloadSdMenu;
00088     private JMenuItem reloadObsMenu;
00089     private JMenuItem reloadFdgMenu;
00090     private JMenuItem reloadSddMenu;
00091     private JMenuItem reloadAllMenu;
00092     private JMenuItem commentMenu;
00093     private JMenuItem uncommentMenu;
00094 
00095     private File lastDir;
00096     private File propFile;
00097     private File sdFile;
00098     private File obsFile;
00099     private File fdgFile;
00100     private File sddFile;
00101 
00102     private boolean supportDepFaults = false;
00103 
00104     private Object focusedComponent;
00105 
00106     private boolean mergeDEs = true;
00107 
00108     private MinHittingSetsFM hsFM = null;
00109     private MinHittingSets hs = null;
00110 
00111     private final static String[] COMMENT_PREFIXES = {"#", "*"};
00112     private final static char AUTO_COMMENT_PREFIX = '#';
00113 
00114     private final static String FDG_EDGE_STR = "=>";
00115 
00116     private final static String ASS_IF = "IF";
00117 
00118     private final static int TAB_LOG_MODEL = 0;
00119     private final static int TAB_FDG = 1;
00120 
00121     private final static String SAVE_PROP_MENU_TEXT = "Save Propositions";
00122     private final static String SAVE_SD_MENU_TEXT = "Save System Description";
00123     private final static String SAVE_OBS_MENU_TEXT = "Save Observations";
00124     private final static String SAVE_FDG_MENU_TEXT = "Save FDG";
00125     private final static String SAVE_SDD_MENU_TEXT = "Save System Dep. Description";
00126 
00127     private final static String RELOAD_PROP_MENU_TEXT = "Reload Propositions";
00128     private final static String RELOAD_SD_MENU_TEXT = "Reload System Description";
00129     private final static String RELOAD_OBS_MENU_TEXT = "Reload Observations";
00130     private final static String RELOAD_FDG_MENU_TEXT = "Reload FDG";
00131     private final static String RELOAD_SDD_MENU_TEXT = "Reload System Dep. Description";
00132 
00133     private final static String TITLE_RESULTS = "Results: ";
00134 
00135     private final static String PR_PROP_FILE = "proposition_file";
00136     private final static String PR_SD_FILE = "system_description_file";
00137     private final static String PR_OBS_FILE = "observations_file";
00138     private final static String PR_FDG_FILE = "fdg_file";
00139     private final static String PR_SDD_FILE = "system_dep_description_file";
00140 
00141     private final static String FILE_SETTINGS = ".GuiHittingSets";
00142 
00143     // command line settings
00144     private final static String CMD_SUPPORT_DEP_FAULTS  = "-df";
00145     private final static String CMD_MERGE_DES = "merge_des";
00146     private final static String OPT_YES = "yes";
00147     private final static String OPT_NO = "no";
00148 
00149 
00150     public static void main(final String[] args) {
00151  
00152         SwingUtilities.invokeLater(new Runnable() {
00153                 public void run() {
00154                     createAndShowGUI(args);
00155                 }
00156             });
00157         
00158     }
00159 
00160     private final static void createAndShowGUI(final String[] args) {
00161         
00162         // set look-and-feel
00163         try {
00164             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
00165         } catch (ClassNotFoundException e) {
00166             System.out.println(e.toString());
00167         } catch (InstantiationException e) {
00168             System.out.println(e.toString());
00169         } catch (IllegalAccessException e) {
00170             System.out.println(e.toString());
00171         } catch (UnsupportedLookAndFeelException e) {
00172             System.out.println(e.toString());
00173         }
00174 
00175         //Make sure we have nice window decorations.
00176         JFrame.setDefaultLookAndFeelDecorated(true);
00177         
00178         //Create and set up the window.
00179         JFrame frame = new JFrame("GuiHittingSets - a front-end to Reiter's HS algorithm");
00180         frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
00181 
00182         // create instance of this class
00183         GuiHittingSets app = new GuiHittingSets(frame, args);
00184         app.fillFrame();
00185         try {
00186             app.readSettings();
00187         } catch (IOException e) {
00188             System.out.println("On reading settings from file: " + e);
00189         }
00190         app.updateMenuStatus();
00191 
00192         //Display the window.
00193         frame.pack();
00194         frame.setVisible(true);
00195     }
00196 
00197     private GuiHittingSets(JFrame frame, String[] args) {
00198         this.frame = frame;
00199         frame.addWindowListener(this);
00200 
00201         parseCmdLine(args);
00202     }
00203 
00204     private void parseCmdLine(String[] args) {
00205         for (int i = 0; i < args.length; ++i) {
00206             
00207             String cmd = args[i];
00208             if (cmd.startsWith(CMD_MERGE_DES) && cmd.endsWith(OPT_NO)) mergeDEs = false;
00209             else if (cmd.equals(CMD_SUPPORT_DEP_FAULTS)) {
00210                 supportDepFaults = true;
00211             }
00212 
00213         }
00214     }
00215 
00216     private void setTextAreaAttributes(JTextArea textArea) {
00217         textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
00218         textArea.setTabSize(4);
00219     }
00220 
00221     private void updateMenuStatus() {
00222         if (propFile != null) {
00223             reloadPropMenu.setEnabled(true);
00224             reloadPropMenu.setText(RELOAD_PROP_MENU_TEXT + " (" + propFile.getName() + ")");
00225 
00226             savePropMenu.setEnabled(true);
00227             savePropMenu.setText(SAVE_PROP_MENU_TEXT + " (" + propFile.getName() + ")");
00228         }
00229         if (sdFile != null) {
00230             reloadSdMenu.setEnabled(true);
00231             reloadSdMenu.setText(RELOAD_SD_MENU_TEXT + " (" + sdFile.getName() + ")");
00232 
00233             saveSdMenu.setEnabled(true);
00234             saveSdMenu.setText(SAVE_SD_MENU_TEXT + " (" + sdFile.getName() + ")");
00235         }
00236         if (obsFile != null) {
00237             reloadObsMenu.setEnabled(true);
00238             reloadObsMenu.setText(RELOAD_OBS_MENU_TEXT + " (" + obsFile.getName() + ")");
00239 
00240             saveObsMenu.setEnabled(true);
00241             saveObsMenu.setText(SAVE_OBS_MENU_TEXT + " (" + obsFile.getName() + ")");
00242         }
00243         if (fdgFile != null) {
00244             reloadFdgMenu.setEnabled(true);
00245             reloadFdgMenu.setText(RELOAD_FDG_MENU_TEXT + " (" + fdgFile.getName() + ")");
00246 
00247             saveFdgMenu.setEnabled(true);
00248             saveFdgMenu.setText(SAVE_FDG_MENU_TEXT + " (" + fdgFile.getName() + ")");
00249         }
00250         if (sddFile != null) {
00251             reloadSddMenu.setEnabled(true);
00252             reloadSddMenu.setText(RELOAD_SDD_MENU_TEXT + " (" + sddFile.getName() + ")");
00253 
00254             saveSddMenu.setEnabled(true);
00255             saveSddMenu.setText(SAVE_SDD_MENU_TEXT + " (" + sddFile.getName() + ")");
00256         }
00257 
00258         if ((propFile != null) && (sdFile != null) && (obsFile != null)) {
00259 
00260             if (!supportDepFaults
00261                 ||
00262                 ((fdgFile != null) || (fdgText.getText().length() == 0))
00263                 && ((sddFile != null) || (sddText.getText().length() == 0))
00264                 ) {
00265                 
00266                 reloadAllMenu.setEnabled(true);
00267                 saveAllMenu.setEnabled(true);
00268             }
00269         }
00270     }
00271 
00272     private void createMenu() {
00273         JMenuBar menuBar = new JMenuBar();
00274 
00275         JMenu fileMenu = new JMenu("File");
00276         fileMenu.setMnemonic(KeyEvent.VK_F);
00277 
00278         openPropMenu = new JMenuItem("Open Proposition File...");
00279         openPropMenu.addActionListener(this);
00280 
00281         openSdMenu = new JMenuItem("Open System Description File...");
00282         openSdMenu.addActionListener(this);
00283         
00284         openObsMenu = new JMenuItem("Open Observations File...");
00285         openObsMenu.addActionListener(this);
00286 
00287         openFdgMenu = new JMenuItem("Open FDG...");
00288         openFdgMenu.addActionListener(this);
00289 
00290         openSddMenu = new JMenuItem("Open System Dep. Description File...");
00291         openSddMenu.addActionListener(this);
00292 
00293         reloadPropMenu = new JMenuItem(RELOAD_PROP_MENU_TEXT);
00294         reloadPropMenu.setEnabled(false);
00295         reloadPropMenu.addActionListener(this);
00296         reloadPropMenu.setAccelerator(KeyStroke
00297                                      .getKeyStroke('P', InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
00298 
00299         reloadSdMenu = new JMenuItem(RELOAD_SD_MENU_TEXT);
00300         reloadSdMenu.setEnabled(false);
00301         reloadSdMenu.addActionListener(this);
00302         reloadSdMenu.setAccelerator(KeyStroke
00303                                      .getKeyStroke('Y', InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
00304         
00305         reloadObsMenu = new JMenuItem(RELOAD_OBS_MENU_TEXT);
00306         reloadObsMenu.setEnabled(false);
00307         reloadObsMenu.addActionListener(this);
00308         reloadObsMenu.setAccelerator(KeyStroke
00309                                      .getKeyStroke('O', InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
00310 
00311         reloadFdgMenu = new JMenuItem(RELOAD_FDG_MENU_TEXT);
00312         reloadFdgMenu.setEnabled(false);
00313         reloadFdgMenu.addActionListener(this);
00314         reloadFdgMenu.setAccelerator(KeyStroke
00315                                      .getKeyStroke('F', InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
00316 
00317         reloadSddMenu = new JMenuItem(RELOAD_SDD_MENU_TEXT);
00318         reloadSddMenu.setEnabled(false);
00319         reloadSddMenu.addActionListener(this);
00320         reloadSddMenu.setAccelerator(KeyStroke
00321                                      .getKeyStroke('D', InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
00322 
00323         reloadAllMenu = new JMenuItem("Reload All");
00324         reloadAllMenu.setEnabled(false);
00325         reloadAllMenu.addActionListener(this);
00326         reloadAllMenu.setMnemonic(KeyEvent.VK_R);
00327         reloadAllMenu.setAccelerator(KeyStroke
00328                                      .getKeyStroke('R', InputEvent.CTRL_MASK));
00329 
00330         savePropAsMenu = new JMenuItem("Save Propositions as...");
00331         savePropAsMenu.addActionListener(this);
00332 
00333         saveSdAsMenu = new JMenuItem("Save System Description as...");
00334         saveSdAsMenu.addActionListener(this);
00335 
00336         saveObsAsMenu = new JMenuItem("Save Observations as...");
00337         saveObsAsMenu.addActionListener(this);
00338 
00339         saveFdgAsMenu = new JMenuItem("Safe FDG as...");
00340         saveFdgAsMenu.addActionListener(this);
00341 
00342         saveSddAsMenu = new JMenuItem("Save System Dep. Description as...");
00343         saveSddAsMenu.addActionListener(this);
00344 
00345         savePropMenu = new JMenuItem(SAVE_PROP_MENU_TEXT);
00346         savePropMenu.setEnabled(false);
00347         savePropMenu.setMnemonic(KeyEvent.VK_P);
00348         savePropMenu.setAccelerator(KeyStroke
00349                                     .getKeyStroke('P', InputEvent.CTRL_MASK));
00350         savePropMenu.addActionListener(this);
00351 
00352         saveFdgMenu = new JMenuItem(SAVE_FDG_MENU_TEXT);
00353         saveFdgMenu.setEnabled(false);
00354         saveFdgMenu.setMnemonic(KeyEvent.VK_F);
00355         saveFdgMenu.setAccelerator(KeyStroke
00356                                    .getKeyStroke('F', InputEvent.CTRL_MASK));
00357         saveFdgMenu.addActionListener(this);
00358 
00359         saveSdMenu = new JMenuItem(SAVE_SD_MENU_TEXT);
00360         saveSdMenu.setEnabled(false);
00361         saveSdMenu.setMnemonic(KeyEvent.VK_Y);
00362         saveSdMenu.setAccelerator(KeyStroke
00363                                     .getKeyStroke('Y', InputEvent.CTRL_MASK));
00364         saveSdMenu.addActionListener(this);
00365 
00366         saveSddMenu = new JMenuItem(SAVE_SDD_MENU_TEXT);
00367         saveSddMenu.setEnabled(false);
00368         saveSddMenu.setMnemonic(KeyEvent.VK_D);
00369         saveSddMenu.setAccelerator(KeyStroke
00370                                     .getKeyStroke('D', InputEvent.CTRL_MASK));
00371         saveSddMenu.addActionListener(this);
00372 
00373         saveObsMenu = new JMenuItem(SAVE_OBS_MENU_TEXT);
00374         saveObsMenu.setEnabled(false);
00375         saveObsMenu.setMnemonic(KeyEvent.VK_O);
00376         saveObsMenu.setAccelerator(KeyStroke
00377                                     .getKeyStroke('O', InputEvent.CTRL_MASK));
00378         saveObsMenu.addActionListener(this);
00379 
00380         saveAllMenu = new JMenuItem("Save All");
00381         saveAllMenu.setEnabled(false);
00382         saveAllMenu.setMnemonic(KeyEvent.VK_S);
00383         saveAllMenu.setAccelerator(KeyStroke
00384                                     .getKeyStroke('S', InputEvent.CTRL_MASK));
00385         saveAllMenu.addActionListener(this);
00386 
00387 
00388         fileMenu.add(openPropMenu);
00389         fileMenu.add(openSdMenu);
00390         fileMenu.add(openObsMenu);
00391         if (supportDepFaults) {
00392             fileMenu.add(openFdgMenu);
00393             fileMenu.add(openSddMenu);
00394         }
00395 
00396         fileMenu.addSeparator();
00397 
00398         fileMenu.add(reloadPropMenu);
00399         fileMenu.add(reloadSdMenu);
00400         fileMenu.add(reloadObsMenu);
00401         if (supportDepFaults) {
00402             fileMenu.add(reloadFdgMenu);
00403             fileMenu.add(reloadSddMenu);
00404         }
00405         fileMenu.add(reloadAllMenu);
00406 
00407         fileMenu.addSeparator();
00408 
00409         fileMenu.add(savePropAsMenu);
00410         fileMenu.add(saveSdAsMenu);
00411         fileMenu.add(saveObsAsMenu);
00412         if (supportDepFaults) {
00413             fileMenu.add(saveFdgAsMenu);
00414             fileMenu.add(saveSddAsMenu);
00415         }
00416 
00417         fileMenu.addSeparator();
00418 
00419         fileMenu.add(savePropMenu);
00420         fileMenu.add(saveSdMenu);
00421         fileMenu.add(saveObsMenu);
00422         if (supportDepFaults) {
00423             fileMenu.add(saveFdgMenu);
00424             fileMenu.add(saveSddMenu);
00425         }
00426         fileMenu.add(saveAllMenu);
00427         
00428         JMenu editMenu = new JMenu("Edit");
00429 
00430         /*
00431         commentMenu = new JMenuItem("Comment");
00432         commentMenu.addActionListener(this);
00433         commentMenu.setAccelerator(KeyStroke
00434                                     .getKeyStroke('D', InputEvent.CTRL_MASK));
00435         commentMenu.addActionListener(this);
00436 
00437         uncommentMenu = new JMenuItem("Uncomment");
00438         uncommentMenu.setAccelerator(KeyStroke
00439                                     .getKeyStroke('D', InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK));
00440         uncommentMenu.addActionListener(this);
00441 
00442         editMenu.add(commentMenu);
00443         editMenu.add(uncommentMenu);
00444         */
00445 
00446         menuBar.add(fileMenu);
00447         menuBar.add(editMenu);
00448         editMenu.setVisible(false);
00449         frame.setJMenuBar(menuBar);
00450     
00451     }  // createMenu
00452 
00453     private void readSettings() throws IOException {
00454         Properties prs = new Properties();
00455         File f = new File(FILE_SETTINGS);
00456         
00457         if (f.exists()) {
00458             FileInputStream stream = new FileInputStream(f);
00459             try {
00460                 prs.load(stream);
00461 
00462                 String s = prs.getProperty(PR_PROP_FILE);
00463                 if (s != null) {
00464                     propFile = new File(s);
00465                     try {
00466                         readFile(propText, propFile);
00467                     } catch(IOException e) {
00468                         propFile = null;
00469                     }
00470                 }
00471                 
00472                 s = prs.getProperty(PR_SD_FILE);
00473                 if (s != null) {
00474                     sdFile = new File(s);
00475                     try {
00476                         readFile(sdText, sdFile);
00477                     } catch(IOException e) {
00478                         sdFile = null;
00479                     }
00480                 }
00481 
00482                 s = prs.getProperty(PR_OBS_FILE);
00483                 if (s != null) {
00484                     obsFile = new File(s);
00485                     try {
00486                         readFile(obsText, obsFile);
00487                     } catch(IOException e) {
00488                         obsFile = null;
00489                     }
00490                 }
00491                 
00492                 s = prs.getProperty(PR_FDG_FILE);
00493                 if (s != null) {
00494                     fdgFile = new File(s);
00495                     try {
00496                         readFile(fdgText, fdgFile);
00497                     } catch(IOException e) {
00498                         fdgFile = null;
00499                     }
00500                 }
00501 
00502                 s = prs.getProperty(PR_SDD_FILE);
00503                 if (s != null) {
00504                     sddFile = new File(s);
00505                     try {
00506                         readFile(sddText, sddFile);
00507                     } catch(IOException e) {
00508                         sddFile = null;
00509                     }
00510                 }
00511 
00512             } finally {
00513                 stream.close();
00514             }
00515         }
00516 
00517     }  // readSettings
00518 
00519     private void writeSettings() throws IOException {
00520         
00521         Properties prs = new Properties();
00522         
00523         if (propFile != null) prs.put(PR_PROP_FILE, propFile.getCanonicalPath());
00524         if (sdFile != null) prs.put(PR_SD_FILE, sdFile.getCanonicalPath());
00525         if (obsFile != null) prs.put(PR_OBS_FILE, obsFile.getCanonicalPath());
00526         if (fdgFile != null) prs.put(PR_FDG_FILE, fdgFile.getCanonicalPath());
00527         if (sddFile != null) prs.put(PR_SDD_FILE, sddFile.getCanonicalPath());
00528 
00529         FileOutputStream stream = new FileOutputStream(FILE_SETTINGS);
00530         try {
00531             prs.store(stream, null);
00532         } catch (IOException err) {
00533             System.out.println("Error on writing settings");
00534             throw err;
00535         } finally {
00536             stream.close();
00537         }
00538     }
00539 
00540     private void saveText(JTextArea textArea, File file) throws IOException {
00541         FileWriter fw = null;
00542         try {
00543             fw = new FileWriter(file);
00544             fw.write(textArea.getText());
00545         } finally {
00546             fw.close();
00547         }
00548     }
00549 
00550     private void readFile(JTextArea textArea, File file) throws IOException {
00551         FileReader fr = new FileReader(file);
00552         try {
00553 
00554             StringBuffer s = new StringBuffer();
00555             boolean eof = false;
00556             
00557             while (!eof) {
00558                 char[] buf = new char[1024];
00559                 int res = fr.read(buf, 0, buf.length);
00560                 if (res == -1) {
00561                     eof = true;
00562                 } else {
00563                     eof = false;
00564                     s.append(buf, 0, res);
00565                 }
00566             }
00567             
00568             textArea.setText(s.toString());
00569 
00570         } finally {
00571             fr.close();
00572         }
00573     }
00574 
00575     private File showFileSaveAsDialog(String title) {
00576         JFileChooser fc;
00577         if (lastDir == null) fc = new JFileChooser();
00578         else fc = new JFileChooser(lastDir);
00579         fc.setDialogTitle("Save " + title + " to file..");
00580 
00581         while (true) {
00582 
00583             int returnVal = fc.showSaveDialog(frame);
00584             
00585             if (returnVal == JFileChooser.APPROVE_OPTION) {
00586                 File f = fc.getSelectedFile();
00587                 if (f.exists()) {
00588                     int opt = JOptionPane
00589                         .showOptionDialog(frame,
00590                                           "File already exists. Overwrite?", "?", 
00591                                           JOptionPane.YES_NO_OPTION,
00592                                           JOptionPane.QUESTION_MESSAGE, null, null, null);
00593                     if (opt == JOptionPane.YES_OPTION) {
00594                         return f;
00595                     }
00596                 } else {
00597                     return f;
00598                 }
00599                 
00600             } else return null;
00601         }
00602     }
00603 
00604     private File showFileOpenDialog(String title) {
00605 
00606         JFileChooser fc;
00607         if (lastDir == null) fc = new JFileChooser();
00608         else fc = new JFileChooser(lastDir);
00609         
00610         fc.setDialogTitle("Read " + title + " from file..");
00611 
00612         int returnVal = fc.showOpenDialog(frame);
00613         if (returnVal == JFileChooser.APPROVE_OPTION) {
00614             File f = fc.getSelectedFile();
00615             return f;
00616         } else return null;
00617     }
00618 
00619     private void savePropAs() throws IOException {
00620         File file = showFileSaveAsDialog("Propositions");
00621         if (file != null) {
00622             saveText(propText, file);
00623             propFile = file;
00624             extractLastDir(file);
00625         }
00626     }
00627 
00628     private void saveSdAs() throws IOException {
00629         File file = showFileSaveAsDialog("System Description");
00630         if (file != null) {
00631             saveText(sdText, file);
00632             sdFile = file;
00633             extractLastDir(file);
00634         }
00635     }
00636 
00637     private void saveObsAs() throws IOException {
00638         File file = showFileSaveAsDialog("Observations");
00639         if (file != null) {
00640             saveText(obsText, file);
00641             obsFile = file;
00642             extractLastDir(file);
00643         }
00644     }
00645 
00646     private void saveFdgAs() throws IOException {
00647         File file = showFileSaveAsDialog("Failure Dependency Graph");
00648         if (file != null) {
00649             saveText(fdgText, file);
00650             fdgFile = file;
00651             extractLastDir(file);
00652         }
00653     }
00654 
00655     private void saveSddAs() throws IOException {
00656         File file = showFileSaveAsDialog("System Dependency Description");
00657         if (file != null) {
00658             saveText(sddText, file);
00659             sddFile = file;
00660             extractLastDir(file);
00661         }
00662     }
00663 
00664     private void saveProp() throws IOException {
00665         saveText(propText, propFile);
00666     }
00667 
00668     private void saveSd() throws IOException {
00669         saveText(sdText, sdFile);
00670     }
00671 
00672     private void saveObs() throws IOException {
00673         saveText(obsText, obsFile);
00674     }
00675 
00676     private void saveFdg() throws IOException {
00677         saveText(fdgText, fdgFile);
00678     }
00679 
00680     private void saveSdd() throws IOException {
00681         saveText(sddText, sddFile);
00682     }
00683 
00684     private void saveAll() throws IOException {
00685         if (propFile != null) saveText(propText, propFile);
00686         if (sdFile != null) saveText(sdText, sdFile);
00687         if (obsFile != null) saveText(obsText, obsFile);
00688         if (fdgFile != null) saveText(fdgText, fdgFile);
00689         if (sddFile != null) saveText(sddText, sddFile);
00690     }
00691 
00692     private void reloadAll() throws IOException {
00693         if (propFile != null) readFile(propText, propFile); 
00694         if (sdFile != null) readFile(sdText, sdFile); 
00695         if (obsFile != null) readFile(obsText, obsFile); 
00696         if (fdgFile != null) readFile(fdgText, fdgFile);
00697         if (sddFile != null) readFile(sddText, sddFile);
00698     }
00699 
00700     private void readPropFile() throws IOException {
00701         File file = showFileOpenDialog("Propositions");
00702         if (file != null) {
00703             readFile(propText, file);
00704             propFile = file;
00705             extractLastDir(file);
00706         }
00707     }
00708 
00709     private void readSdFile() throws IOException {
00710         File file = showFileOpenDialog("System Description");
00711         if (file != null) {
00712             readFile(sdText, file);
00713             sdFile = file;
00714             extractLastDir(file);
00715         }
00716     }
00717 
00718     private void readObsFile() throws IOException {
00719         File file = showFileOpenDialog("Observations");
00720         if (file != null) {
00721             readFile(obsText, file);
00722             obsFile = file;
00723             extractLastDir(file);
00724         }
00725     }
00726 
00727     private void readFdgFile() throws IOException {
00728         File file = showFileOpenDialog("Failure Dependency Graph");
00729         if (file != null) {
00730             readFile(fdgText, file);
00731             fdgFile = file;
00732             extractLastDir(file);
00733         }
00734     }
00735 
00736     private void readSddFile() throws IOException {
00737         File file = showFileOpenDialog("System Dependency Description");
00738         if (file != null) {
00739             readFile(sddText, file);
00740             sddFile = file;
00741             extractLastDir(file);
00742         }
00743     }
00744 
00745     private void extractLastDir(File file) {
00746         lastDir = file.getParentFile();
00747     }
00748 
00749     private void fillSettingsPanel(JPanel settingsPanel) {
00750    
00751         JPanel explSettingsPanel = new JPanel();
00752         explSettingsPanel.setBorder(BorderFactory.createTitledBorder("Explanations"));
00753         explSettingsPanel.setLayout(new SpringLayout());
00754         
00755         JLabel explSizeLabel = new JLabel("max. size:");
00756         explSettingsPanel.add(explSizeLabel);
00757         SpinnerModel explSizeModel = new SpinnerNumberModel(10, 1, 100, 1);
00758         explSizeSpinner = new JSpinner(explSizeModel);
00759         explSizeLabel.setLabelFor(explSizeSpinner);
00760         explSettingsPanel.add(explSizeSpinner);
00761 
00762         JLabel numExplLabel = new JLabel("max. number:");
00763         explSettingsPanel.add(numExplLabel);
00764         numExplText = new JTextField("100");
00765         numExplLabel.setLabelFor(numExplText);
00766         explSettingsPanel.add(numExplText);
00767 
00768         SpringUtilities.makeCompactGrid(explSettingsPanel, 2, 2, 3, 3, 3, 3);
00769         settingsPanel.add(explSettingsPanel);
00770 
00771         JPanel fmSettingsPanel = new JPanel();
00772         fmSettingsPanel.setBorder(BorderFactory.createTitledBorder("Fault modes"));
00773         fmSettingsPanel.setLayout(new BoxLayout(fmSettingsPanel, BoxLayout.Y_AXIS));
00774         
00775         useFaultModelsCB = new JCheckBox("use fault models", true);
00776         useFaultModelsCB.addItemListener(this);
00777         Box useFaultModelsBox = new Box(BoxLayout.X_AXIS);
00778         useFaultModelsBox.add(useFaultModelsCB);
00779         useFaultModelsBox.add(Box.createGlue());
00780         fmSettingsPanel.add(useFaultModelsBox);
00781         
00782         if (supportDepFaults) {
00783             depFaultsCB = new JCheckBox("dependent faults", true);
00784             depFaultsCB.addItemListener(this);
00785         
00786             Box depFaultsBox = new Box(BoxLayout.X_AXIS);
00787             depFaultsBox.add(depFaultsCB);
00788             depFaultsBox.add(Box.createGlue());
00789             fmSettingsPanel.add(depFaultsBox);
00790         }
00791 
00792         /*
00793           useProbabilitiesCB = new JCheckBox("use prob.", false);
00794         useProbabilitiesCB.addItemListener(this);
00795         Box useProbBox = new Box(BoxLayout.X_AXIS);
00796         useProbBox.add(useProbabilitiesCB);
00797         useProbBox.add(Box.createGlue());
00798         fmSettingsPanel.add(useProbBox);
00799         */
00800 
00801         JPanel assSettingsPanel = new JPanel();
00802         assSettingsPanel.setLayout(new SpringLayout());
00803         abLabel = new JLabel("AB assumpt.:");
00804         assSettingsPanel.add(abLabel);
00805         assABText = new JTextField("AB");
00806         abLabel.setLabelFor(assABText);
00807         assSettingsPanel.add(assABText);
00808         nabLabel = new JLabel("-AB assumpt.:");
00809         assSettingsPanel.add(nabLabel);
00810         assNABText = new JTextField("NAB");
00811         nabLabel.setLabelFor(assNABText);
00812         assSettingsPanel.add(assNABText);
00813         
00814         if (supportDepFaults) {
00815             dfLabel = new JLabel("DF assumpt.:");
00816             dfLabel.setEnabled(true);
00817             assSettingsPanel.add(dfLabel);
00818             assDFText = new JTextField("DF");
00819             assDFText.setEditable(true);
00820             assSettingsPanel.add(assDFText);
00821         }
00822 
00823         if (supportDepFaults) SpringUtilities.makeCompactGrid(assSettingsPanel, 3, 2, 3, 3, 3, 3);
00824         else SpringUtilities.makeCompactGrid(assSettingsPanel, 2, 2, 3, 3, 3, 3);
00825         fmSettingsPanel.add(assSettingsPanel);
00826         settingsPanel.add(fmSettingsPanel);
00827 
00828         JPanel propSettingsPanel = new JPanel();
00829         propSettingsPanel.setBorder(BorderFactory.createTitledBorder("Propositions"));
00830         propSettingsPanel.setLayout(new SpringLayout());
00831         JLabel negPrefixLabel = new JLabel("negation prefix:");
00832         propSettingsPanel.add(negPrefixLabel);
00833         negPrefixText = new JTextField("n_");
00834         negPrefixLabel.setLabelFor(negPrefixText);
00835         propSettingsPanel.add(negPrefixText);        
00836         SpringUtilities.makeCompactGrid(propSettingsPanel, 1, 2, 3, 3, 3, 3);
00837 
00838         settingsPanel.add(propSettingsPanel);
00839 
00840         if (supportDepFaults) {
00841             dfSettingsPanel = new JPanel();
00842             dfSettingsPanel.setBorder(BorderFactory.createTitledBorder("Dependent faults"));
00843             dfSettingsPanel.setLayout(new SpringLayout());
00844             
00845             betaEnvCB = new JCheckBox("compute BETA env.", true);
00846             betaEnvCB.addItemListener(this);
00847             Box betaEnvBox = new Box(BoxLayout.X_AXIS);
00848             betaEnvBox.add(betaEnvCB);
00849             betaEnvBox.add(Box.createGlue());
00850             dfSettingsPanel.add(betaEnvBox);
00851             
00852             mergeDEsCB = new JCheckBox("merge DEs", true);
00853             mergeDEsCB.addItemListener(this);
00854             Box mergeDEsBox = new Box(BoxLayout.X_AXIS);
00855             mergeDEsBox.add(mergeDEsCB);
00856             mergeDEsBox.add(Box.createGlue());
00857             dfSettingsPanel.add(mergeDEsBox);
00858             
00859             discardOrderPermsCB = new JCheckBox("no order perms.", false);
00860             discardOrderPermsCB.addItemListener(this);
00861             Box discardOrderPermsBox = new Box(BoxLayout.X_AXIS);
00862             discardOrderPermsBox.add(discardOrderPermsCB);
00863             discardOrderPermsBox.add(Box.createGlue());
00864             dfSettingsPanel.add(discardOrderPermsBox);
00865 
00866             Box maxDFChainBox = new Box(BoxLayout.X_AXIS);
00867             
00868             JLabel maxDFChainLabel = new JLabel("max. DF:");
00869             maxDFChainBox.add(maxDFChainLabel);
00870             maxDFChainBox.add(Box.createGlue());
00871             SpinnerModel maxDFChainModel = new SpinnerNumberModel(10, 1, 1000, 1);
00872             maxDFChainSpinner = new JSpinner(maxDFChainModel);
00873             maxDFChainBox.add(maxDFChainSpinner);
00874             maxDFChainLabel.setLabelFor(maxDFChainSpinner);
00875             dfSettingsPanel.add(maxDFChainBox);
00876             
00877             SpringUtilities.makeCompactGrid(dfSettingsPanel, 4, 1, 3, 3, 3, 3);
00878             
00879             settingsPanel.add(dfSettingsPanel);
00880         }
00881 
00882     }  // fillSettingsPanel()
00883 
00884     private void fillActionsPanel(JPanel actionsPanel) {
00885 
00886         actionsPanel.setLayout(new SpringLayout());
00887 
00888         /*
00889         JPanel clearPanel = new JPanel();
00890         clearPanel.setLayout(new GridLayout(3, 1, 3, 3));
00891         
00892         clearPropBtn = new JButton("Clear propositions");
00893         clearPropBtn.addActionListener(this);
00894         clearPanel.add(clearPropBtn);
00895 
00896         clearSdBtn = new JButton("Clear System Descr.");
00897         clearSdBtn.addActionListener(this);
00898         clearPanel.add(clearSdBtn);
00899 
00900         clearObsBtn = new JButton("Clear Observations");
00901         clearObsBtn.addActionListener(this);
00902         clearPanel.add(clearObsBtn);
00903 
00904         actionsPanel.add(clearPanel);
00905         */
00906 
00907         JPanel commentPanel = new JPanel();
00908         commentPanel.setLayout(new GridLayout(2, 1, 3, 3));
00909 
00910         commentBtn = new JButton("Comment");
00911         commentBtn.addActionListener(this);
00912         commentPanel.add(commentBtn);
00913 
00914         uncommentBtn = new JButton("Uncomment");
00915         uncommentBtn.addActionListener(this);
00916         commentPanel.add(uncommentBtn);
00917         actionsPanel.add(commentPanel);
00918 
00919         JPanel searchPanel = new JPanel();
00920         
00921         searchPanel.setLayout(new GridLayout(3, 1, 5, 5));
00922         searchPanel.setBorder(BorderFactory.createTitledBorder("Search"));
00923         
00924         searchText = new JTextField();
00925         searchPanel.add(searchText);
00926 
00927         searchFirstBtn = new JButton("First");
00928         searchFirstBtn.addActionListener(this);
00929         searchNextBtn = new JButton("Next");
00930         searchNextBtn.addActionListener(this);
00931 
00932         Box searchBtnBox = new Box(BoxLayout.X_AXIS);
00933         searchBtnBox.add(searchFirstBtn);
00934         searchBtnBox.add(Box.createGlue());
00935         searchBtnBox.add(searchNextBtn);
00936         searchPanel.add(searchBtnBox);
00937 
00938         searchResultLabel = new JLabel("");
00939         searchPanel.add(searchResultLabel);
00940 
00941         actionsPanel.add(searchPanel);
00942 
00943         SpringUtilities.makeCompactGrid(actionsPanel, 2, 1, 10, 10, 10, 10);
00944 
00945     }  // fillActionsPanel()
00946 
00947     private void fillDepFaultPanel(JPanel depFaultPanel) {        
00948 
00949         depFaultPanel.setLayout(new BorderLayout());
00950 
00951         JPanel fdgPanel = new JPanel();
00952         JPanel fdgBgPanel = new JPanel();
00953         fdgBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
00954         fdgBgPanel.setLayout(new BorderLayout());
00955         fdgBgPanel.add(fdgPanel, BorderLayout.CENTER);
00956         fdgPanel.setBorder(BorderFactory.createTitledBorder("Failure Dependency Graph"));
00957         fdgPanel.setLayout(new BorderLayout());
00958         fdgText = new JTextArea(15, 40);
00959         setTextAreaAttributes(fdgText);
00960         fdgText.addFocusListener(this);
00961         JScrollPane fdgTextScrollPane = new JScrollPane(fdgText,
00962                                                         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
00963                                                         JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
00964         fdgPanel.add(fdgTextScrollPane, BorderLayout.CENTER);
00965 
00966         JPanel sddPanel = new JPanel();
00967         JPanel sddBgPanel = new JPanel();
00968         sddBgPanel.setLayout(new BorderLayout());
00969         sddBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
00970         sddBgPanel.add(sddPanel, BorderLayout.CENTER);
00971         sddPanel.setBorder(BorderFactory.createTitledBorder("System Dependency Description"));
00972         sddPanel.setLayout(new BorderLayout());
00973         sddText = new JTextArea(15, 40);
00974         setTextAreaAttributes(sddText);
00975         sddText.addFocusListener(this);
00976         JScrollPane sddTextScrollPane  = new JScrollPane(sddText,
00977                                                          JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
00978                                                          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
00979         sddPanel.add(sddTextScrollPane, BorderLayout.CENTER);
00980 
00981         JSplitPane depFaultSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, fdgBgPanel, 
00982                                                       sddBgPanel);
00983         depFaultPanel.add(depFaultSplitPane, BorderLayout.CENTER);
00984     }
00985 
00986     private void fillFrame() {
00987          
00988         JPanel settingsPanel = new JPanel();        
00989         settingsPanel.setBorder(BorderFactory.createTitledBorder("Settings"));
00990         settingsPanel.setLayout(new SpringLayout());
00991         fillSettingsPanel(settingsPanel);
00992 
00993         if (supportDepFaults) SpringUtilities.makeCompactGrid(settingsPanel, 4, 1, 5, 5, 5, 5);
00994         else SpringUtilities.makeCompactGrid(settingsPanel, 3, 1, 5, 5, 5, 5);
00995         
00996         JPanel settingsBgPanel = new JPanel();
00997         settingsBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
00998         settingsBgPanel.setLayout(new BorderLayout());
00999         settingsBgPanel.add(settingsPanel, BorderLayout.NORTH);
01000   
01001         JPanel actionsPanel = new JPanel();
01002         actionsPanel.setBorder(BorderFactory.createTitledBorder("Edit Actions"));
01003         fillActionsPanel(actionsPanel);
01004 
01005         settingsBgPanel.add(actionsPanel, BorderLayout.SOUTH);
01006         
01007         JPanel propBgPanel = new JPanel();
01008         propBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
01009         propBgPanel.setLayout(new BorderLayout());
01010         JPanel propPanel = new JPanel();
01011         propBgPanel.add(propPanel, BorderLayout.CENTER);
01012         propPanel.setBorder(BorderFactory.createTitledBorder("Propositions")); 
01013         propPanel.setLayout(new BorderLayout());
01014         propText = new JTextArea(30, 30);
01015         setTextAreaAttributes(propText);      
01016         propText.addFocusListener(this);
01017         JScrollPane propScrollPane = new JScrollPane(propText,
01018                                                      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
01019                                                      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
01020         propPanel.add(propScrollPane, BorderLayout.CENTER);
01021 
01022         JPanel sdBgPanel = new JPanel();
01023         sdBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
01024         sdBgPanel.setLayout(new BorderLayout());
01025         JPanel sdPanel = new JPanel();
01026         sdBgPanel.add(sdPanel, BorderLayout.CENTER);
01027         sdPanel.setBorder(BorderFactory.createTitledBorder("System Description"));
01028         sdPanel.setLayout(new BorderLayout());
01029         sdText = new JTextArea(22, 70);        
01030         setTextAreaAttributes(sdText);
01031         sdText.addFocusListener(this);
01032         JScrollPane sdScrollPane = new JScrollPane(sdText,
01033                                                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
01034                                                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
01035         sdPanel.add(sdScrollPane, BorderLayout.CENTER);
01036 
01037         JPanel obsBgPanel = new JPanel();
01038         obsBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
01039         obsBgPanel.setLayout(new BorderLayout());
01040         JPanel obsPanel = new JPanel();
01041         obsBgPanel.add(obsPanel, BorderLayout.CENTER);
01042         obsPanel.setBorder(BorderFactory.createTitledBorder("Observations"));
01043         obsPanel.setLayout(new BorderLayout());
01044         obsText = new JTextArea(8, 70);        
01045         setTextAreaAttributes(obsText);
01046         obsText.addFocusListener(this);
01047         JScrollPane obsScrollPane = new JScrollPane(obsText,
01048                                                     JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
01049                                                     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
01050         obsPanel.add(obsScrollPane, BorderLayout.CENTER);
01051 
01052         JSplitPane sdObsSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, sdBgPanel, obsBgPanel);
01053         //sdObsSplitPane.setDividerLocation(0.8);
01054         JSplitPane propSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, propBgPanel, sdObsSplitPane);
01055         propSplitPane.setDividerLocation(0.8);
01056         JPanel propSplitPaneBgPanel = new JPanel();
01057         propSplitPaneBgPanel.setLayout(new BorderLayout());
01058         propSplitPaneBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
01059         propSplitPaneBgPanel.add(propSplitPane, BorderLayout.CENTER);
01060 
01061         JPanel depFaultBgPanel = new JPanel();
01062         depFaultBgPanel.setLayout(new BorderLayout());
01063         depFaultBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
01064         JPanel depFaultPanel = new JPanel();
01065         fillDepFaultPanel(depFaultPanel);
01066         depFaultBgPanel.add(depFaultPanel, BorderLayout.CENTER);
01067 
01068         tabbedPane = new JTabbedPane();
01069         tabbedPane.addTab("Logical Model", null, propSplitPaneBgPanel);
01070         if (supportDepFaults) {
01071             tabbedPane.addTab("Dependent Fault Models", null, depFaultBgPanel);
01072         }
01073 
01074         JPanel topPanel = new JPanel();
01075         topPanel.setLayout(new BorderLayout());
01076         topPanel.add(settingsBgPanel, BorderLayout.WEST);
01077         topPanel.add(tabbedPane, BorderLayout.CENTER);
01078 
01079         JPanel resultButtonBgPanel = new JPanel();
01080         resultButtonBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
01081         resultButtonBgPanel.setLayout(new BorderLayout());
01082         JPanel resultButtonPanel = new JPanel();
01083         resultButtonBgPanel.add(resultButtonPanel, BorderLayout.CENTER);
01084         resultButtonPanel.setBorder(BorderFactory.createEmptyBorder());
01085         resultButtonPanel.setLayout(new GridLayout(4, 1, 10, 10));
01086         checkConsBtn = new JButton("Check Consistency");
01087         checkConsBtn.addActionListener(this);
01088         resultButtonPanel.add(checkConsBtn);
01089         computeMinHSBtn = new JButton("Compute Min. HS");
01090         computeMinHSBtn.addActionListener(this);
01091         resultButtonPanel.add(computeMinHSBtn);
01092         computeMoreBtn = new JButton("Compute more HS");
01093         computeMoreBtn.addActionListener(this);
01094         computeMoreBtn.setEnabled(false);
01095         resultButtonPanel.add(computeMoreBtn);
01096         
01097         if (supportDepFaults) {
01098             computeMinDiagEnvBtn = new JButton("Compute Min. DEs");
01099             computeMinDiagEnvBtn.addActionListener(this);
01100             resultButtonPanel.add(computeMinDiagEnvBtn);
01101         }
01102 
01103         JPanel resultBgPanel = new JPanel();
01104         resultBgPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
01105         resultBgPanel.setLayout(new BorderLayout());
01106         resultsPanel = new JPanel();
01107         resultBgPanel.add(resultsPanel, BorderLayout.CENTER);
01108         resultsBorder = BorderFactory.createTitledBorder(TITLE_RESULTS);
01109         resultsPanel.setBorder(resultsBorder);
01110         resultsPanel.setLayout(new GridLayout(1, 2, 10, 10));
01111 
01112         fillResultPanel(resultsPanel);
01113 
01114         JPanel bottomPanel = new JPanel();
01115         bottomPanel.setLayout(new BorderLayout());
01116         bottomPanel.add(resultButtonBgPanel, BorderLayout.WEST);
01117         bottomPanel.add(resultBgPanel, BorderLayout.CENTER);
01118 
01119         JSplitPane mainSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel);
01120         //mainSplitPane.setDividerLocation(0.8);
01121         frame.getContentPane().add(mainSplitPane);
01122 
01123         createMenu();
01124 
01125     }  // fillFrame()
01126 
01127     void fillResultPanel(JPanel resultsPanel) {
01128 
01129         results1Text = new JTextArea(8, 10);
01130         setTextAreaAttributes(results1Text);
01131         results1Text.setEditable(false);
01132         JScrollPane res1ScrollPane = new JScrollPane(results1Text,
01133                                                      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
01134                                                      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
01135         resultsPanel.add(res1ScrollPane, BorderLayout.WEST);
01136         
01137         results2Text = new JTextArea(8, 10);
01138         setTextAreaAttributes(results2Text);
01139         results2Text.setEditable(false);
01140         JScrollPane res2ScrollPane = new JScrollPane(results2Text,
01141                                                      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
01142                                                      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
01143         resultsPanel.add(res2ScrollPane, BorderLayout.CENTER);
01144 
01145     }
01146 
01147     /* 
01148      * ignoredAss: list of Strings; contains assumption names. Sentences containing
01149      * one of these assumptions are NOT parsed!
01150      */
01151     protected LSentence parseSD() throws ParseError, IllegalUserInput {
01152 
01153         LogicParser parser = new LogicParser();
01154         LSentence allRules = new LSentence();
01155 
01156         generatePropNegationAxioms(propText, parser, allRules);
01157         parseLogSentences(sdText, parser, allRules);
01158   
01159         return allRules;
01160     }
01161 
01162     protected LSentence parseOBS() throws ParseError, IllegalUserInput {
01163         LogicParser parser = new LogicParser();
01164         LSentence allRules = new LSentence();
01165 
01166         parseLogSentences(obsText, parser, allRules);
01167 
01168         return allRules;
01169     }
01170 
01171     protected LSentence parseSDD() throws ParseError, IllegalUserInput {
01172         LogicParser parser = new LogicParser();
01173         LSentence allRules = new LSentence();
01174 
01175         parseLogSentences(sddText, parser, allRules);
01176 
01177         return allRules;
01178     }
01179 
01180     protected String readAssAB() {
01181         String res = assABText.getText().trim();
01182         if ((res.length() == 0) || (Character.isLowerCase(res.charAt(0)))) {
01183             return null;
01184         } else return res;
01185     }
01186 
01187     protected String readAssNAB() {
01188         String res = assNABText.getText().trim();
01189         if ((res.length() == 0) || (Character.isLowerCase(res.charAt(0)))) {
01190             return null;
01191         } else return res;
01192     }
01193 
01194     protected String readAssDF() {
01195         String res = assDFText.getText().trim();
01196         if ((res.length() == 0) || (Character.isLowerCase(res.charAt(0)))) {
01197             return null;
01198         } else return res;
01199     }
01200 
01201     protected boolean checkConsistency(LSentence allRules) throws ParseError,
01202         IllegalUserInput {
01203         
01204         // prepare theorem prover, then check consistency
01205 
01206         ABTheoremProver theoremProver = new ABTheoremProver();
01207         theoremProver = allRules.asABPropositionalSentence(theoremProver);
01208         if (theoremProver == null) {
01209             throw new ParseError("[unknown]");
01210         }
01211 
01212         boolean consistent;
01213 
01214         if (useFaultModelsCB.isSelected()) {
01215             String assAB = readAssAB();
01216             String assNAB = readAssNAB();
01217 
01218             if (assAB == null) {
01219                 throw new IllegalUserInput("Invalid AB assumption defined!");
01220             } else if (assNAB == null) {
01221                 throw new IllegalUserInput("Invalid NAB assumption defined!");
01222             }
01223 
01224             ArrayList posAssPrefixes = new ArrayList();
01225             posAssPrefixes.add(assNAB);
01226             consistent = theoremProver.checkConsistency(posAssPrefixes);
01227         } else {
01228             consistent = theoremProver.checkConsistency();
01229         }
01230 
01231         // print conflict set to console
01232 
01233         ArrayList conflict = theoremProver.contradiction().collectAssumptions();
01234         printConflictSet(conflict);
01235 
01236         return consistent;
01237     }
01238 
01239     protected void printConflictSet(ArrayList conflict) {        
01240 
01241         String cs = "";
01242         Iterator itC = conflict.iterator();
01243         while (itC.hasNext()) {
01244             Assumption a = (Assumption)itC.next();
01245             if (cs.length() == 0) cs += "-";
01246             else cs += " \\/ -";
01247             cs += (String)a.identifier;
01248         }
01249 
01250         System.out.println("\n\nConflict set returned by theorem prover: " + cs + "\n\n");
01251     }
01252 
01253     protected int readExplSize() throws IllegalUserInput {
01254         Number n = (Number)explSizeSpinner.getValue();
01255         return n.intValue();
01256     }
01257 
01258     protected int readMaxDFChain()  throws IllegalUserInput {
01259         Number n = (Number)maxDFChainSpinner.getValue();
01260         return n.intValue();
01261     }
01262 
01263     protected int readNumExpl() throws IllegalUserInput {
01264         String text = numExplText.getText().trim();
01265 
01266         if (text.length() == 0) throw new IllegalUserInput("The max. number of explanations must be defined!");
01267 
01268         int numExpl;
01269         try {
01270             numExpl = Integer.parseInt(text);
01271         } catch (NumberFormatException e) {
01272             throw new IllegalUserInput("The max. number of explanations must be defined as integer!");
01273         }
01274 
01275         if (numExpl < 1) throw new IllegalUserInput("The max. number of explanations must be larger than 0");
01276 
01277         return numExpl;
01278     }
01279 
01280     /*
01281      * The results of the computation are returned in diagnoses and conflictSets.
01282      * These lists contain String's.
01283      *
01284      * Returns true iff all min. diagnoses have been computed. Returns false if there may be more min. diagnoses.
01285      */
01286     protected boolean computeMinHS(LSentence allRules, ArrayList diagnoses, ArrayList conflictSets) 
01287         throws ParseError, IllegalUserInput {
01288         
01289         Date startComputationTime = new Date();
01290 
01291         hs = null;
01292         hsFM = null;
01293         boolean hasMoreDiags;
01294         ABTheoremProver theoremProver = new ABTheoremProver();
01295         theoremProver = allRules.asABPropositionalSentence(theoremProver);
01296         if (theoremProver == null) {
01297             throw new ParseError("[unknown]");
01298         }
01299 
01300         // results from the algorithm
01301         ArrayList minHittingSetsAsAss;
01302         ArrayList conflictsAsAss;
01303 
01304         int explSize = readExplSize();
01305         int numExpl = readNumExpl();
01306         if (useFaultModelsCB.isSelected()) {
01307             
01308            String assAB = readAssAB();
01309            String assNAB = readAssNAB();
01310            if (assAB == null) {
01311                throw new IllegalUserInput("Invalid AB assumption defined!");
01312            } else if (assNAB == null) {
01313                throw new IllegalUserInput("Invalid NAB assumption defined!");
01314            } 
01315 
01316            try {
01317                hsFM = new MinHittingSetsFM(false, theoremProver, assAB, assNAB);
01318            } catch (IllegalAssumption e) {
01319                throw new IllegalUserInput("Illegal assumption in sentence: " + e.getAssumption());
01320            }
01321            
01322            int computationResult = hsFM.compute(explSize, numExpl);
01323            hasMoreDiags = (computationResult != MinHittingSetsFM.CS_ALL_MIN_DIAGS_COMPUTED);
01324            minHittingSetsAsAss = hsFM.getMinHS();
01325            conflictsAsAss = hsFM.getConflictsAsAss();
01326         
01327         } else {
01328             hs = new MinHittingSets(false, theoremProver);
01329             int computationResult = hs.compute(explSize, numExpl);
01330             hasMoreDiags = (computationResult != MinHittingSets.CS_ALL_MIN_DIAGS_COMPUTED);
01331             minHittingSetsAsAss = hs.getMinHS();
01332             conflictsAsAss = hs.getConflictsAsAss();
01333         }
01334         
01335         Date endComputationTime = new Date();
01336         long passedTime = endComputationTime.getTime() - startComputationTime.getTime();        
01337         System.out.println("Computation time [ms]: " + passedTime);
01338         
01339         // compose the "diagnoses" list which is returned
01340         composeDiagnosesAsStrings(minHittingSetsAsAss, diagnoses);        
01341         // compose the "conflictSets" list which is returned
01342         composeConflictSetsAsStrings(conflictsAsAss, conflictSets);        
01343        
01344         return !hasMoreDiags;
01345 
01346     }  // computeMinHS()
01347 
01348     protected boolean computeMoreHS(ArrayList diagnoses, ArrayList conflictSets) 
01349         throws ParseError, IllegalUserInput {
01350 
01351         assert((hs != null) || (hsFM != null));
01352         
01353         Date startComputationTime = new Date();
01354         
01355         // results from the algorithm
01356         boolean hasMoreDiags;
01357         ArrayList minHittingSetsAsAss;
01358         ArrayList conflictsAsAss;
01359 
01360         int explSize = readExplSize();
01361         int numExpl = readNumExpl();
01362         if (hsFM != null) {
01363             int computationResult = hsFM.computeMore(explSize, numExpl);
01364             hasMoreDiags = (computationResult != MinHittingSetsFM.CS_ALL_MIN_DIAGS_COMPUTED);
01365             minHittingSetsAsAss = hsFM.getMinHS();
01366             conflictsAsAss = hsFM.getConflictsAsAss();
01367         } else {
01368             int computationResult = hs.computeMore(explSize, numExpl);
01369             hasMoreDiags = (computationResult != MinHittingSets.CS_ALL_MIN_DIAGS_COMPUTED);
01370             minHittingSetsAsAss = hs.getMinHS();
01371             conflictsAsAss = hs.getConflictsAsAss();
01372         }
01373 
01374         Date endComputationTime = new Date();
01375         long passedTime = endComputationTime.getTime() - startComputationTime.getTime();        
01376         System.out.println("Computation time [ms]: " + passedTime);
01377 
01378         // compose the "diagnoses" list which is returned
01379         composeDiagnosesAsStrings(minHittingSetsAsAss, diagnoses);        
01380         // compose the "conflictSets" list which is returned
01381         composeConflictSetsAsStrings(conflictsAsAss, conflictSets); 
01382 
01383         return !hasMoreDiags;
01384 
01385     }  // computeMoreHS()
01386 
01387     protected void composeDiagnosesAsStrings(ArrayList minHittingSetsAsAss, ArrayList result) {
01388         int index = 1;
01389 
01390         Iterator itMinHS = minHittingSetsAsAss.iterator();
01391         while(itMinHS.hasNext()) {
01392             String diagStr = "" + index + ": ";
01393             ++index;
01394             
01395             ArrayList expl = (ArrayList)itMinHS.next();
01396             boolean firstExpl = true;
01397             Iterator itExpl = expl.iterator();
01398             while(itExpl.hasNext()) {
01399                 Assumption a = ((Assumption)itExpl.next());
01400                 if (firstExpl) firstExpl = false;
01401                 else diagStr += ", ";
01402                 diagStr += a.toString();
01403             }         
01404             
01405             result.add(diagStr);
01406         }
01407     }
01408     
01409     protected void composeConflictSetsAsStrings(ArrayList conflictsAsAss, ArrayList result) {
01410         Iterator itCS = conflictsAsAss.iterator();
01411         while (itCS.hasNext()) {
01412             String conflictStr = "";
01413             ArrayList cs = (ArrayList)itCS.next();
01414             Iterator itAss = cs.iterator();
01415             while (itAss.hasNext()) {
01416                 Object o = itAss.next();
01417                 assert (o != null);
01418                 Assumption a = (Assumption)o;
01419                 if (conflictStr.length() > 0) conflictStr += " \\/ -";
01420                 else conflictStr += "-";
01421                 conflictStr += a.toString();
01422             }
01423             result.add(conflictStr);
01424         }
01425     }
01426 
01427     /*
01428      * Parses the FDG from the GUI and adds the nodes/edges to the FDG in the diagnosis problem.
01429      */
01430     protected void parseFDG(DiagnosisProblem diagProblem, boolean useProb) throws ParseError {
01431         String fdgStr = fdgText.getText();
01432         StringTokenizer tokenizer = new StringTokenizer(fdgStr, "\n");
01433         
01434         while(tokenizer.hasMoreTokens()) {  // iterate through FDG tokens
01435             
01436             String line = tokenizer.nextToken().trim();
01437             if (isCommentLine(line)) continue;
01438             Double prob = null;
01439             
01440             int pos = line.indexOf("[");
01441             if (pos == 0) throw new ParseError(line);
01442             else if ((pos < 0) && useProb) throw new ParseError(line);
01443             else if (pos > 0) {
01444                 int pos1 = line.indexOf("]");
01445                 if (pos1 < pos) throw new ParseError(line);
01446                 String probStr = line.substring(pos + 1, pos1).trim();
01447                 try {
01448                     prob = new Double(probStr);
01449                     line = line.substring(0, pos).trim();
01450                 } catch (NumberFormatException err) {
01451                     throw new ParseError(line);
01452                     }
01453             }
01454             assert(!useProb || (prob != null));
01455             
01456             pos = line.indexOf(FDG_EDGE_STR);
01457             
01458             if (pos < 0) {
01459                 if (line.length() > 0) {
01460                     String comp1Str = line;
01461                     if (diagProblem.hasComponent(comp1Str)) {
01462                         throw new ParseError(line);
01463                     }
01464                     if (useProb) diagProblem.addComponent(comp1Str, prob.doubleValue());
01465                         else diagProblem.addComponent(comp1Str);
01466                     
01467                 }
01468             } else {
01469                 String comp1Str = line.substring(0, pos).trim();
01470                 String comp2Str = line.substring(pos + FDG_EDGE_STR.length()).trim();
01471                 
01472                 if ((comp1Str.length() == 0) || (comp2Str.length() == 0)) {
01473                     throw new ParseError(line);
01474                 }
01475                 if (!diagProblem.hasComponent(comp1Str) || !diagProblem.hasComponent(comp2Str)) {
01476                     throw new ParseError(line);
01477                 } 
01478                 
01479                 if (useProb) diagProblem.addFailureDep(comp1Str, comp2Str, prob.doubleValue());
01480                 else diagProblem.addFailureDep(comp1Str, comp2Str);
01481             }
01482             
01483         }  // iterate through lines
01484     
01485     }  // parseFDG()
01486     
01487     protected ArrayList computeDiagEnv(boolean mergeDEs, boolean discardOrderPerms) throws ParseError, 
01488         IllegalUserInput {
01489         
01490         System.out.println("computeDiagEnv");
01491 
01492         LSentence sd = parseSD();
01493         LSentence obs = parseOBS();
01494         LSentence indepModel = new LSentence();
01495         indepModel.addRules(sd);
01496         indepModel.addRules(obs);
01497 
01498         ArrayList result;
01499 
01500         boolean consistent = checkConsistency(indepModel);
01501         if (consistent) {
01502             result = new ArrayList();
01503             result.add("Consistent!");
01504             System.out.println("consistent");
01505 
01506         } else {  // inconsistent
01507 
01508             System.out.println("inconsistent");
01509 
01510             // read user settings
01511 
01512             int explSize = readExplSize();
01513             int numExpl = readNumExpl();
01514 
01515             boolean computeBetaDE = betaEnvCB.isSelected();
01516             //boolean useProb = useProbabilitiesCB.isSelected();
01517             boolean useProb = false;
01518             int maxDFChain = readMaxDFChain();
01519 
01520             String assAB = readAssAB();
01521             String assNAB = readAssNAB();
01522             String assDF = readAssDF();
01523             if (assAB == null) {
01524                 throw new IllegalUserInput("Invalid AB assumption defined!");
01525             } else if (assNAB == null) {
01526                 throw new IllegalUserInput("Invalid NAB assumption defined!");
01527             } else if (assDF == null) {
01528                 throw new IllegalUserInput("Invalid DF assumption defined!");
01529             }
01530 
01531             // create DiagnosisProblem, assign logical model
01532 
01533             LSentence sdd = parseSDD();
01534 
01535             DiagnosisProblem diagProblem = new DiagnosisProblem(useProb, assAB, assNAB, ASS_IF, assDF);
01536             diagProblem.setSD(sd);
01537             diagProblem.setOBS(obs);
01538             diagProblem.setSDD(sdd);
01539             
01540             // parse FDG
01541 
01542             parseFDG(diagProblem, useProb);
01543 
01544             // compute diagnoses and DEs
01545             
01546             ArrayList minHS;
01547             ArrayList conflictSets = new ArrayList();
01548             try {
01549                 minHS = diagProblem.computeMinHittingSets(explSize, numExpl, conflictSets);
01550             } catch (IllegalAssumption e) {
01551                 throw new IllegalUserInput("Illegal assumption in sentence: " + e.getAssumption());
01552             }
01553      
01554             if (mergeDEs || discardOrderPerms) {
01555                 RepairCandidates rcs 
01556                     = diagProblem.computeRepairCandidates(minHS, computeBetaDE,
01557                                                           maxDFChain, discardOrderPerms, conflictSets);
01558                 result = composeRepairCandidateResult(rcs);
01559             } else {
01560                 ArrayList minDEs = diagProblem.computeDEs(minHS, computeBetaDE, 
01561                                                           maxDFChain, conflictSets);
01562                 result = composeMinDEsResult(diagProblem, minDEs, useProb);
01563             }
01564             
01565         } // inconsistent
01566         
01567         return result;
01568         
01569     }  // computeDiagEnv
01570     
01571     protected ArrayList composeRepairCandidateResult(RepairCandidates rcs) {
01572         ArrayList result = new ArrayList(rcs.size());
01573         
01574         Iterator itCands = rcs.iterator();
01575         while (itCands.hasNext()) {
01576             RepairCandidate rc = (RepairCandidate)itCands.next();
01577             result.add(rc.toString());
01578         }
01579 
01580         return result;
01581     }
01582     
01583     protected ArrayList composeMinDEsResult(DiagnosisProblem diagProblem, ArrayList minDEs, boolean useProb) {
01584         ArrayList result = new ArrayList(minDEs.size());
01585 
01586         if (useProb) {
01587 
01588             ArrayList rankedDEs = diagProblem.computeDERanking(minDEs, true);
01589             result = new ArrayList(rankedDEs.size());
01590             
01591             DecimalFormat formatter = new DecimalFormat("0.###E0");
01592             int rank = 1;
01593             Iterator itDE = rankedDEs.iterator();
01594             while (itDE.hasNext()) {
01595                 ObjectPair op = (ObjectPair)itDE.next();
01596                 ModeAssignment de = (ModeAssignment)op.first;
01597                 double prob = ((Double)op.last).doubleValue();
01598                 String probStr = formatter.format(prob);                    
01599                 String resultStr = "" + rank + ". [" + probStr + "]: " + de.toStringShort(); 
01600                 result.add(resultStr);
01601                 ++rank;
01602             }
01603             
01604         } else {
01605             result = new ArrayList(minDEs.size());
01606             Iterator itDE = minDEs.iterator();
01607             while (itDE.hasNext()) {
01608                 ModeAssignment de = (ModeAssignment)itDE.next();
01609                 result.add(de.toStringShort());
01610             }
01611         }
01612         
01613         return result;
01614     }
01615 
01616     protected void onComputeMinHSClick() throws IllegalUserInput, ParseError {
01617         LSentence allRules = parseSD();
01618         LSentence obs = parseOBS();
01619         allRules.addRules(obs);
01620         boolean consistent = checkConsistency(allRules);
01621         
01622         if (consistent) {
01623             displayResult("Consistent!", "");
01624             computeMoreBtn.setEnabled(false);
01625         } else {
01626             
01627             ArrayList diagnoses = new ArrayList();  // list of String
01628             ArrayList conflictSets = new ArrayList();  // list of String
01629             boolean allDiagsComputed = computeMinHS(allRules, diagnoses, conflictSets);
01630             displayResults(diagnoses, (new Integer(diagnoses.size())).toString());
01631             //printConflictSets(conflictSets);
01632             computeMoreBtn.setEnabled(!allDiagsComputed);
01633         }
01634     }
01635 
01636     protected void onComputeMoreHSClick() throws IllegalUserInput, ParseError {
01637         assert((hs != null) || (hsFM != null));
01638 
01639         ArrayList diagnoses = new ArrayList();  // list of String
01640         ArrayList conflictSets = new ArrayList();  // list of String
01641         boolean allDiagsComputed = computeMoreHS(diagnoses, conflictSets);
01642         displayResults(diagnoses, (new Integer(diagnoses.size())).toString());
01643         //printConflictSets(conflictSets);
01644         computeMoreBtn.setEnabled(!allDiagsComputed);
01645     }
01646 
01647     public void actionPerformed(ActionEvent e) {
01648         
01649         try {
01650 
01651             if (e.getSource() == checkConsBtn) {
01652                 
01653                 LSentence allRules = parseSD();
01654                 LSentence obs = parseOBS();
01655                 allRules.addRules(obs);
01656                 boolean consistent = checkConsistency(allRules);
01657                 
01658                 if (consistent) displayResult("Consistent!", "");
01659                 else displayResult("Not consistent!", "");
01660 
01661             } else if (e.getSource() == computeMinHSBtn) {
01662                 onComputeMinHSClick();
01663             } else if (e.getSource() == computeMoreBtn) {
01664                 onComputeMoreHSClick();
01665             } else if (e.getSource() == computeMinDiagEnvBtn) {
01666                 ArrayList results = computeDiagEnv(mergeDEsCB.isSelected(), discardOrderPermsCB.isSelected());
01667                 displayResults(results, (new Integer(results.size())).toString());
01668                 /*} else if (e.getSource() == computeMergedDEBtn) {
01669                 ArrayList results = computeDiagEnv(true);
01670                 displayResults(results, (new Integer(results.size())).toString());  */
01671             } else if (e.getSource() == savePropAsMenu) {
01672                 savePropAs();
01673             } else if (e.getSource() == saveSdAsMenu) {
01674                 saveSdAs();
01675             } else if (e.getSource() == saveObsAsMenu) {
01676                 saveObsAs();
01677             } else if (e.getSource() == saveFdgAsMenu) {
01678                 saveFdgAs();
01679             } else if (e.getSource() == saveSddAsMenu) {
01680                 saveSddAs();
01681             } else if (e.getSource() == openPropMenu) {
01682                 readPropFile();  
01683             } else if (e.getSource() == openSdMenu) {
01684                 readSdFile();  
01685             } else if (e.getSource() == openObsMenu) {
01686                 readObsFile();  
01687             } else if (e.getSource() == openFdgMenu) {
01688                 readFdgFile();  
01689             } else if (e.getSource() == openSddMenu) {
01690                 readSddFile();  
01691             } else if (e.getSource() == reloadPropMenu) {
01692                 readFile(propText, propFile);  
01693             } else if (e.getSource() == reloadSdMenu) {
01694                 readFile(sdText, sdFile);  
01695             } else if (e.getSource() == reloadObsMenu) {
01696                 readFile(obsText, obsFile); 
01697             } else if (e.getSource() == reloadFdgMenu) {
01698                 readFile(fdgText, fdgFile);    
01699              } else if (e.getSource() == reloadSddMenu) {
01700                 readFile(sddText, sddFile);    
01701             } else if (e.getSource() == reloadAllMenu) {
01702                 reloadAll();
01703             } else if (e.getSource() == savePropMenu) {
01704                 saveProp();
01705             } else if (e.getSource() == saveSdMenu) {
01706                 saveSd();
01707             } else if (e.getSource() == saveObsMenu) {
01708                 saveObs();
01709             } else if (e.getSource() == saveFdgMenu) {
01710                 saveFdg();
01711             } else if (e.getSource() == saveSddMenu) {
01712                 saveSdd();  
01713             } else if (e.getSource() == saveAllMenu) {
01714                 saveAll();
01715             } else if (e.getSource() == clearPropBtn) {
01716                 propText.setText("");
01717             } else if (e.getSource() == clearSdBtn) {
01718                 sdText.setText("");
01719             } else if (e.getSource() == clearObsBtn) {
01720                 obsText.setText("");  
01721             } else if ((e.getSource() == commentBtn) || (e.getSource() == commentMenu)) {
01722                 addCommentChars();
01723             } else if ((e.getSource() == uncommentBtn) || (e.getSource() == uncommentMenu)) {
01724                 removeCommentChars();
01725             } else if ((e.getSource() == searchFirstBtn)) {
01726                 searchFirst();
01727             } else if ((e.getSource() == searchNextBtn)) {
01728                 searchNext();
01729             } else assert(false);
01730             
01731         } catch (ParseError err) {
01732 
01733             displayResult("Parse error in line: " + err.getLine(), "");
01734 
01735         } catch (IllegalUserInput err) {
01736             displayResult("Illegal user input: " + err.getMessage(), "");
01737         } catch (IOException err) {
01738             JOptionPane.showMessageDialog(frame, err.getMessage(),
01739                                           "IO error", JOptionPane.ERROR_MESSAGE);
01740         }
01741 
01742         updateMenuStatus();
01743     }
01744 
01745     // print conflict sets passed as strings
01746     protected void printConflictSets(ArrayList conflictSets) {
01747         System.out.println("\n\n");
01748         System.out.println("***********  Conflict sets:  **********");
01749         Iterator itCS = conflictSets.iterator();
01750         while (itCS.hasNext()) {
01751             String cs = (String)itCS.next();
01752             System.out.println(cs);
01753         }
01754 
01755         System.out.println("\n\n ***********************************\n\n");
01756     }
01757 
01758     protected void doSearch(boolean fromCursor) {
01759         String stext = searchText.getText().trim();
01760         
01761         if (stext.length() > 0) {
01762             
01763             JTextArea searchDest = null;
01764             if (focusedComponent == propText) searchDest = propText;
01765             else if (focusedComponent == sdText) searchDest = sdText;
01766             else if (focusedComponent == obsText) searchDest = obsText;
01767             else if (focusedComponent == fdgText) searchDest = fdgText;
01768             else if (focusedComponent == sddText) searchDest = sddText;
01769 
01770             if (searchDest != null) {
01771                 String text = searchDest.getText();
01772                 int searchStartPos = 0;
01773                 if (fromCursor) searchStartPos = searchDest.getSelectionEnd();
01774 
01775                 int foundPos = text.indexOf(stext, searchStartPos);
01776                 if (foundPos != -1) {
01777                     searchDest.setSelectionStart(foundPos);
01778                     searchDest.setSelectionEnd(foundPos + stext.length());
01779                     searchDest.grabFocus();
01780                     
01781                     searchResultLabel.setText("Found!");
01782                 } else {
01783                     searchResultLabel.setText("Not found!");
01784                 }
01785 
01786             } 
01787 
01788         }
01789     }
01790 
01791     protected void searchFirst() {
01792         doSearch(false);
01793     }
01794 
01795     protected void searchNext() {
01796         doSearch(true);
01797     }
01798 
01799     protected void addCommentChars(JTextArea textArea) {
01800 
01801         try {
01802             int startLine = textArea.getLineOfOffset(textArea.getSelectionStart());
01803             int endLine = textArea.getLineOfOffset(textArea.getSelectionEnd());
01804             
01805             System.out.println("startLine: " + startLine);
01806             System.out.println("endLine: " + endLine);
01807 
01808             for (int iLine = startLine; iLine <= endLine; ++iLine) {
01809                 System.out.println("addCommentChars: line: " + iLine);
01810                 int startOff = textArea.getLineStartOffset(iLine);
01811                 textArea.insert((new Character(AUTO_COMMENT_PREFIX)).toString(), startOff);
01812             }
01813         } catch (BadLocationException e) {
01814             assert(false);
01815         }
01816     }
01817 
01818     public void focusGained(FocusEvent e) {
01819         focusedComponent = e.getSource();
01820     }
01821 
01822     public void focusLost(FocusEvent e) {
01823     }
01824 
01825     protected void removeCommentChars(JTextArea textArea) {
01826         try {
01827             int startLine = textArea.getLineOfOffset(textArea.getSelectionStart());
01828             int endLine = textArea.getLineOfOffset(textArea.getSelectionEnd());
01829             
01830             String text = textArea.getText();
01831             int startOffIncr = 0;
01832 
01833             for (int iLine = startLine; iLine <= endLine; ++iLine) {
01834                 int startOff = textArea.getLineStartOffset(iLine);
01835                 
01836                 System.out.println("line " + iLine + ": " + text.charAt(startOff));
01837                 if (text.charAt(startOff + startOffIncr) == AUTO_COMMENT_PREFIX) {
01838                     textArea.replaceRange("", startOff, startOff + 1);
01839                     ++startOffIncr;
01840                 }
01841             }
01842         } catch (BadLocationException e) {
01843             assert(false);
01844         }
01845     }
01846 
01847     protected void addCommentChars() {
01848         if (focusedComponent == propText) addCommentChars(propText);
01849         else if (focusedComponent == sdText) addCommentChars(sdText);
01850         else if (focusedComponent == obsText) addCommentChars(obsText);
01851         else if (focusedComponent == fdgText) addCommentChars(fdgText);
01852         else if (focusedComponent == sddText) addCommentChars(sddText);
01853     }
01854 
01855     protected void removeCommentChars() {
01856         if (focusedComponent == propText) removeCommentChars(propText);
01857         else if (focusedComponent == sdText) removeCommentChars(sdText);
01858         else if (focusedComponent == obsText) removeCommentChars(obsText);
01859         else if (focusedComponent == fdgText) removeCommentChars(fdgText);
01860         else if (focusedComponent == sddText) removeCommentChars(sddText);
01861     }
01862 
01863     protected void displayResult(String result, String title) {
01864         
01865         ArrayList resultLines = new ArrayList();
01866         resultLines.add(result);
01867         displayResults(resultLines, title);
01868     }
01869 
01870     protected void displayResults(ArrayList resultLines, String title) {
01871 
01872         resultsBorder.setTitle(TITLE_RESULTS + title);
01873         resultsPanel.repaint();
01874 
01875         StringBuffer text1 = new StringBuffer();
01876         StringBuffer text2 = new StringBuffer();
01877 
01878         for (int i = 0; i < resultLines.size(); ++i) {
01879             if (i % 2 == 0) {
01880                 text1.append(resultLines.get(i));
01881                 text1.append('\n');
01882             } else {
01883                 text2.append(resultLines.get(i));
01884                 text2.append('\n');
01885             }
01886         }
01887 
01888         results1Text.setText(text1.toString());
01889         results2Text.setText(text2.toString());
01890     }
01891 
01892     public void itemStateChanged(ItemEvent e) {
01893         Object source = e.getItemSelectable();
01894 
01895         if (supportDepFaults && (source == useFaultModelsCB)) {            
01896             if (useFaultModelsCB.isSelected()) {
01897                 depFaultsCB.setEnabled(true);
01898                 abLabel.setEnabled(true);
01899                 nabLabel.setEnabled(true);
01900                 assABText.setEditable(true);
01901                 assNABText.setEditable(true);
01902             } else {
01903                 depFaultsCB.setEnabled(false);
01904                 depFaultsCB.setSelected(false);
01905                 abLabel.setEnabled(false);
01906                 nabLabel.setEnabled(false);
01907                 dfLabel.setEnabled(false);
01908                 assABText.setEditable(false);
01909                 assNABText.setEditable(false);
01910                 assDFText.setEditable(false);
01911                 betaEnvCB.setEnabled(false);
01912             }
01913 
01914         } else if (supportDepFaults && (source == depFaultsCB)) {
01915 
01916             if (depFaultsCB.isSelected()) {
01917                 //useProbabilitiesCB.setEnabled(true);
01918                 dfLabel.setEnabled(true);
01919                 assDFText.setEditable(true);
01920                 computeMinDiagEnvBtn.setEnabled(true);
01921                 betaEnvCB.setEnabled(true);
01922                 mergeDEsCB.setEnabled(true);
01923                 discardOrderPermsCB.setEnabled(true);
01924             } else {
01925                 /*useProbabilitiesCB.setSelected(false);
01926                   useProbabilitiesCB.setEnabled(false);*/
01927                 dfLabel.setEnabled(false);
01928                 assDFText.setEditable(false);
01929                 computeMinDiagEnvBtn.setEnabled(false);
01930                 betaEnvCB.setEnabled(false);
01931                 mergeDEsCB.setEnabled(false);
01932                 discardOrderPermsCB.setEnabled(false);
01933             }
01934         } else if (source == betaEnvCB) {
01935             // do nothing
01936         } else if (source == useProbabilitiesCB) {
01937             assert(false);
01938         } else if (supportDepFaults && (source == mergeDEsCB)) {
01939             if (mergeDEsCB.isSelected()) {
01940                 discardOrderPermsCB.setSelected(false);
01941             }
01942         } else if (supportDepFaults && (source == discardOrderPermsCB)) {
01943             if (discardOrderPermsCB.isSelected()) {
01944                 mergeDEsCB.setSelected(false);
01945             }
01946         }
01947  
01948     }  // itemStateChanged()
01949 
01950     // it is assumed that the line is already trimmed
01951     protected boolean isCommentLine(String line) {
01952         for (int i = 0; i < COMMENT_PREFIXES.length; ++i) {
01953             if (line.startsWith(COMMENT_PREFIXES[i])) return true;
01954         }
01955         return false;
01956     }
01957 
01958     protected void generatePropNegationAxioms(JTextArea propTextArea, LogicParser parser, 
01959                                               LSentence allRules) 
01960         throws ParseError, IllegalUserInput {
01961 
01962         String text = propTextArea.getText();
01963         StringTokenizer tokenizer = new StringTokenizer(text, "\n");
01964 
01965         String negationPrefix = getNegPrefix();
01966 
01967         while(tokenizer.hasMoreTokens()) {
01968             String prop = tokenizer.nextToken().trim();
01969             
01970             if ((prop.length() > 0) && !isCommentLine(prop)) { 
01971                 String line = prop + ", " + negationPrefix + prop + " -> false.";
01972                 
01973                 if (parser.parse(line)) {
01974                     allRules.addRules((LSentence)parser.result());
01975                 } else throw new ParseError(prop);
01976             }
01977         }
01978     }
01979 
01980     protected String getNegPrefix() throws IllegalUserInput {
01981         String prefix = negPrefixText.getText().trim();
01982         if (prefix.length() == 0) throw new IllegalUserInput("Invalid negation prefix defined!");
01983 
01984         return prefix;
01985     }
01986 
01987     /* 
01988      * ignoredAss: list of Strings; contains assumption names. Sentences containing
01989      * one of these assumptions are NOT parsed!
01990      */
01991     protected void parseLogSentences(JTextArea textArea, LogicParser parser, LSentence allRules) 
01992         throws ParseError {
01993 
01994         String text = textArea.getText();
01995         StringTokenizer tokenizer = new StringTokenizer(text, "\n");
01996         
01997         while(tokenizer.hasMoreTokens()) {
01998             String line = tokenizer.nextToken().trim();
01999             
02000             if ((line.length() > 0) && !isCommentLine(line)) {                
02001                 if (parser.parse(line)) {   
02002                     allRules.addRules((LSentence)parser.result());
02003                 } else throw new ParseError(line);
02004             }
02005             
02006         } 
02007     }
02008 
02009     public void windowClosing(WindowEvent e) {
02010         
02011         int opt = JOptionPane
02012             .showOptionDialog(frame,
02013                               "Are you sure you want to exit? Everything saved?", "GuiHittingSets", 
02014                               JOptionPane.YES_NO_OPTION,
02015                               JOptionPane.QUESTION_MESSAGE, null, null, null);
02016         if (opt == JOptionPane.YES_OPTION) {
02017             try {
02018                 writeSettings();
02019             } catch (IOException exc) {}
02020 
02021             System.exit(0);
02022         }
02023         
02024     }
02025 
02026     public void windowClosed(WindowEvent e) {
02027     }
02028 
02029     public void windowOpened(WindowEvent e) {
02030     }
02031 
02032     public void windowIconified(WindowEvent e) {
02033     }
02034 
02035     public void windowDeiconified(WindowEvent e) {
02036     }
02037 
02038     public void windowActivated(WindowEvent e) {
02039     }
02040 
02041     public void windowDeactivated(WindowEvent e) {
02042     }
02043 
02044 }
02045 
02046 
02047 class IllegalUserInput extends Exception {
02048 
02049     public IllegalUserInput(String msg) {
02050         super(msg);
02051     }
02052 
02053 }
02054 
02055 // The following code is from the Java documentation
02056 
02063 class SpringUtilities {
02068     public static void printSizes(java.awt.Component c) {
02069         System.out.println("minimumSize = " + c.getMinimumSize());
02070         System.out.println("preferredSize = " + c.getPreferredSize());
02071         System.out.println("maximumSize = " + c.getMaximumSize());
02072     }
02073 
02088     public static void makeGrid(Container parent,
02089                                 int rows, int cols,
02090                                 int initialX, int initialY,
02091                                 int xPad, int yPad) {
02092         SpringLayout layout;
02093         try {
02094             layout = (SpringLayout)parent.getLayout();
02095         } catch (ClassCastException exc) {
02096             System.err.println("The first argument to makeGrid must use SpringLayout.");
02097             return;
02098         }
02099 
02100         Spring xPadSpring = Spring.constant(xPad);
02101         Spring yPadSpring = Spring.constant(yPad);
02102         Spring initialXSpring = Spring.constant(initialX);
02103         Spring initialYSpring = Spring.constant(initialY);
02104         int max = rows * cols;
02105 
02106         //Calculate Springs that are the max of the width/height so that all
02107         //cells have the same size.
02108         Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
02109                                     getWidth();
02110         Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
02111                                     getWidth();
02112         for (int i = 1; i < max; i++) {
02113             SpringLayout.Constraints cons = layout.getConstraints(
02114                                             parent.getComponent(i));
02115 
02116             maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
02117             maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
02118         }
02119 
02120         //Apply the new width/height Spring. This forces all the
02121         //components to have the same size.
02122         for (int i = 0; i < max; i++) {
02123             SpringLayout.Constraints cons = layout.getConstraints(
02124                                             parent.getComponent(i));
02125 
02126             cons.setWidth(maxWidthSpring);
02127             cons.setHeight(maxHeightSpring);
02128         }
02129 
02130         //Then adjust the x/y constraints of all the cells so that they
02131         //are aligned in a grid.
02132         SpringLayout.Constraints lastCons = null;
02133         SpringLayout.Constraints lastRowCons = null;
02134         for (int i = 0; i < max; i++) {
02135             SpringLayout.Constraints cons = layout.getConstraints(
02136                                                  parent.getComponent(i));
02137             if (i % cols == 0) { //start of new row
02138                 lastRowCons = lastCons;
02139                 cons.setX(initialXSpring);
02140             } else { //x position depends on previous component
02141                 cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
02142                                      xPadSpring));
02143             }
02144 
02145             if (i / cols == 0) { //first row
02146                 cons.setY(initialYSpring);
02147             } else { //y position depends on previous row
02148                 cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
02149                                      yPadSpring));
02150             }
02151             lastCons = cons;
02152         }
02153 
02154         //Set the parent's size.
02155         SpringLayout.Constraints pCons = layout.getConstraints(parent);
02156         pCons.setConstraint(SpringLayout.SOUTH,
02157                             Spring.sum(
02158                                 Spring.constant(yPad),
02159                                 lastCons.getConstraint(SpringLayout.SOUTH)));
02160         pCons.setConstraint(SpringLayout.EAST,
02161                             Spring.sum(
02162                                 Spring.constant(xPad),
02163                                 lastCons.getConstraint(SpringLayout.EAST)));
02164     }
02165 
02166     /* Used by makeCompactGrid. */
02167     private static SpringLayout.Constraints getConstraintsForCell(
02168         int row, int col,
02169         Container parent,
02170         int cols) {
02171         SpringLayout layout = (SpringLayout) parent.getLayout();
02172         java.awt.Component c = parent.getComponent(row * cols + col);
02173         return layout.getConstraints(c);
02174     }
02175 
02191     public static void makeCompactGrid(Container parent,
02192                                        int rows, int cols,
02193                                        int initialX, int initialY,
02194                                        int xPad, int yPad) {
02195         SpringLayout layout;
02196         try {
02197             layout = (SpringLayout)parent.getLayout();
02198         } catch (ClassCastException exc) {
02199             System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
02200             return;
02201         }
02202 
02203         //Align all cells in each column and make them the same width.
02204         Spring x = Spring.constant(initialX);
02205         for (int c = 0; c < cols; c++) {
02206             Spring width = Spring.constant(0);
02207             for (int r = 0; r < rows; r++) {
02208                 width = Spring.max(width,
02209                                    getConstraintsForCell(r, c, parent, cols).
02210                                        getWidth());
02211             }
02212             for (int r = 0; r < rows; r++) {
02213                 SpringLayout.Constraints constraints =
02214                         getConstraintsForCell(r, c, parent, cols);
02215                 constraints.setX(x);
02216                 constraints.setWidth(width);
02217             }
02218             x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
02219         }
02220 
02221         //Align all cells in each row and make them the same height.
02222         Spring y = Spring.constant(initialY);
02223         for (int r = 0; r < rows; r++) {
02224             Spring height = Spring.constant(0);
02225             for (int c = 0; c < cols; c++) {
02226                 height = Spring.max(height,
02227                                     getConstraintsForCell(r, c, parent, cols).
02228                                         getHeight());
02229             }
02230             for (int c = 0; c < cols; c++) {
02231                 SpringLayout.Constraints constraints =
02232                         getConstraintsForCell(r, c, parent, cols);
02233                 constraints.setY(y);
02234                 constraints.setHeight(height);
02235             }
02236             y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
02237         }
02238 
02239         //Set the parent's size.
02240         SpringLayout.Constraints pCons = layout.getConstraints(parent);
02241         pCons.setConstraint(SpringLayout.SOUTH, y);
02242         pCons.setConstraint(SpringLayout.EAST, x);
02243     }
02244 }


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16