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

Last change on this file since 733 was 733, checked in by sherbold, 12 years ago
  • refactored command packages in quest-ui-core
  • Property svn:mime-type set to text/plain
File size: 2.2 KB
Line 
1package de.ugoe.cs.quest.commands.sequences;
2
3import java.security.InvalidParameterException;
4import java.util.Collection;
5import java.util.List;
6import java.util.Map.Entry;
7import java.util.SortedMap;
8import java.util.TreeMap;
9
10import de.ugoe.cs.quest.CommandHelpers;
11import de.ugoe.cs.quest.SequenceInstanceOf;
12import de.ugoe.cs.quest.eventcore.Event;
13import de.ugoe.cs.util.console.Command;
14import de.ugoe.cs.util.console.Console;
15import de.ugoe.cs.util.console.GlobalDataContainer;
16
17/**
18 * <p>
19 * Command to print basic statistical information about stored sequences, e.g.,
20 * how many there are of which length.
21 * </p>
22 *
23 * @author Steffen Herbold
24 * @version 1.0
25 */
26public class CMDsequenceStatistics implements Command {
27
28        /*
29         * (non-Javadoc)
30         *
31         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
32         */
33        @SuppressWarnings("unchecked")
34        @Override
35        public void run(List<Object> parameters) {
36                String sequencesName;
37                try {
38                        sequencesName = (String) parameters.get(0);
39                } catch (Exception e) {
40                        throw new InvalidParameterException();
41                }
42
43                Collection<List<Event>> sequences = null;
44                Object dataObject = GlobalDataContainer.getInstance().getData(
45                                sequencesName);
46                if (dataObject == null) {
47                        CommandHelpers.objectNotFoundMessage(sequencesName);
48                        return;
49                }
50                if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
51                        CommandHelpers.objectNotType(sequencesName,
52                                        "Collection<List<Event<?>>>");
53                        return;
54                }
55
56                sequences = (Collection<List<Event>>) dataObject;
57                Console.println("Number of Sequences: " + sequences.size());
58                SortedMap<Integer, Integer> lengthMap = new TreeMap<Integer, Integer>();
59                for (List<Event> sequence : sequences) {
60                        Integer currentSize = sequence.size();
61                        if (lengthMap.containsKey(currentSize)) {
62                                lengthMap.put(currentSize, lengthMap.get(currentSize) + 1);
63                        } else {
64                                lengthMap.put(currentSize, 1);
65                        }
66                }
67                for (Entry<Integer, Integer> entry : lengthMap.entrySet()) {
68                        Console.println("Of length " + entry.getKey() + ": "
69                                        + entry.getValue());
70                }
71        }
72
73        /*
74         * (non-Javadoc)
75         *
76         * @see de.ugoe.cs.util.console.Command#help()
77         */
78        @Override
79        public String help() {
80                return "sequenceStatistics <sequencesName>";
81        }
82
83}
Note: See TracBrowser for help on using the repository browser.