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.PredictionByPartialMatch;
|
---|
10 | import de.ugoe.cs.util.console.Command;
|
---|
11 | import de.ugoe.cs.util.console.Console;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * <p>
|
---|
15 | * Command that trains Prediction by Partial Match (PPM) models.
|
---|
16 | * </p>
|
---|
17 | *
|
---|
18 | * @author Steffen Herbold
|
---|
19 | * @version 1.0
|
---|
20 | */
|
---|
21 | public class CMDtrainPPM implements Command {
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * (non-Javadoc)
|
---|
25 | *
|
---|
26 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
27 | */
|
---|
28 | @Override
|
---|
29 | public void help() {
|
---|
30 | Console.println("Usage: trainPPM <modelName> <probEscape> <maxOrder> {<minOrder>}");
|
---|
31 | }
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * (non-Javadoc)
|
---|
35 | *
|
---|
36 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
37 | */
|
---|
38 | @SuppressWarnings("unchecked")
|
---|
39 | @Override
|
---|
40 | public void run(List<Object> parameters) {
|
---|
41 | String modelname;
|
---|
42 | double probEscape;
|
---|
43 | int maxOrder;
|
---|
44 | int minOrder = 0;
|
---|
45 | try {
|
---|
46 | modelname = (String) parameters.get(0);
|
---|
47 | probEscape = Double.parseDouble((String) parameters.get(1));
|
---|
48 | maxOrder = Integer.parseInt((String) parameters.get(2));
|
---|
49 | if (parameters.size() == 4) {
|
---|
50 | minOrder = Integer.parseInt((String) parameters.get(3));
|
---|
51 | }
|
---|
52 | } catch (Exception e) {
|
---|
53 | throw new InvalidParameterException();
|
---|
54 | }
|
---|
55 |
|
---|
56 | List<List<Event<?>>> sequences = null;
|
---|
57 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
58 | "sequences");
|
---|
59 |
|
---|
60 | try {
|
---|
61 | sequences = (List<List<Event<?>>>) dataObject;
|
---|
62 | if (sequences.size() > 0) {
|
---|
63 | if (sequences.get(0).get(0) instanceof Event) {
|
---|
64 | PredictionByPartialMatch model = new PredictionByPartialMatch(
|
---|
65 | maxOrder, minOrder, new Random(), probEscape);
|
---|
66 | model.train(sequences);
|
---|
67 | if (GlobalDataContainer.getInstance().addData(modelname,
|
---|
68 | model)) {
|
---|
69 | Console.traceln("Old data \"" + modelname
|
---|
70 | + "\" overwritten");
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | Console.traceln("Illegal use of \"sequences\" parameter in the GlobalDataContainer.");
|
---|
74 | Console.traceln("The parameter should always be of type List<List<Event>>!");
|
---|
75 | }
|
---|
76 | }
|
---|
77 | } catch (ClassCastException e) {
|
---|
78 | Console.println("Sequences need to be loaded first using parseXML");
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|