1 | package de.ugoe.cs.autoquest.usageprofiles;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Random;
|
---|
8 | import org.junit.*;
|
---|
9 |
|
---|
10 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
11 | import de.ugoe.cs.autoquest.eventcore.StringEventType;
|
---|
12 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
13 | import de.ugoe.cs.autoquest.usageprofiles.HighOrderMarkovModel;
|
---|
14 | import de.ugoe.cs.autoquest.usageprofiles.ModelFlattener;
|
---|
15 | import de.ugoe.cs.autoquest.usageprofiles.PredictionByPartialMatch;
|
---|
16 | import de.ugoe.cs.autoquest.usageprofiles.TrieNode;
|
---|
17 | import static org.junit.Assert.*;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * The class <code>ModelFlattenerTest</code> contains tests for the class
|
---|
21 | * <code>{@link ModelFlattener}</code>.
|
---|
22 | *
|
---|
23 | * @author Steffen Herbold
|
---|
24 | * @version 1.0
|
---|
25 | */
|
---|
26 | public class ModelFlattenerTest {
|
---|
27 |
|
---|
28 | List<Event> sequence;
|
---|
29 |
|
---|
30 | private static void assertCollectionContent(Collection<?> c1, Collection<?> c2) {
|
---|
31 | assertEquals(c1.size(), c2.size());
|
---|
32 | for (Object obj : c1) {
|
---|
33 | assertTrue(c2.contains(obj));
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Test
|
---|
38 | public void testFlattenHighOrderMarkovModel_1() throws Exception {
|
---|
39 | ModelFlattener fixture = new ModelFlattener();
|
---|
40 | HighOrderMarkovModel model = new HighOrderMarkovModel(2, new Random());
|
---|
41 | Collection<List<Event>> sequences = new ArrayList<List<Event>>();
|
---|
42 | sequences.add(sequence);
|
---|
43 | model.train(sequences);
|
---|
44 |
|
---|
45 | Collection<Event> expectedSymbols = new HashSet<Event>();
|
---|
46 | expectedSymbols.add(new Event(new StringEventType("a-=-END")));
|
---|
47 | expectedSymbols.add(new Event(new StringEventType("a-=-b")));
|
---|
48 | expectedSymbols.add(new Event(new StringEventType("a-=-c")));
|
---|
49 | expectedSymbols.add(new Event(new StringEventType("a-=-d")));
|
---|
50 | expectedSymbols.add(new Event(new StringEventType("b-=-r")));
|
---|
51 | expectedSymbols.add(new Event(new StringEventType("c-=-a")));
|
---|
52 | expectedSymbols.add(new Event(new StringEventType("d-=-a")));
|
---|
53 | expectedSymbols.add(new Event(new StringEventType("r-=-a")));
|
---|
54 | expectedSymbols.add(new Event(new StringEventType("START-=-a")));
|
---|
55 |
|
---|
56 | FirstOrderMarkovModel result = fixture.flattenHighOrderMarkovModel(model);
|
---|
57 |
|
---|
58 | assertCollectionContent(expectedSymbols, result.getEvents());
|
---|
59 |
|
---|
60 | TrieNode<Event> root = result.trie.find(null);
|
---|
61 | TrieNode<Event> root_aEnd = root.getChild(new Event(new StringEventType("a-=-END")));
|
---|
62 | TrieNode<Event> root_ab = root.getChild(new Event(new StringEventType("a-=-b")));
|
---|
63 | TrieNode<Event> root_ab_br = root_ab.getChild(new Event(new StringEventType("b-=-r")));
|
---|
64 | TrieNode<Event> root_ac = root.getChild(new Event(new StringEventType("a-=-c")));
|
---|
65 | TrieNode<Event> root_ac_ca = root_ac.getChild(new Event(new StringEventType("c-=-a")));
|
---|
66 | TrieNode<Event> root_ad = root.getChild(new Event(new StringEventType("a-=-d")));
|
---|
67 | TrieNode<Event> root_ad_da = root_ad.getChild(new Event(new StringEventType("d-=-a")));
|
---|
68 | TrieNode<Event> root_br = root.getChild(new Event(new StringEventType("b-=-r")));
|
---|
69 | TrieNode<Event> root_br_ra = root_br.getChild(new Event(new StringEventType("r-=-a")));
|
---|
70 | TrieNode<Event> root_ca = root.getChild(new Event(new StringEventType("c-=-a")));
|
---|
71 | TrieNode<Event> root_ca_ad = root_ca.getChild(new Event(new StringEventType("a-=-d")));
|
---|
72 | TrieNode<Event> root_da = root.getChild(new Event(new StringEventType("d-=-a")));
|
---|
73 | TrieNode<Event> root_da_ab = root_da.getChild(new Event(new StringEventType("a-=-b")));
|
---|
74 | TrieNode<Event> root_ra = root.getChild(new Event(new StringEventType("r-=-a")));
|
---|
75 | TrieNode<Event> root_ra_ac = root_ra.getChild(new Event(new StringEventType("a-=-c")));
|
---|
76 | TrieNode<Event> root_ra_aEnd = root_ra.getChild(new Event(new StringEventType("a-=-END")));
|
---|
77 | TrieNode<Event> root_startA = root.getChild(new Event(new StringEventType("START-=-a")));
|
---|
78 | TrieNode<Event> root_startA_ab =
|
---|
79 | root_startA.getChild(new Event(new StringEventType("a-=-b")));
|
---|
80 |
|
---|
81 | assertEquals(1, root_aEnd.getCount());
|
---|
82 | assertTrue(root_aEnd.isLeaf());
|
---|
83 | assertEquals(2, root_ab.getCount());
|
---|
84 | assertEquals(1, root_ab.getChildren().size());
|
---|
85 | assertEquals(2, root_ab_br.getCount());
|
---|
86 | assertTrue(root_ab_br.isLeaf());
|
---|
87 | assertEquals(1, root_ac.getCount());
|
---|
88 | assertEquals(1, root_ac.getChildren().size());
|
---|
89 | assertEquals(1, root_ac_ca.getCount());
|
---|
90 | assertTrue(root_ac_ca.isLeaf());
|
---|
91 | assertEquals(1, root_ad.getCount());
|
---|
92 | assertEquals(1, root_ad.getChildren().size());
|
---|
93 | assertEquals(1, root_ad_da.getCount());
|
---|
94 | assertTrue(root_ad_da.isLeaf());
|
---|
95 | assertEquals(2, root_br.getCount());
|
---|
96 | assertEquals(1, root_br.getChildren().size());
|
---|
97 | assertEquals(2, root_br_ra.getCount());
|
---|
98 | assertTrue(root_br_ra.isLeaf());
|
---|
99 | assertEquals(1, root_ca.getCount());
|
---|
100 | assertEquals(1, root_ca.getChildren().size());
|
---|
101 | assertEquals(1, root_ca_ad.getCount());
|
---|
102 | assertTrue(root_ca_ad.isLeaf());
|
---|
103 | assertEquals(1, root_da.getCount());
|
---|
104 | assertEquals(1, root_da.getChildren().size());
|
---|
105 | assertEquals(1, root_da_ab.getCount());
|
---|
106 | assertTrue(root_da_ab.isLeaf());
|
---|
107 | assertEquals(2, root_ra.getCount());
|
---|
108 | assertEquals(2, root_ra.getChildren().size());
|
---|
109 | assertEquals(1, root_ra_ac.getCount());
|
---|
110 | assertTrue(root_ra_ac.isLeaf());
|
---|
111 | assertEquals(1, root_ra_aEnd.getCount());
|
---|
112 | assertTrue(root_ra_aEnd.isLeaf());
|
---|
113 | assertEquals(1, root_startA.getCount());
|
---|
114 | assertEquals(1, root_startA.getChildren().size());
|
---|
115 | assertEquals(1, root_startA_ab.getCount());
|
---|
116 | assertTrue(root_startA_ab.isLeaf());
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Test
|
---|
120 | public void testFlattenPredictionByPartialMatch_1() throws Exception {
|
---|
121 | ModelFlattener fixture = new ModelFlattener();
|
---|
122 | PredictionByPartialMatch model = new PredictionByPartialMatch(1, new Random());
|
---|
123 |
|
---|
124 | FirstOrderMarkovModel result = fixture.flattenPredictionByPartialMatch(model);
|
---|
125 |
|
---|
126 | assertEquals(null, result);
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Before
|
---|
130 | public void setUp() throws Exception {
|
---|
131 | sequence = new ArrayList<Event>();
|
---|
132 | sequence.add(new Event(new StringEventType("a")));
|
---|
133 | sequence.add(new Event(new StringEventType("b")));
|
---|
134 | sequence.add(new Event(new StringEventType("r")));
|
---|
135 | sequence.add(new Event(new StringEventType("a")));
|
---|
136 | sequence.add(new Event(new StringEventType("c")));
|
---|
137 | sequence.add(new Event(new StringEventType("a")));
|
---|
138 | sequence.add(new Event(new StringEventType("d")));
|
---|
139 | sequence.add(new Event(new StringEventType("a")));
|
---|
140 | sequence.add(new Event(new StringEventType("b")));
|
---|
141 | sequence.add(new Event(new StringEventType("r")));
|
---|
142 | sequence.add(new Event(new StringEventType("a")));
|
---|
143 | }
|
---|
144 |
|
---|
145 | public static void main(String[] args) {
|
---|
146 | new org.junit.runner.JUnitCore().run(ModelFlattenerTest.class);
|
---|
147 | }
|
---|
148 | }
|
---|