1 | package de.ugoe.cs.eventbench.models;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Random;
|
---|
8 | import de.ugoe.cs.eventbench.data.Event;
|
---|
9 | import org.junit.*;
|
---|
10 | import static org.junit.Assert.*;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * The class <code>TrieBasedModelTest</code> contains tests for the class <code>{@link TrieBasedModel}</code>.
|
---|
14 | *
|
---|
15 | * @author Steffen Herbold
|
---|
16 | * @version 1.0
|
---|
17 | */
|
---|
18 | public class TrieBasedModelTest {
|
---|
19 |
|
---|
20 | List<Event<?>> sequence;
|
---|
21 | Collection<Event<?>> symbols;
|
---|
22 |
|
---|
23 | private void assertTrieStructure(Trie<Event<?>> trie, int numSequences) {
|
---|
24 | TrieNode<Event<?>> root = trie.find(null);
|
---|
25 | TrieNode<Event<?>> root_a = root.getChild(new Event<String>("a"));
|
---|
26 | TrieNode<Event<?>> root_a_a = root_a.getChild(new Event<String>("a"));
|
---|
27 | TrieNode<Event<?>> root_a_b = root_a.getChild(new Event<String>("b"));
|
---|
28 | TrieNode<Event<?>> root_a_b_a = root_a_b.getChild(new Event<String>("a"));
|
---|
29 | TrieNode<Event<?>> root_a_b_b = root_a_b.getChild(new Event<String>("b"));
|
---|
30 | TrieNode<Event<?>> root_a_b_c = root_a_b.getChild(new Event<String>("c"));
|
---|
31 | TrieNode<Event<?>> root_a_b_d = root_a_b.getChild(new Event<String>("d"));
|
---|
32 | TrieNode<Event<?>> root_a_b_r = root_a_b.getChild(new Event<String>("r"));
|
---|
33 | TrieNode<Event<?>> root_a_c = root_a.getChild(new Event<String>("c"));
|
---|
34 | TrieNode<Event<?>> root_a_c_a = root_a_c.getChild(new Event<String>("a"));
|
---|
35 | TrieNode<Event<?>> root_a_c_b = root_a_c.getChild(new Event<String>("b"));
|
---|
36 | TrieNode<Event<?>> root_a_c_c = root_a_c.getChild(new Event<String>("c"));
|
---|
37 | TrieNode<Event<?>> root_a_c_d = root_a_c.getChild(new Event<String>("d"));
|
---|
38 | TrieNode<Event<?>> root_a_c_r = root_a_c.getChild(new Event<String>("r"));
|
---|
39 | TrieNode<Event<?>> root_a_d = root_a.getChild(new Event<String>("d"));
|
---|
40 | TrieNode<Event<?>> root_a_d_a = root_a_d.getChild(new Event<String>("a"));
|
---|
41 | TrieNode<Event<?>> root_a_d_b = root_a_d.getChild(new Event<String>("b"));
|
---|
42 | TrieNode<Event<?>> root_a_d_c = root_a_d.getChild(new Event<String>("c"));
|
---|
43 | TrieNode<Event<?>> root_a_d_d = root_a_d.getChild(new Event<String>("d"));
|
---|
44 | TrieNode<Event<?>> root_a_d_r = root_a_d.getChild(new Event<String>("r"));
|
---|
45 | TrieNode<Event<?>> root_a_r = root_a.getChild(new Event<String>("r"));
|
---|
46 | TrieNode<Event<?>> root_b = root.getChild(new Event<String>("b"));
|
---|
47 | TrieNode<Event<?>> root_b_a = root_b.getChild(new Event<String>("a"));
|
---|
48 | TrieNode<Event<?>> root_b_b = root_b.getChild(new Event<String>("b"));
|
---|
49 | TrieNode<Event<?>> root_b_c = root_b.getChild(new Event<String>("c"));
|
---|
50 | TrieNode<Event<?>> root_b_d = root_b.getChild(new Event<String>("d"));
|
---|
51 | TrieNode<Event<?>> root_b_r = root_b.getChild(new Event<String>("r"));
|
---|
52 | TrieNode<Event<?>> root_b_r_a = root_b_r.getChild(new Event<String>("a"));
|
---|
53 | TrieNode<Event<?>> root_b_r_b = root_b_r.getChild(new Event<String>("b"));
|
---|
54 | TrieNode<Event<?>> root_b_r_c = root_b_r.getChild(new Event<String>("c"));
|
---|
55 | TrieNode<Event<?>> root_b_r_d = root_b_r.getChild(new Event<String>("d"));
|
---|
56 | TrieNode<Event<?>> root_b_r_r = root_b_r.getChild(new Event<String>("r"));
|
---|
57 | TrieNode<Event<?>> root_c = root.getChild(new Event<String>("c"));
|
---|
58 | TrieNode<Event<?>> root_c_a = root_c.getChild(new Event<String>("a"));
|
---|
59 | TrieNode<Event<?>> root_c_a_a = root_c_a.getChild(new Event<String>("a"));
|
---|
60 | TrieNode<Event<?>> root_c_a_b = root_c_a.getChild(new Event<String>("b"));
|
---|
61 | TrieNode<Event<?>> root_c_a_c = root_c_a.getChild(new Event<String>("c"));
|
---|
62 | TrieNode<Event<?>> root_c_a_d = root_c_a.getChild(new Event<String>("d"));
|
---|
63 | TrieNode<Event<?>> root_c_a_r = root_c_a.getChild(new Event<String>("r"));
|
---|
64 | TrieNode<Event<?>> root_c_b = root_c.getChild(new Event<String>("b"));
|
---|
65 | TrieNode<Event<?>> root_c_c = root_c.getChild(new Event<String>("c"));
|
---|
66 | TrieNode<Event<?>> root_c_d = root_c.getChild(new Event<String>("d"));
|
---|
67 | TrieNode<Event<?>> root_c_r = root_c.getChild(new Event<String>("r"));
|
---|
68 | TrieNode<Event<?>> root_d = root.getChild(new Event<String>("d"));
|
---|
69 | TrieNode<Event<?>> root_d_a = root_d.getChild(new Event<String>("a"));
|
---|
70 | TrieNode<Event<?>> root_d_a_a = root_d_a.getChild(new Event<String>("a"));
|
---|
71 | TrieNode<Event<?>> root_d_a_b = root_d_a.getChild(new Event<String>("b"));
|
---|
72 | TrieNode<Event<?>> root_d_a_c = root_d_a.getChild(new Event<String>("c"));
|
---|
73 | TrieNode<Event<?>> root_d_a_d = root_d_a.getChild(new Event<String>("d"));
|
---|
74 | TrieNode<Event<?>> root_d_a_r = root_d_a.getChild(new Event<String>("r"));
|
---|
75 | TrieNode<Event<?>> root_d_b = root_d.getChild(new Event<String>("b"));
|
---|
76 | TrieNode<Event<?>> root_d_c = root_d.getChild(new Event<String>("c"));
|
---|
77 | TrieNode<Event<?>> root_d_d = root_d.getChild(new Event<String>("d"));
|
---|
78 | TrieNode<Event<?>> root_d_r = root_d.getChild(new Event<String>("r"));
|
---|
79 | TrieNode<Event<?>> root_r = root.getChild(new Event<String>("r"));
|
---|
80 | TrieNode<Event<?>> root_r_a = root_r.getChild(new Event<String>("a"));
|
---|
81 | TrieNode<Event<?>> root_r_a_a = root_r_a.getChild(new Event<String>("a"));
|
---|
82 | TrieNode<Event<?>> root_r_a_b = root_r_a.getChild(new Event<String>("b"));
|
---|
83 | TrieNode<Event<?>> root_r_a_c = root_r_a.getChild(new Event<String>("c"));
|
---|
84 | TrieNode<Event<?>> root_r_a_d = root_r_a.getChild(new Event<String>("d"));
|
---|
85 | TrieNode<Event<?>> root_r_a_r = root_r_a.getChild(new Event<String>("r"));
|
---|
86 | TrieNode<Event<?>> root_r_a_end = root_r_a.getChild(Event.ENDEVENT);
|
---|
87 | TrieNode<Event<?>> root_r_b = root_r.getChild(new Event<String>("b"));
|
---|
88 | TrieNode<Event<?>> root_r_c = root_r.getChild(new Event<String>("c"));
|
---|
89 | TrieNode<Event<?>> root_r_d = root_r.getChild(new Event<String>("d"));
|
---|
90 | TrieNode<Event<?>> root_r_r = root_r.getChild(new Event<String>("r"));
|
---|
91 | TrieNode<Event<?>> root_start = root.getChild(Event.STARTEVENT);
|
---|
92 | TrieNode<Event<?>> root_start_a = root_start.getChild(new Event<String>("a"));
|
---|
93 | TrieNode<Event<?>> root_start_a_a = root_start_a.getChild(new Event<String>("a"));
|
---|
94 | TrieNode<Event<?>> root_start_a_b = root_start_a.getChild(new Event<String>("b"));
|
---|
95 | TrieNode<Event<?>> root_start_a_c = root_start_a.getChild(new Event<String>("c"));
|
---|
96 | TrieNode<Event<?>> root_start_a_d = root_start_a.getChild(new Event<String>("d"));
|
---|
97 | TrieNode<Event<?>> root_start_a_r = root_start_a.getChild(new Event<String>("r"));
|
---|
98 | TrieNode<Event<?>> root_start_b = root_start.getChild(new Event<String>("b"));
|
---|
99 | TrieNode<Event<?>> root_start_c = root_start.getChild(new Event<String>("c"));
|
---|
100 | TrieNode<Event<?>> root_start_d = root_start.getChild(new Event<String>("d"));
|
---|
101 | TrieNode<Event<?>> root_start_r = root_start.getChild(new Event<String>("r"));
|
---|
102 |
|
---|
103 | assertEquals(numSequences*5, root_a.getCount());
|
---|
104 | assertNull(root_a_a);
|
---|
105 | assertEquals(numSequences*2, root_a_b.getCount());
|
---|
106 | assertNull(root_a_b_a);
|
---|
107 | assertNull(root_a_b_b);
|
---|
108 | assertNull(root_a_b_c);
|
---|
109 | assertNull(root_a_b_d);
|
---|
110 | assertEquals(numSequences*2, root_a_b_r.getCount());
|
---|
111 | assertEquals(numSequences*1, root_a_c.getCount());
|
---|
112 | assertEquals(numSequences*1, root_a_c_a.getCount());
|
---|
113 | assertNull(root_a_c_b);
|
---|
114 | assertNull(root_a_c_c);
|
---|
115 | assertNull(root_a_c_d);
|
---|
116 | assertNull(root_a_c_r);
|
---|
117 | assertEquals(numSequences*1, root_a_d.getCount());
|
---|
118 | assertEquals(numSequences*1, root_a_d_a.getCount());
|
---|
119 | assertNull(root_a_d_b);
|
---|
120 | assertNull(root_a_d_c);
|
---|
121 | assertNull(root_a_d_d);
|
---|
122 | assertNull(root_a_d_r);
|
---|
123 | assertNull(root_a_r);
|
---|
124 |
|
---|
125 | assertEquals(numSequences*2, root_b.getCount());
|
---|
126 | assertNull(root_b_a);
|
---|
127 | assertNull(root_b_b);
|
---|
128 | assertNull(root_b_c);
|
---|
129 | assertNull(root_b_d);
|
---|
130 | assertEquals(numSequences*2, root_b_r.getCount());
|
---|
131 | assertEquals(numSequences*2, root_b_r_a.getCount());
|
---|
132 | assertNull(root_b_r_b);
|
---|
133 | assertNull(root_b_r_c);
|
---|
134 | assertNull(root_b_r_d);
|
---|
135 | assertNull(root_b_r_r);
|
---|
136 |
|
---|
137 | assertEquals(numSequences*1, root_c.getCount());
|
---|
138 | assertEquals(numSequences*1, root_c_a.getCount());
|
---|
139 | assertNull(root_c_a_a);
|
---|
140 | assertNull(root_c_a_b);
|
---|
141 | assertNull(root_c_a_c);
|
---|
142 | assertEquals(numSequences*1, root_c_a_d.getCount());
|
---|
143 | assertNull(root_c_a_r);
|
---|
144 | assertNull(root_c_b);
|
---|
145 | assertNull(root_c_c);
|
---|
146 | assertNull(root_c_d);
|
---|
147 | assertNull(root_c_r);
|
---|
148 |
|
---|
149 | assertEquals(numSequences*1, root_d.getCount());
|
---|
150 | assertEquals(numSequences*1, root_d_a.getCount());
|
---|
151 | assertNull(root_d_a_a);
|
---|
152 | assertEquals(numSequences*1, root_d_a_b.getCount());
|
---|
153 | assertNull(root_d_a_c);
|
---|
154 | assertNull(root_d_a_d);
|
---|
155 | assertNull(root_d_a_r);
|
---|
156 | assertNull(root_d_b);
|
---|
157 | assertNull(root_d_c);
|
---|
158 | assertNull(root_d_d);
|
---|
159 | assertNull(root_d_r);
|
---|
160 |
|
---|
161 | assertEquals(numSequences*2, root_r.getCount());
|
---|
162 | assertEquals(numSequences*2, root_r_a.getCount());
|
---|
163 | assertNull(root_r_a_a);
|
---|
164 | assertNull(root_r_a_b);
|
---|
165 | assertEquals(numSequences*1, root_r_a_c.getCount());
|
---|
166 | assertNull(root_r_a_d);
|
---|
167 | assertNull(root_r_a_r);
|
---|
168 | assertEquals(numSequences*1, root_r_a_end.getCount());
|
---|
169 | assertNull(root_r_b);
|
---|
170 | assertNull(root_r_c);
|
---|
171 | assertNull(root_r_d);
|
---|
172 | assertNull(root_r_r);
|
---|
173 |
|
---|
174 | assertEquals(numSequences*1, root_start.getCount());
|
---|
175 | assertEquals(numSequences*1, root_start_a.getCount());
|
---|
176 | assertNull(root_start_a_a);
|
---|
177 | assertEquals(numSequences*1, root_start_a_b.getCount());
|
---|
178 | assertNull(root_start_a_c);
|
---|
179 | assertNull(root_start_a_d);
|
---|
180 | assertNull(root_start_a_r);
|
---|
181 | assertNull(root_start_b);
|
---|
182 | assertNull(root_start_c);
|
---|
183 | assertNull(root_start_d);
|
---|
184 | assertNull(root_start_r);
|
---|
185 |
|
---|
186 | // check if leafs are really leafs
|
---|
187 | assertTrue(root_a_b_r.isLeaf());
|
---|
188 | assertTrue(root_a_c_a.isLeaf());
|
---|
189 | assertTrue(root_a_d_a.isLeaf());
|
---|
190 | assertTrue(root_b_r_a.isLeaf());
|
---|
191 | assertTrue(root_c_a_d.isLeaf());
|
---|
192 | assertTrue(root_d_a_b.isLeaf());
|
---|
193 | assertTrue(root_r_a_c.isLeaf());
|
---|
194 | assertTrue(root_r_a_end.isLeaf());
|
---|
195 | }
|
---|
196 |
|
---|
197 | private static void assertCollectionContent(Collection<?> c1, Collection<?> c2) {
|
---|
198 | assertEquals(c1.size(), c2.size());
|
---|
199 | for( Object obj : c1 ) {
|
---|
200 | assertTrue(c2.contains(obj));
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | private class MockTrieBasedModel extends TrieBasedModel {
|
---|
205 | private static final long serialVersionUID = 1L;
|
---|
206 |
|
---|
207 | public MockTrieBasedModel(int markovOrder, Random r) {
|
---|
208 | super(markovOrder, r);
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public double getProbability(List<? extends Event<?>> context,
|
---|
213 | Event<?> symbol) {
|
---|
214 | List<Event<?>> list = new ArrayList<Event<?>>();
|
---|
215 | list.add(context.get(context.size()-1));
|
---|
216 | if( trie.find(list).getFollowingSymbols().contains(symbol) ) {
|
---|
217 | return 1;
|
---|
218 | } else {
|
---|
219 | return 0;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Test
|
---|
225 | public void testGenerateSequences_1()
|
---|
226 | throws Exception {
|
---|
227 | int markovOrder = 2;
|
---|
228 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
229 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
230 | sequences.add(sequence);
|
---|
231 | fixture.train(sequences);
|
---|
232 | int length = 2;
|
---|
233 |
|
---|
234 | Collection<List<Event<?>>> expected = new HashSet<List<Event<?>>>();
|
---|
235 | ArrayList<Event<?>> list;
|
---|
236 | list = new ArrayList<Event<?>>();
|
---|
237 | list.add(new Event<String>("a"));
|
---|
238 | list.add(Event.ENDEVENT);
|
---|
239 | expected.add(list);
|
---|
240 | list = new ArrayList<Event<?>>();
|
---|
241 | list.add(new Event<String>("a"));
|
---|
242 | list.add(new Event<String>("b"));
|
---|
243 | expected.add(list);
|
---|
244 | list = new ArrayList<Event<?>>();
|
---|
245 | list.add(new Event<String>("a"));
|
---|
246 | list.add(new Event<String>("c"));
|
---|
247 | expected.add(list);
|
---|
248 | list = new ArrayList<Event<?>>();
|
---|
249 | list.add(new Event<String>("a"));
|
---|
250 | list.add(new Event<String>("d"));
|
---|
251 | expected.add(list);
|
---|
252 | list = new ArrayList<Event<?>>();
|
---|
253 | list.add(new Event<String>("b"));
|
---|
254 | list.add(new Event<String>("r"));
|
---|
255 | expected.add(list);
|
---|
256 | list = new ArrayList<Event<?>>();
|
---|
257 | list.add(new Event<String>("c"));
|
---|
258 | list.add(new Event<String>("a"));
|
---|
259 | expected.add(list);
|
---|
260 | list = new ArrayList<Event<?>>();
|
---|
261 | list.add(new Event<String>("d"));
|
---|
262 | list.add(new Event<String>("a"));
|
---|
263 | expected.add(list);
|
---|
264 | list = new ArrayList<Event<?>>();
|
---|
265 | list.add(new Event<String>("r"));
|
---|
266 | list.add(new Event<String>("a"));
|
---|
267 | expected.add(list);
|
---|
268 | list = new ArrayList<Event<?>>();
|
---|
269 | list.add(Event.STARTEVENT);
|
---|
270 | list.add(new Event<String>("a"));
|
---|
271 | expected.add(list);
|
---|
272 |
|
---|
273 |
|
---|
274 | Collection<List<? extends Event<?>>> result = fixture.generateSequences(length);
|
---|
275 |
|
---|
276 | assertCollectionContent(expected, result);
|
---|
277 | }
|
---|
278 |
|
---|
279 | @Test
|
---|
280 | public void testGenerateSequences_2()
|
---|
281 | throws Exception {
|
---|
282 | int markovOrder = 2;
|
---|
283 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
284 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
285 | sequences.add(sequence);
|
---|
286 | fixture.train(sequences);
|
---|
287 | int length = 3;
|
---|
288 |
|
---|
289 | Collection<List<Event<?>>> expected = new HashSet<List<Event<?>>>();
|
---|
290 | ArrayList<Event<?>> list;
|
---|
291 | list = new ArrayList<Event<?>>();
|
---|
292 | list.add(Event.STARTEVENT);
|
---|
293 | list.add(new Event<String>("a"));
|
---|
294 | list.add(Event.ENDEVENT);
|
---|
295 | expected.add(list);
|
---|
296 | list = new ArrayList<Event<?>>();
|
---|
297 | list.add(Event.STARTEVENT);
|
---|
298 | list.add(new Event<String>("a"));
|
---|
299 | list.add(new Event<String>("b"));
|
---|
300 | expected.add(list);
|
---|
301 | list = new ArrayList<Event<?>>();
|
---|
302 | list.add(Event.STARTEVENT);
|
---|
303 | list.add(new Event<String>("a"));
|
---|
304 | list.add(new Event<String>("c"));
|
---|
305 | expected.add(list);
|
---|
306 | list = new ArrayList<Event<?>>();
|
---|
307 | list.add(Event.STARTEVENT);
|
---|
308 | list.add(new Event<String>("a"));
|
---|
309 | list.add(new Event<String>("d"));
|
---|
310 | expected.add(list);
|
---|
311 |
|
---|
312 | Collection<List<? extends Event<?>>> result = fixture.generateSequences(length, true);
|
---|
313 |
|
---|
314 | assertCollectionContent(expected, result);
|
---|
315 | }
|
---|
316 |
|
---|
317 | @Test( expected = java.security.InvalidParameterException.class )
|
---|
318 | public void testGenerateSequences_3()
|
---|
319 | throws Exception {
|
---|
320 | int markovOrder = 2;
|
---|
321 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
322 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
323 | sequences.add(sequence);
|
---|
324 | fixture.train(sequences);
|
---|
325 | int length = 0;
|
---|
326 |
|
---|
327 | fixture.generateSequences(length, false);
|
---|
328 | }
|
---|
329 |
|
---|
330 | @Test
|
---|
331 | public void testGenerateValidSequences_1()
|
---|
332 | throws Exception {
|
---|
333 | int markovOrder = 2;
|
---|
334 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
335 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
336 | sequences.add(sequence);
|
---|
337 | fixture.train(sequences);
|
---|
338 | int length = 5;
|
---|
339 |
|
---|
340 | Collection<List<Event<?>>> expected = new HashSet<List<Event<?>>>();
|
---|
341 | ArrayList<Event<?>> list;
|
---|
342 | list = new ArrayList<Event<?>>();
|
---|
343 | list.add(Event.STARTEVENT);
|
---|
344 | list.add(new Event<String>("a"));
|
---|
345 | list.add(new Event<String>("c"));
|
---|
346 | list.add(new Event<String>("a"));
|
---|
347 | list.add(Event.ENDEVENT);
|
---|
348 | expected.add(list);
|
---|
349 | list = new ArrayList<Event<?>>();
|
---|
350 | list.add(Event.STARTEVENT);
|
---|
351 | list.add(new Event<String>("a"));
|
---|
352 | list.add(new Event<String>("d"));
|
---|
353 | list.add(new Event<String>("a"));
|
---|
354 | list.add(Event.ENDEVENT);
|
---|
355 | expected.add(list);
|
---|
356 |
|
---|
357 | Collection<List<? extends Event<?>>> result = fixture.generateValidSequences(length);
|
---|
358 |
|
---|
359 | assertCollectionContent(expected, result);
|
---|
360 | }
|
---|
361 |
|
---|
362 | @Test(expected = java.security.InvalidParameterException.class )
|
---|
363 | public void testGenerateValidSequences_2()
|
---|
364 | throws Exception {
|
---|
365 | int markovOrder = 2;
|
---|
366 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
367 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
368 | sequences.add(sequence);
|
---|
369 | fixture.train(sequences);
|
---|
370 | int length = 0;
|
---|
371 |
|
---|
372 | fixture.generateValidSequences(length);
|
---|
373 | }
|
---|
374 |
|
---|
375 | @Test
|
---|
376 | public void testGetEvents_1()
|
---|
377 | throws Exception {
|
---|
378 | int markovOrder = 2;
|
---|
379 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
380 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
381 | sequences.add(sequence);
|
---|
382 |
|
---|
383 | fixture.train(sequences);
|
---|
384 |
|
---|
385 | Collection<? extends Event<?>> result = fixture.getEvents();
|
---|
386 |
|
---|
387 | assertCollectionContent(symbols, result);
|
---|
388 | }
|
---|
389 |
|
---|
390 | @Test
|
---|
391 | public void testGetEvents_2()
|
---|
392 | throws Exception {
|
---|
393 | int markovOrder = 2;
|
---|
394 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
395 |
|
---|
396 | Collection<? extends Event<?>> result = fixture.getEvents();
|
---|
397 |
|
---|
398 | assertCollectionContent(new HashSet<Event<?>>(), result);
|
---|
399 | }
|
---|
400 |
|
---|
401 | @Test
|
---|
402 | public void testGetNumFOMStates_1()
|
---|
403 | throws Exception {
|
---|
404 | int markovOrder = 2;
|
---|
405 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
406 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
407 | sequences.add(sequence);
|
---|
408 |
|
---|
409 | fixture.train(sequences);
|
---|
410 |
|
---|
411 | int result = fixture.getNumFOMStates();
|
---|
412 |
|
---|
413 | assertEquals(10, result);
|
---|
414 | }
|
---|
415 |
|
---|
416 | @Test
|
---|
417 | public void testGetNumFOMStates_2()
|
---|
418 | throws Exception {
|
---|
419 | int markovOrder = 2;
|
---|
420 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());;
|
---|
421 |
|
---|
422 | int result = fixture.getNumFOMStates();
|
---|
423 |
|
---|
424 | assertEquals(0, result);
|
---|
425 | }
|
---|
426 |
|
---|
427 | @Test
|
---|
428 | public void testGetNumSymbols_1()
|
---|
429 | throws Exception {
|
---|
430 | int markovOrder = 2;
|
---|
431 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
432 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
433 | sequences.add(sequence);
|
---|
434 | fixture.train(sequences);
|
---|
435 |
|
---|
436 | int result = fixture.getNumSymbols();
|
---|
437 |
|
---|
438 | assertEquals(7, result);
|
---|
439 | }
|
---|
440 |
|
---|
441 | @Test
|
---|
442 | public void testGetNumSymbols_2()
|
---|
443 | throws Exception {
|
---|
444 | int markovOrder = 2;
|
---|
445 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
446 |
|
---|
447 | int result = fixture.getNumSymbols();
|
---|
448 |
|
---|
449 | assertEquals(0, result);
|
---|
450 | }
|
---|
451 |
|
---|
452 | @Test
|
---|
453 | public void testGetNumTransitions_1()
|
---|
454 | throws Exception {
|
---|
455 | int markovOrder = 2;
|
---|
456 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
457 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
458 | sequences.add(sequence);
|
---|
459 | fixture.train(sequences);
|
---|
460 |
|
---|
461 | int result = fixture.getNumTransitions();
|
---|
462 |
|
---|
463 | assertEquals(11, result);
|
---|
464 | }
|
---|
465 |
|
---|
466 | @Test
|
---|
467 | public void testGetNumTransitions_2()
|
---|
468 | throws Exception {
|
---|
469 | int markovOrder = 2;
|
---|
470 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
471 |
|
---|
472 | int result = fixture.getNumTransitions();
|
---|
473 |
|
---|
474 | assertEquals(0, result);
|
---|
475 | }
|
---|
476 |
|
---|
477 | @Test
|
---|
478 | public void testTrain_1 ()
|
---|
479 | throws Exception {
|
---|
480 | int markovOrder = 2;
|
---|
481 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
482 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
483 | sequences.add(sequence);
|
---|
484 |
|
---|
485 | fixture.train(sequences);
|
---|
486 |
|
---|
487 | assertCollectionContent(symbols, fixture.getEvents());
|
---|
488 |
|
---|
489 | assertTrieStructure(fixture.trie, 1); }
|
---|
490 |
|
---|
491 | @Test
|
---|
492 | public void testTrain_2 ()
|
---|
493 | throws Exception {
|
---|
494 | int markovOrder = 2;
|
---|
495 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
496 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
497 | sequences.add(sequence);
|
---|
498 | sequences.add(sequence);
|
---|
499 |
|
---|
500 | fixture.train(sequences);
|
---|
501 |
|
---|
502 | assertCollectionContent(symbols, fixture.getEvents());
|
---|
503 |
|
---|
504 | assertTrieStructure(fixture.trie, 2);
|
---|
505 | }
|
---|
506 |
|
---|
507 | @Test
|
---|
508 | public void testUpdate_1()
|
---|
509 | throws Exception {
|
---|
510 | int markovOrder = 2;
|
---|
511 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
512 | Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
|
---|
513 | sequences.add(sequence);
|
---|
514 | fixture.train(sequences);
|
---|
515 |
|
---|
516 | fixture.update(sequences);
|
---|
517 |
|
---|
518 | assertCollectionContent(symbols, fixture.getEvents());
|
---|
519 | assertTrieStructure(fixture.trie, 2);
|
---|
520 | }
|
---|
521 |
|
---|
522 | @Test
|
---|
523 | public void testUpdate_2()
|
---|
524 | throws Exception {
|
---|
525 | int markovOrder = 2;
|
---|
526 | MockTrieBasedModel fixture = new MockTrieBasedModel(markovOrder, new Random());
|
---|
527 | Collection<List<Event<?>>> sequences = null;
|
---|
528 | fixture.trie = null;
|
---|
529 |
|
---|
530 | fixture.update(sequences);
|
---|
531 |
|
---|
532 | assertNull(fixture.trie);
|
---|
533 | }
|
---|
534 |
|
---|
535 | @Before
|
---|
536 | public void setUp()
|
---|
537 | throws Exception {
|
---|
538 | sequence = new ArrayList<Event<?>>();
|
---|
539 | sequence.add(new Event<String>("a"));
|
---|
540 | sequence.add(new Event<String>("b"));
|
---|
541 | sequence.add(new Event<String>("r"));
|
---|
542 | sequence.add(new Event<String>("a"));
|
---|
543 | sequence.add(new Event<String>("c"));
|
---|
544 | sequence.add(new Event<String>("a"));
|
---|
545 | sequence.add(new Event<String>("d"));
|
---|
546 | sequence.add(new Event<String>("a"));
|
---|
547 | sequence.add(new Event<String>("b"));
|
---|
548 | sequence.add(new Event<String>("r"));
|
---|
549 | sequence.add(new Event<String>("a"));
|
---|
550 |
|
---|
551 | symbols = new HashSet<Event<?>>();
|
---|
552 | symbols.add(new Event<String>("a"));
|
---|
553 | symbols.add(new Event<String>("b"));
|
---|
554 | symbols.add(new Event<String>("c"));
|
---|
555 | symbols.add(new Event<String>("d"));
|
---|
556 | symbols.add(new Event<String>("r"));
|
---|
557 | symbols.add(Event.STARTEVENT);
|
---|
558 | symbols.add(Event.ENDEVENT);
|
---|
559 | }
|
---|
560 |
|
---|
561 | @After
|
---|
562 | public void tearDown()
|
---|
563 | throws Exception {
|
---|
564 | // Add additional tear down code here
|
---|
565 | }
|
---|
566 |
|
---|
567 | public static void main(String[] args) {
|
---|
568 | new org.junit.runner.JUnitCore().run(TrieBasedModelTest.class);
|
---|
569 | }
|
---|
570 | } |
---|