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.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.HashMap;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Map;
|
---|
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.GlobalDataContainer;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * <p>
|
---|
33 | * TODO comment
|
---|
34 | * </p>
|
---|
35 | *
|
---|
36 | * @author Patrick Harms
|
---|
37 | * @version 1.0
|
---|
38 | */
|
---|
39 | public class CMDtargetStatistics 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 | List<String> sequenceNames = new ArrayList<>(parameters.size());
|
---|
50 | try {
|
---|
51 | for (Object parameter : parameters) {
|
---|
52 | sequenceNames.add((String) parameter);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | catch (Exception e) {
|
---|
56 | throw new IllegalArgumentException();
|
---|
57 | }
|
---|
58 |
|
---|
59 | List<Collection<List<Event>>> sequences = new ArrayList<>(sequenceNames.size());
|
---|
60 |
|
---|
61 | for (String sequenceName : sequenceNames) {
|
---|
62 | Object dataObject = GlobalDataContainer.getInstance().getData(sequenceName);
|
---|
63 | if (dataObject == null) {
|
---|
64 | CommandHelpers.objectNotFoundMessage(sequenceName);
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
68 | CommandHelpers.objectNotType(sequenceName, "Collection<List<Event<?>>>");
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | sequences.add((Collection<List<Event>>) dataObject);
|
---|
73 | }
|
---|
74 |
|
---|
75 | Map<String, Map<String, Integer>> counters = new TreeMap<>();
|
---|
76 |
|
---|
77 | for (int i = 0; i < sequences.size(); i++) {
|
---|
78 | String sequenceName = sequenceNames.get(i);
|
---|
79 | for (List<Event> sequence : sequences.get(i)) {
|
---|
80 | for (Event event : sequence) {
|
---|
81 | String targetId = event.getTarget().toString() + " (" +
|
---|
82 | event.getTarget().getStringIdentifier() + ")";
|
---|
83 | Map<String, Integer> counterMap = counters.get(targetId);
|
---|
84 |
|
---|
85 | if (counterMap == null) {
|
---|
86 | counterMap = new HashMap<>();
|
---|
87 | counters.put(targetId, counterMap);
|
---|
88 | }
|
---|
89 |
|
---|
90 | Integer counter = counterMap.get(sequenceName);
|
---|
91 | if (counter == null) {
|
---|
92 | counterMap.put(sequenceName, 1);
|
---|
93 | }
|
---|
94 | else {
|
---|
95 | counterMap.put(sequenceName, counter + 1);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | int maxEventNameLength = 0;
|
---|
102 | for (Map.Entry<String, Map<String, Integer>> sequenceStats : counters.entrySet()) {
|
---|
103 | maxEventNameLength = Math.max(maxEventNameLength, sequenceStats.getKey().length());
|
---|
104 | }
|
---|
105 |
|
---|
106 | List<StringBuffer> lines = new LinkedList<>();
|
---|
107 | for (Map.Entry<String, Map<String, Integer>> sequenceStats : counters.entrySet()) {
|
---|
108 | StringBuffer line = new StringBuffer();
|
---|
109 | line.append(sequenceStats.getKey());
|
---|
110 |
|
---|
111 | for (int i = sequenceStats.getKey().length(); i < maxEventNameLength; i++) {
|
---|
112 | line.append(' ');
|
---|
113 | }
|
---|
114 |
|
---|
115 | for (String sequenceName : sequenceNames) {
|
---|
116 | String numberStr = "";
|
---|
117 |
|
---|
118 | if (sequenceStats.getValue().get(sequenceName) != null) {
|
---|
119 | numberStr += sequenceStats.getValue().get(sequenceName);
|
---|
120 | }
|
---|
121 |
|
---|
122 | line.append(" | ");
|
---|
123 | line.append(numberStr);
|
---|
124 |
|
---|
125 | for (int i = numberStr.length(); i < sequenceName.length(); i++) {
|
---|
126 | line.append(' ');
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | lines.add(line);
|
---|
131 | }
|
---|
132 |
|
---|
133 | for (int i = 0; i < maxEventNameLength; i++) {
|
---|
134 | System.out.print(' ');
|
---|
135 | }
|
---|
136 |
|
---|
137 | for (String sequenceName : sequenceNames) {
|
---|
138 | System.out.print(" | " + sequenceName);
|
---|
139 | }
|
---|
140 |
|
---|
141 | System.out.println();
|
---|
142 |
|
---|
143 | for (StringBuffer line : lines) {
|
---|
144 | System.out.println(line);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * (non-Javadoc)
|
---|
150 | *
|
---|
151 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
152 | */
|
---|
153 | @Override
|
---|
154 | public String help() {
|
---|
155 | return "eventStatistics [<sequencesName>]*";
|
---|
156 | }
|
---|
157 |
|
---|
158 | }
|
---|