[1493] | 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.usability; |
---|
| 16 | |
---|
[2042] | 17 | import java.util.ArrayList; |
---|
[1493] | 18 | import java.util.HashMap; |
---|
| 19 | import java.util.HashSet; |
---|
| 20 | import java.util.LinkedList; |
---|
| 21 | import java.util.List; |
---|
| 22 | import java.util.Map; |
---|
| 23 | import java.util.Set; |
---|
| 24 | |
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
[2146] | 26 | import de.ugoe.cs.autoquest.eventcore.IHierarchicalEventTargetModel; |
---|
[1493] | 27 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 28 | import de.ugoe.cs.autoquest.eventcore.guimodel.IButton; |
---|
| 29 | import de.ugoe.cs.autoquest.eventcore.guimodel.ICheckBox; |
---|
| 30 | import de.ugoe.cs.autoquest.eventcore.guimodel.IComboBox; |
---|
| 31 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[2042] | 32 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIView; |
---|
[1493] | 33 | import de.ugoe.cs.autoquest.eventcore.guimodel.IListBox; |
---|
| 34 | import de.ugoe.cs.autoquest.eventcore.guimodel.IMenuButton; |
---|
| 35 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITextArea; |
---|
| 36 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITextField; |
---|
[2042] | 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskInstanceTraversingVisitor; |
---|
[1493] | 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel; |
---|
[2042] | 43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[1493] | 44 | |
---|
| 45 | /** |
---|
| 46 | * TODO comment |
---|
| 47 | * |
---|
| 48 | * @version $Revision: $ $Date: 16.07.2012$ |
---|
| 49 | * @author 2012, last modified by $Author: pharms$ |
---|
| 50 | */ |
---|
| 51 | public class UnusedGUIElementsRule implements UsabilityEvaluationRule { |
---|
| 52 | |
---|
| 53 | /* |
---|
| 54 | * (non-Javadoc) |
---|
| 55 | * |
---|
| 56 | * @see de.ugoe.cs.usability.UsabilityEvaluationRule#evaluate(TaskTree) |
---|
| 57 | */ |
---|
| 58 | @Override |
---|
| 59 | public UsabilityEvaluationResult evaluate(ITaskModel taskModel) { |
---|
| 60 | UsabilityEvaluationResult results = new UsabilityEvaluationResult(taskModel); |
---|
[2042] | 61 | |
---|
| 62 | Map<IGUIView, List<Set<IGUIElement>>> viewDisplays = |
---|
| 63 | getViewDisplays(taskModel.getUserSessions()); |
---|
| 64 | |
---|
| 65 | Map<IGUIView, Set<IGUIElement>> allGUIElements = getAllGUIElements(taskModel); |
---|
| 66 | |
---|
| 67 | for (Map.Entry<IGUIView, List<Set<IGUIElement>>> viewDisplay : viewDisplays.entrySet()) { |
---|
| 68 | handleUnusedGUIElements |
---|
| 69 | (allGUIElements, viewDisplay.getKey(), viewDisplay.getValue(), results); |
---|
| 70 | } |
---|
[1493] | 71 | |
---|
| 72 | return results; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
[2042] | 76 | * @param results |
---|
[1493] | 77 | * |
---|
| 78 | */ |
---|
[2042] | 79 | private void handleUnusedGUIElements(Map<IGUIView, Set<IGUIElement>> usedGUIElements, |
---|
| 80 | IGUIView view, |
---|
| 81 | List<Set<IGUIElement>> viewUsages, |
---|
| 82 | UsabilityEvaluationResult results) |
---|
[1493] | 83 | { |
---|
[2042] | 84 | Set<IGUIElement> allElementsInView = usedGUIElements.get(view); |
---|
[1493] | 85 | |
---|
[2042] | 86 | if (allElementsInView == null) { |
---|
| 87 | return; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | Map<Integer, List<IGUIElement>> usageCounters = new HashMap<>(); |
---|
| 91 | |
---|
| 92 | for (IGUIElement relevantElement : allElementsInView) { |
---|
| 93 | int usageCounter = 0; |
---|
[1493] | 94 | |
---|
[2042] | 95 | for (Set<IGUIElement> viewUsage : viewUsages) { |
---|
| 96 | if (viewUsage.contains(relevantElement)) { |
---|
| 97 | usageCounter++; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | List<IGUIElement> elementsWithSameUsage = usageCounters.get(usageCounter); |
---|
| 102 | |
---|
| 103 | if (elementsWithSameUsage == null) { |
---|
| 104 | elementsWithSameUsage = new LinkedList<>(); |
---|
| 105 | usageCounters.put(usageCounter, elementsWithSameUsage); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | elementsWithSameUsage.add(relevantElement); |
---|
[1493] | 109 | } |
---|
[2042] | 110 | |
---|
| 111 | int cumulativeGuiElementUsage = 0; |
---|
| 112 | for (Set<IGUIElement> viewUsage : viewUsages) { |
---|
| 113 | cumulativeGuiElementUsage += viewUsage.size(); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | List<IGUIElement> unusedElements = usageCounters.get(0); |
---|
| 117 | |
---|
| 118 | if (unusedElements != null) { |
---|
| 119 | int ratio = 1000 * unusedElements.size() / allElementsInView.size(); |
---|
[1493] | 120 | |
---|
[2042] | 121 | UsabilitySmellIntensity severity = UsabilitySmellIntensity.getIntensity |
---|
| 122 | (ratio, cumulativeGuiElementUsage, -1); |
---|
| 123 | |
---|
| 124 | if (severity != null) { |
---|
| 125 | Map<String, Object> parameters = new HashMap<String, Object>(); |
---|
| 126 | |
---|
| 127 | parameters.put("ratio", ratio / 10); |
---|
| 128 | parameters.put("allDisplays", viewUsages.size()); |
---|
| 129 | parameters.put("view", view); |
---|
| 130 | parameters.put("unusedGuiElements", unusedElements); |
---|
| 131 | parameters.put("allGuiElements", allElementsInView.size()); |
---|
| 132 | |
---|
| 133 | results.addSmell |
---|
| 134 | (severity, UsabilitySmellDescription.UNUSED_GUI_ELEMENTS, parameters); |
---|
[1493] | 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * |
---|
| 141 | */ |
---|
[2042] | 142 | private Map<IGUIView, Set<IGUIElement>> getAllGUIElements(ITaskModel taskModel) { |
---|
| 143 | Map<IGUIView, Set<IGUIElement>> result = new HashMap<>(); |
---|
[1493] | 144 | |
---|
| 145 | for (ITask task : taskModel.getTasks()) { |
---|
| 146 | if (task instanceof IEventTask) { |
---|
| 147 | for (ITaskInstance instance : task.getInstances()) { |
---|
| 148 | Event event = ((IEventTaskInstance) instance).getEvent(); |
---|
| 149 | |
---|
[2042] | 150 | if ((event.getTarget() instanceof IGUIElement) && |
---|
| 151 | (isRelevant((IGUIElement) event.getTarget()))) |
---|
| 152 | { |
---|
| 153 | IGUIView view = ((IGUIElement) event.getTarget()).getView(); |
---|
| 154 | |
---|
| 155 | Set<IGUIElement> elements = result.get(view); |
---|
| 156 | |
---|
| 157 | if (elements == null) { |
---|
| 158 | elements = new HashSet<>(); |
---|
| 159 | result.put(view, elements); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | elements.add((IGUIElement) event.getTarget()); |
---|
[1493] | 163 | } |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
[2135] | 168 | if (result.size() <= 0) { |
---|
| 169 | return result; |
---|
| 170 | } |
---|
| 171 | |
---|
[2042] | 172 | // the problem is, that using the GUI model does not allow to find all in a specific view |
---|
| 173 | // as the GUI model may return a merged element instead. But anyway, we can add those, which |
---|
| 174 | // are in the GUI model and have the same view. |
---|
[1493] | 175 | |
---|
[2042] | 176 | GUIModel model = result.values().iterator().next().iterator().next().getGUIModel(); |
---|
| 177 | |
---|
[2146] | 178 | IHierarchicalEventTargetModel.Traverser<IGUIElement> traverser = model.getTraverser(); |
---|
[1493] | 179 | |
---|
[2042] | 180 | IGUIElement currentGUIElement = null; |
---|
| 181 | do { |
---|
| 182 | if (traverser.hasFirstChild()) { |
---|
| 183 | currentGUIElement = traverser.firstChild(); |
---|
| 184 | } |
---|
| 185 | else if (traverser.hasNextSibling()) { |
---|
| 186 | currentGUIElement = traverser.nextSibling(); |
---|
| 187 | } |
---|
| 188 | else { |
---|
| 189 | while (currentGUIElement != null) { |
---|
| 190 | currentGUIElement = traverser.parent(); |
---|
| 191 | if (traverser.hasNextSibling()) { |
---|
| 192 | currentGUIElement = traverser.nextSibling(); |
---|
| 193 | break; |
---|
[1493] | 194 | } |
---|
| 195 | } |
---|
[2042] | 196 | } |
---|
[1493] | 197 | |
---|
[2042] | 198 | if (isRelevant(currentGUIElement)) { |
---|
| 199 | IGUIView view = currentGUIElement.getView(); |
---|
| 200 | |
---|
| 201 | Set<IGUIElement> elements = result.get(view); |
---|
| 202 | |
---|
| 203 | if (elements == null) { |
---|
| 204 | elements = new HashSet<>(); |
---|
| 205 | result.put(view, elements); |
---|
[1493] | 206 | } |
---|
[2042] | 207 | |
---|
| 208 | elements.add(currentGUIElement); |
---|
[1493] | 209 | } |
---|
| 210 | } |
---|
[2042] | 211 | while (currentGUIElement != null); |
---|
[1493] | 212 | |
---|
[2042] | 213 | return result; |
---|
[1493] | 214 | } |
---|
[2042] | 215 | |
---|
[1493] | 216 | /** |
---|
[2042] | 217 | * |
---|
[1493] | 218 | */ |
---|
[2042] | 219 | private Map<IGUIView, List<Set<IGUIElement>>> getViewDisplays(List<IUserSession> sessions) { |
---|
| 220 | final IGUIView[] currentView = new IGUIView[1]; |
---|
| 221 | final List<IEventTaskInstance> actionInstances = new ArrayList<>(); |
---|
| 222 | final Map<IGUIView, List<Set<IGUIElement>>> result = new HashMap<>(); |
---|
| 223 | |
---|
| 224 | for (IUserSession session : sessions) { |
---|
| 225 | currentView[0] = null; |
---|
| 226 | actionInstances.clear(); |
---|
| 227 | |
---|
| 228 | for (final ITaskInstance currentRoot : session) { |
---|
| 229 | currentRoot.accept(new DefaultTaskInstanceTraversingVisitor() { |
---|
| 230 | @Override |
---|
| 231 | public void visit(IEventTaskInstance eventTaskInstance) { |
---|
| 232 | if (eventTaskInstance.getEvent().getTarget() instanceof IGUIElement) { |
---|
| 233 | IGUIView view = |
---|
| 234 | ((IGUIElement) eventTaskInstance.getEvent().getTarget()).getView(); |
---|
| 235 | |
---|
| 236 | if ((currentView[0] == null) && (view != null)) { |
---|
| 237 | currentView[0] = view; |
---|
| 238 | actionInstances.clear(); |
---|
| 239 | } |
---|
| 240 | else if ((currentView[0] != null) && (!currentView[0].equals(view))) { |
---|
| 241 | addRelevantTargets(currentView[0], actionInstances, result); |
---|
| 242 | |
---|
| 243 | currentView[0] = view; |
---|
| 244 | actionInstances.clear(); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | if (eventTaskInstance.getEvent().getTarget() instanceof IGUIElement) { |
---|
| 249 | actionInstances.add(eventTaskInstance); |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | }); |
---|
[1493] | 253 | } |
---|
[2042] | 254 | |
---|
| 255 | // add the used GUI elements of the last shown view in the session |
---|
| 256 | if (currentView[0] != null) { |
---|
| 257 | addRelevantTargets(currentView[0], actionInstances, result); |
---|
| 258 | } |
---|
[1493] | 259 | } |
---|
| 260 | |
---|
[2042] | 261 | return result; |
---|
[1493] | 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * |
---|
| 266 | */ |
---|
[2042] | 267 | private void addRelevantTargets(IGUIView view, |
---|
| 268 | List<IEventTaskInstance> actionInstances, |
---|
| 269 | Map<IGUIView, List<Set<IGUIElement>>> result) |
---|
[1493] | 270 | { |
---|
[2042] | 271 | List<Set<IGUIElement>> usedGUIElements = result.get(view); |
---|
| 272 | |
---|
| 273 | if (usedGUIElements == null) { |
---|
| 274 | usedGUIElements = new LinkedList<>(); |
---|
| 275 | result.put(view, usedGUIElements); |
---|
[1493] | 276 | } |
---|
[2042] | 277 | |
---|
| 278 | Set<IGUIElement> elementsInViewDisplay = new HashSet<>(); |
---|
| 279 | |
---|
| 280 | for (IEventTaskInstance actionInstance : actionInstances) { |
---|
| 281 | IGUIElement element = (IGUIElement) actionInstance.getEvent().getTarget(); |
---|
[1493] | 282 | |
---|
[2042] | 283 | while (element != null) { |
---|
| 284 | if (isRelevant(element)) { |
---|
| 285 | elementsInViewDisplay.add(element); |
---|
[1493] | 286 | } |
---|
[2042] | 287 | |
---|
| 288 | element = element.getParent(); |
---|
[1493] | 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
[2042] | 292 | usedGUIElements.add(elementsInViewDisplay); |
---|
[1493] | 293 | } |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | * |
---|
| 297 | */ |
---|
| 298 | private boolean isRelevant(IGUIElement currentGUIElement) { |
---|
| 299 | if (currentGUIElement == null) { |
---|
| 300 | return false; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | if ((currentGUIElement instanceof IButton) || |
---|
| 304 | (currentGUIElement instanceof ICheckBox) || |
---|
| 305 | (currentGUIElement instanceof IComboBox) || |
---|
| 306 | (currentGUIElement instanceof IListBox) || |
---|
| 307 | (currentGUIElement instanceof IMenuButton) || |
---|
| 308 | (currentGUIElement instanceof ITextArea) || |
---|
| 309 | (currentGUIElement instanceof ITextField)) |
---|
| 310 | { |
---|
| 311 | return true; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | return false; |
---|
| 315 | } |
---|
| 316 | } |
---|