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