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