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

Last change on this file since 2166 was 2166, checked in by pharms, 7 years ago
  • changes for first VR oriented usability evaluation
  • Property svn:mime-type set to text/plain
File size: 5.6 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.ArrayList;
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23import java.util.TreeMap;
24
25import de.ugoe.cs.autoquest.CommandHelpers;
26import de.ugoe.cs.autoquest.SequenceInstanceOf;
27import de.ugoe.cs.autoquest.eventcore.Event;
28import de.ugoe.cs.util.console.Command;
29import 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 */
39public class CMDeventStatistics 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 eventId = event.getType().toString() + "." + event.getTarget().toString();
82                    Map<String, Integer> counterMap = counters.get(eventId);
83                   
84                    if (counterMap == null) {
85                        counterMap = new HashMap<>();
86                        counters.put(eventId, counterMap);
87                    }
88                   
89                    Integer counter = counterMap.get(sequenceName);
90                    if (counter == null) {
91                        counterMap.put(sequenceName, 1);
92                    }
93                    else {
94                        counterMap.put(sequenceName, counter + 1);
95                    }
96                }
97            }
98        }
99       
100        int maxEventNameLength = 0;
101        for (Map.Entry<String, Map<String, Integer>> sequenceStats : counters.entrySet()) {
102            maxEventNameLength = Math.max(maxEventNameLength, sequenceStats.getKey().length());
103        }
104
105        List<StringBuffer> lines = new LinkedList<>();
106        for (Map.Entry<String, Map<String, Integer>> sequenceStats : counters.entrySet()) {
107            StringBuffer line = new StringBuffer();
108            line.append(sequenceStats.getKey());
109           
110            for (int i = sequenceStats.getKey().length(); i < maxEventNameLength; i++) {
111                line.append(' ');
112            }
113           
114            for (int i = 0; i < sequenceNames.size(); i++) {
115                String sequenceName = sequenceNames.get(i);
116                String numberStr = "";
117               
118                if (sequenceStats.getValue().get(sequenceName) != null) {
119                    int value = sequenceStats.getValue().get(sequenceName);
120                    numberStr += value;
121                    numberStr += " (";
122                    numberStr += ((float) value) / sequences.get(i).size();
123                    numberStr += ")";
124                }
125               
126                line.append(" | ");
127                line.append(numberStr);
128               
129                for (int j = numberStr.length(); j < sequenceName.length(); j++) {
130                    line.append(' ');
131                }
132            }
133           
134            lines.add(line);
135        }
136       
137        for (int i = 0; i < maxEventNameLength; i++) {
138            System.out.print(' ');
139        }
140       
141        for (String sequenceName : sequenceNames) {
142            System.out.print(" | " + sequenceName);
143        }
144       
145        System.out.println();
146       
147        for (StringBuffer line : lines) {
148            System.out.println(line);
149        }
150    }
151
152    /*
153     * (non-Javadoc)
154     *
155     * @see de.ugoe.cs.util.console.Command#help()
156     */
157    @Override
158    public String help() {
159        return "eventStatistics [<sequencesName>]*";
160    }
161
162}
Note: See TracBrowser for help on using the repository browser.