1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.usageprofiles;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
22 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
23 | import de.ugoe.cs.autoquest.usageprofiles.PredictionByPartialMatch;
|
---|
24 |
|
---|
25 | import java.util.Random;
|
---|
26 | import org.junit.*;
|
---|
27 |
|
---|
28 | import static org.junit.Assert.*;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The class <code>PredictionByPartialMatchTest</code> contains tests for the
|
---|
32 | * class <code>{@link PredictionByPartialMatch}</code>.
|
---|
33 | *
|
---|
34 | * @author Steffen Herbold
|
---|
35 | * @version 1.0
|
---|
36 | */
|
---|
37 | public class PredictionByPartialMatchTest {
|
---|
38 |
|
---|
39 | Collection<List<Event>> sequences;
|
---|
40 |
|
---|
41 | @Test
|
---|
42 | public void testPredictionByPartialMatch_1() throws Exception {
|
---|
43 | int markovOrder = 2;
|
---|
44 | Random r = new Random();
|
---|
45 |
|
---|
46 | PredictionByPartialMatch result = new PredictionByPartialMatch(
|
---|
47 | markovOrder, r);
|
---|
48 |
|
---|
49 | assertNotNull(result);
|
---|
50 | assertEquals(markovOrder+1, result.trieOrder);
|
---|
51 | assertEquals(0, result.minOrder);
|
---|
52 | assertEquals(r, result.r);
|
---|
53 | assertEquals(0.1, result.probEscape, 0.0001);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
57 | public void testPredictionByPartialMatch_2() throws Exception {
|
---|
58 | int markovOrder = -1;
|
---|
59 | Random r = new Random();
|
---|
60 |
|
---|
61 | new PredictionByPartialMatch(markovOrder, r);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
65 | public void testPredictionByPartialMatch_3() throws Exception {
|
---|
66 | int markovOrder = 2;
|
---|
67 | Random r = null;
|
---|
68 |
|
---|
69 | new PredictionByPartialMatch(markovOrder, r);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test
|
---|
73 | public void testPredictionByPartialMatch_4() throws Exception {
|
---|
74 | int markovOrder = 2;
|
---|
75 | Random r = new Random();
|
---|
76 | double probEscape = 0.2;
|
---|
77 |
|
---|
78 | PredictionByPartialMatch result = new PredictionByPartialMatch(
|
---|
79 | markovOrder, r, probEscape);
|
---|
80 |
|
---|
81 | assertNotNull(result);
|
---|
82 | assertEquals(markovOrder+1, result.trieOrder);
|
---|
83 | assertEquals(0, result.minOrder);
|
---|
84 | assertEquals(r, result.r);
|
---|
85 | assertEquals(probEscape, result.probEscape, 0.0001);
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
89 | public void testPredictionByPartialMatch_5() throws Exception {
|
---|
90 | int markovOrder = -1;
|
---|
91 | Random r = new Random();
|
---|
92 | double probEscape = 0.2;
|
---|
93 |
|
---|
94 | new PredictionByPartialMatch(markovOrder, r, probEscape);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
98 | public void testPredictionByPartialMatch_6() throws Exception {
|
---|
99 | int markovOrder = 2;
|
---|
100 | Random r = null;
|
---|
101 | double probEscape = 0.2;
|
---|
102 |
|
---|
103 | new PredictionByPartialMatch(markovOrder, r, probEscape);
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
107 | public void testPredictionByPartialMatch_7() throws Exception {
|
---|
108 | int markovOrder = 2;
|
---|
109 | Random r = new Random();
|
---|
110 | double probEscape = 0.0;
|
---|
111 |
|
---|
112 | new PredictionByPartialMatch(markovOrder, r, probEscape);
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
116 | public void testPredictionByPartialMatch_8() throws Exception {
|
---|
117 | int markovOrder = 2;
|
---|
118 | Random r = new Random();
|
---|
119 | double probEscape = 1.0;
|
---|
120 |
|
---|
121 | new PredictionByPartialMatch(markovOrder, r, probEscape);
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Test
|
---|
125 | public void testPredictionByPartialMatch_9() throws Exception {
|
---|
126 | int markovOrder = 2;
|
---|
127 | Random r = new Random();
|
---|
128 | double probEscape = 0.2;
|
---|
129 | int minOrder = 1;
|
---|
130 |
|
---|
131 | PredictionByPartialMatch result = new PredictionByPartialMatch(
|
---|
132 | markovOrder, minOrder, r, probEscape);
|
---|
133 |
|
---|
134 | assertNotNull(result);
|
---|
135 | assertEquals(markovOrder+1, result.trieOrder);
|
---|
136 | assertEquals(minOrder, result.minOrder);
|
---|
137 | assertEquals(r, result.r);
|
---|
138 | assertEquals(probEscape, result.probEscape, 0.0001);
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
142 | public void testPredictionByPartialMatch_10() throws Exception {
|
---|
143 | int markovOrder = -1;
|
---|
144 | Random r = new Random();
|
---|
145 | double probEscape = 0.2;
|
---|
146 | int minOrder = 1;
|
---|
147 |
|
---|
148 | new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
152 | public void testPredictionByPartialMatch_11() throws Exception {
|
---|
153 | int markovOrder = 2;
|
---|
154 | Random r = null;
|
---|
155 | double probEscape = 0.2;
|
---|
156 | int minOrder = 1;
|
---|
157 |
|
---|
158 | new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
162 | public void testPredictionByPartialMatch_12() throws Exception {
|
---|
163 | int markovOrder = 2;
|
---|
164 | Random r = new Random();
|
---|
165 | double probEscape = 0.0;
|
---|
166 | int minOrder = 1;
|
---|
167 |
|
---|
168 | new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
172 | public void testPredictionByPartialMatch_13() throws Exception {
|
---|
173 | int markovOrder = 2;
|
---|
174 | Random r = new Random();
|
---|
175 | double probEscape = 1.0;
|
---|
176 | int minOrder = 1;
|
---|
177 |
|
---|
178 | new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
182 | public void testPredictionByPartialMatch_14() throws Exception {
|
---|
183 | int markovOrder = 2;
|
---|
184 | Random r = new Random();
|
---|
185 | double probEscape = 0.2;
|
---|
186 | int minOrder = 3;
|
---|
187 |
|
---|
188 | new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
192 | public void testPredictionByPartialMatch_15() throws Exception {
|
---|
193 | int markovOrder = 2;
|
---|
194 | Random r = new Random();
|
---|
195 | double probEscape = 0.2;
|
---|
196 | int minOrder = -1;
|
---|
197 |
|
---|
198 | new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
199 | }
|
---|
200 |
|
---|
201 | @Test
|
---|
202 | public void testGetProbEscape_1() throws Exception {
|
---|
203 | int markovOrder = 2;
|
---|
204 | Random r = new Random();
|
---|
205 | double probEscape = 0.2;
|
---|
206 | int minOrder = 1;
|
---|
207 |
|
---|
208 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
209 | fixture.probEscape = probEscape;
|
---|
210 |
|
---|
211 | double result = fixture.getProbEscape();
|
---|
212 |
|
---|
213 | assertEquals(probEscape, result, 0.0001);
|
---|
214 | }
|
---|
215 |
|
---|
216 | @Test
|
---|
217 | public void testGetProbability_1() throws Exception {
|
---|
218 | int markovOrder = 2;
|
---|
219 | Random r = new Random();
|
---|
220 | double probEscape = 0.2;
|
---|
221 | int minOrder = 1;
|
---|
222 |
|
---|
223 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
224 | fixture.train(sequences);
|
---|
225 |
|
---|
226 | List<Event> context = new ArrayList<Event>();
|
---|
227 | context.add(Event.STARTEVENT);
|
---|
228 | context.add(new Event(new StringEventType("a")));
|
---|
229 |
|
---|
230 | Event symbol = new Event(new StringEventType("b"));
|
---|
231 |
|
---|
232 | double result = fixture.getProbability(context, symbol);
|
---|
233 |
|
---|
234 | assertEquals(0.88d, result, 0.0001);
|
---|
235 | }
|
---|
236 |
|
---|
237 | @Test
|
---|
238 | public void testGetProbability_2() throws Exception {
|
---|
239 | int markovOrder = 2;
|
---|
240 | Random r = new Random();
|
---|
241 | double probEscape = 0.2;
|
---|
242 | int minOrder = 1;
|
---|
243 |
|
---|
244 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
245 | fixture.train(sequences);
|
---|
246 |
|
---|
247 | List<Event> context = new ArrayList<Event>();
|
---|
248 | context.add(Event.STARTEVENT);
|
---|
249 | context.add(new Event(new StringEventType("a")));
|
---|
250 |
|
---|
251 | Event symbol = new Event(new StringEventType("c"));
|
---|
252 |
|
---|
253 | double result = fixture.getProbability(context, symbol);
|
---|
254 |
|
---|
255 | assertEquals(0.04d, result, 0.0001);
|
---|
256 | }
|
---|
257 |
|
---|
258 | @Test
|
---|
259 | public void testGetProbability_3() throws Exception {
|
---|
260 | int markovOrder = 2;
|
---|
261 | Random r = new Random();
|
---|
262 | double probEscape = 0.2;
|
---|
263 | int minOrder = 2;
|
---|
264 |
|
---|
265 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
266 | fixture.train(sequences);
|
---|
267 |
|
---|
268 | List<Event> context = new ArrayList<Event>();
|
---|
269 | context.add(Event.STARTEVENT);
|
---|
270 | context.add(new Event(new StringEventType("a")));
|
---|
271 |
|
---|
272 | Event symbol = new Event(new StringEventType("b"));
|
---|
273 |
|
---|
274 | double result = fixture.getProbability(context, symbol);
|
---|
275 |
|
---|
276 | assertEquals(1.0d, result, 0.0001);
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Test
|
---|
280 | public void testGetProbability_4() throws Exception {
|
---|
281 | int markovOrder = 2;
|
---|
282 | Random r = new Random();
|
---|
283 | double probEscape = 0.2;
|
---|
284 | int minOrder = 2;
|
---|
285 |
|
---|
286 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
287 | fixture.train(sequences);
|
---|
288 |
|
---|
289 | List<Event> context = new ArrayList<Event>();
|
---|
290 | context.add(Event.STARTEVENT);
|
---|
291 | context.add(new Event(new StringEventType("a")));
|
---|
292 |
|
---|
293 | Event symbol = new Event(new StringEventType("c"));
|
---|
294 |
|
---|
295 | double result = fixture.getProbability(context, symbol);
|
---|
296 |
|
---|
297 | assertEquals(0.0d, result, 0.0001);
|
---|
298 | }
|
---|
299 |
|
---|
300 | @Test
|
---|
301 | public void testGetProbability_5() throws Exception {
|
---|
302 | int markovOrder = 2;
|
---|
303 | Random r = new Random();
|
---|
304 | double probEscape = 0.2;
|
---|
305 | int minOrder = 0;
|
---|
306 |
|
---|
307 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
308 | fixture.train(sequences);
|
---|
309 |
|
---|
310 | List<Event> context = new ArrayList<Event>();
|
---|
311 | context.add(Event.STARTEVENT);
|
---|
312 | context.add(new Event(new StringEventType("a")));
|
---|
313 |
|
---|
314 | Event symbol = new Event(new StringEventType("b"));
|
---|
315 |
|
---|
316 | double result = fixture.getProbability(context, symbol);
|
---|
317 |
|
---|
318 | assertEquals(0.8701d, result, 0.0001);
|
---|
319 | }
|
---|
320 |
|
---|
321 | @Test
|
---|
322 | public void testGetProbability_6() throws Exception {
|
---|
323 | int markovOrder = 2;
|
---|
324 | Random r = new Random();
|
---|
325 | double probEscape = 0.2;
|
---|
326 | int minOrder = 0;
|
---|
327 |
|
---|
328 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
329 | fixture.train(sequences);
|
---|
330 |
|
---|
331 | List<Event> context = new ArrayList<Event>();
|
---|
332 | context.add(Event.STARTEVENT);
|
---|
333 | context.add(new Event(new StringEventType("a")));
|
---|
334 |
|
---|
335 | Event symbol = new Event(new StringEventType("c"));
|
---|
336 |
|
---|
337 | double result = fixture.getProbability(context, symbol);
|
---|
338 |
|
---|
339 | assertEquals(0.0350, result, 0.0001);
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Test
|
---|
343 | public void testSetProbEscape_1() throws Exception {
|
---|
344 | int markovOrder = 2;
|
---|
345 | Random r = new Random();
|
---|
346 | double probEscape = 0.2;
|
---|
347 | int minOrder = 1;
|
---|
348 | double newProbEscape = 0.3;
|
---|
349 |
|
---|
350 | PredictionByPartialMatch fixture = new PredictionByPartialMatch(markovOrder, minOrder, r, probEscape);
|
---|
351 |
|
---|
352 | fixture.setProbEscape(newProbEscape);
|
---|
353 |
|
---|
354 | assertEquals(newProbEscape, fixture.probEscape, 0.0001);
|
---|
355 | }
|
---|
356 |
|
---|
357 | @Before
|
---|
358 | public void setUp() throws Exception {
|
---|
359 | List<Event> sequence = new ArrayList<Event>();
|
---|
360 | sequence.add(new Event(new StringEventType("a")));
|
---|
361 | sequence.add(new Event(new StringEventType("b")));
|
---|
362 | sequence.add(new Event(new StringEventType("r")));
|
---|
363 | sequence.add(new Event(new StringEventType("a")));
|
---|
364 | sequence.add(new Event(new StringEventType("c")));
|
---|
365 | sequence.add(new Event(new StringEventType("a")));
|
---|
366 | sequence.add(new Event(new StringEventType("d")));
|
---|
367 | sequence.add(new Event(new StringEventType("a")));
|
---|
368 | sequence.add(new Event(new StringEventType("b")));
|
---|
369 | sequence.add(new Event(new StringEventType("r")));
|
---|
370 | sequence.add(new Event(new StringEventType("a")));
|
---|
371 |
|
---|
372 | sequences = new ArrayList<List<Event>>();
|
---|
373 | sequences.add(sequence);
|
---|
374 | }
|
---|
375 |
|
---|
376 | public static void main(String[] args) {
|
---|
377 | new org.junit.runner.JUnitCore()
|
---|
378 | .run(PredictionByPartialMatchTest.class);
|
---|
379 | }
|
---|
380 | } |
---|