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