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