1 | package de.ugoe.cs.eventbench.commands;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Random;
|
---|
6 |
|
---|
7 | import de.ugoe.cs.eventbench.data.Event;
|
---|
8 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
9 | import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel;
|
---|
10 | import de.ugoe.cs.eventbench.models.HighOrderMarkovModel;
|
---|
11 | import de.ugoe.cs.util.console.Command;
|
---|
12 | import de.ugoe.cs.util.console.Console;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * <p>
|
---|
16 | * Command to train first-order and high-order Markov models.
|
---|
17 | * </p>
|
---|
18 | *
|
---|
19 | * @author Steffen Herbold
|
---|
20 | * @version 1.0
|
---|
21 | */
|
---|
22 | public class CMDtrainMarkovModel implements Command {
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * (non-Javadoc)
|
---|
26 | *
|
---|
27 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
28 | */
|
---|
29 | @Override
|
---|
30 | public void help() {
|
---|
31 | Console.println("Usage: trainMarkovModel <modelName> {<order>}");
|
---|
32 | }
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * (non-Javadoc)
|
---|
36 | *
|
---|
37 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
38 | */
|
---|
39 | @SuppressWarnings("unchecked")
|
---|
40 | @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");
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|