1 package org.sat4j.sat;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.swing.JButton;
11 import javax.swing.JComboBox;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.border.CompoundBorder;
15 import javax.swing.border.TitledBorder;
16
17 import org.sat4j.minisat.core.IPhaseSelectionStrategy;
18
19 public class PhaseCommandComponent extends CommandComponent {
20
21
22
23
24 private static final long serialVersionUID = 1L;
25
26 private String currentPhaseSelectionStrategy;
27
28 private JComboBox phaseList;
29 private JLabel phaseListLabel;
30 private final static String PHASE_STRATEGY = "Choose phase strategy :";
31
32 private JButton phaseApplyButton;
33 private final static String PHASE_APPLY = "Apply";
34
35 private final static String PHASE_STRATEGY_CLASS = "org.sat4j.minisat.core.IPhaseSelectionStrategy";
36 private final static String PHASE_PATH_SAT = "org.sat4j.minisat.orders";
37
38
39
40 private SolverController solverController;
41
42 public PhaseCommandComponent(String name, SolverController commandPanel,
43 String initialPhaseStrategyName) {
44 this.currentPhaseSelectionStrategy = initialPhaseStrategyName;
45 this.solverController = commandPanel;
46 this.setName(name);
47 createPanel();
48 }
49
50 @Override
51 public void createPanel() {
52 createPhasePanel();
53 }
54
55 public void createPhasePanel() {
56
57 this.setBorder(new CompoundBorder(new TitledBorder(null,
58 this.getName(), TitledBorder.LEFT, TitledBorder.TOP),
59 DetailedCommandPanel.border5));
60
61 this.setLayout(new BorderLayout());
62
63 JPanel tmpPanel1 = new JPanel();
64 tmpPanel1.setLayout(new FlowLayout());
65
66 this.phaseListLabel = new JLabel(PHASE_STRATEGY);
67
68 this.phaseList = new JComboBox(getListOfPhaseStrategies().toArray());
69 this.phaseList.setSelectedItem(this.currentPhaseSelectionStrategy);
70
71
72
73
74
75
76
77 tmpPanel1.add(this.phaseListLabel);
78 tmpPanel1.add(this.phaseList);
79
80 this.phaseApplyButton = new JButton(PHASE_APPLY);
81
82 this.phaseApplyButton.addActionListener(new ActionListener() {
83 public void actionPerformed(ActionEvent e) {
84 hasClickedOnApplyPhase();
85 }
86 });
87
88 JPanel tmpPanel2 = new JPanel();
89 tmpPanel2.add(this.phaseApplyButton);
90
91 this.add(tmpPanel1, BorderLayout.CENTER);
92 this.add(tmpPanel2, BorderLayout.SOUTH);
93 }
94
95 public void hasClickedOnApplyPhase() {
96 String phaseName = (String) this.phaseList.getSelectedItem();
97 this.currentPhaseSelectionStrategy = phaseName;
98 IPhaseSelectionStrategy phase = null;
99 try {
100 phase = (IPhaseSelectionStrategy) Class.forName(
101 PHASE_PATH_SAT + "." + phaseName).newInstance();
102 phase.init(this.solverController.getNVar() + 1);
103 this.solverController.setPhaseSelectionStrategy(phase);
104
105 } catch (ClassNotFoundException e) {
106 e.printStackTrace();
107 } catch (IllegalAccessException e) {
108 e.printStackTrace();
109 } catch (InstantiationException e) {
110 e.printStackTrace();
111 }
112
113 }
114
115 public void setPhasePanelEnabled(boolean enabled) {
116 this.phaseList.setEnabled(enabled);
117 this.phaseListLabel.setEnabled(enabled);
118 this.phaseApplyButton.setEnabled(enabled);
119 repaint();
120 }
121
122 public List<String> getListOfPhaseStrategies() {
123 List<String> resultRTSI = RTSI.find(PHASE_STRATEGY_CLASS);
124 List<String> finalResult = new ArrayList<String>();
125
126
127
128 for (String s : resultRTSI) {
129 if (!s.contains("Remote")) {
130 finalResult.add(s);
131 }
132 }
133
134 return finalResult;
135 }
136
137 public void setPhaseListSelectedItem(String name) {
138 this.currentPhaseSelectionStrategy = name;
139 this.phaseList.setSelectedItem(this.currentPhaseSelectionStrategy);
140 repaint();
141 }
142 }