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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 2.9 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.List;
19import java.util.Map.Entry;
20import java.util.SortedMap;
21import java.util.TreeMap;
22
23import de.ugoe.cs.autoquest.CommandHelpers;
24import de.ugoe.cs.autoquest.SequenceInstanceOf;
25import de.ugoe.cs.autoquest.eventcore.Event;
26import de.ugoe.cs.util.console.Command;
27import de.ugoe.cs.util.console.Console;
28import 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 */
39public 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}
Note: See TracBrowser for help on using the repository browser.