Changeset 184 for trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDtrainMarkovModel.java
- Timestamp:
- 09/14/11 20:17:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDtrainMarkovModel.java
r171 r184 1 1 package de.ugoe.cs.eventbench.commands; 2 2 3 import java.security.InvalidParameterException;4 3 import java.util.List; 5 4 import java.util.Random; 6 5 7 import de.ugoe.cs.eventbench.data.Event;8 import de.ugoe.cs.eventbench.data.GlobalDataContainer;9 6 import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel; 10 7 import de.ugoe.cs.eventbench.models.HighOrderMarkovModel; 11 import de.ugoe.cs. util.console.Command;8 import de.ugoe.cs.eventbench.models.TrieBasedModel; 12 9 import de.ugoe.cs.util.console.Console; 13 10 … … 18 15 * 19 16 * @author Steffen Herbold 20 * @version 1.017 * @version 2.0 21 18 */ 22 public class CMDtrainMarkovModel implements Command { 19 public class CMDtrainMarkovModel extends AbstractTrainCommand { 20 21 /** 22 * <p> 23 * Order of the Markov model. 24 * </p> 25 */ 26 int order; 23 27 24 28 /* … … 29 33 @Override 30 34 public void help() { 31 Console.println("Usage: trainMarkovModel <modelName> {<order>}"); 35 Console.println("Usage: trainMarkovModel <modelName> <sequencesName> {<order>}"); 36 } 37 38 /** 39 * <p> 40 * Handles the parameter order. 41 * </p> 42 * 43 * @see de.ugoe.cs.eventbench.commands.AbstractTrainCommand#handleOptionalParameters(java.util.List) 44 */ 45 @Override 46 void handleAdditionalParameters(List<Object> parameters) throws Exception { 47 if (parameters.size() >= 3) { 48 order = Integer.parseInt((String) parameters.get(2)); 49 } 32 50 } 33 51 … … 35 53 * (non-Javadoc) 36 54 * 37 * @see de.ugoe.cs. util.console.Command#run(java.util.List)55 * @see de.ugoe.cs.eventbench.commands.AbstractTrainCommand#createModel() 38 56 */ 39 @SuppressWarnings("unchecked")40 57 @Override 41 public void run(List<Object> parameters) { 42 String modelname; 43 int order = 1; 44 try { 45 modelname = (String) parameters.get(0); 46 if (parameters.size() == 2) { 47 order = Integer.parseInt((String) parameters.get(1)); 48 } 49 } catch (Exception e) { 50 throw new InvalidParameterException(); 51 } 52 53 List<List<Event<?>>> sequences = null; 54 Object dataObject = GlobalDataContainer.getInstance().getData( 55 "sequences"); 56 57 try { 58 sequences = (List<List<Event<?>>>) dataObject; 59 if (sequences.size() > 0) { 60 if (sequences.get(0).get(0) instanceof Event) { 61 HighOrderMarkovModel model; 62 if (order == 1) { 63 model = new FirstOrderMarkovModel(new Random()); 64 } else { 65 model = new HighOrderMarkovModel(order, new Random()); 66 } 67 model.train(sequences); 68 if (GlobalDataContainer.getInstance().addData(modelname, 69 model)) { 70 Console.traceln("Old data \"" + modelname 71 + "\" overwritten"); 72 } 73 } else { 74 Console.traceln("Illegal use of \"sequences\" parameter in the GlobalDataContainer."); 75 Console.traceln("The parameter should always be of type List<List<Event>>!"); 76 } 77 } 78 } catch (ClassCastException e) { 79 Console.println("Sequences need to be loaded first using parseXML"); 58 TrieBasedModel createModel() { 59 if (order == 1) { 60 return new FirstOrderMarkovModel(new Random()); 61 } else { 62 return new HighOrderMarkovModel(order, new Random()); 80 63 } 81 64 }
Note: See TracChangeset
for help on using the changeset viewer.