[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.commands.usage;
|
---|
[184] | 16 |
|
---|
[203] | 17 | import java.util.Collection;
|
---|
[184] | 18 | import java.util.List;
|
---|
| 19 |
|
---|
[922] | 20 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
| 21 | import de.ugoe.cs.autoquest.SequenceInstanceOf;
|
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 23 | import de.ugoe.cs.autoquest.usageprofiles.TrieBasedModel;
|
---|
[184] | 24 | import de.ugoe.cs.util.console.Command;
|
---|
[667] | 25 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
[184] | 26 |
|
---|
| 27 | /**
|
---|
| 28 | * <p>
|
---|
| 29 | * Abstract class for commands to train {@link TrieBasedModel}s.
|
---|
| 30 | * </p>
|
---|
| 31 | *
|
---|
| 32 | * @author Steffen Herbold
|
---|
| 33 | * @version 1.0
|
---|
| 34 | */
|
---|
| 35 | public abstract class AbstractTrainCommand implements Command {
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * <p>
|
---|
| 39 | * Handling of additional parameters.
|
---|
| 40 | * </p>
|
---|
| 41 | *
|
---|
| 42 | * @param parameters
|
---|
| 43 | * same as the parameters passed to {@link #run(List)}.
|
---|
| 44 | * @throws Exception
|
---|
| 45 | * thrown, if there is an error parsing the parameters
|
---|
| 46 | */
|
---|
| 47 | abstract void handleAdditionalParameters(List<Object> parameters)
|
---|
| 48 | throws Exception;
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * <p>
|
---|
| 52 | * Returns a concrete instance of {@link TrieBasedModel} to be trained. This
|
---|
| 53 | * is a factory method.
|
---|
| 54 | * </p>
|
---|
| 55 | *
|
---|
| 56 | * @return instance of {@link TrieBasedModel}
|
---|
| 57 | */
|
---|
| 58 | abstract TrieBasedModel createModel();
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * <p>
|
---|
| 62 | * The command is implemented as a template method. The general structure of
|
---|
| 63 | * the command is always the same, only the parameters of the command and
|
---|
| 64 | * the creation of the {@link TrieBasedModel} instance. The former is
|
---|
[246] | 65 | * handled by {@link #handleAdditionalParameters(List)}, the latter by
|
---|
[184] | 66 | * {@link #createModel()}.
|
---|
| 67 | * </p>
|
---|
| 68 | *
|
---|
| 69 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 70 | */
|
---|
| 71 | @SuppressWarnings("unchecked")
|
---|
| 72 | @Override
|
---|
| 73 | public void run(List<Object> parameters) {
|
---|
| 74 | String modelname;
|
---|
| 75 | String sequencesName;
|
---|
| 76 |
|
---|
| 77 | try {
|
---|
| 78 | modelname = (String) parameters.get(0);
|
---|
| 79 | sequencesName = (String) parameters.get(1);
|
---|
| 80 | handleAdditionalParameters(parameters);
|
---|
| 81 | } catch (Exception e) {
|
---|
[766] | 82 | throw new IllegalArgumentException();
|
---|
[184] | 83 | }
|
---|
| 84 |
|
---|
| 85 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
| 86 | sequencesName);
|
---|
| 87 | if (dataObject == null) {
|
---|
[240] | 88 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
[184] | 89 | return;
|
---|
| 90 | }
|
---|
[209] | 91 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
[240] | 92 | CommandHelpers.objectNotType(sequencesName,
|
---|
| 93 | "Collection<List<Event<?>>>");
|
---|
[203] | 94 | return;
|
---|
[184] | 95 | }
|
---|
[547] | 96 | Collection<List<Event>> sequences = (Collection<List<Event>>) dataObject;
|
---|
[184] | 97 |
|
---|
| 98 | TrieBasedModel model = createModel();
|
---|
| 99 | model.train(sequences);
|
---|
[209] | 100 | if (GlobalDataContainer.getInstance().addData(modelname, model)) {
|
---|
[244] | 101 | CommandHelpers.dataOverwritten(modelname);
|
---|
[184] | 102 | }
|
---|
[209] | 103 |
|
---|
[184] | 104 | }
|
---|
| 105 |
|
---|
| 106 | }
|
---|