File: RunnerConfig.java

package info (click to toggle)
libjaba-client-java 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,052 kB
  • sloc: java: 17,308; makefile: 12
file content (387 lines) | stat: -rw-r--r-- 11,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* Copyright (c) 2009 Peter Troshin
 *  
 *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
 * 
 *  This library is free software; you can redistribute it and/or modify it under the terms of the
 *  Apache License version 2 as published by the Apache Software Foundation
 * 
 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache 
 *  License for more details.
 * 
 *  A copy of the license is in apache_license.txt. It is also available here:
 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 * Any republication or derived work distributed in source code form
 * must include this copyright and license notice.
 */

package compbio.metadata;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.ValidationException;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import compbio.util.SysPrefs;

/**
 * The list of {@link Parameter}s and {@link Option}s supported by executable.
 * The lists is defined in and loaded from <ExecutableName>Parameters.xml file.
 * 
 * @author pvtroshin
 * 
 * @version 1.0 October 2009
 * @param <T>
 *            type of an Executable
 */
@XmlRootElement
public class RunnerConfig<T> {

	/*
	 * Please note that the order of the fields in this class is important to
	 * generate xml compliant to the hand written schema!!!
	 */

	/**
	 * The class name of a runnable e.g. T
	 */
	private String runnerClassName;
	List<Option<T>> options = new ArrayList<Option<T>>();
	String prmSeparator;
	List<Parameter<T>> parameters = new ArrayList<Parameter<T>>();

	@XmlTransient
	List<Option<T>> arguments;

	public RunnerConfig() {
		// JAXB default constructor
	}

	public RunnerConfig<T> copyAndValidateRConfig(RunnerConfig<?> runnerConf) {
		if (this.runnerClassName != runnerConf.runnerClassName) {
			throw new InvalidParameterException("Wrong runner configuration! ");
		}
		RunnerConfig<T> newrconfig = new RunnerConfig<T>();
		newrconfig.runnerClassName = runnerConf.runnerClassName;
		newrconfig.options = new ArrayList<Option<T>>(options);
		return newrconfig;
	}

	/**
	 * Returns the list of the Options supported by the executable of type T
	 * 
	 * @return list of {@link Option} supported by type T
	 * @see Option
	 */
	public List<Option<T>> getOptions() {
		return options;
	}

	/**
	 * Adds parameter to the internal parameter list
	 * 
	 * @param param
	 *            the {@link Parameter} to add
	 * @see Parameter
	 */
	public void addParameter(Parameter<T> param) {
		assert param != null;
		parameters.add(param);
	}

	/**
	 * Adds Option to the internal list of options
	 * 
	 * @param option
	 *            the {@link Option} to add
	 */
	public void addOption(Option<T> option) {
		assert option != null;
		options.add(option);
	}

	/**
	 * Returns list of {@link Parameter} and {@link Option} supported by current
	 * runner
	 * 
	 * @return list of {@link Option} and {@link Parameter} supported by type T
	 */
	@XmlTransient
	public List<Option<T>> getArguments() {
		arguments = new ArrayList<Option<T>>(options);
		arguments.addAll(parameters);
		return arguments;
	}

	/**
	 * 
	 * @return name value separator character
	 */
	public String getPrmSeparator() {
		return prmSeparator;
	}

	/**
	 * Sets name value separator character
	 * 
	 * @param prmSeparator
	 *            the separator char
	 */
	public void setPrmSeparator(String prmSeparator) {
		this.prmSeparator = prmSeparator;
	}

	/**
	 * Adds the list of options or parameters to the internal list of options
	 * 
	 * @param parameters
	 *            the list of parameters to add
	 * 
	 */
	public void setOptions(List<Option<T>> parameters) {
		this.options = parameters;
	}

	/**
	 * 
	 * @return fully qualified class name for type T
	 */
	@XmlElement(required = true)
	public String getRunnerClassName() {
		return runnerClassName;
	}

	/**
	 * Set the name of a runner class
	 * 
	 * @param runnerClassName
	 *            the name of the executable wrapping class
	 */
	public void setRunnerClassName(String runnerClassName) {
		this.runnerClassName = runnerClassName;
	}

	/**
	 * Sets the list of parameters as internal list
	 * 
	 * @param parameters
	 *            the list of parameters
	 */
	public void setParameters(List<Parameter<T>> parameters) {
		this.parameters = parameters;
	}

	/**
	 * Returns the list of parameters supported executable of type T. Where
	 * {@link Parameter} is an {@link Option} with value.
	 * 
	 * @return List of {@link Parameter} supported by type T.
	 */
	public List<Parameter<T>> getParameters() {
		return parameters;
	}

	@Override
	public String toString() {
		String value = "Runner: " + this.runnerClassName + SysPrefs.newlinechar;
		for (Option<T> par : this.getArguments()) {
			value += par;
		}
		return value;
	}

