View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.sat;
31  
32  import java.awt.BorderLayout;
33  import java.awt.Container;
34  import java.awt.Dimension;
35  import java.awt.Toolkit;
36  import java.awt.event.ActionEvent;
37  import java.awt.event.ActionListener;
38  import java.awt.event.WindowAdapter;
39  import java.awt.event.WindowEvent;
40  import java.io.BufferedReader;
41  import java.io.IOException;
42  import java.io.InputStreamReader;
43  import java.net.URL;
44  
45  import javax.swing.Box;
46  import javax.swing.ButtonGroup;
47  import javax.swing.JFrame;
48  import javax.swing.JLabel;
49  import javax.swing.JMenu;
50  import javax.swing.JMenuBar;
51  import javax.swing.JMenuItem;
52  import javax.swing.JOptionPane;
53  import javax.swing.JRadioButtonMenuItem;
54  import javax.swing.JScrollPane;
55  
56  import org.sat4j.minisat.core.ICDCLLogger;
57  import org.sat4j.minisat.orders.RandomWalkDecorator;
58  import org.sat4j.sat.visu.VisuPreferencesFrame;
59  
60  /**
61   * 
62   * JFrame for the remote control.
63   * 
64   * @author sroussel
65   * 
66   */
67  public class RemoteControlFrame extends JFrame implements ICDCLLogger {
68  
69      /**
70  	 * 
71  	 */
72      private static final long serialVersionUID = 1L;
73  
74      private String lookAndFeel;
75  
76      public static final Dimension dim = Toolkit.getDefaultToolkit()
77              .getScreenSize();
78  
79      private JMenuBar barreMenu;
80      private JMenu menu;
81      private JMenuItem activateTracing;
82  
83      private DetailedCommandPanel commandePanel;
84      private String filename;
85  
86      private String ramdisk;
87  
88      private RemoteControlStrategy telecomStrategy;
89      private RandomWalkDecorator randomWalk;
90      // private ICDCL solver;
91      private String[] args;
92      private VisuPreferencesFrame visuFrame;
93  
94      private final static String ACTIVATE = "Activate Tracing";
95      private final static String DEACTIVATE = "Deactivate Tracing";
96  
97      private JRadioButtonMenuItem gnuplotBasedRadio;
98      private JRadioButtonMenuItem jChartBasedRadio;
99  
100     public RemoteControlFrame(String filename, String ramdisk, String[] args) {
101         super("Remote Control");
102 
103         this.filename = filename;
104         this.ramdisk = ramdisk;
105         // this.solver=solver;
106         this.args = args;
107         initLookAndFeel();
108 
109         createAndShowGUI();
110     }
111 
112     // public RemoteControlFrame(String filename, String ramdisk, ICDCL solver){
113     // super("Remote Control");
114     //
115     // this.filename=filename;
116     // this.ramdisk=ramdisk;
117     // this.solver=solver;
118     // initLookAndFeel();
119     //
120     // createAndShowGUI();
121     // }
122 
123     public RemoteControlFrame(String filename, String ramdisk) {
124         this(filename, ramdisk, new String[] {});
125     }
126 
127     public RemoteControlFrame(String filename) {
128         this(filename, "", new String[] {});
129     }
130 
131     // public RemoteControlFrame(String filename, ICDCL solver){
132     // this(filename, "",solver);
133     // }
134 
135     public RemoteControlFrame(String filename, String[] args) {
136         this(filename, "", args);
137     }
138 
139     public void reinitialiser() {
140     }
141 
142     public void setActivateGnuplot(boolean b) {
143         this.activateTracing.setSelected(b);
144         activateTracing(b);
145     }
146 
147     public void initLookAndFeel() {
148         JFrame.setDefaultLookAndFeelDecorated(true);
149     }
150 
151     public void createAndShowGUI() {
152         Container c = this.getContentPane();
153         c.setLayout(new BorderLayout());
154 
155         createMenuBar();
156 
157         this.commandePanel = new DetailedCommandPanel(this.filename,
158                 this.ramdisk, this.args, this);
159 
160         this.commandePanel.setChartBased(true);
161         this.commandePanel.activateGnuplotTracing(true);
162 
163         this.visuFrame = new VisuPreferencesFrame(
164                 this.commandePanel.getGnuplotPreferences());
165 
166         JScrollPane scrollPane = new JScrollPane(this.commandePanel);
167 
168         this.add(scrollPane);
169 
170         // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
171         this.addWindowListener(new WindowAdapter() {
172 
173             @Override
174             public void windowClosing(WindowEvent e) {
175                 RemoteControlFrame.this.commandePanel.stopVisu();
176                 System.exit(NORMAL);
177             }
178 
179         });
180 
181         this.pack();
182         this.setVisible(true);
183     }
184 
185     public void clickOnAboutSolver() {
186         if (this.commandePanel.getSolver() != null) {
187             JOptionPane.showMessageDialog(this, this.commandePanel.getSolver()
188                     .toString());
189         } else {
190             JOptionPane.showMessageDialog(this,
191                     "No solver is running at the moment. Please start solver.");
192         }
193     }
194 
195     public void setActivateTracingEditableUnderCondition(boolean b) {
196         if (this.activateTracing.getText().equals(ACTIVATE)) {
197             this.activateTracing.setEnabled(b);
198         }
199     }
200 
201     public void setActivateTracingEditable(boolean b) {
202         this.activateTracing.setEnabled(b);
203     }
204 
205     public void createMenuBar() {
206         this.barreMenu = new JMenuBar();
207         this.menu = new JMenu("File");
208         this.barreMenu.add(this.menu);
209 
210         // JMenuItem aboutSolver = new JMenuItem("About Solver");
211         // menu.add(aboutSolver);
212         //
213         // aboutSolver.addActionListener(new ActionListener() {
214         // public void actionPerformed(ActionEvent e) {
215         // clickOnAboutSolver();
216         // }
217         // });
218 
219         this.activateTracing = new JMenuItem(DEACTIVATE);
220         this.menu.add(this.activateTracing);
221 
222         this.activateTracing.addActionListener(new ActionListener() {
223             public void actionPerformed(ActionEvent e) {
224                 activateTracing(RemoteControlFrame.this.activateTracing
225                         .getText().equals(ACTIVATE));
226             }
227         });
228 
229         this.menu.addSeparator();
230 
231         this.gnuplotBasedRadio = new JRadioButtonMenuItem("Trace with Gnuplot");
232         this.jChartBasedRadio = new JRadioButtonMenuItem("Trace with Java");
233 
234         ButtonGroup visuGroup = new ButtonGroup();
235         visuGroup.add(this.gnuplotBasedRadio);
236         visuGroup.add(this.jChartBasedRadio);
237 
238         this.menu.add(this.gnuplotBasedRadio);
239 
240         this.gnuplotBasedRadio.addActionListener(new ActionListener() {
241             public void actionPerformed(ActionEvent e) {
242                 RemoteControlFrame.this.commandePanel.setGnuplotBased(true);
243                 RemoteControlFrame.this.commandePanel.setChartBased(false);
244                 RemoteControlFrame.this.commandePanel
245                         .activateGnuplotTracing(RemoteControlFrame.this.activateTracing
246                                 .getText().equals(DEACTIVATE));
247                 // activateTracing(is)
248                 log("Use gnuplot tracing");
249             }
250         });
251         this.jChartBasedRadio.setSelected(true);
252 
253         this.menu.add(this.jChartBasedRadio);
254 
255         this.jChartBasedRadio.addActionListener(new ActionListener() {
256             public void actionPerformed(ActionEvent e) {
257                 RemoteControlFrame.this.commandePanel.setGnuplotBased(false);
258                 RemoteControlFrame.this.commandePanel.setChartBased(true);
259                 RemoteControlFrame.this.commandePanel
260                         .activateGnuplotTracing(RemoteControlFrame.this.activateTracing
261                                 .getText().equals(DEACTIVATE));
262                 log("Use java tracing");
263             }
264         });
265 
266         // JMenuItem reinitialiserItem = new JMenuItem("R�initialiser");
267         // menu.add(reinitialiserItem);
268         //
269         // reinitialiserItem.addActionListener(new ActionListener() {
270         // public void actionPerformed(ActionEvent e) {
271         // reinitialiser();
272         // }
273         // });
274 
275         this.menu.addSeparator();
276 
277         JMenuItem quit = new JMenuItem("Exit");
278         this.menu.add(quit);
279 
280         quit.addActionListener(new ActionListener() {
281             public void actionPerformed(ActionEvent e) {
282                 // String[] cmdarray = new String[]{"killall","gnuplot"};
283                 // try{
284                 // Runtime.getRuntime().exec(cmdarray);
285                 // }
286                 // catch(IOException ex){
287                 // ex.printStackTrace();
288                 // }
289                 // System.exit(NORMAL);
290                 RemoteControlFrame.this.commandePanel.stopVisu();
291                 System.exit(NORMAL);
292             }
293         });
294 
295         JMenu preferences = new JMenu("Preferences");
296         JMenuItem gnuplotPreferencesItem = new JMenuItem(
297                 "Visualisation preferences");
298 
299         gnuplotPreferencesItem.addActionListener(new ActionListener() {
300             public void actionPerformed(ActionEvent e) {
301                 RemoteControlFrame.this.visuFrame.setVisible(true);
302             }
303         });
304 
305         preferences.add(gnuplotPreferencesItem);
306 
307         this.barreMenu.add(preferences);
308 
309         this.barreMenu.add(Box.createHorizontalGlue());
310 
311         // ...create the rightmost menu...
312         JLabel version = new JLabel(getVersion());
313         this.barreMenu.add(version);
314 
315         this.setJMenuBar(this.barreMenu);
316 
317     }
318 
319     public void log(String message) {
320         this.commandePanel.log(message);
321     }
322 
323     private String getVersion() {
324         URL url = RemoteControlFrame.class.getResource("/sat4j.version"); //$NON-NLS-1$
325         String s = "";
326         if (url == null) {
327             s = "no version file found!!!"; //$NON-NLS-1$			
328         } else {
329             BufferedReader in = null;
330             try {
331                 in = new BufferedReader(new InputStreamReader(url.openStream()));
332                 s = "version " + in.readLine(); //$NON-NLS-1$
333             } catch (IOException e) {
334                 s = "c ERROR: " + e.getMessage();
335             } finally {
336                 if (in != null) {
337                     try {
338                         in.close();
339                     } catch (IOException e) {
340                         s = "c ERROR: " + e.getMessage();
341                     }
342                 }
343             }
344         }
345         return s;
346     }
347 
348     public void activateTracing(boolean b) {
349         if (b) {
350             log("Activated tracing");
351             this.activateTracing.setText(DEACTIVATE);
352             // commandePanel.startVisu();
353             this.commandePanel.setPlotActivated(true);
354         } else {
355             log("Deactivated tracing.");
356             this.activateTracing.setText(ACTIVATE);
357             this.commandePanel.stopVisu();
358             this.commandePanel.setPlotActivated(false);
359             // commandePanel.activateGnuplotTracing(b);
360         }
361         if (this.commandePanel.getStartStopText().equals("Stop")
362                 && this.activateTracing.getText().equals(ACTIVATE)) {
363             this.activateTracing.setEnabled(false);
364         } else {
365             this.activateTracing.setEnabled(true);
366         }
367     }
368 
369     public void setOptimisationMode(boolean optimizationMode) {
370         this.commandePanel.setOptimisationMode(optimizationMode);
371     }
372 
373 }