View Javadoc

1   package org.sat4j.sat;
2   
3   import java.awt.Component;
4   import java.awt.FlowLayout;
5   import java.awt.Font;
6   import java.awt.GridBagConstraints;
7   import java.awt.GridBagLayout;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import javax.swing.JButton;
14  import javax.swing.JComboBox;
15  import javax.swing.JLabel;
16  import javax.swing.JPanel;
17  import javax.swing.JTextField;
18  import javax.swing.border.CompoundBorder;
19  import javax.swing.border.TitledBorder;
20  
21  import org.sat4j.minisat.core.RestartStrategy;
22  import org.sat4j.minisat.core.SearchParams;
23  import org.sat4j.minisat.core.SolverStats;
24  import org.sat4j.minisat.restarts.LubyRestarts;
25  import org.sat4j.minisat.restarts.NoRestarts;
26  import org.sat4j.specs.ILogAble;
27  
28  public class RestartCommandComponent extends CommandComponent {
29  
30      private static final long serialVersionUID = 1L;
31  
32      private JPanel restartPropertiesPanel;
33  
34      private JLabel chooseRestartStrategyLabel;
35      private JLabel noParameterLabel;
36      private JComboBox listeRestarts;
37      private JButton restartButton;
38  
39      private JButton changeRestartMode;
40  
41      private JLabel factorLabel;
42      private static final String FACTOR = "Factor: ";
43      private JTextField factorField;
44  
45      private String currentRestart;
46  
47      private static final String RESTART = "Restart";
48      private static final String CHOOSE_RESTART_STRATEGY = "Choose restart strategy: ";
49      private static final String CHANGE_RESTART_STRATEGY = "Apply";
50      private static final String MANUAL_RESTART = "Manual Restart";
51      private static final String NO_PARAMETER_FOR_THIS_STRATEGY = "No paramaters for this strategy";
52      private static final String RESTART_STRATEGY_CLASS = "org.sat4j.minisat.core.RestartStrategy";
53      private static final String RESTART_PATH = "org.sat4j.minisat.restarts";
54  
55      private SolverController controller;
56  
57      private ILogAble logger;
58  
59      public RestartCommandComponent(String name, SolverController controller,
60              String initialRestartStrategy, ILogAble logger) {
61          this.setName(name);
62          this.currentRestart = initialRestartStrategy;
63          this.controller = controller;
64          this.logger = logger;
65          createPanel();
66          initFactorParam();
67      }
68  
69      @Override
70      public void createPanel() {
71  
72          this.setLayout(new GridBagLayout());
73  
74          GridBagConstraints c = new GridBagConstraints();
75          c.anchor = GridBagConstraints.PAGE_START;
76          c.fill = GridBagConstraints.HORIZONTAL;
77          c.weightx = 1;
78          c.gridx = 0;
79  
80          JPanel chooseRestartPanel = new JPanel();
81          chooseRestartPanel.setLayout(new GridBagLayout());
82  
83          GridBagConstraints c1 = new GridBagConstraints();
84          c1.anchor = GridBagConstraints.PAGE_START;
85          c1.fill = GridBagConstraints.HORIZONTAL;
86          c1.weightx = 1;
87          c1.gridx = 0;
88  
89          chooseRestartPanel.setBorder(new CompoundBorder(new TitledBorder(null,
90                  this.getName(), TitledBorder.LEFT, TitledBorder.TOP),
91                  DetailedCommandPanel.BORDER5));
92  
93          JPanel tmpPanel1 = new JPanel();
94          tmpPanel1.setLayout(new FlowLayout());
95  
96          this.chooseRestartStrategyLabel = new JLabel(CHOOSE_RESTART_STRATEGY);
97  
98          this.listeRestarts = new JComboBox(getListOfRestartStrategies()
99                  .toArray());
100 
101         this.listeRestarts.setSelectedItem(this.currentRestart);
102 
103         this.listeRestarts.addActionListener(new ActionListener() {
104             public void actionPerformed(ActionEvent e) {
105                 modifyRestartParamPanel();
106             }
107         });
108 
109         tmpPanel1.add(this.chooseRestartStrategyLabel);
110         tmpPanel1.add(this.listeRestarts);
111 
112         this.changeRestartMode = new JButton(CHANGE_RESTART_STRATEGY);
113 
114         this.changeRestartMode.addActionListener(new ActionListener() {
115             public void actionPerformed(ActionEvent e) {
116                 hasClickedOnChange();
117             }
118         });
119 
120         tmpPanel1.add(this.changeRestartMode);
121 
122         this.noParameterLabel = new JLabel(NO_PARAMETER_FOR_THIS_STRATEGY);
123 
124         Font newLabelFont = new Font(this.noParameterLabel.getFont().getName(),
125                 Font.ITALIC, this.noParameterLabel.getFont().getSize());
126 
127         this.noParameterLabel.setFont(newLabelFont);
128 
129         this.restartPropertiesPanel = new JPanel();
130         this.restartPropertiesPanel.add(this.noParameterLabel);
131 
132         this.restartButton = new JButton(RESTART);
133 
134         this.restartButton.addActionListener(new ActionListener() {
135             public void actionPerformed(ActionEvent e) {
136                 hasClickedOnRestart();
137             }
138         });
139 
140         JPanel restartButtonPanel = new JPanel();
141         restartButtonPanel.setName(MANUAL_RESTART);
142         restartButtonPanel.setBorder(new CompoundBorder(new TitledBorder(null,
143                 restartButtonPanel.getName(), TitledBorder.LEFT,
144                 TitledBorder.TOP), DetailedCommandPanel.BORDER5));
145 
146         restartButtonPanel.add(this.restartButton);
147 
148         c1.gridy = 0;
149         chooseRestartPanel.add(tmpPanel1, c1);
150 
151         c1.gridy = 1;
152         chooseRestartPanel.add(this.restartPropertiesPanel, c1);
153 
154         c.gridy = 0;
155         this.add(chooseRestartPanel, c);
156 
157         c.gridy = 1;
158         this.add(restartButtonPanel, c);
159     }
160 
161     public void initFactorParam() {
162 
163         this.factorLabel = new JLabel(FACTOR);
164         this.factorField = new JTextField(
165                 LubyRestarts.DEFAULT_LUBY_FACTOR + "", 5);
166 
167     }
168 
169     public void modifyRestartParamPanel() {
170         this.restartPropertiesPanel.removeAll();
171         if (this.listeRestarts.getSelectedItem().equals("LubyRestarts")) {
172             this.restartPropertiesPanel.add(this.factorLabel);
173             this.restartPropertiesPanel.add(this.factorField);
174         } else {
175             this.restartPropertiesPanel.add(this.noParameterLabel);
176         }
177         setRestartPropertiesPanelEnabled(true);
178         this.restartPropertiesPanel.repaint();
179         this.repaint();
180         this.paintAll(this.getGraphics());
181         this.repaint();
182     }
183 
184     public void setRestartPanelEnabled(boolean enabled) {
185         this.listeRestarts.setEnabled(enabled);
186         this.restartButton.setEnabled(enabled);
187         this.chooseRestartStrategyLabel.setEnabled(enabled);
188         setRestartPropertiesPanelEnabled(enabled);
189         this.repaint();
190     }
191 
192     public void setRestartPropertiesPanelEnabled(boolean enabled) {
193         for (Component c : this.restartPropertiesPanel.getComponents()) {
194             c.setEnabled(enabled);
195         }
196         this.restartPropertiesPanel.repaint();
197     }
198 
199     public void updateRestartStrategyPanel() {
200         this.listeRestarts.setSelectedItem(this.currentRestart);
201     }
202 
203     public void hasClickedOnChange() {
204         this.controller.shouldRestartNow();
205 
206         String choix = (String) this.listeRestarts.getSelectedItem();
207 
208         boolean isNotSameRestart = !choix.equals(this.currentRestart);
209         boolean shouldInit = isNotSameRestart;
210 
211         RestartStrategy restart = new NoRestarts();
212         SearchParams params = this.controller.getSearchParams();
213         SolverStats stats = this.controller.getSolverStats();
214         if (choix.equals("LubyRestarts")) {
215             boolean factorChanged = false;
216             int factor = LubyRestarts.DEFAULT_LUBY_FACTOR;
217             if (this.factorField.getText() != null) {
218                 factor = Integer.parseInt(this.factorField.getText());
219             }
220             // if the current restart is a LubyRestart
221             if (isNotSameRestart) {
222                 restart = new LubyRestarts(factor);
223                 this.controller.setRestartStrategy(restart);
224             } else {
225                 factorChanged = !(factor == ((LubyRestarts) this.controller
226                         .getRestartStrategy()).getFactor());
227             }
228             // if the factor has changed
229             if (factorChanged) {
230                 restart = this.controller.getRestartStrategy();
231                 ((LubyRestarts) restart).setFactor(factor);
232             }
233             shouldInit = isNotSameRestart || factorChanged;
234 
235             if (shouldInit) {
236                 this.controller.init(params, stats);
237             }
238 
239         } else {
240             try {
241                 restart = (RestartStrategy) Class.forName(
242                         RESTART_PATH + "." + choix).newInstance();
243                 assert restart != null;
244                 this.controller.setRestartStrategy(restart);
245                 this.controller.init(params, stats);
246 
247             } catch (ClassNotFoundException e) {
248                 logger.log(e.getMessage());
249             } catch (IllegalAccessException e) {
250                 logger.log(e.getMessage());
251             } catch (InstantiationException e) {
252                 logger.log(e.getMessage());
253             }
254         }
255 
256         this.currentRestart = choix;
257 
258     }
259 
260     public void hasClickedOnRestart() {
261         this.controller.shouldRestartNow();
262     }
263 
264     public List<String> getListOfRestartStrategies() {
265         List<String> resultRTSI = RTSI.find(RESTART_STRATEGY_CLASS);
266         List<String> finalResult = new ArrayList<String>();
267 
268         for (String s : resultRTSI) {
269             if (!s.contains("Remote")) {
270                 finalResult.add(s);
271             }
272         }
273 
274         return finalResult;
275     }
276 
277     public String getCurrentRestart() {
278         return this.currentRestart;
279     }
280 
281     public void setCurrentRestart(String currentRestart) {
282         this.currentRestart = currentRestart;
283         updateRestartStrategyPanel();
284         modifyRestartParamPanel();
285     }
286 }