	/*
	 * Cast is safe as runnerClassNames equality checked (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		RunnerConfig<?> rconf = null;
		if (obj instanceof RunnerConfig) {
			rconf = (RunnerConfig) obj;
		}
		if (!rconf.runnerClassName.equals(this.runnerClassName)) {
			return false;
		}
		if (this.options.size() != rconf.options.size()) {
			return false;
		}
		if (this.parameters.size() != rconf.parameters.size()) {
			return false;
		}
		if (!this.prmSeparator.equals(rconf.prmSeparator)) {
			return false;
		}
		// Size of option list is the same at that point
		for (Option<T> op : options) {
			Option<T> roption = (Option<T>) rconf.getArgument(op.getName());
			if (roption == null) {
				return false;
			}
			if (!op.equals(roption)) {
				return false;
			}
		}
		// Size of parameters list is the same at that point
		for (Parameter<T> par : parameters) {
			Parameter<T> rpar = (Parameter<T>) rconf.getArgument(par.getName());
			if (rpar == null) {
				return false;
			}
			if (!par.equals(rpar)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Returns the argument by its name if found, NULL otherwise. Where the
	 * Argument is a common interface for {@link Option} and {@link Parameter}
	 * therefore this method can return either. If you need to retrieve the
	 * Option by its optionNames use @link
	 * {@link RunnerConfig#getArgumentByOptionName(String)} method. The
	 * difference between option name and optionName is explained by the
	 * following example:
	 * 
	 * <pre>
	 * <name>Sequence type</name>
	 *         <description>
	 *         --nuc - Assume the sequences are nucleotide.
	 *         --amino - Assume the sequences are amino acid. </description>
	 *         <optionNames>--amino</optionNames>
	 *         <optionNames>--nuc</optionNames>
	 *         <optionNames>--auto</optionNames>
	 * </pre>
	 * 
	 * In the example, the "Sequence type" is a name whereas --amino, --nuc and
	 * --auto are all optionNames. This dichotomy only manifests in
	 * <code>Option</code> never in <code>Parameters</code> as the latter can
	 * only have single <optioNames> element
	 * 
	 * @param name
	 *            the Parameter of Option name
	 * @return {@link Argument}
	 */
	public Option<T> getArgument(String name) {
		for (Option<T> par : getArguments()) {
			if (par.getName().equalsIgnoreCase(name)) {
				return par;
			}
		}
		return null;
	}

	/**
	 * Removes the argument {@link Argument} if found. Where Argument is either
	 * {@link Option} or {@link Parameter}.
	 * 
	 * @param name
	 *            of the argument
	 * @return true if argument was removed, false otherwise
	 */
	@SuppressWarnings("unchecked")
	// Just use raw type in instanceof this is safe
	public boolean removeArgument(String name) {
		Option<T> par = getArgument(name);
		if (par != null) {
			if (par instanceof Parameter) {
				parameters.remove(par);
				return true;
			} else {
				this.options.remove(par);
				return true;
			}
		}
		return false;
	}

	/**
	 * Returns the argument by option name, NULL if the argument is not found
	 * 
	 * @param optionName
	 *            - the optionName. This is not the same as an Option name.
	 * 
	 *            For example:
	 * 
	 *            <pre>
	 *            <name>Output sequences order</name>
	 *         		  <description>--inputorder - Output order: same as input. 
	 *         		   --reorder - Output order: aligned. Default: same as input</description>
	 *         		  <optionNames>--inputorder</optionNames>
	 *         		  <optionNames>--reorder</optionNames>
	 * </pre>
	 * 
	 *            The name of the option in the example is
	 *            "Output sequences order" whereas optionNames are
	 *            "--inputorder" and "--reorder". If you need to retrieve the
	 *            Option or Parameter by its names use
	 *            {@link RunnerConfig#getArgument(String)} method
	 * @return Option
	 */
	public Option<T> getArgumentByOptionName(String optionName) {
		for (Option<T> opt : getArguments()) {
			for (String val : opt.getOptionNames()) {
				if (val.equalsIgnoreCase(optionName)) {
					return opt;
				}
			}
		}

		return null;
	}

	/**
	 * Removes the argument which can be a Parameter or an Option instance by
	 * the value in <optionNames> element of the runner configuration
	 * descriptor.
	 * 
	 * @param optionName
	 *            the optionName of the option, do not confuse with the name!
	 * @return true if argument with optionName exists and was removed, false
	 *         otherwise
	 * @see RunnerConfig#getArgumentByOptionName(String) for destinctions
	 *      between optionNames and the name of the Option
	 */
	@SuppressWarnings("unchecked")
	// Just use raw type in instanceof this is safe
	public boolean removeArgumentByOptionName(String optionName) {
		Option<T> par = getArgumentByOptionName(optionName);
		if (par != null) {
			if (par instanceof Parameter) {
				this.parameters.remove(par);
				return true;
			} else {
				this.options.remove(par);
				return true;
			}
		}
		return false;
	}

	/**
	 * Validate the value of the argument. Checks whether the argument value is
	 * in the valid values range.
	 * 
	 * @throws ValidationException
	 *             if any of the arguments found invalid which is when
	 *             <dl>
	 *             <li>Parameter value outside {@link ValueConstrain} boundary</li>
	 *             <li>Parameter name is not listed in possible values</li>
	 *             </dl>
	 */
	public void validate() throws ValidationException {
		for (Option<?> option : getArguments()) {
			option.validate();
		}
	}
}