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

Last change on this file since 230 was 209, checked in by sherbold, 13 years ago
  • greatly improved type checking and consistency of type checking for objects checked out of the GlobalDataContainer?
  • 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.SequenceInstanceOf;
8import de.ugoe.cs.eventbench.data.Event;
9import de.ugoe.cs.eventbench.data.GlobalDataContainer;
10import de.ugoe.cs.eventbench.models.TrieBasedModel;
11import de.ugoe.cs.util.console.Command;
12import de.ugoe.cs.util.console.Console;
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 #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                        Console.println("Object " + sequencesName
76                                        + " not found in storage.");
77                        return;
78                }
79                if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
80                        Console.println("Object " + sequencesName
81                                        + "not of type 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.