View Javadoc

1   /*
2    * @(#)$Id: Configuration.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      : 	Configuration object used by the algorithms. The
7    *                  configuration object contains information about the
8    *                  used gesture set (1..*), the algorithm(s) to be used
9    *                  (1..*), the parameters for the algorithms, the
10   *                  minimal accuracy as well as the size of the result
11   *                  list in the result set.
12   *
13   * -----------------------------------------------------------------------
14   *
15   * Revision Information:
16   *
17   * Date             Who         Reason
18   *
19   * Dec 26, 2006     ukurmann    Initial Release
20   * Mar 22, 2007     bsigner     Cleanup
21   *
22   * -----------------------------------------------------------------------
23   *
24   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
25   *
26   * This software is the proprietary information of ETH Zurich.
27   * Use is subject to license terms.
28   * 
29   */
30  
31  
32  package org.ximtec.igesture.configuration;
33  
34  import java.util.ArrayList;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.logging.Level;
39  import java.util.logging.Logger;
40  
41  import org.sigtec.util.Constant;
42  import org.ximtec.igesture.core.DefaultDataObject;
43  import org.ximtec.igesture.core.GestureSet;
44  import org.ximtec.igesture.core.Visitor;
45  import org.ximtec.igesture.event.GestureHandler;
46  import org.ximtec.igesture.util.GestureTool;
47  
48  
49  /**
50   * Configuration object used by the algorithms. The configuration object contains
51   * information about the used gesture set (1..*), the algorithm(s) to be used
52   * (1..*), the parameters for the algorithms, the minimal accuracy as well as the
53   * size of the result list in the result set.
54   * 
55   * @version 1.0 Dec 2006
56   * @author Ueli Kurmann, igesture@uelikurmann.ch
57   * @author Beat Signer, signer@inf.ethz.ch
58   */
59  public class Configuration extends DefaultDataObject implements Cloneable {
60  
61     private static final Logger LOGGER = Logger.getLogger(Configuration.class
62           .getName());
63  
64     public static final String PROPERTY_NAME = "name";
65  
66     private static final String PROPERTY_MIN_ACCURACY = "minAccuracy";
67  
68     private static final String PROPERTY_MAX_RESULT_SET_SIZE = "maxResultSetSize";
69  
70     /**
71      * The list of gesture sets which are selected for the recognition process.
72      */
73     private List<GestureSet> gestureSets;
74  
75     /**
76      * Name of the configuration.
77      */
78     private String name;
79  
80     /**
81      * A list of algorithms. The input is sent to each algorithm.
82      */
83     private List<String> algorithms;
84  
85     /**
86      * Algorithm specific parameters. Each algorithm may have arbitrary
87      * parameters.
88      */
89     private HashMap<String, HashMap<String, String>> algorithmParameters;
90  
91     /**
92      * The instance of the gesture handler.
93      */
94     private transient GestureHandler gestureHandler;
95  
96     /**
97      * The maximal size of the result set.
98      */
99     private int maxResultSetSize;
100 
101    /**
102     * The minimum Accuracy a result must have.
103     */
104    private double minAccuracy;
105 
106 
107    /**
108     * Constructs a new configuration and initialises the used containers.
109     */
110    public Configuration() {
111       // FIXME resource file
112       this("New Configuration");
113    }
114 
115 
116    public Configuration(String name) {
117       super();
118       this.gestureSets = new ArrayList<GestureSet>();
119       this.algorithms = new ArrayList<String>();
120       this.algorithmParameters = new HashMap<String, HashMap<String, String>>();
121       this.maxResultSetSize = Integer.MAX_VALUE;
122       this.minAccuracy = 0;
123       this.name = name;
124    }
125 
126 
127    /**
128     * Adds a gesture set to the configuration.
129     * 
130     * @param gestureSet the gesture set to be added.
131     */
132    public void addGestureSet(GestureSet gestureSet) {
133       gestureSets.add(gestureSet);
134    } // addGestureSet
135 
136 
137    /**
138     * Adds a list of gesture sets to the configuration.
139     * 
140     * @param gestureSets the list of gesture sets to be added.
141     */
142    public void addGestureSets(List<GestureSet> gestureSets) {
143       this.gestureSets.addAll(gestureSets);
144    } // addGestureSets
145 
146 
147    /**
148     * Removes all gesture sets.
149     */
150    public void removeAllGestureSets() {
151       this.gestureSets.clear();
152    } // removeAllGestureSets
153 
154 
155    /**
156     * Removes a gesture set from the configuration.
157     * 
158     * @param gestureSet the gesture set to be removed.
159     */
160    public void removeGestureSet(GestureSet gestureSet) {
161       gestureSets.remove(gestureSet);
162    } // removeGestureSet
163 
164 
165    /**
166     * Returns the list of gesture sets.
167     * 
168     * @return List<GestureSet> the list of gesture sets.
169     */
170    public List<GestureSet> getGestureSets() {
171       return gestureSets;
172    } // getGestureSets
173 
174 
175    /**
176     * Returns the combined gesture set.
177     * 
178     * @return the combined gesture set.
179     */
180    public GestureSet getGestureSet() {
181       return GestureTool.combine(getGestureSets());
182    } // getGestureSet
183 
184 
185    /**
186     * Adds an algorithm to the configuration.
187     * 
188     * @param algorithm the algorithm to be added to the configuration.
189     */
190    public void addAlgorithm(String algorithm) {
191       algorithms.add(algorithm);
192       algorithmParameters.put(algorithm, new HashMap<String, String>());
193    } // addAlgorithm
194 
195 
196    /**
197     * Removes an algorithm from the configuration.
198     * 
199     * @param algorithm the algorithm to be removed.
200     */
201    public void removeAlgorithm(String algorithm) {
202       algorithms.remove(algorithm);
203       algorithmParameters.remove(algorithm);
204    } // removeAlgorithm
205 
206 
207    /**
208     * Returns the list with all algorithms.
209     * 
210     * @return the list with all algorithms.
211     */
212    public List<String> getAlgorithms() {
213       return algorithms;
214    } // getAlgorithms
215 
216 
217    /**
218     * Adds a key/value parameter to the specified algorithm. If the parameter map
219     * does not exist it will be created.
220     * 
221     * @param algorithm the algorithm the parameter has to be added to.
222     * @param key the key of the parameter to be added.
223     * @param value the parameter value the value of the parameter to be added.
224     */
225    public void addParameter(String algorithm, String key, String value) {
226       Map<String, String> parameters = getParameters(algorithm);
227       parameters.put(key, value);
228       propertyChangeSupport.firePropertyChange("parameters", null, value);
229    } // addParameter
230 
231 
232    /**
233     * Returns the map of parameters for a specific algorithm.
234     * 
235     * @param classname the classname of the algorithm whose parameters have to be
236     *            returned.
237     * @return a hashmap with the algorithms parameters.
238     */
239    public Map<String, String> getParameters(String classname) {
240       HashMap<String, String> parameters = algorithmParameters.get(classname);
241 
242       if (parameters == null) {
243          parameters = new HashMap<String, String>();
244          algorithmParameters.put(classname, parameters);
245       }
246 
247       return parameters;
248    } // getParameters
249 
250 
251    /**
252     * Removes all parameter for a specific algorithm.
253     * 
254     * @param classname the classname of the algorithm whose parameters have to be
255     *            removed.
256     */
257    public void removeParameters(String classname) {
258       algorithmParameters.remove(classname);
259    } // removeParameters
260 
261 
262    /**
263     * Sets the gesture handler.
264     * 
265     * @param gestureHandler the gesture handler to be set.
266     */
267    public void setGestureHandler(GestureHandler gestureHandler) {
268       this.gestureHandler = gestureHandler;
269    } // setGestureHandler
270 
271 
272    /**
273     * Returns the gesture handler.
274     * 
275     * @return the gesture handler.
276     */
277    public GestureHandler getGestureHandler() {
278       return gestureHandler;
279    } // getGestureHandler
280 
281 
282    /**
283     * Sets the minimal accuracy.
284     * 
285     * @param minAccuracy the minimal accuracy to be set.
286     */
287    public void setMinAccuracy(double minAccuracy) {
288       double oldValue = this.minAccuracy;
289       this.minAccuracy = minAccuracy;
290       propertyChangeSupport.firePropertyChange(PROPERTY_MIN_ACCURACY, oldValue,
291             minAccuracy);
292    } // setMinAccuracy
293 
294 
295    /**
296     * Returns the minimal accuracy.
297     * 
298     * @return minimal accuracy.
299     */
300    public double getMinAccuracy() {
301       return minAccuracy;
302    } // getMinAccuracy
303 
304 
305    public void setName(String name) {
306       String oldValue = this.name;
307       this.name = name;
308       propertyChangeSupport.firePropertyChange(PROPERTY_NAME, oldValue, name);
309    } // setName
310 
311 
312    public String getName() {
313       return this.name;
314    } // getName
315 
316 
317    /**
318     * Sets the maximal result set size.
319     * 
320     * @param maxResultSetSize the maximal result set size.
321     */
322    public void setMaxResultSetSize(int maxResultSetSize) {
323       int oldValue = maxResultSetSize;
324       this.maxResultSetSize = maxResultSetSize;
325       propertyChangeSupport.firePropertyChange(PROPERTY_MAX_RESULT_SET_SIZE,
326             oldValue, maxResultSetSize);
327    } // setMaxResultSetSize
328 
329 
330    /**
331     * Returns the maximal result set size.
332     * 
333     * @return the maximal result set size.
334     */
335    public int getMaxResultSetSize() {
336       return maxResultSetSize;
337    } // getMaxResultSetSize
338 
339 
340    /**
341     * Clones the Configuration instance. The GestureSet and the EventManager are
342     * not cloned.
343     */
344    @Override
345    public Object clone() {
346       Configuration clone = null;
347 
348       try {
349          clone = (Configuration)super.clone();
350          // Clone Algorithm Parameters
351          clone.algorithmParameters = new HashMap<String, HashMap<String, String>>();
352 
353          for (String key : algorithmParameters.keySet()) {
354             HashMap<String, String> paramsClone = new HashMap<String, String>();
355             HashMap<String, String> paramsOriginal = algorithmParameters
356                   .get(key);
357 
358             for (String paramKey : paramsOriginal.keySet()) {
359                paramsClone
360                      .put(paramKey, paramsOriginal.get(paramKey).toString());
361             }
362 
363             clone.algorithmParameters.put(key, paramsClone);
364          }
365 
366          // Clone Algorithm Names
367          clone.algorithms = new ArrayList<String>();
368 
369          for (String s : algorithms) {
370             clone.algorithms.add(s.toString());
371          }
372 
373          // References GestureHandler
374          clone.gestureHandler = gestureHandler;
375 
376          // References GestureSets
377          clone.gestureSets = new ArrayList<GestureSet>();
378       }
379       catch (CloneNotSupportedException e) {
380          LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
381       }
382 
383       return clone;
384    } // clone
385 
386 
387    @Override
388    public void accept(Visitor visitor) {
389       visitor.visit(this);
390       
391       for (GestureSet gestureSet : gestureSets) {
392          gestureSet.accept(visitor);
393       }
394 
395    } // accept
396 
397 
398    @Override
399    public String toString() {
400       return name;
401    } // toString
402 
403 }