1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.metadata;
\r
21 import java.util.List;
\r
23 import javax.xml.bind.ValidationException;
\r
24 import javax.xml.bind.annotation.XmlAccessType;
\r
25 import javax.xml.bind.annotation.XmlAccessorType;
\r
26 import javax.xml.bind.annotation.XmlElement;
\r
27 import javax.xml.bind.annotation.XmlRootElement;
\r
29 import compbio.util.Util;
\r
32 * Collection of presets and methods to manipulate them @see {@link Preset}
\r
36 * Date December 2009
\r
38 * type of executable.
\r
40 @XmlRootElement(name = "presets")
\r
41 @XmlAccessorType(XmlAccessType.FIELD)
\r
42 public class PresetManager<T> {
\r
44 @XmlElement(required = true)
\r
45 String runnerClassName;
\r
47 @XmlElement(required = true)
\r
48 List<Preset<T>> preset;
\r
50 public static final String LOCAL_ENGINE_LIMIT_PRESET = "# LocalEngineExecutionLimit #";
\r
52 public List<Preset<T>> getPresets() {
\r
56 public void setPresets(List<Preset<T>> presets) {
\r
57 this.preset = presets;
\r
62 * @return fully qualified class name of type T
\r
64 public String getRunnerClassName() {
\r
65 return runnerClassName;
\r
68 public void setRunnerClassName(String runnerClassName) {
\r
69 this.runnerClassName = runnerClassName;
\r
75 * @return preset by its name, null if no preset found
\r
77 public Preset<T> getPresetByName(String presetName) {
\r
78 for (Preset<T> p : preset) {
\r
79 if (p.getName().equalsIgnoreCase(presetName)) {
\r
86 boolean isComposite(String value) {
\r
87 assert value != null;
\r
88 return value.contains(" ");
\r
91 boolean containsValue(List<String> values, String value) {
\r
92 assert !Util.isEmpty(value);
\r
93 for (String v : values) {
\r
94 if (v.equals(value)) {
\r
101 boolean isNumeric(String value) {
\r
102 assert value != null;
\r
104 Double.parseDouble(value);
\r
106 } catch (NumberFormatException e) {
\r
113 * Checks whether preset option and parameter are defined in RunnerConfig
\r
116 * TODO handle parameters with values properly!
\r
118 * @throws ValidationException
\r
119 * if preset is found to be invalid.
\r
122 public void validate(RunnerConfig<T> options) throws ValidationException {
\r
123 for (Preset<T> p : preset) {
\r
124 if (Util.isEmpty(p.name)) {
\r
125 throw new ValidationException("Preset name must not be empty!");
\r
127 List<String> optionNames = p.getOptions();
\r
128 if (optionNames == null || optionNames.size() == 0) {
\r
129 throw new ValidationException(
\r
130 "At lease one option must be defined for a preset!");
\r
132 for (String oName : optionNames) {
\r
133 if (isComposite(oName)) {
\r
134 String name = oName.split(" ")[0];
\r
135 Argument<T> arg = getArgument(options, name);
\r
136 String value = oName.split(" ")[1];
\r
137 // Ignore numeric values
\r
138 if (!isNumeric(value)
\r
139 && !containsValue(arg.getPossibleValues(), value)) {
\r
140 throw new ValidationException("Value " + value
\r
141 + " is not found in the option " + name);
\r
144 getArgument(options, oName);
\r
150 Argument<T> getArgument(RunnerConfig<T> options, String optionName)
\r
151 throws ValidationException {
\r
152 Argument<T> arg = options.getArgumentByOptionName(optionName);
\r
154 throw new ValidationException(
\r
157 + " is not found in the option list (<RunnerName>Parameter.xml file)");
\r
163 public String toString() {
\r
164 String value = "Runner: " + this.runnerClassName;
\r
165 for (Preset<T> p : preset) {
\r
166 value += p.toString() + "\n";
\r