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

Last change on this file since 302 was 246, checked in by sherbold, 13 years ago
  • fixed Javadoc comment
  • Property svn:mime-type set to text/plain
File size: 2.6 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;
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 */
22public 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 #handleAdditionalParameters(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}
Note: See TracBrowser for help on using the repository browser.