View Javadoc

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