package de.ugoe.cs.eventbench.commands; import java.security.InvalidParameterException; import java.util.Collection; import java.util.List; import de.ugoe.cs.eventbench.SequenceInstanceOf; import de.ugoe.cs.eventbench.data.Event; import de.ugoe.cs.eventbench.data.GlobalDataContainer; import de.ugoe.cs.eventbench.models.TrieBasedModel; import de.ugoe.cs.util.console.Command; import de.ugoe.cs.util.console.Console; /** *

* Abstract class for commands to train {@link TrieBasedModel}s. *

* * @author Steffen Herbold * @version 1.0 */ public abstract class AbstractTrainCommand implements Command { /** *

* Handling of additional parameters. *

* * @param parameters * same as the parameters passed to {@link #run(List)}. * @throws Exception * thrown, if there is an error parsing the parameters */ abstract void handleAdditionalParameters(List parameters) throws Exception; /** *

* Returns a concrete instance of {@link TrieBasedModel} to be trained. This * is a factory method. *

* * @return instance of {@link TrieBasedModel} */ abstract TrieBasedModel createModel(); /** *

* The command is implemented as a template method. The general structure of * the command is always the same, only the parameters of the command and * the creation of the {@link TrieBasedModel} instance. The former is * handled by {@link #handleOptionalParameters(List)}, the latter by * {@link #createModel()}. *

* * @see de.ugoe.cs.util.console.Command#run(java.util.List) */ @SuppressWarnings("unchecked") @Override public void run(List parameters) { String modelname; String sequencesName; try { modelname = (String) parameters.get(0); sequencesName = (String) parameters.get(1); handleAdditionalParameters(parameters); } catch (Exception e) { throw new InvalidParameterException(); } Object dataObject = GlobalDataContainer.getInstance().getData( sequencesName); if (dataObject == null) { Console.println("Object " + sequencesName + " not found in storage."); return; } if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) { Console.println("Object " + sequencesName + "not of type Collection>>."); return; } Collection>> sequences = (Collection>>) dataObject; TrieBasedModel model = createModel(); model.train(sequences); if (GlobalDataContainer.getInstance().addData(modelname, model)) { Console.traceln("Old data \"" + modelname + "\" overwritten"); } } }