001 /**
002 * Copyright (C) 2007-2011, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
018 */
019
020 package org.dllearner.core.options;
021
022 import java.util.SortedSet;
023 import java.util.TreeSet;
024
025 /**
026 * This class represents a configuration option (without a value for the
027 * option).
028 *
029 * Note: Currently, handling the type of a configuration option is not
030 * straightforward to implement, because Java Generics information is erased at
031 * runtime. This will be fixed in Java 7, in particular JSR 308, which is due at
032 * approx. the beginning of 2009.
033 *
034 * @param <T> The type of the config option, e.g. Integer, String etc.
035 * @author Jens Lehmann
036 *
037 */
038 public abstract class ConfigOption<T> {
039
040 /**
041 * Name of this option.
042 */
043 protected String name;
044
045 /**
046 * A short description explaining the effect of this option.
047 */
048 protected String description;
049
050 /**
051 * The default value of this option.
052 */
053 protected T defaultValue;
054
055 /**
056 * Specifies whether this option is mandatory for a component,
057 * e.g. if a value other than the default value needs to be given.
058 */
059 protected boolean mandatory = false;
060
061 /**
062 * Specifies whether a change of the value of the option requires
063 * running the init method of the component again. For some options
064 * (e.g. url of an OWL file component) a new run of init is needed,
065 * while others can be changed without the need to re-init the
066 * component.
067 */
068 protected boolean requiresInit = false;
069
070 /**
071 * Calls this(name, description, null, false, true).
072 * @param name Name of config option.
073 * @param description Explanation of option.
074 */
075 public ConfigOption(String name, String description) {
076 this(name, description, null, false, true);
077 }
078
079 /**
080 * Calls this(name, description, defaultValue, false, true).
081 * @param name Name of config option.
082 * @param description Explanation of option.
083 * @param defaultValue Standard value of option.
084 */
085 public ConfigOption(String name, String description, T defaultValue) {
086 this(name, description, defaultValue, false, true);
087 }
088
089 /**
090 * Constructs a component configuration option.
091 * @param name Name of config option.
092 * @param description Explanation of option.
093 * @param defaultValue Standard value of option.
094 * @param mandatory Specifies whether assigning a value to the option is required.
095 * @param requiresInit Says whether init() has to be called again when the option is changed.
096 */
097 public ConfigOption(String name, String description, T defaultValue, boolean mandatory, boolean requiresInit) {
098 this.name = name;
099 this.description = description;
100 this.defaultValue = defaultValue;
101 this.mandatory = mandatory;
102 this.requiresInit = requiresInit;
103 }
104
105 /**
106 *
107 * @return The name of this option.
108 */
109 public String getName() {
110 return name;
111 }
112
113 /**
114 *
115 * @return The textual description of this option.
116 */
117 public String getDescription() {
118 return description;
119 }
120
121 /**
122 * @return the defaultValue
123 */
124 public T getDefaultValue() {
125 return defaultValue;
126 }
127
128 /**
129 * @return the defaultValue
130 */
131 public String getDefaultValueInJava() {
132 return defaultValue+"";
133 }
134
135 /**
136 * Specifies whether this option is mandatory for the component.
137 * (Mandatory means that this component cannot be initialised
138 * without setting a value for this option.)
139 * @return True if option is mandatory for component, false otherwise.
140 */
141 public boolean isMandatory() {
142 return mandatory;
143 }
144
145 /**
146 * Specifies whether setting this option requires that the
147 * component or any components depending on this component
148 * need to be (re-)initialised.
149 * @return True if option requires init, false otherwise.
150 */
151 public boolean requiresInit() {
152 return requiresInit;
153 }
154
155 /**
156 * TODO Method should be removed and a mapping table in ConfigJavaGenerator
157 * created instead.
158 * gets java imports
159 * @return Java imports.
160 */
161 public SortedSet<String> getJavaImports() {
162 return new TreeSet<String>();
163 }
164
165 /**
166 * Checks whether the object has the correct type to be used as a value for
167 * this option (this method is necessary, because generic information is
168 * erased at runtime in Java).
169 *
170 * @param object
171 * The object to check.
172 * @return True of the type is correct, false otherwise.
173 */
174 public abstract boolean checkType(Object object);
175
176 /**
177 * Checks whether the value is valid, e.g. passing 1985 as
178 * integer value for an option, which requires values between
179 * 0 and 1, is not valid.
180 * @param value A value for the option.
181 * @return True if the value is valid and false otherwise.
182 */
183 public abstract boolean isValidValue(T value);
184
185 public abstract String getValueTypeAsJavaString();
186
187 //TODO maybe change the function getClass in the options to get simpleName
188 public String getAllowedValuesDescription() {
189 return getValueTypeAsJavaString();
190 }
191
192 /**
193 * @param defaultValue the defaultValue to set
194 */
195 public void setDefaultValue(T defaultValue) {
196 this.defaultValue = defaultValue;
197 }
198
199 /**
200 * @param mandatory the mandatory to set
201 */
202 public void setMandatory(boolean mandatory) {
203 this.mandatory = mandatory;
204 }
205
206 /**
207 * @param requiresInit the requiresInit to set
208 */
209 public void setRequiresInit(boolean requiresInit) {
210 this.requiresInit = requiresInit;
211 }
212
213 @Override
214 public String toString() {
215 String mand = (isMandatory())?" (mandatory)":"";
216 String defval = (defaultValue==null)?"not set":defaultValue+"";
217 return "option name: " + name + "\n" +
218 "description: " + description + "\n" +
219 "allowed values: " + getAllowedValuesDescription() + "\n" +
220 //"values: " + getValueTypeAsJavaString() + "\n" +
221 "default value: " + defval + mand+ "\n";
222 }
223
224 public String getJavaDocString() {
225 String line = "* @param " + name + " " + description + ".\n";
226 line += "* mandatory: "+isMandatory()+"| reinit necessary: "+requiresInit()+"\n";
227 line += "* default value: " + defaultValue + "\n";
228 return line;
229 }
230
231 /**
232 * Get a formatted value to put into configuration file.
233 *
234 * @param value Option value.
235 *
236 * @return A string to put into a conf file.
237 */
238 public abstract String getValueFormatting(T value);
239
240 }