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.DeterministicFiniteAutomaton;
|
---|
10 | import de.ugoe.cs.util.console.Command;
|
---|
11 | import de.ugoe.cs.util.console.Console;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * <p>
|
---|
15 | * Command to train a Deterministic Finite Automaton (DFA).
|
---|
16 | * </p>
|
---|
17 | *
|
---|
18 | * @author Steffen Herbold
|
---|
19 | * @version 1.0
|
---|
20 | */
|
---|
21 | public class CMDtrainDFA 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: trainDFA <modelName>");
|
---|
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 | try {
|
---|
43 | modelname = (String) parameters.get(0);
|
---|
44 | } catch (Exception e) {
|
---|
45 | throw new InvalidParameterException();
|
---|
46 | }
|
---|
47 |
|
---|
48 | List<List<Event<?>>> sequences = null;
|
---|
49 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
50 | "sequences");
|
---|
51 |
|
---|
52 | try {
|
---|
53 | sequences = (List<List<Event<?>>>) dataObject;
|
---|
54 | if (sequences.size() > 0) {
|
---|
55 | if (sequences.get(0).get(0) instanceof Event) {
|
---|
56 | DeterministicFiniteAutomaton model = new DeterministicFiniteAutomaton(
|
---|
57 | new Random());
|
---|
58 | model.train(sequences);
|
---|
59 | if (GlobalDataContainer.getInstance().addData(modelname,
|
---|
60 | model)) {
|
---|
61 | Console.traceln("Old data \"" + modelname
|
---|
62 | + "\" overwritten");
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | Console.traceln("Illegal use of \"sequences\" parameter in the GlobalDataContainer.");
|
---|
66 | Console.traceln("The parameter should always be of type List<List<? extends Event<?>>!");
|
---|
67 | }
|
---|
68 | }
|
---|
69 | } catch (ClassCastException e) {
|
---|
70 | Console.println("Sequences need to be loaded first using parseXML");
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | }
|
---|