source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDsplitSequences.java @ 1091

Last change on this file since 1091 was 1091, checked in by pharms, 11 years ago
  • added a sequence merge and split command depending on the events timestamps
File size: 4.6 KB
Line 
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
15package de.ugoe.cs.autoquest.commands.sequences;
16
17import java.util.Collection;
18import java.util.LinkedList;
19import java.util.List;
20
21import de.ugoe.cs.autoquest.CommandHelpers;
22import de.ugoe.cs.autoquest.SequenceInstanceOf;
23import de.ugoe.cs.autoquest.eventcore.Event;
24import de.ugoe.cs.util.console.Command;
25import de.ugoe.cs.util.console.Console;
26import 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 */
39public 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               
93                if (previous == null) {
94                    previous = currentEvent;
95                }
96                else if (Math.abs(previous.getTimestamp() - currentEvent.getTimestamp()) > diff) {
97                    newSequences.add(resultingSequence);
98                    resultingSequence = new LinkedList<Event>();
99                }
100               
101                resultingSequence.add(currentEvent);
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}
Note: See TracBrowser for help on using the repository browser.