Changeset 547 for trunk/quest-core-usageprofiles/src
- Timestamp:
- 08/16/12 12:34:24 (12 years ago)
- Location:
- trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/DeterministicFiniteAutomaton.java
r518 r547 51 51 */ 52 52 @Override 53 public double getProbability(List< ? extends Event<?>> context,54 Event <?>symbol) {53 public double getProbability(List<Event> context, 54 Event symbol) { 55 55 if( context==null ) { 56 56 throw new InvalidParameterException("context must not be null"); … … 61 61 double result = 0.0d; 62 62 63 List<Event <?>> contextCopy;63 List<Event> contextCopy; 64 64 if (context.size() >= trieOrder) { 65 contextCopy = new LinkedList<Event <?>>(context.subList(65 contextCopy = new LinkedList<Event>(context.subList( 66 66 context.size() - trieOrder + 1, context.size())); 67 67 } else { 68 contextCopy = new LinkedList<Event <?>>(context);68 contextCopy = new LinkedList<Event>(context); 69 69 } 70 70 71 Collection<Event <?>> followers = trie.getFollowingSymbols(contextCopy);71 Collection<Event> followers = trie.getFollowingSymbols(contextCopy); 72 72 73 73 if (followers.size() != 0 && followers.contains(symbol)) { -
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/FirstOrderMarkovModel.java
r518 r547 67 67 */ 68 68 private Matrix getTransmissionMatrix() { 69 List<Event <?>> knownSymbols = new ArrayList<Event<?>>(69 List<Event> knownSymbols = new ArrayList<Event>( 70 70 trie.getKnownSymbols()); 71 71 int numStates = knownSymbols.size(); … … 73 73 74 74 for (int i = 0; i < numStates; i++) { 75 Event <?>currentSymbol = knownSymbols.get(i);76 List<Event <?>> context = new ArrayList<Event<?>>();75 Event currentSymbol = knownSymbols.get(i); 76 List<Event> context = new ArrayList<Event>(); 77 77 context.add(currentSymbol); 78 78 for (int j = 0; j < numStates; j++) { 79 Event <?>follower = knownSymbols.get(j);79 Event follower = knownSymbols.get(j); 80 80 double prob = getProbability(context, follower); 81 81 transmissionMatrix.set(i, j, prob); … … 96 96 public double calcEntropy() { 97 97 Matrix transmissionMatrix = getTransmissionMatrix(); 98 List<Event <?>> knownSymbols = new ArrayList<Event<?>>(98 List<Event> knownSymbols = new ArrayList<Event>( 99 99 trie.getKnownSymbols()); 100 100 int numStates = knownSymbols.size(); … … 173 173 stringBuilder.append("digraph model {" + StringTools.ENDLINE); 174 174 175 List<Event <?>> knownSymbols = new ArrayList<Event<?>>(176 trie.getKnownSymbols()); 177 for (Event <?>symbol : knownSymbols) {175 List<Event> knownSymbols = new ArrayList<Event>( 176 trie.getKnownSymbols()); 177 for (Event symbol : knownSymbols) { 178 178 final String thisSaneId = symbol.getShortId().replace("\"", "\\\"") 179 179 .replaceAll("[\r\n]", ""); 180 180 stringBuilder.append(" " + knownSymbols.indexOf(symbol) + " [label=\"" 181 181 + thisSaneId + "\"];" + StringTools.ENDLINE); 182 List<Event <?>> context = new ArrayList<Event<?>>();182 List<Event> context = new ArrayList<Event>(); 183 183 context.add(symbol); 184 Collection<Event <?>> followers = trie.getFollowingSymbols(context);185 for (Event <?>follower : followers) {184 Collection<Event> followers = trie.getFollowingSymbols(context); 185 for (Event follower : followers) { 186 186 stringBuilder.append(" " + knownSymbols.indexOf(symbol) + " -> " 187 187 + knownSymbols.indexOf(follower) + " "); … … 206 206 Graph<String, MarkovEdge> graph = new SparseMultigraph<String, MarkovEdge>(); 207 207 208 List<Event <?>> knownSymbols = new ArrayList<Event<?>>(209 trie.getKnownSymbols()); 210 211 for (Event <?>symbol : knownSymbols) {208 List<Event> knownSymbols = new ArrayList<Event>( 209 trie.getKnownSymbols()); 210 211 for (Event symbol : knownSymbols) { 212 212 String from = symbol.getShortId(); 213 List<Event <?>> context = new ArrayList<Event<?>>();213 List<Event> context = new ArrayList<Event>(); 214 214 context.add(symbol); 215 215 216 Collection<Event <?>> followers = trie.getFollowingSymbols(context);217 218 for (Event <?>follower : followers) {216 Collection<Event> followers = trie.getFollowingSymbols(context); 217 218 for (Event follower : followers) { 219 219 String to = follower.getShortId(); 220 220 MarkovEdge prob = new MarkovEdge(getProbability(context, -
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/HighOrderMarkovModel.java
r518 r547 53 53 */ 54 54 @Override 55 public double getProbability(List< ? extends Event<?>> context,56 Event <?>symbol) {55 public double getProbability(List<Event> context, 56 Event symbol) { 57 57 if (context == null) { 58 58 throw new InvalidParameterException("context must not be null"); … … 63 63 double result = 0.0d; 64 64 65 List<Event <?>> contextCopy;65 List<Event> contextCopy; 66 66 if (context.size() >= trieOrder) { 67 contextCopy = new LinkedList<Event <?>>(context.subList(67 contextCopy = new LinkedList<Event>(context.subList( 68 68 context.size() - trieOrder + 1, context.size())); 69 69 } else { 70 contextCopy = new LinkedList<Event <?>>(context);70 contextCopy = new LinkedList<Event>(context); 71 71 } 72 72 73 Collection<Event <?>> followers = trie.getFollowingSymbols(contextCopy);73 Collection<Event> followers = trie.getFollowingSymbols(contextCopy); 74 74 int sumCountFollowers = 0; // N(s\sigma') 75 for (Event <?>follower : followers) {75 for (Event follower : followers) { 76 76 sumCountFollowers += trie.getCount(contextCopy, follower); 77 77 } -
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/IStochasticProcess.java
r518 r547 34 34 * thrown if context or symbol is null 35 35 */ 36 double getProbability(List< ? extends Event<?>> context, Event<?>symbol);36 double getProbability(List<Event> context, Event symbol); 37 37 38 38 /** … … 48 48 * thrown if sequence is null 49 49 */ 50 double getProbability(List< ? extends Event<?>> sequence);50 double getProbability(List<Event> sequence); 51 51 52 52 /** … … 58 58 * @return randomly generated sequence 59 59 */ 60 public List< ? extends Event<?>> randomSequence();60 public List<Event> randomSequence(); 61 61 62 62 /** … … 81 81 * 82 82 */ 83 public List< ? extends Event<?>> randomSequence(int maxLength,83 public List<Event> randomSequence(int maxLength, 84 84 boolean validEnd); 85 85 … … 98 98 * thrown if length is less than or equal to 0 99 99 */ 100 public Collection<List< ? extends Event<?>>> generateSequences(int length);100 public Collection<List<Event>> generateSequences(int length); 101 101 102 102 /** … … 118 118 * thrown if length is less than or equal to 0 119 119 */ 120 public Collection<List< ? extends Event<?>>> generateSequences(int length,120 public Collection<List<Event>> generateSequences(int length, 121 121 boolean fromStart); 122 122 … … 135 135 * thrown if length is less than or equal to 0 136 136 */ 137 public Collection<List< ? extends Event<?>>> generateValidSequences(137 public Collection<List<Event>> generateValidSequences( 138 138 int length); 139 139 … … 190 190 * @return events known by the process 191 191 */ 192 public Collection< ? extends Event<?>> getEvents();192 public Collection<Event> getEvents(); 193 193 194 194 } -
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/ModelFlattener.java
r518 r547 27 27 private static final String EVENT_SEPARATOR = "-=-"; 28 28 29 Trie<Event <?>> firstOrderTrie;29 Trie<Event> firstOrderTrie; 30 30 31 31 /** … … 47 47 firstOrderModel.trieOrder = 2; 48 48 if (markovOrder == 1) { 49 firstOrderModel.trie = new Trie<Event <?>>(model.trie);49 firstOrderModel.trie = new Trie<Event>(model.trie); 50 50 firstOrderModel.trieOrder = 2; 51 51 } else { 52 firstOrderTrie = new Trie<Event <?>>();53 TrieNode<Event <?>> rootNode = model.trie.find(null);52 firstOrderTrie = new Trie<Event>(); 53 TrieNode<Event> rootNode = model.trie.find(null); 54 54 generateFirstOrderTrie(rootNode, new LinkedList<String>(), markovOrder); 55 55 firstOrderTrie.updateKnownSymbols(); … … 98 98 * depth to go - NOT the current depth. 99 99 */ 100 private void generateFirstOrderTrie(TrieNode<Event <?>> currentNode,100 private void generateFirstOrderTrie(TrieNode<Event> currentNode, 101 101 List<String> parentIDs, int depth) { 102 for (TrieNode<Event <?>> child : currentNode.getChildren()) {102 for (TrieNode<Event> child : currentNode.getChildren()) { 103 103 String currentId = child.getSymbol().getStandardId(); 104 104 if (depth > 1) { … … 113 113 } 114 114 firstOrderID.append(currentId); 115 TrieNode<Event <?>> firstOrderNode = firstOrderTrie115 TrieNode<Event> firstOrderNode = firstOrderTrie 116 116 .getChildCreate(new Event<Object>(firstOrderID 117 117 .toString())); 118 118 firstOrderNode.setCount(child.getCount()); 119 for (TrieNode<Event <?>> transitionChild : child.getChildren()) {119 for (TrieNode<Event> transitionChild : child.getChildren()) { 120 120 StringBuilder transitionID = new StringBuilder(); 121 121 for (String parentID : parentIDs.subList(1, … … 126 126 transitionID.append(transitionChild.getSymbol() 127 127 .getStandardId()); 128 TrieNode<Event <?>> firstOrderTransitionChild = firstOrderNode128 TrieNode<Event> firstOrderTransitionChild = firstOrderNode 129 129 .getChildCreate(new Event<Object>(transitionID 130 130 .toString())); -
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/PredictionByPartialMatch.java
r518 r547 149 149 */ 150 150 @Override 151 public double getProbability(List< ? extends Event<?>> context,152 Event <?>symbol) {151 public double getProbability(List<Event> context, 152 Event symbol) { 153 153 if (context == null) { 154 154 throw new InvalidParameterException("context must not be null"); … … 161 161 double resultShorterContex = 0.0d; 162 162 163 List<Event <?>> contextCopy;163 List<Event> contextCopy; 164 164 if (context.size() >= trieOrder) { 165 contextCopy = new LinkedList<Event <?>>(context.subList(165 contextCopy = new LinkedList<Event>(context.subList( 166 166 context.size() - trieOrder + 1, context.size())); 167 167 } else { 168 contextCopy = new LinkedList<Event <?>>(context);169 } 170 171 Collection<Event <?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'168 contextCopy = new LinkedList<Event>(context); 169 } 170 171 Collection<Event> followers = trie.getFollowingSymbols(contextCopy); // \Sigma' 172 172 int sumCountFollowers = 0; // N(s\sigma') 173 for (Event <?>follower : followers) {173 for (Event follower : followers) { 174 174 sumCountFollowers += trie.getCount(contextCopy, follower); 175 175 } -
trunk/quest-core-usageprofiles/src/main/java/de/ugoe/cs/quest/usageprofiles/TrieBasedModel.java
r518 r547 49 49 * </p> 50 50 */ 51 protected Trie<Event <?>> trie = null;51 protected Trie<Event> trie = null; 52 52 53 53 /** … … 101 101 * thrown is sequences is null 102 102 */ 103 public void train(Collection<List< ? extends Event<?>>> sequences) {103 public void train(Collection<List<Event>> sequences) { 104 104 trie = null; 105 105 update(sequences); … … 119 119 * thrown is sequences is null 120 120 */ 121 public void update(Collection<List< ? extends Event<?>>> sequences) {121 public void update(Collection<List<Event>> sequences) { 122 122 if (sequences == null) { 123 123 throw new InvalidParameterException("sequences must not be null"); 124 124 } 125 125 if (trie == null) { 126 trie = new Trie<Event <?>>();127 } 128 for (List< ? extends Event<?>> sequence : sequences) {129 List<Event <?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive126 trie = new Trie<Event>(); 127 } 128 for (List<Event> sequence : sequences) { 129 List<Event> currentSequence = new LinkedList<Event>(sequence); // defensive 130 130 // copy 131 131 currentSequence.add(0, Event.STARTEVENT); … … 142 142 */ 143 143 @Override 144 public List< ? extends Event<?>> randomSequence() {144 public List<Event> randomSequence() { 145 145 return randomSequence(Integer.MAX_VALUE, true); 146 146 } … … 152 152 */ 153 153 @Override 154 public List< ? extends Event<?>> randomSequence(int maxLength,154 public List<Event> randomSequence(int maxLength, 155 155 boolean validEnd) { 156 List<Event <?>> sequence = new LinkedList<Event<?>>();156 List<Event> sequence = new LinkedList<Event>(); 157 157 if (trie != null) { 158 158 boolean endFound = false; 159 159 while (!endFound) { // outer loop for length checking 160 sequence = new LinkedList<Event <?>>();161 IncompleteMemory<Event <?>> context = new IncompleteMemory<Event<?>>(160 sequence = new LinkedList<Event>(); 161 IncompleteMemory<Event> context = new IncompleteMemory<Event>( 162 162 trieOrder - 1); 163 163 context.add(Event.STARTEVENT); … … 166 166 double randVal = r.nextDouble(); 167 167 double probSum = 0.0; 168 List<Event <?>> currentContext = context.getLast(trieOrder);169 for (Event <?>symbol : trie.getKnownSymbols()) {168 List<Event> currentContext = context.getLast(trieOrder); 169 for (Event symbol : trie.getKnownSymbols()) { 170 170 probSum += getProbability(currentContext, symbol); 171 171 if (probSum >= randVal) { … … 262 262 String[] stateStrings = new String[getNumSymbols()]; 263 263 int i = 0; 264 for (Event <?>symbol : trie.getKnownSymbols()) {264 for (Event symbol : trie.getKnownSymbols()) { 265 265 if (symbol.toString() == null) { 266 266 stateStrings[i] = "null"; … … 279 279 */ 280 280 @Override 281 public Collection< ? extends Event<?>> getEvents() {282 if (trie == null) { 283 return new HashSet<Event <?>>();281 public Collection<Event> getEvents() { 282 if (trie == null) { 283 return new HashSet<Event>(); 284 284 } else { 285 285 return trie.getKnownSymbols(); … … 294 294 */ 295 295 @Override 296 public Collection<List< ? extends Event<?>>> generateSequences(int length) {296 public Collection<List<Event>> generateSequences(int length) { 297 297 return generateSequences(length, false); 298 298 } … … 306 306 */ 307 307 @Override 308 public Set<List< ? extends Event<?>>> generateSequences(int length,308 public Set<List<Event>> generateSequences(int length, 309 309 boolean fromStart) { 310 Set<List< ? extends Event<?>>> sequenceSet = new LinkedHashSet<List<? extends Event<?>>>();310 Set<List<Event>> sequenceSet = new LinkedHashSet<List<Event>>(); 311 311 if (length < 1) { 312 312 throw new InvalidParameterException( … … 315 315 if (length == 1) { 316 316 if (fromStart) { 317 List<Event <?>> subSeq = new LinkedList<Event<?>>();317 List<Event> subSeq = new LinkedList<Event>(); 318 318 subSeq.add(Event.STARTEVENT); 319 319 sequenceSet.add(subSeq); 320 320 } else { 321 for (Event <?>event : getEvents()) {322 List<Event <?>> subSeq = new LinkedList<Event<?>>();321 for (Event event : getEvents()) { 322 List<Event> subSeq = new LinkedList<Event>(); 323 323 subSeq.add(event); 324 324 sequenceSet.add(subSeq); … … 327 327 return sequenceSet; 328 328 } 329 Collection< ? extends Event<?>> events = getEvents();330 Collection<List< ? extends Event<?>>> seqsShorter = generateSequences(329 Collection<Event> events = getEvents(); 330 Collection<List<Event>> seqsShorter = generateSequences( 331 331 length - 1, fromStart); 332 for (Event <?>event : events) {333 for (List< ? extends Event<?>> seqShorter : seqsShorter) {334 Event <?>lastEvent = event;332 for (Event event : events) { 333 for (List<Event> seqShorter : seqsShorter) { 334 Event lastEvent = event; 335 335 if (getProbability(seqShorter, lastEvent) > 0.0) { 336 List<Event <?>> subSeq = new ArrayList<Event<?>>(seqShorter);336 List<Event> subSeq = new ArrayList<Event>(seqShorter); 337 337 subSeq.add(lastEvent); 338 338 sequenceSet.add(subSeq); … … 351 351 */ 352 352 @Override 353 public Collection<List< ? extends Event<?>>> generateValidSequences(353 public Collection<List<Event>> generateValidSequences( 354 354 int length) { 355 355 // check for min-length implicitly done by generateSequences 356 Collection<List< ? extends Event<?>>> allSequences = generateSequences(356 Collection<List<Event>> allSequences = generateSequences( 357 357 length, true); 358 Collection<List< ? extends Event<?>>> validSequences = new LinkedHashSet<List<? extends Event<?>>>();359 for (List< ? extends Event<?>> sequence : allSequences) {358 Collection<List<Event>> validSequences = new LinkedHashSet<List<Event>>(); 359 for (List<Event> sequence : allSequences) { 360 360 if (sequence.size() == length 361 361 && Event.ENDEVENT.equals(sequence.get(sequence.size() - 1))) { … … 374 374 */ 375 375 @Override 376 public double getProbability(List< ? extends Event<?>> sequence) {376 public double getProbability(List<Event> sequence) { 377 377 if (sequence == null) { 378 378 throw new InvalidParameterException("sequence must not be null"); 379 379 } 380 380 double prob = 1.0; 381 List<Event <?>> context = new LinkedList<Event<?>>();382 for (Event <?>event : sequence) {381 List<Event> context = new LinkedList<Event>(); 382 for (Event event : sequence) { 383 383 prob *= getProbability(context, event); 384 384 context.add(event);
Note: See TracChangeset
for help on using the changeset viewer.