source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/sequences/CMDmergeSequences.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 merge all sequences of a collection of sequences into one long sequence depending on
31 * the timestamps of the events
32 * </p>
33 *
34 * @author Patrick Harms
35 * @version 1.0
36 */
37public class CMDmergeSequences implements Command {
38
39    /*
40     * (non-Javadoc)
41     *
42     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
43     */
44    @SuppressWarnings("unchecked")
45    @Override
46    public void run(List<Object> parameters) {
47        String sequencesName;
48        String newSequencesName;
49        try {
50            sequencesName = (String) parameters.get(0);
51            if (parameters.size() > 1) {
52                newSequencesName = (String) parameters.get(1);
53            }
54            else {
55                newSequencesName = sequencesName;
56            }
57        }
58        catch (Exception e) {
59            throw new IllegalArgumentException("must provide a sequences name");
60        }
61
62        Collection<List<Event>> sequences = null;
63        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
64        if (dataObject == null) {
65            CommandHelpers.objectNotFoundMessage(sequencesName);
66            return;
67        }
68        if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
69            CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
70            return;
71        }
72
73        sequences = (Collection<List<Event>>) dataObject;
74       
75        List<Event> resultingSequence = new LinkedList<Event>();
76        long first = 0;
77        long last = 0;
78       
79        Console.println("merging " + sequences.size() + " sequences");
80        for (List<Event> sequence : sequences) {
81            for (Event event : sequence) {
82                if (event.getTimestamp() < 0) {
83                    Console.printerrln("at least one of the events does not have a correct " +
84                                       "timestamp required for the merging of sequences and " +
85                                       "sorting of events");
86                }
87               
88                if (resultingSequence.isEmpty()) {
89                    resultingSequence.add(event);
90                    first = event.getTimestamp();
91                    last = event.getTimestamp();
92                }
93                else if (event.getTimestamp() < first) {
94                    resultingSequence.add(0, event);
95                    first = event.getTimestamp();
96                }
97                else if (event.getTimestamp() > last) {
98                    resultingSequence.add(event);
99                    last = event.getTimestamp();
100                }
101                else {
102                    for (int i = 0; i < resultingSequence.size(); i++) {
103                        if (event.getTimestamp() <= resultingSequence.get(i).getTimestamp()) {
104                            resultingSequence.add(i, event);
105                            break;
106                        }
107                    }
108                }
109            }
110        }
111       
112        Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
113        newSequences.add(resultingSequence);
114       
115        if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
116            CommandHelpers.dataOverwritten(newSequencesName);
117        }
118       
119        Console.println("sequences merged");
120    }
121
122    /*
123     * (non-Javadoc)
124     *
125     * @see de.ugoe.cs.util.console.Command#help()
126     */
127    @Override
128    public String help() {
129        return "mergeSequences <sequencesName> {<new sequences>}";
130    }
131
132}
Note: See TracBrowser for help on using the repository browser.