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.text.SimpleDateFormat;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Date;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map.Entry;
|
---|
22 | import java.util.SortedMap;
|
---|
23 | import java.util.TreeMap;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
26 | import de.ugoe.cs.autoquest.SequenceInstanceOf;
|
---|
27 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
28 | import de.ugoe.cs.util.console.Command;
|
---|
29 | import de.ugoe.cs.util.console.Console;
|
---|
30 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * <p>
|
---|
34 | * Command to print basic statistical information about stored sequences, e.g., how many there are
|
---|
35 | * of which length.
|
---|
36 | * </p>
|
---|
37 | *
|
---|
38 | * @author Steffen Herbold
|
---|
39 | * @version 1.0
|
---|
40 | */
|
---|
41 | public class CMDsequenceStatistics implements Command {
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * (non-Javadoc)
|
---|
45 | *
|
---|
46 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
47 | */
|
---|
48 | @SuppressWarnings("unchecked")
|
---|
49 | @Override
|
---|
50 | public void run(List<Object> parameters) {
|
---|
51 | String sequencesName;
|
---|
52 | try {
|
---|
53 | sequencesName = (String) parameters.get(0);
|
---|
54 | }
|
---|
55 | catch (Exception e) {
|
---|
56 | throw new IllegalArgumentException();
|
---|
57 | }
|
---|
58 |
|
---|
59 | Collection<List<Event>> sequences = null;
|
---|
60 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
61 | if (dataObject == null) {
|
---|
62 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
63 | return;
|
---|
64 | }
|
---|
65 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
66 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | sequences = (Collection<List<Event>>) dataObject;
|
---|
71 | Console.println("Number of Sequences: " + sequences.size());
|
---|
72 |
|
---|
73 | long smallestTimeStamp = Long.MAX_VALUE;
|
---|
74 | long largestTimeStamp = 0;
|
---|
75 | int noOfEvents = 0;
|
---|
76 |
|
---|
77 | SortedMap<Integer, Integer> lengthMap = new TreeMap<Integer, Integer>();
|
---|
78 | for (List<Event> sequence : sequences) {
|
---|
79 | Integer currentSize = sequence.size();
|
---|
80 | noOfEvents += currentSize;
|
---|
81 |
|
---|
82 | if (lengthMap.containsKey(currentSize)) {
|
---|
83 | lengthMap.put(currentSize, lengthMap.get(currentSize) + 1);
|
---|
84 | }
|
---|
85 | else {
|
---|
86 | lengthMap.put(currentSize, 1);
|
---|
87 | }
|
---|
88 |
|
---|
89 | for (Event event : sequence) {
|
---|
90 | if (event.getTimestamp() > 0) {
|
---|
91 | smallestTimeStamp = Math.min(smallestTimeStamp, event.getTimestamp());
|
---|
92 | largestTimeStamp = Math.max(largestTimeStamp, event.getTimestamp());
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | Console.println("Number of Events: " + noOfEvents);
|
---|
98 | Console.println("First Event Recorded At: " +
|
---|
99 | SimpleDateFormat.getDateTimeInstance().format(new Date(smallestTimeStamp)));
|
---|
100 | Console.println("Last Event Recorded At: " +
|
---|
101 | SimpleDateFormat.getDateTimeInstance().format(new Date(largestTimeStamp)));
|
---|
102 |
|
---|
103 | for (Entry<Integer, Integer> entry : lengthMap.entrySet()) {
|
---|
104 | Console.println("Sequences of Length " + entry.getKey() + ": " + entry.getValue());
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * (non-Javadoc)
|
---|
110 | *
|
---|
111 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
112 | */
|
---|
113 | @Override
|
---|
114 | public String help() {
|
---|
115 | return "sequenceStatistics <sequencesName>";
|
---|
116 | }
|
---|
117 |
|
---|
118 | }
|
---|