source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/AbstractTrainCommand.java @ 242

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