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