[1091] | 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 |
|
---|
| 15 | package de.ugoe.cs.autoquest.commands.sequences;
|
---|
| 16 |
|
---|
| 17 | import java.util.Collection;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 |
|
---|
| 21 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
| 22 | import de.ugoe.cs.autoquest.SequenceInstanceOf;
|
---|
| 23 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 24 | import de.ugoe.cs.util.console.Command;
|
---|
| 25 | import de.ugoe.cs.util.console.Console;
|
---|
| 26 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * <p>
|
---|
| 30 | * Command to split all sequences of a collection of sequences into subsequences depending on
|
---|
| 31 | * the timestamps of the events. If the difference of the timestamps of two subsequent events is
|
---|
| 32 | * larger than the provided number of milliseconds, the sequence to which the events belong is
|
---|
| 33 | * split up into two subsequences between the two events.
|
---|
| 34 | * </p>
|
---|
| 35 | *
|
---|
| 36 | * @author Patrick Harms
|
---|
| 37 | * @version 1.0
|
---|
| 38 | */
|
---|
| 39 | public class CMDsplitSequences implements Command {
|
---|
| 40 |
|
---|
| 41 | /*
|
---|
| 42 | * (non-Javadoc)
|
---|
| 43 | *
|
---|
| 44 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 45 | */
|
---|
| 46 | @SuppressWarnings("unchecked")
|
---|
| 47 | @Override
|
---|
| 48 | public void run(List<Object> parameters) {
|
---|
| 49 | long diff;
|
---|
| 50 | String sequencesName;
|
---|
| 51 | String newSequencesName;
|
---|
| 52 | try {
|
---|
| 53 | diff = Long.parseLong((String) parameters.get(0));
|
---|
| 54 | sequencesName = (String) parameters.get(1);
|
---|
| 55 | if (parameters.size() > 2) {
|
---|
| 56 | newSequencesName = (String) parameters.get(2);
|
---|
| 57 | }
|
---|
| 58 | else {
|
---|
| 59 | newSequencesName = sequencesName;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | catch (Exception e) {
|
---|
| 63 | throw new IllegalArgumentException("must provide an integer for the maximum " +
|
---|
| 64 | "timestamp difference and a sequences name");
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | Collection<List<Event>> sequences = null;
|
---|
| 68 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
| 69 | if (dataObject == null) {
|
---|
| 70 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
| 71 | return;
|
---|
| 72 | }
|
---|
| 73 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
| 74 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
|
---|
| 75 | return;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | sequences = (Collection<List<Event>>) dataObject;
|
---|
| 79 |
|
---|
| 80 | Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
|
---|
| 81 |
|
---|
| 82 | Console.println("splitting " + sequences.size() + " sequences");
|
---|
| 83 | for (List<Event> sequence : sequences) {
|
---|
| 84 | Event previous = null;
|
---|
| 85 | List<Event> resultingSequence = new LinkedList<Event>();
|
---|
| 86 | for (Event currentEvent : sequence) {
|
---|
| 87 | if (currentEvent.getTimestamp() < 0) {
|
---|
| 88 | Console.printerrln("at least one of the events does not have a correct " +
|
---|
| 89 | "timestamp required for the merging of sequences and " +
|
---|
| 90 | "sorting of events");
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[1096] | 93 | if ((previous != null) &&
|
---|
| 94 | (Math.abs(previous.getTimestamp() - currentEvent.getTimestamp()) > diff))
|
---|
| 95 | {
|
---|
[1091] | 96 | newSequences.add(resultingSequence);
|
---|
| 97 | resultingSequence = new LinkedList<Event>();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | resultingSequence.add(currentEvent);
|
---|
[1096] | 101 | previous = currentEvent;
|
---|
[1091] | 102 | }
|
---|
| 103 |
|
---|
| 104 | newSequences.add(resultingSequence);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
|
---|
| 108 | CommandHelpers.dataOverwritten(newSequencesName);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | Console.println("sequences splitted");
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /*
|
---|
| 115 | * (non-Javadoc)
|
---|
| 116 | *
|
---|
| 117 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 118 | */
|
---|
| 119 | @Override
|
---|
| 120 | public String help() {
|
---|
| 121 | return "splitSequences <maximumTimestampDifference> <sequencesName> {<newSequences>}";
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | }
|
---|