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