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.usability;
|
---|
16 |
|
---|
17 | import java.text.DecimalFormat;
|
---|
18 | import java.util.HashSet;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Set;
|
---|
22 |
|
---|
23 | import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskInstanceTraversingVisitor;
|
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
|
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
|
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
|
---|
31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
|
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
|
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
|
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
|
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskTreeUtils;
|
---|
38 | import de.ugoe.cs.util.console.Command;
|
---|
39 | import de.ugoe.cs.util.console.Console;
|
---|
40 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * <p>
|
---|
44 | * This command provides statistical information about a given task tree.
|
---|
45 | * </p>
|
---|
46 | *
|
---|
47 | * @author Patrick Harms
|
---|
48 | * @version 1.0
|
---|
49 | */
|
---|
50 | public class CMDtaskTreeStatistics implements Command {
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * (non-Javadoc)
|
---|
54 | *
|
---|
55 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
56 | */
|
---|
57 | @Override
|
---|
58 | public String help() {
|
---|
59 | return "taskTreeStatistics <tasktree>";
|
---|
60 | }
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * (non-Javadoc)
|
---|
64 | *
|
---|
65 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
66 | */
|
---|
67 | @Override
|
---|
68 | public void run(List<Object> parameters) {
|
---|
69 | String tasktreeName;
|
---|
70 | try {
|
---|
71 | tasktreeName = (String) parameters.get(0);
|
---|
72 | }
|
---|
73 | catch (Exception e) {
|
---|
74 | throw new IllegalArgumentException("must provide a task tree name");
|
---|
75 | }
|
---|
76 |
|
---|
77 | Object dataObject = GlobalDataContainer.getInstance().getData(tasktreeName);
|
---|
78 | if (dataObject == null) {
|
---|
79 | CommandHelpers.objectNotFoundMessage(tasktreeName);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | if (!(dataObject instanceof ITaskModel)) {
|
---|
83 | CommandHelpers.objectNotType(tasktreeName, "ITaskModel");
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | ITaskModel taskModel = (ITaskModel) dataObject;
|
---|
88 |
|
---|
89 | int allEvents = 0;
|
---|
90 |
|
---|
91 | int noOfEventTasks = 0;
|
---|
92 | int noOfSequences = 0;
|
---|
93 | int noOfIterations = 0;
|
---|
94 | int noOfSelections = 0;
|
---|
95 | int noOfOptionals = 0;
|
---|
96 | Set<IEventTaskInstance> coveredEvents = new HashSet<IEventTaskInstance>();
|
---|
97 |
|
---|
98 | for (ITask task : taskModel.getTasks()) {
|
---|
99 | if (task instanceof IEventTask) {
|
---|
100 | noOfEventTasks++;
|
---|
101 | allEvents += task.getInstances().size();
|
---|
102 | }
|
---|
103 | else if (task instanceof ISequence) {
|
---|
104 | noOfSequences++;
|
---|
105 | getCoveredEvents(task, coveredEvents);
|
---|
106 | }
|
---|
107 | else if (task instanceof IIteration) {
|
---|
108 | noOfIterations++;
|
---|
109 | }
|
---|
110 | else if (task instanceof ISelection) {
|
---|
111 | noOfSelections++;
|
---|
112 | }
|
---|
113 | else if (task instanceof IOptional) {
|
---|
114 | noOfOptionals++;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | DescriptiveStatistics stats = new DescriptiveStatistics();
|
---|
119 |
|
---|
120 | for (IUserSession session : taskModel.getUserSessions()) {
|
---|
121 | if (session.size() > 0) {
|
---|
122 | final List<IEventTaskInstance> events = new LinkedList<>();
|
---|
123 |
|
---|
124 | for (ITaskInstance instance : session) {
|
---|
125 | instance.accept(new DefaultTaskInstanceTraversingVisitor() {
|
---|
126 | @Override
|
---|
127 | public void visit(IEventTaskInstance eventTaskInstance) {
|
---|
128 | events.add(eventTaskInstance);
|
---|
129 | }
|
---|
130 | });
|
---|
131 | }
|
---|
132 |
|
---|
133 | stats.addValue(events.size());
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | Console.println(stats.getN() + " sessions");
|
---|
138 | Console.println("average session length " + stats.getMean() + " (std dev " +
|
---|
139 | stats.getStandardDeviation() + ")");
|
---|
140 | Console.println(allEvents + " events");
|
---|
141 |
|
---|
142 | Console.println(noOfEventTasks + " eventTasks");
|
---|
143 | Console.println(noOfSequences + " sequences");
|
---|
144 | Console.println(noOfIterations + " iterations");
|
---|
145 | Console.println(noOfSelections + " selections");
|
---|
146 | Console.println(noOfOptionals + " optionals");
|
---|
147 |
|
---|
148 | Set<ISequence> mostProminent =
|
---|
149 | TaskTreeUtils.getMostProminentTasks(taskModel, taskModel.getTasks());
|
---|
150 |
|
---|
151 |
|
---|
152 | int mpCoveredEvents =
|
---|
153 | TaskTreeUtils.getNoOfEventsCoveredBySequences(mostProminent);
|
---|
154 |
|
---|
155 | Console.println(noOfSequences + " sequences cover " +
|
---|
156 | coveredEvents.size() + " of " + allEvents + " recorded events (" +
|
---|
157 | formatPercent(coveredEvents.size(), allEvents) + ")");
|
---|
158 |
|
---|
159 | Console.println(mostProminent.size() + " of " + noOfSequences + " sequences (" +
|
---|
160 | formatPercent(mostProminent.size(), noOfSequences) + ") cover " +
|
---|
161 | mpCoveredEvents + " of " + allEvents + " recorded events (" +
|
---|
162 | formatPercent(mpCoveredEvents, allEvents) + ")");
|
---|
163 |
|
---|
164 | Console.println("CSV: " + tasktreeName + ";" + noOfEventTasks + ";" + noOfSequences + ";" +
|
---|
165 | noOfIterations + ";" + noOfSelections + ";" + noOfOptionals + ";" +
|
---|
166 | allEvents + ";" + coveredEvents.size() + ";" +
|
---|
167 | formatPercent(coveredEvents.size(), allEvents) + ";" +
|
---|
168 | mostProminent.size() + ";" +
|
---|
169 | formatPercent(mostProminent.size(), noOfSequences) + ";" +
|
---|
170 | mpCoveredEvents + ";" + formatPercent(mpCoveredEvents, allEvents));
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * convenience method to format a percentage
|
---|
175 | */
|
---|
176 | private String formatPercent(int ratio, int full) {
|
---|
177 | return new DecimalFormat("#,##0.0%").format(((double) ratio) / full);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * <p>
|
---|
182 | * convenience method to determine the number of covered events by a task
|
---|
183 | * </p>
|
---|
184 | *
|
---|
185 | * @param task the task to count the covered events for
|
---|
186 | * @param coveredEvents the events covered by the task (in/out parameter)
|
---|
187 | */
|
---|
188 | private void getCoveredEvents(ITask task, final Set<IEventTaskInstance> coveredEvents) {
|
---|
189 | for (ITaskInstance instance : task.getInstances()) {
|
---|
190 | instance.accept(new DefaultTaskInstanceTraversingVisitor() {
|
---|
191 | @Override
|
---|
192 | public void visit(IEventTaskInstance eventTaskInstance) {
|
---|
193 | coveredEvents.add(eventTaskInstance);
|
---|
194 | }
|
---|
195 | });
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | }
|
---|