View Javadoc

1   /*
2    * @(#)$Id: TestSet.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose		: 	Set of gesture samples used to evaluate algorithms.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date				Who			Reason
13   *
14   * Nov 20, 2006		ukurmann	Initial Release
15   * Mar 22, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.core;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  
33  /**
34   * Set of gesture samples used to evaluate algorithms.
35   * 
36   * @version 1.0, Nov 2006
37   * @author Ueli Kurmann, igesture@uelikurmann.ch
38   * @author Beat Signer, signer@inf.ethz.ch
39   */
40  public class TestSet extends DefaultDataObject {
41  
42     public static final String PROPERTY_NAME = "name";
43     public static final String PROPERTY_TEST_CLASSES = "testClasses";
44  
45     public static final String NOISE = "None";
46  
47     private List<TestClass> testClasses;
48  
49     private String name;
50  
51  
52     /**
53      * Constructs a new test set.
54      * 
55      * @param name the name of the test set.
56      */
57     public TestSet(String name) {
58        super();
59        testClasses = new ArrayList<TestClass>();
60        setName(name);
61        //addTestClass(new TestClass(NOISE));
62     }
63  
64  
65     /**
66      * Adds a sample to the list of samples.
67      * 
68      * @param sample the sample to be added.
69      */
70     public void add(Gesture<?> sample) {
71        if (sample == null) {
72           return;
73        }
74  
75        TestClass testClass = getTestClass(sample.getName());
76        if (testClass == null) {
77           testClass = new TestClass(sample.getName());
78           addTestClass(testClass);
79  
80        }
81        testClass.add(sample);
82     } // add
83  
84     
85     public TestClass getTestClass(String name){
86        for(TestClass testClass:testClasses){
87           if(testClass.getName().equals(name)){
88              return testClass;
89           }
90        }
91        return null;
92     }
93  
94     public void addTestClass(TestClass testClass) {
95        if (testClass != null) {
96           if (getTestClass(testClass.getName()) == null) {
97              testClasses.add(testClass);
98  
99              propertyChangeSupport.fireIndexedPropertyChange(
100                   PROPERTY_TEST_CLASSES, 0, null, testClass);
101          }
102          else {
103             addAll(testClass.getGestures());
104          }
105       }
106    }
107 
108 
109    public void addTestClasses(List<TestClass> testClasses) {
110       if (testClasses == null) {
111          return;
112       }
113       for (TestClass testClass : testClasses) {
114          addTestClass(testClass);
115       }
116    }
117 
118 
119    /**
120     * Adds a list of samples.
121     * 
122     * @param samples the samples to be added.
123     */
124    public void addAll(List<Gesture<?>> samples) {
125       for (Gesture<?> sample : samples) {
126          add(sample);
127       }
128 
129       for (Gesture< ? > sample : samples) {
130          propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_TEST_CLASSES,
131                samples.indexOf(sample), null, sample);
132       }
133 
134    } // addAll
135 
136 
137    /**
138     * Removes a the sample from the list of samples.
139     * 
140     * @param sample the sample to be removed.
141     */
142    public void remove(TestClass testClass) {
143       testClasses.remove(testClass);
144       propertyChangeSupport.fireIndexedPropertyChange(PROPERTY_TEST_CLASSES, 0,
145             testClass, null);
146    } // remove
147 
148 
149    /**
150     * Returns all samples.
151     * 
152     * @return a list with all samples.
153     */
154    public List<TestClass> getTestClasses() {
155       return new ArrayList<TestClass>(testClasses);
156    } // getSamples
157 
158 
159 
160    /**
161     * Returns the number of samples which should be rejected by the recogniser.
162     * 
163     * @return the number of "noise" entries in the test set.
164     */
165    public int getNoiseSize() {
166 
167       TestClass testClass = getTestClass(TestSet.NOISE);
168       return testClass == null ? 0 : testClass.size();
169 
170    } // getNoiseSize
171 
172 
173    /**
174     * Sets the name of the test set.
175     * 
176     * @param name the name of the test set.
177     */
178    public void setName(String name) {
179       String oldValue = this.name;
180       this.name = name;
181       propertyChangeSupport.firePropertyChange(PROPERTY_NAME, oldValue, name);
182    } // setName
183 
184 
185    /**
186     * Returns the name of the test set.
187     * 
188     * @return the name of the test set.
189     */
190    public String getName() {
191       if (name == null) {
192          name = String.valueOf(System.currentTimeMillis());
193       }
194 
195       return name;
196    } // getName
197 
198 
199    /**
200     * Returns the size of the test set. (Number of test classes)
201     * 
202     * @return the size of the test set.
203     */
204    public int size() {
205       return testClasses.size();
206    } // size
207 
208 
209    /**
210     * Returns the number of samples in the test set. This is the sum of all
211     * samples in all test classes.
212     * @return the number of samples
213     */
214    public int getNumberOfSamples() {
215       int result = 0;
216 
217       for (TestClass testClass : testClasses) {
218          result += testClass.size();
219       }
220 
221       return result;
222    }
223 
224 
225    /**
226     * Returns true if the test set is empty.
227     * 
228     * @return true if the test set is empty.
229     */
230    public boolean isEmpty() {
231       return testClasses.isEmpty();
232    } // isEmpty
233 
234 
235    /**
236     * {@inheritDoc}
237     */
238    @Override
239    public void accept(Visitor visitor) {
240       visitor.visit(this);
241 
242       for (TestClass testClass : testClasses) {
243          testClass.accept(visitor);
244       }
245 
246    } // accept
247 
248 
249    @Override
250    public String toString() {
251       return name;
252    } // toString
253 
254 }