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 |
|
---|
15 | package de.ugoe.cs.autoquest.usageprofiles;
|
---|
16 |
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
22 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
23 | import de.ugoe.cs.autoquest.usageprofiles.HighOrderMarkovModel;
|
---|
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 |
|
---|
39 | Collection<List<Event>> sequences;
|
---|
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 |
|
---|
65 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
66 | public void testHighOrderMarkovModel_3() throws Exception {
|
---|
67 | int maxOrder = 1;
|
---|
68 | Random r = null;
|
---|
69 |
|
---|
70 | new HighOrderMarkovModel(maxOrder, r);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
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 |
|
---|
88 | List<Event> context = new ArrayList<Event>();
|
---|
89 | context.add(new Event(new StringEventType("a")));
|
---|
90 |
|
---|
91 | Event symbol = new Event(new StringEventType("b"));
|
---|
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 |
|
---|
105 | List<Event> context = new ArrayList<Event>();
|
---|
106 | context.add(new Event(new StringEventType("a")));
|
---|
107 |
|
---|
108 | Event symbol = new Event(new StringEventType("r"));
|
---|
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 |
|
---|
122 | List<Event> context = new ArrayList<Event>();
|
---|
123 | context.add(new Event(new StringEventType("a")));
|
---|
124 |
|
---|
125 | Event symbol = new Event(new StringEventType("c"));
|
---|
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 |
|
---|
139 | List<Event> context = new ArrayList<Event>();
|
---|
140 | context.add(Event.STARTEVENT);
|
---|
141 | context.add(new Event(new StringEventType("a")));
|
---|
142 |
|
---|
143 | Event symbol = new Event(new StringEventType("b"));
|
---|
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 |
|
---|
157 | List<Event> context = new ArrayList<Event>();
|
---|
158 | context.add(Event.STARTEVENT);
|
---|
159 | context.add(new Event(new StringEventType("a")));
|
---|
160 |
|
---|
161 | Event symbol = new Event(new StringEventType("b"));
|
---|
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 |
|
---|
175 | List<Event> context = new ArrayList<Event>();
|
---|
176 | context.add(Event.STARTEVENT);
|
---|
177 | context.add(new Event(new StringEventType("b")));
|
---|
178 |
|
---|
179 | Event symbol = new Event(new StringEventType("b"));
|
---|
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 |
|
---|
193 | List<Event> context = new ArrayList<Event>();
|
---|
194 | context.add(Event.STARTEVENT);
|
---|
195 | context.add(new Event(new StringEventType("b")));
|
---|
196 |
|
---|
197 | Event symbol = new Event(new StringEventType("a"));
|
---|
198 |
|
---|
199 | double result = fixture.getProbability(context, symbol);
|
---|
200 |
|
---|
201 | assertEquals(5.0d / 13.0, result, 0.0001);
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
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 |
|
---|
211 | List<Event> context = new ArrayList<Event>();
|
---|
212 | context.add(Event.STARTEVENT);
|
---|
213 | context.add(new Event(new StringEventType("b")));
|
---|
214 |
|
---|
215 | Event symbol = null;
|
---|
216 |
|
---|
217 | fixture.getProbability(context, symbol);
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Test(expected = java.lang.IllegalArgumentException.class)
|
---|
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 |
|
---|
227 | List<Event> context = null;
|
---|
228 |
|
---|
229 | Event symbol = new Event(new StringEventType("b"));
|
---|
230 |
|
---|
231 | fixture.getProbability(context, symbol);
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Before
|
---|
235 | public void setUp() throws Exception {
|
---|
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")));
|
---|
248 |
|
---|
249 | sequences = new ArrayList<List<Event>>();
|
---|
250 | sequences.add(sequence);
|
---|
251 | }
|
---|
252 |
|
---|
253 | public static void main(String[] args) {
|
---|
254 | new org.junit.runner.JUnitCore().run(HighOrderMarkovModelTest.class);
|
---|
255 | }
|
---|
256 | } |
---|