source: trunk/EventBenchCoreTest/src/de/ugoe/cs/eventbench/models/PredictionByPartialMatchTest.java @ 343

Last change on this file since 343 was 343, checked in by sherbold, 12 years ago

*added JUnit test cases for de.ugoe.cs.eventbench.models.DeterministicFiniteAutomaton?, HighOrderMarkovModel?, FirstOrderMarkovModel?, and PredictionByPartialMatch?

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