1 | package de.uka.ipd.sdq.dsexplore.launch; |
2 | |
3 | import java.util.Hashtable; |
4 | |
5 | import org.eclipse.core.runtime.CoreException; |
6 | import org.eclipse.debug.core.ILaunchConfiguration; |
7 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
8 | import org.eclipse.swt.SWT; |
9 | import org.eclipse.swt.events.ModifyEvent; |
10 | import org.eclipse.swt.events.ModifyListener; |
11 | import org.eclipse.swt.events.SelectionAdapter; |
12 | import org.eclipse.swt.events.SelectionEvent; |
13 | import org.eclipse.swt.events.SelectionListener; |
14 | import org.eclipse.swt.layout.GridData; |
15 | import org.eclipse.swt.layout.GridLayout; |
16 | import org.eclipse.swt.widgets.Button; |
17 | import org.eclipse.swt.widgets.Composite; |
18 | import org.eclipse.swt.widgets.Group; |
19 | import org.eclipse.swt.widgets.Label; |
20 | import org.eclipse.swt.widgets.Text; |
21 | |
22 | import de.uka.ipd.sdq.workflow.launchconfig.RunConfigPlugin; |
23 | import de.uka.ipd.sdq.workflow.pcm.runconfig.FileNamesInputTab; |
24 | |
25 | public class StartingPopulationHeuristicTab extends FileNamesInputTab { |
26 | final static String TAB_NAME = "Starting Population Heuristic"; |
27 | private static final int DEFAULT_MARGIN = 15; |
28 | private static final int DEFAULT_HORIZONTAL_SPACING = 30; |
29 | private ModifyListener modifyListener; |
30 | |
31 | /* |
32 | * Reallocation Settings |
33 | */ |
34 | |
35 | final static String STARTING_POPULATION_GROUP_NAME = "Starting Population Heuristic"; |
36 | final static String USE_STARTING_POPULATION_HEURISTIC = "Use starting population heuristic"; |
37 | final static String MAX_NUMBER_RESOURCE_CONTAINERS = "Maximum number of resource containers"; |
38 | final static String MIN_NUMBER_RESOURCE_CONTAINERS = "Minimum number of resource containers"; |
39 | final static String NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL = "Number of candidates per allocation level"; |
40 | |
41 | private Button useStartingPopulationHeuristicButton; |
42 | private Text minNumberOfResourceContainers; |
43 | private Text maxNumberOfResourceContainers; |
44 | private Text numberOfCandidatesPerAllocationLevel; |
45 | |
46 | private Label minNumberOfResourceContainersLabel; |
47 | private Label maxNumberOfResourceContainersLabel; |
48 | private Label numberOfCandidatesPerAllocationLevelLabel; |
49 | |
50 | final static boolean USE_STARTING_POPULATION_HEURISTIC_DEFAULT = false; |
51 | final static int MIN_NUMBER_OF_RESOURCE_CONTAINERS_DEFAULT = 2; |
52 | final static int MAX_NUMBER_OF_RESOURCE_CONTAINERS_DEFAULT = 9; |
53 | final static int NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL_DEFAULT = 10; |
54 | |
55 | @Override |
56 | public void createControl(Composite parent) { |
57 | modifyListener = new ModifyListener() { |
58 | public void modifyText(ModifyEvent e) { |
59 | StartingPopulationHeuristicTab.this.setDirty(true); |
60 | StartingPopulationHeuristicTab.this.updateLaunchConfigurationDialog(); |
61 | } |
62 | }; |
63 | |
64 | final SelectionListener selectionListener = new SelectionListener() { |
65 | |
66 | public void widgetDefaultSelected(SelectionEvent e) { |
67 | StartingPopulationHeuristicTab.this.setDirty(true); |
68 | StartingPopulationHeuristicTab.this.updateLaunchConfigurationDialog(); |
69 | } |
70 | |
71 | public void widgetSelected(SelectionEvent e) { |
72 | StartingPopulationHeuristicTab.this.setDirty(true); |
73 | StartingPopulationHeuristicTab.this.updateLaunchConfigurationDialog(); |
74 | } |
75 | }; |
76 | |
77 | Composite container = new Composite(parent, SWT.NONE); |
78 | setControl(container); |
79 | container.setLayout(new GridLayout()); |
80 | |
81 | /* |
82 | * Reallocation |
83 | */ |
84 | { |
85 | final Group startingPopulationGroup = new Group(container, SWT.NONE); |
86 | final GridLayout startingPopulationGroupLayout = new GridLayout(); |
87 | startingPopulationGroupLayout.numColumns = 4; |
88 | startingPopulationGroupLayout.horizontalSpacing = DEFAULT_HORIZONTAL_SPACING; |
89 | startingPopulationGroupLayout.marginHeight = DEFAULT_MARGIN; |
90 | startingPopulationGroupLayout.marginWidth = DEFAULT_MARGIN; |
91 | startingPopulationGroup.setLayout(startingPopulationGroupLayout); |
92 | startingPopulationGroup.setText(STARTING_POPULATION_GROUP_NAME); |
93 | startingPopulationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, |
94 | true, false)); |
95 | |
96 | useStartingPopulationHeuristicButton = new Button(startingPopulationGroup, SWT.CHECK); |
97 | useStartingPopulationHeuristicButton.setEnabled(true); |
98 | useStartingPopulationHeuristicButton.setSelection(true); |
99 | useStartingPopulationHeuristicButton.setText(USE_STARTING_POPULATION_HEURISTIC); |
100 | useStartingPopulationHeuristicButton.addSelectionListener(selectionListener); |
101 | useStartingPopulationHeuristicButton.addSelectionListener(new SelectionAdapter() { |
102 | @Override |
103 | public void widgetSelected(SelectionEvent e) { |
104 | // enable level and half-with fields if and only if check |
105 | // box is checked |
106 | updateStartingPopulationSelection(); |
107 | } |
108 | |
109 | }); |
110 | |
111 | // spacer |
112 | new Label(startingPopulationGroup, SWT.NONE); |
113 | new Label(startingPopulationGroup, SWT.NONE); |
114 | new Label(startingPopulationGroup, SWT.NONE); |
115 | |
116 | maxNumberOfResourceContainersLabel = new Label( |
117 | startingPopulationGroup, SWT.NONE); |
118 | maxNumberOfResourceContainersLabel |
119 | .setText(MAX_NUMBER_RESOURCE_CONTAINERS); |
120 | maxNumberOfResourceContainers = new Text(startingPopulationGroup, |
121 | SWT.SINGLE | SWT.BORDER); |
122 | maxNumberOfResourceContainers.setEnabled(true); |
123 | maxNumberOfResourceContainers |
124 | .addModifyListener(modifyListener); |
125 | |
126 | minNumberOfResourceContainersLabel = new Label( |
127 | startingPopulationGroup, SWT.NONE); |
128 | minNumberOfResourceContainersLabel |
129 | .setText(MIN_NUMBER_RESOURCE_CONTAINERS); |
130 | minNumberOfResourceContainers = new Text(startingPopulationGroup, |
131 | SWT.SINGLE | SWT.BORDER); |
132 | minNumberOfResourceContainers.setEnabled(true); |
133 | minNumberOfResourceContainers |
134 | .addModifyListener(modifyListener); |
135 | |
136 | numberOfCandidatesPerAllocationLevelLabel = new Label( |
137 | startingPopulationGroup, SWT.NONE); |
138 | numberOfCandidatesPerAllocationLevelLabel |
139 | .setText(NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL); |
140 | numberOfCandidatesPerAllocationLevel = new Text(startingPopulationGroup, |
141 | SWT.SINGLE | SWT.BORDER); |
142 | numberOfCandidatesPerAllocationLevel.setEnabled(true); |
143 | numberOfCandidatesPerAllocationLevel |
144 | .addModifyListener(modifyListener); |
145 | } |
146 | } |
147 | |
148 | @Override |
149 | public String getName() { |
150 | return TAB_NAME; |
151 | } |
152 | |
153 | @Override |
154 | public void initializeFrom(ILaunchConfiguration configuration) { |
155 | try { |
156 | useStartingPopulationHeuristicButton.setSelection(configuration.getAttribute(DSEConstantsContainer.USE_STARTING_POPULATION_HEURISTIC, USE_STARTING_POPULATION_HEURISTIC_DEFAULT)); |
157 | } catch (CoreException e) { |
158 | RunConfigPlugin.errorLogger(getName(), USE_STARTING_POPULATION_HEURISTIC , e.getMessage()); |
159 | } |
160 | |
161 | try { |
162 | maxNumberOfResourceContainers.setText(configuration.getAttribute(DSEConstantsContainer.MAX_NUMBER_RESOURCE_CONTAINERS, "")); |
163 | } catch (CoreException e) { |
164 | RunConfigPlugin.errorLogger(getName(), MAX_NUMBER_RESOURCE_CONTAINERS , e.getMessage()); |
165 | } |
166 | |
167 | try { |
168 | minNumberOfResourceContainers.setText(configuration.getAttribute(DSEConstantsContainer.MIN_NUMBER_RESOURCE_CONTAINERS, "")); |
169 | } catch (CoreException e) { |
170 | RunConfigPlugin.errorLogger(getName(), MIN_NUMBER_RESOURCE_CONTAINERS , e.getMessage()); |
171 | } |
172 | |
173 | try { |
174 | numberOfCandidatesPerAllocationLevel.setText(configuration.getAttribute(DSEConstantsContainer.NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL, "")); |
175 | } catch (CoreException e) { |
176 | RunConfigPlugin.errorLogger(getName(), NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL , e.getMessage()); |
177 | } |
178 | updateSelections(); |
179 | } |
180 | |
181 | @Override |
182 | public void performApply(ILaunchConfigurationWorkingCopy configuration) { |
183 | configuration.setAttribute(DSEConstantsContainer.USE_STARTING_POPULATION_HEURISTIC, useStartingPopulationHeuristicButton.getSelection()); |
184 | configuration.setAttribute(DSEConstantsContainer.MAX_NUMBER_RESOURCE_CONTAINERS, maxNumberOfResourceContainers.getText()); |
185 | configuration.setAttribute(DSEConstantsContainer.MIN_NUMBER_RESOURCE_CONTAINERS, minNumberOfResourceContainers.getText()); |
186 | configuration.setAttribute(DSEConstantsContainer.NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL, numberOfCandidatesPerAllocationLevel.getText()); |
187 | } |
188 | |
189 | @Override |
190 | public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { |
191 | configuration.setAttribute(DSEConstantsContainer.USE_STARTING_POPULATION_HEURISTIC, USE_STARTING_POPULATION_HEURISTIC_DEFAULT); |
192 | configuration.setAttribute(DSEConstantsContainer.MIN_NUMBER_RESOURCE_CONTAINERS, Integer.toString(MIN_NUMBER_OF_RESOURCE_CONTAINERS_DEFAULT)); |
193 | configuration.setAttribute(DSEConstantsContainer.MAX_NUMBER_RESOURCE_CONTAINERS, Integer.toString(MAX_NUMBER_OF_RESOURCE_CONTAINERS_DEFAULT)); |
194 | configuration.setAttribute(DSEConstantsContainer.NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL, Integer.toString(NUMBER_OF_CANDIDATES_PER_ALLOCATION_LEVEL_DEFAULT)); |
195 | } |
196 | |
197 | /* |
198 | * (non-Javadoc) |
199 | * |
200 | * @see |
201 | * org.eclipse.debug.ui.AbstractLaunchConfigurationTab#isValid(org.eclipse |
202 | * .debug.core.ILaunchConfiguration) |
203 | */ |
204 | @Override |
205 | public boolean isValid(ILaunchConfiguration launchConfig) { |
206 | setErrorMessage(null); |
207 | if (!useStartingPopulationHeuristicButton.getSelection()) { |
208 | return true; |
209 | } |
210 | |
211 | // starting population cannot be used together with a predefined start population |
212 | String startingPopulationFileName; |
213 | try { |
214 | startingPopulationFileName = launchConfig.getAttribute(DSEConstantsContainer.PREDEFINED_INSTANCES, ""); |
215 | if (startingPopulationFileName != null && ! "".equals(startingPopulationFileName)){ |
216 | setErrorMessage("Starting population heuristics cannot be enabled if a starting population has been predefined in the DSE Options Tab"); |
217 | return false; |
218 | } |
219 | } catch (CoreException e1) { |
220 | e1.printStackTrace(); |
221 | } |
222 | |
223 | |
224 | /* |
225 | * Check: is int and > 0 ? |
226 | */ |
227 | Hashtable<Text, String> valuesToCheck = new Hashtable<Text, String>(); |
228 | valuesToCheck.put(minNumberOfResourceContainers, "Minimum number of resouce containers"); |
229 | valuesToCheck.put(maxNumberOfResourceContainers, "Maximum number of resource containers"); |
230 | valuesToCheck.put(numberOfCandidatesPerAllocationLevel, "Number of candidates per processing rate level"); |
231 | for (Text text : valuesToCheck.keySet()) { |
232 | try { |
233 | int i = Integer.parseInt(text.getText()); |
234 | if (i <= 0) { |
235 | throw new NumberFormatException(); |
236 | } |
237 | } catch (NumberFormatException e) { |
238 | setErrorMessage(valuesToCheck.get(text) + " (" + text.getText() |
239 | + ") must be an integer value >0 or empty."); |
240 | return false; |
241 | } |
242 | } |
243 | |
244 | /* |
245 | * Check minNumberOfResourceContainers <= maxNumberOfResourceContainers |
246 | */ |
247 | try { |
248 | int min = Integer.parseInt(minNumberOfResourceContainers.getText()); |
249 | int max = Integer.parseInt(maxNumberOfResourceContainers.getText()); |
250 | if (min > max) { |
251 | throw new NumberFormatException(); |
252 | } |
253 | } catch (NumberFormatException e) { |
254 | setErrorMessage("Minimum number of resource containers must not be larger than maxmimum number of resource containers."); |
255 | return false; |
256 | } |
257 | return true; |
258 | } |
259 | |
260 | @Override |
261 | public void activated(ILaunchConfigurationWorkingCopy workingCopy) { |
262 | // Leave this method empty to prevent unnecessary invocation of |
263 | // initializeFrom() and multiple resulting invocations of |
264 | // performApply(). |
265 | } |
266 | |
267 | @Override |
268 | public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { |
269 | } |
270 | |
271 | private void updateSelections() { |
272 | updateStartingPopulationSelection(); |
273 | } |
274 | |
275 | |
276 | /** |
277 | * |
278 | */ |
279 | private void updateStartingPopulationSelection() { |
280 | boolean selected = useStartingPopulationHeuristicButton.getSelection(); |
281 | maxNumberOfResourceContainersLabel |
282 | .setEnabled(selected); |
283 | maxNumberOfResourceContainers.setEnabled(selected); |
284 | minNumberOfResourceContainersLabel |
285 | .setEnabled(selected); |
286 | minNumberOfResourceContainers.setEnabled(selected); |
287 | numberOfCandidatesPerAllocationLevelLabel |
288 | .setEnabled(selected); |
289 | numberOfCandidatesPerAllocationLevel.setEnabled(selected); |
290 | StartingPopulationHeuristicTab.this.updateLaunchConfigurationDialog(); |
291 | } |
292 | |
293 | |
294 | } |