1 | package de.ugoe.cs.eventbench.commands;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
8 | import de.ugoe.cs.eventbench.SequenceInstanceOf;
|
---|
9 | import de.ugoe.cs.eventbench.data.Event;
|
---|
10 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
11 | import de.ugoe.cs.eventbench.models.TrieBasedModel;
|
---|
12 | import de.ugoe.cs.util.console.Command;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * <p>
|
---|
16 | * Abstract class for commands to train {@link TrieBasedModel}s.
|
---|
17 | * </p>
|
---|
18 | *
|
---|
19 | * @author Steffen Herbold
|
---|
20 | * @version 1.0
|
---|
21 | */
|
---|
22 | public abstract class AbstractTrainCommand implements Command {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * <p>
|
---|
26 | * Handling of additional parameters.
|
---|
27 | * </p>
|
---|
28 | *
|
---|
29 | * @param parameters
|
---|
30 | * same as the parameters passed to {@link #run(List)}.
|
---|
31 | * @throws Exception
|
---|
32 | * thrown, if there is an error parsing the parameters
|
---|
33 | */
|
---|
34 | abstract void handleAdditionalParameters(List<Object> parameters)
|
---|
35 | throws Exception;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * <p>
|
---|
39 | * Returns a concrete instance of {@link TrieBasedModel} to be trained. This
|
---|
40 | * is a factory method.
|
---|
41 | * </p>
|
---|
42 | *
|
---|
43 | * @return instance of {@link TrieBasedModel}
|
---|
44 | */
|
---|
45 | abstract TrieBasedModel createModel();
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <p>
|
---|
49 | * The command is implemented as a template method. The general structure of
|
---|
50 | * the command is always the same, only the parameters of the command and
|
---|
51 | * the creation of the {@link TrieBasedModel} instance. The former is
|
---|
52 | * handled by {@link #handleOptionalParameters(List)}, the latter by
|
---|
53 | * {@link #createModel()}.
|
---|
54 | * </p>
|
---|
55 | *
|
---|
56 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
57 | */
|
---|
58 | @SuppressWarnings("unchecked")
|
---|
59 | @Override
|
---|
60 | public void run(List<Object> parameters) {
|
---|
61 | String modelname;
|
---|
62 | String sequencesName;
|
---|
63 |
|
---|
64 | try {
|
---|
65 | modelname = (String) parameters.get(0);
|
---|
66 | sequencesName = (String) parameters.get(1);
|
---|
67 | handleAdditionalParameters(parameters);
|
---|
68 | } catch (Exception e) {
|
---|
69 | throw new InvalidParameterException();
|
---|
70 | }
|
---|
71 |
|
---|
72 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
73 | sequencesName);
|
---|
74 | if (dataObject == null) {
|
---|
75 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
76 | return;
|
---|
77 | }
|
---|
78 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
79 | CommandHelpers.objectNotType(sequencesName,
|
---|
80 | "Collection<List<Event<?>>>");
|
---|
81 | return;
|
---|
82 | }
|
---|
83 | Collection<List<Event<?>>> sequences = (Collection<List<Event<?>>>) dataObject;
|
---|
84 |
|
---|
85 | TrieBasedModel model = createModel();
|
---|
86 | model.train(sequences);
|
---|
87 | if (GlobalDataContainer.getInstance().addData(modelname, model)) {
|
---|
88 | CommandHelpers.dataOverwritten(modelname);
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | }
|
---|