1 | package de.ugoe.cs.eventbench.commands;
|
---|
2 |
|
---|
3 |
|
---|
4 | import java.security.InvalidParameterException;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Random;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.eventbench.data.Event;
|
---|
9 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
10 | import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel;
|
---|
11 | import de.ugoe.cs.eventbench.models.HighOrderMarkovModel;
|
---|
12 | import de.ugoe.cs.util.console.Command;
|
---|
13 | import de.ugoe.cs.util.console.Console;
|
---|
14 |
|
---|
15 | public class CMDtrainMarkovModel implements Command {
|
---|
16 |
|
---|
17 | @Override
|
---|
18 | public void help() {
|
---|
19 | Console.println("Usage: trainMarkovModel <modelName> {<order>}");
|
---|
20 | }
|
---|
21 |
|
---|
22 | @SuppressWarnings("unchecked")
|
---|
23 | @Override
|
---|
24 | public void run(List<Object> parameters) {
|
---|
25 | String modelname;
|
---|
26 | int order = 1;
|
---|
27 | try {
|
---|
28 | modelname = (String) parameters.get(0);
|
---|
29 | if( parameters.size()==2 ) {
|
---|
30 | order = Integer.parseInt((String) parameters.get(0));
|
---|
31 | }
|
---|
32 | } catch (Exception e) {
|
---|
33 | throw new InvalidParameterException();
|
---|
34 | }
|
---|
35 |
|
---|
36 | List<List<Event<?>>> sequences = null;
|
---|
37 | Object dataObject = GlobalDataContainer.getInstance().getData("sequences");
|
---|
38 |
|
---|
39 | try {
|
---|
40 | sequences = (List<List<Event<?>>>) dataObject;
|
---|
41 | if( sequences.size()>0 ) {
|
---|
42 | if( sequences.get(0).get(0) instanceof Event ) {
|
---|
43 | HighOrderMarkovModel model;
|
---|
44 | if( order==1 ) {
|
---|
45 | model = new FirstOrderMarkovModel(new Random());
|
---|
46 | } else {
|
---|
47 | model = new HighOrderMarkovModel(order, new Random());
|
---|
48 | }
|
---|
49 | model.train(sequences);
|
---|
50 | if( GlobalDataContainer.getInstance().addData(modelname, model) ) {
|
---|
51 | Console.traceln("Old model overwritten");
|
---|
52 | }
|
---|
53 | } else {
|
---|
54 | Console.traceln("Illegal use of \"sequences\" parameter in the GlobalDataContainer.");
|
---|
55 | Console.traceln("The parameter should always be of type List<List<Event>>!");
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | catch(ClassCastException e) {
|
---|
60 | Console.println("Sequences need to be loaded first using parseXML");
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|