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

Last change on this file since 1438 was 1438, checked in by pharms, 10 years ago
  • added a new command to identify sequences with invalid timestamp orders of the events and to remove them from a collection of sequences
File size: 7.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;
20import java.util.logging.Level;
21
22import de.ugoe.cs.autoquest.CommandHelpers;
23import de.ugoe.cs.autoquest.SequenceInstanceOf;
24import de.ugoe.cs.autoquest.eventcore.Event;
25import de.ugoe.cs.util.console.Command;
26import de.ugoe.cs.util.console.Console;
27import de.ugoe.cs.util.console.GlobalDataContainer;
28
29/**
30 * <p>
31 * Command to check all sequences of a collection of sequences for correct timestamps and event
32 * order. It supports counting the number of invalid sequences, listing details about invalid
33 * sequences and timestamps, as well as deleting invalid sequences.
34 * </p>
35 *
36 * @author Patrick Harms
37 * @version 1.0
38 */
39public class CMDcheckEventTimestamps 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        String command;
50        String sequencesName;
51        String newSequencesName;
52        try {
53            command = (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 a command and a sequences name");
64        }
65
66        Collection<List<Event>> sequences = null;
67        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
68        if (dataObject == null) {
69            CommandHelpers.objectNotFoundMessage(sequencesName);
70            return;
71        }
72        if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
73            CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
74            return;
75        }
76
77        sequences = (Collection<List<Event>>) dataObject;
78       
79        if ("count".equals(command)) {
80            countSequencesWithInvalidTimestamps(sequences);
81        }
82        else if ("list".equals(command)) {
83            listSequencesWithInvalidTimestamps(sequences);
84            Console.println("sequences with timestamp issues listed");
85        }
86        else if ("delete".equals(command)) {
87            deleteSequencesWithInvalidTimestamps(sequences, newSequencesName);
88        }
89    }
90
91    /**
92     * <p>
93     * counts the number of invalid sequences in the provided collections of invalid sequences
94     * </p>
95     *
96     * @param sequences collection of sequences to check for invalid ones
97     */
98    private void countSequencesWithInvalidTimestamps(Collection<List<Event>> sequences) {
99        int sequenceCount = 0;
100        for (List<Event> sequence : sequences) {
101            Event previous = null;
102           
103            for (int i = 0; i < sequence.size(); i++) {
104                Event currentEvent = sequence.get(i);
105               
106                if ((previous != null) && (previous.getTimestamp() > currentEvent.getTimestamp())) {
107                    sequenceCount++;
108                    break;
109                }
110               
111                previous = currentEvent;
112            }
113           
114        }
115       
116        if (sequenceCount == 0) {
117            Console.println("no sequences have issues");
118        }
119        else {
120            Console.traceln(Level.WARNING, sequenceCount + " sequences have timestamp issues");
121        }
122    }
123
124    /**
125     * <p>
126     * lists details about invalid timestamp orders of events in the provided collection of
127     * sequences
128     * </p>
129     *
130     * @param sequences the collection of sequences to check
131     */
132    private void listSequencesWithInvalidTimestamps(Collection<List<Event>> sequences) {
133        int sequenceCount = 0;
134        for (List<Event> sequence : sequences) {
135            sequenceCount++;
136            Event previous = null;
137            List<String> issues = new LinkedList<String>();
138           
139            for (int i = 0; i < sequence.size(); i++) {
140                Event currentEvent = sequence.get(i);
141               
142                if ((previous != null) && (previous.getTimestamp() > currentEvent.getTimestamp())) {
143                    issues.add(currentEvent + " has an earlier timestamp than the preceeding " +
144                               previous);
145                }
146               
147                previous = currentEvent;
148            }
149           
150            if (issues.size() > 0) {
151                Console.traceln(Level.WARNING, "sequence " + sequenceCount +
152                                " has the following issues:");
153               
154                for (String issue : issues) {
155                    Console.traceln(Level.WARNING, "  " + issue);
156                }
157            }
158        }
159    }
160
161    /**
162     * <p>
163     * deletes sequences with invalid timestamps from the provided collection of sequences and
164     * stores them under new name
165     * </p>
166     *
167     * @param sequences        the collection of sequences to check for invalid sequences
168     * @param newSequencesName the name for the new corrected collection of sequences
169     */
170    private void deleteSequencesWithInvalidTimestamps(Collection<List<Event>> sequences,
171                                                      String                  newSequencesName)
172    {
173        Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
174        for (List<Event> sequence : sequences) {
175            Event previous = null;
176            boolean allFine = true;
177           
178            for (int i = 0; i < sequence.size(); i++) {
179                Event currentEvent = sequence.get(i);
180               
181                if ((previous != null) && (previous.getTimestamp() > currentEvent.getTimestamp())) {
182                    allFine = false;
183                    break;
184                }
185               
186                previous = currentEvent;
187            }
188           
189            if (allFine) {
190                newSequences.add(sequence);
191            }
192        }
193       
194        if (newSequences.size() == sequences.size()) {
195            Console.println("no sequences with issues deleted");
196        }
197        else {
198            Console.traceln(Level.WARNING, (sequences.size() - newSequences.size()) +
199                            " sequences with timestamp issues deleted");
200           
201            if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
202                CommandHelpers.dataOverwritten(newSequencesName);
203            }
204        }
205    }
206
207    /*
208     * (non-Javadoc)
209     *
210     * @see de.ugoe.cs.util.console.Command#help()
211     */
212    @Override
213    public String help() {
214        return "checkEventTimestamps <command> <sequencesName> {<newSequences>}";
215    }
216
217}
Note: See TracBrowser for help on using the repository browser.