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.List;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 | import java.util.SortedMap;
|
---|
21 | import java.util.TreeMap;
|
---|
22 |
|
---|
23 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
24 | import de.ugoe.cs.autoquest.SequenceInstanceOf;
|
---|
25 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
26 | import de.ugoe.cs.util.console.Command;
|
---|
27 | import de.ugoe.cs.util.console.Console;
|
---|
28 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>
|
---|
32 | * Command to print basic statistical information about stored sequences, e.g.,
|
---|
33 | * how many there are of which length.
|
---|
34 | * </p>
|
---|
35 | *
|
---|
36 | * @author Steffen Herbold
|
---|
37 | * @version 1.0
|
---|
38 | */
|
---|
39 | public class CMDsequenceStatistics 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 sequencesName;
|
---|
50 | try {
|
---|
51 | sequencesName = (String) parameters.get(0);
|
---|
52 | } catch (Exception e) {
|
---|
53 | throw new IllegalArgumentException();
|
---|
54 | }
|
---|
55 |
|
---|
56 | Collection<List<Event>> sequences = null;
|
---|
57 | Object dataObject = GlobalDataContainer.getInstance().getData(
|
---|
58 | sequencesName);
|
---|
59 | if (dataObject == null) {
|
---|
60 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
64 | CommandHelpers.objectNotType(sequencesName,
|
---|
65 | "Collection<List<Event<?>>>");
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | sequences = (Collection<List<Event>>) dataObject;
|
---|
70 | Console.println("Number of Sequences: " + sequences.size());
|
---|
71 | SortedMap<Integer, Integer> lengthMap = new TreeMap<Integer, Integer>();
|
---|
72 | for (List<Event> sequence : sequences) {
|
---|
73 | Integer currentSize = sequence.size();
|
---|
74 | if (lengthMap.containsKey(currentSize)) {
|
---|
75 | lengthMap.put(currentSize, lengthMap.get(currentSize) + 1);
|
---|
76 | } else {
|
---|
77 | lengthMap.put(currentSize, 1);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | for (Entry<Integer, Integer> entry : lengthMap.entrySet()) {
|
---|
81 | Console.println("Of length " + entry.getKey() + ": "
|
---|
82 | + entry.getValue());
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * (non-Javadoc)
|
---|
88 | *
|
---|
89 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
90 | */
|
---|
91 | @Override
|
---|
92 | public String help() {
|
---|
93 | return "sequenceStatistics <sequencesName>";
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|