source: trunk/quest-ui-core/src/main/java/de/ugoe/cs/quest/commands/usage/AbstractTrainCommand.java @ 766

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