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