[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.List;
|
---|
| 20 |
|
---|
[922] | 21 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
| 23 | import de.ugoe.cs.autoquest.usageprofiles.HighOrderMarkovModel;
|
---|
[518] | 24 |
|
---|
| 25 | import java.util.Random;
|
---|
| 26 | import org.junit.*;
|
---|
| 27 |
|
---|
| 28 | import static org.junit.Assert.*;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * The class <code>HighOrderMarkovModelTest</code> contains tests for the class
|
---|
| 32 | * <code>{@link HighOrderMarkovModel}</code>.
|
---|
| 33 | *
|
---|
| 34 | * @author Steffen Herbold
|
---|
| 35 | * @version 1.0
|
---|
| 36 | */
|
---|
| 37 | public class HighOrderMarkovModelTest {
|
---|
| 38 |
|
---|
[548] | 39 | Collection<List<Event>> sequences;
|
---|
[518] | 40 |
|
---|
| 41 | @Test
|
---|
| 42 | public void testHighOrderMarkovModel_1() throws Exception {
|
---|
| 43 | int maxOrder = 1;
|
---|
| 44 | Random r = new Random();
|
---|
| 45 |
|
---|
| 46 | HighOrderMarkovModel result = new HighOrderMarkovModel(maxOrder, r);
|
---|
| 47 |
|
---|
| 48 | assertNotNull(result);
|
---|
| 49 | assertEquals(r, result.r);
|
---|
| 50 | assertEquals(maxOrder + 1, result.trieOrder);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Test
|
---|
| 54 | public void testHighOrderMarkovModel_2() throws Exception {
|
---|
| 55 | int maxOrder = 0;
|
---|
| 56 | Random r = new Random();
|
---|
| 57 |
|
---|
| 58 | HighOrderMarkovModel result = new HighOrderMarkovModel(maxOrder, r);
|
---|
| 59 |
|
---|
| 60 | assertNotNull(result);
|
---|
| 61 | assertEquals(r, result.r);
|
---|
| 62 | assertEquals(maxOrder + 1, result.trieOrder);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[766] | 65 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
[518] | 66 | public void testHighOrderMarkovModel_3() throws Exception {
|
---|
| 67 | int maxOrder = 1;
|
---|
| 68 | Random r = null;
|
---|
| 69 |
|
---|
| 70 | new HighOrderMarkovModel(maxOrder, r);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[766] | 73 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
[518] | 74 | public void testHighOrderMarkovModel_4() throws Exception {
|
---|
| 75 | int maxOrder = -1;
|
---|
| 76 | Random r = new Random();
|
---|
| 77 |
|
---|
| 78 | new HighOrderMarkovModel(maxOrder, r);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Test
|
---|
| 82 | public void testGetProbability_1() throws Exception {
|
---|
| 83 | int markovOrder = 1;
|
---|
| 84 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 85 | new Random());
|
---|
| 86 | fixture.train(sequences);
|
---|
| 87 |
|
---|
[548] | 88 | List<Event> context = new ArrayList<Event>();
|
---|
| 89 | context.add(new Event(new StringEventType("a")));
|
---|
[518] | 90 |
|
---|
[548] | 91 | Event symbol = new Event(new StringEventType("b"));
|
---|
[518] | 92 |
|
---|
| 93 | double result = fixture.getProbability(context, symbol);
|
---|
| 94 |
|
---|
| 95 | assertEquals(2.0d / 5.0, result, 0.0001);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Test
|
---|
| 99 | public void testGetProbability_2() throws Exception {
|
---|
| 100 | int markovOrder = 1;
|
---|
| 101 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 102 | new Random());
|
---|
| 103 | fixture.train(sequences);
|
---|
| 104 |
|
---|
[548] | 105 | List<Event> context = new ArrayList<Event>();
|
---|
| 106 | context.add(new Event(new StringEventType("a")));
|
---|
[518] | 107 |
|
---|
[548] | 108 | Event symbol = new Event(new StringEventType("r"));
|
---|
[518] | 109 |
|
---|
| 110 | double result = fixture.getProbability(context, symbol);
|
---|
| 111 |
|
---|
| 112 | assertEquals(0.0d / 5.0, result, 0.0001);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | @Test
|
---|
| 116 | public void testGetProbability_3() throws Exception {
|
---|
| 117 | int markovOrder = 1;
|
---|
| 118 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 119 | new Random());
|
---|
| 120 | fixture.train(sequences);
|
---|
| 121 |
|
---|
[548] | 122 | List<Event> context = new ArrayList<Event>();
|
---|
| 123 | context.add(new Event(new StringEventType("a")));
|
---|
[518] | 124 |
|
---|
[548] | 125 | Event symbol = new Event(new StringEventType("c"));
|
---|
[518] | 126 |
|
---|
| 127 | double result = fixture.getProbability(context, symbol);
|
---|
| 128 |
|
---|
| 129 | assertEquals(1.0d / 5.0, result, 0.0001);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | @Test
|
---|
| 133 | public void testGetProbability_4() throws Exception {
|
---|
| 134 | int markovOrder = 1;
|
---|
| 135 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 136 | new Random());
|
---|
| 137 | fixture.train(sequences);
|
---|
| 138 |
|
---|
[548] | 139 | List<Event> context = new ArrayList<Event>();
|
---|
[518] | 140 | context.add(Event.STARTEVENT);
|
---|
[548] | 141 | context.add(new Event(new StringEventType("a")));
|
---|
[518] | 142 |
|
---|
[548] | 143 | Event symbol = new Event(new StringEventType("b"));
|
---|
[518] | 144 |
|
---|
| 145 | double result = fixture.getProbability(context, symbol);
|
---|
| 146 |
|
---|
| 147 | assertEquals(2.0d / 5.0, result, 0.0001);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | @Test
|
---|
| 151 | public void testGetProbability_5() throws Exception {
|
---|
| 152 | int markovOrder = 2;
|
---|
| 153 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 154 | new Random());
|
---|
| 155 | fixture.train(sequences);
|
---|
| 156 |
|
---|
[548] | 157 | List<Event> context = new ArrayList<Event>();
|
---|
[518] | 158 | context.add(Event.STARTEVENT);
|
---|
[548] | 159 | context.add(new Event(new StringEventType("a")));
|
---|
[518] | 160 |
|
---|
[548] | 161 | Event symbol = new Event(new StringEventType("b"));
|
---|
[518] | 162 |
|
---|
| 163 | double result = fixture.getProbability(context, symbol);
|
---|
| 164 |
|
---|
| 165 | assertEquals(1.0d, result, 0.0001);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | @Test
|
---|
| 169 | public void testGetProbability_6() throws Exception {
|
---|
| 170 | int markovOrder = 2;
|
---|
| 171 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 172 | new Random());
|
---|
| 173 | fixture.train(sequences);
|
---|
| 174 |
|
---|
[548] | 175 | List<Event> context = new ArrayList<Event>();
|
---|
[518] | 176 | context.add(Event.STARTEVENT);
|
---|
[548] | 177 | context.add(new Event(new StringEventType("b")));
|
---|
[518] | 178 |
|
---|
[548] | 179 | Event symbol = new Event(new StringEventType("b"));
|
---|
[518] | 180 |
|
---|
| 181 | double result = fixture.getProbability(context, symbol);
|
---|
| 182 |
|
---|
| 183 | assertEquals(0.0d, result, 0.0001);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | @Test
|
---|
| 187 | public void testGetProbability_7() throws Exception {
|
---|
| 188 | int markovOrder = 0;
|
---|
| 189 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 190 | new Random());
|
---|
| 191 | fixture.train(sequences);
|
---|
| 192 |
|
---|
[548] | 193 | List<Event> context = new ArrayList<Event>();
|
---|
[518] | 194 | context.add(Event.STARTEVENT);
|
---|
[548] | 195 | context.add(new Event(new StringEventType("b")));
|
---|
[518] | 196 |
|
---|
[548] | 197 | Event symbol = new Event(new StringEventType("a"));
|
---|
[518] | 198 |
|
---|
| 199 | double result = fixture.getProbability(context, symbol);
|
---|
| 200 |
|
---|
| 201 | assertEquals(5.0d / 13.0, result, 0.0001);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[766] | 204 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
[518] | 205 | public void testGetProbability_8() throws Exception {
|
---|
| 206 | int markovOrder = 0;
|
---|
| 207 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 208 | new Random());
|
---|
| 209 | fixture.train(sequences);
|
---|
| 210 |
|
---|
[548] | 211 | List<Event> context = new ArrayList<Event>();
|
---|
[518] | 212 | context.add(Event.STARTEVENT);
|
---|
[548] | 213 | context.add(new Event(new StringEventType("b")));
|
---|
[518] | 214 |
|
---|
[548] | 215 | Event symbol = null;
|
---|
[518] | 216 |
|
---|
| 217 | fixture.getProbability(context, symbol);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[766] | 220 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
[518] | 221 | public void testGetProbability_9() throws Exception {
|
---|
| 222 | int markovOrder = 0;
|
---|
| 223 | HighOrderMarkovModel fixture = new HighOrderMarkovModel(markovOrder,
|
---|
| 224 | new Random());
|
---|
| 225 | fixture.train(sequences);
|
---|
| 226 |
|
---|
[548] | 227 | List<Event> context = null;
|
---|
[518] | 228 |
|
---|
[548] | 229 | Event symbol = new Event(new StringEventType("b"));
|
---|
[518] | 230 |
|
---|
| 231 | fixture.getProbability(context, symbol);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | @Before
|
---|
| 235 | public void setUp() throws Exception {
|
---|
[548] | 236 | List<Event> sequence = new ArrayList<Event>();
|
---|
| 237 | sequence.add(new Event(new StringEventType("a")));
|
---|
| 238 | sequence.add(new Event(new StringEventType("b")));
|
---|
| 239 | sequence.add(new Event(new StringEventType("r")));
|
---|
| 240 | sequence.add(new Event(new StringEventType("a")));
|
---|
| 241 | sequence.add(new Event(new StringEventType("c")));
|
---|
| 242 | sequence.add(new Event(new StringEventType("a")));
|
---|
| 243 | sequence.add(new Event(new StringEventType("d")));
|
---|
| 244 | sequence.add(new Event(new StringEventType("a")));
|
---|
| 245 | sequence.add(new Event(new StringEventType("b")));
|
---|
| 246 | sequence.add(new Event(new StringEventType("r")));
|
---|
| 247 | sequence.add(new Event(new StringEventType("a")));
|
---|
[518] | 248 |
|
---|
[548] | 249 | sequences = new ArrayList<List<Event>>();
|
---|
[518] | 250 | sequences.add(sequence);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | public static void main(String[] args) {
|
---|
| 254 | new org.junit.runner.JUnitCore().run(HighOrderMarkovModelTest.class);
|
---|
| 255 | }
|
---|
| 256 | } |
---|