[2260] | 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.LinkedList;
|
---|
| 20 | import java.util.List;
|
---|
| 21 |
|
---|
| 22 | import de.ugoe.cs.autoquest.CommandHelpers;
|
---|
| 23 | import de.ugoe.cs.autoquest.SequenceInstanceOf;
|
---|
| 24 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.IHierarchicalEventTarget;
|
---|
| 26 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonDown;
|
---|
| 27 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction;
|
---|
| 28 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick;
|
---|
| 29 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick;
|
---|
| 30 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
|
---|
| 31 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
|
---|
| 32 | import de.ugoe.cs.util.console.Command;
|
---|
| 33 | import de.ugoe.cs.util.console.Console;
|
---|
| 34 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * <p>
|
---|
| 38 | * TODO comment
|
---|
| 39 | * </p>
|
---|
| 40 | *
|
---|
| 41 | * @author Patrick Harms
|
---|
| 42 | * @version 1.0
|
---|
| 43 | */
|
---|
| 44 | public class CMDremoveEventDuplicates implements Command {
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | * (non-Javadoc)
|
---|
| 48 | *
|
---|
| 49 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 50 | */
|
---|
| 51 | @Override
|
---|
| 52 | public String help() {
|
---|
| 53 | return "removeEventDuplicates <sequences> {<new sequences>}";
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /*
|
---|
| 57 | * (non-Javadoc)
|
---|
| 58 | *
|
---|
| 59 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 60 | */
|
---|
| 61 | @SuppressWarnings("unchecked")
|
---|
| 62 | @Override
|
---|
| 63 | public void run(List<Object> parameters) {
|
---|
| 64 | String sequencesName;
|
---|
| 65 | String newSequencesName;
|
---|
| 66 | try {
|
---|
| 67 | sequencesName = (String) parameters.get(0);
|
---|
| 68 | if (parameters.size() > 1) {
|
---|
| 69 | newSequencesName = (String) parameters.get(1);
|
---|
| 70 | }
|
---|
| 71 | else {
|
---|
| 72 | newSequencesName = sequencesName;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | catch (Exception e) {
|
---|
| 76 | throw new IllegalArgumentException("must provide a sequences name");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
| 80 | if (dataObject == null) {
|
---|
| 81 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
| 82 | return;
|
---|
| 83 | }
|
---|
| 84 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
| 85 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | Collection<List<Event>> newSequences =
|
---|
| 90 | removeEventDuplicates((Collection<List<Event>>) dataObject);
|
---|
| 91 |
|
---|
| 92 | if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
|
---|
| 93 | CommandHelpers.dataOverwritten(newSequencesName);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | *
|
---|
| 100 | */
|
---|
| 101 | private Collection<List<Event>> removeEventDuplicates(Collection<List<Event>> sequences) {
|
---|
| 102 | Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
|
---|
| 103 |
|
---|
| 104 | //long[] diffCounter = new long[1000];
|
---|
| 105 | //Map<String, long[]> diffCounterForPairs = new HashMap<>();
|
---|
| 106 |
|
---|
| 107 | int eventsRemoved = 0;
|
---|
| 108 |
|
---|
| 109 | for (List<Event> sequence : sequences) {
|
---|
| 110 | Event predecessor = null;
|
---|
| 111 | List<Event> result = new ArrayList<Event>();
|
---|
| 112 |
|
---|
| 113 | System.out.println("sequence");
|
---|
| 114 |
|
---|
| 115 | for (Event event : sequence) {
|
---|
| 116 | long diff = (predecessor != null) ?
|
---|
| 117 | (event.getTimestamp() - predecessor.getTimestamp()) : Long.MAX_VALUE;
|
---|
| 118 |
|
---|
| 119 | System.out.println(" " + event.getType() + "\t" + ((IHierarchicalEventTarget) event.getTarget()).getSpecification().toString() + "\t" + event.getTimestamp() + "\t" + diff);
|
---|
| 120 | /*if (diff < diffCounter.length) {
|
---|
| 121 | diffCounter[(int) diff]++;
|
---|
| 122 |
|
---|
| 123 | long[] specCounter = diffCounterForPairs.get(getKey(predecessor, event));
|
---|
| 124 |
|
---|
| 125 | if (specCounter == null) {
|
---|
| 126 | specCounter = new long[diffCounter.length];
|
---|
| 127 | diffCounterForPairs.put(getKey(predecessor, event), specCounter);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | specCounter[(int) diff]++;
|
---|
| 131 | }*/
|
---|
| 132 |
|
---|
| 133 | // this border of 30 milliseconds was determined by plotting the time differences
|
---|
| 134 | // and seeing that there is a real drop of time differences above 30 milliseconds.
|
---|
| 135 | // And 30 milliseconds is still small enough to expect, that this is not triggered
|
---|
| 136 | // by a human.
|
---|
| 137 | if (diff < 30) {
|
---|
| 138 | Event toPrefer = getEventToPrefer(predecessor, event);
|
---|
| 139 |
|
---|
| 140 | if (toPrefer != null) {
|
---|
| 141 | result.set(result.size() - 1, toPrefer);
|
---|
| 142 | eventsRemoved++;
|
---|
| 143 | }
|
---|
| 144 | else {
|
---|
| 145 | // do not prefer any of them
|
---|
| 146 | result.add(event);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | else {
|
---|
| 150 | result.add(event);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | predecessor = result.get(result.size() - 1);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | newSequences.add(result);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /*for (int i = 0; i < diffCounter.length; i++) {
|
---|
| 160 | System.out.print((i + 1) + "ms :\t ");
|
---|
| 161 | for (int j = 0; j < diffCounter[i]; j = j + 10) {
|
---|
| 162 | System.out.print('|');
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | System.out.println();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | for (Map.Entry<String, long[]> entry : diffCounterForPairs.entrySet()) {
|
---|
| 169 | System.out.println("\n\n" + entry.getKey());
|
---|
| 170 | for (int i = 0; i < entry.getValue().length; i++) {
|
---|
| 171 | if (entry.getValue()[i] > 0) {
|
---|
| 172 | System.out.print((i + 1) + "ms :\t ");
|
---|
| 173 | for (int j = 0; j < entry.getValue()[i]; j = j + 10) {
|
---|
| 174 | System.out.print('|');
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | System.out.println();
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }*/
|
---|
| 181 |
|
---|
| 182 | Console.println("removed " + eventsRemoved + " events");
|
---|
| 183 |
|
---|
| 184 | return newSequences;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | *
|
---|
| 189 | */
|
---|
| 190 | /*private String getKey(Event event1, Event event2) {
|
---|
| 191 | return event1.getType().getClass().getSimpleName() + " on " + event1.getTarget().getClass()
|
---|
| 192 | + " --> " + event2.getType().getClass().getSimpleName() + " on " +
|
---|
| 193 | event2.getTarget().getClass();
|
---|
| 194 | }*/
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | *
|
---|
| 198 | */
|
---|
| 199 | private Event getEventToPrefer(Event event1, Event event2) {
|
---|
| 200 | Event result = null;
|
---|
| 201 |
|
---|
| 202 | if (!isSameEventType(event1, event2)) {
|
---|
| 203 | if (isSameEventTarget(event1, event2) ||
|
---|
| 204 | oneIsParentTargetOfOtherTarget(event1, event2))
|
---|
| 205 | {
|
---|
| 206 | // in a combination of click and value selection on the same element or an element
|
---|
| 207 | // and its parent, prefer the value selection as it is triggered by the click
|
---|
| 208 | if (isLeftMouseButtonInteraction(event1) && isValueSelection(event2)) {
|
---|
| 209 | result = event2;
|
---|
| 210 | }
|
---|
| 211 | else if (isLeftMouseButtonInteraction(event2) && isValueSelection(event1)) {
|
---|
| 212 | result = event1;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | return result;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | *
|
---|
| 222 | */
|
---|
| 223 | private boolean isSameEventType(Event event1, Event event2) {
|
---|
| 224 | return event1.getType().getName().equals(event2.getType().getName());
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
| 228 | *
|
---|
| 229 | */
|
---|
| 230 | private boolean isSameEventTarget(Event event1, Event event2) {
|
---|
| 231 | return event1.getTarget().equals(event2.getTarget());
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | *
|
---|
| 236 | */
|
---|
| 237 | private boolean oneIsParentTargetOfOtherTarget(Event event1, Event event2) {
|
---|
| 238 | if ((event1.getTarget() instanceof IGUIElement) &&
|
---|
| 239 | (event2.getTarget() instanceof IGUIElement))
|
---|
| 240 | {
|
---|
| 241 | IGUIElement elem1 = (IGUIElement) event1.getTarget();
|
---|
| 242 | IGUIElement elem2 = (IGUIElement) event2.getTarget();
|
---|
| 243 |
|
---|
| 244 | return isParent(elem1, elem2) || isParent(elem2, elem1);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | return false;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /**
|
---|
| 251 | *
|
---|
| 252 | */
|
---|
| 253 | private boolean isParent(IGUIElement elem1, IGUIElement elem2) {
|
---|
| 254 | IGUIElement parent = elem2.getParent();
|
---|
| 255 |
|
---|
| 256 | while (parent != null) {
|
---|
| 257 | if (parent.equals(elem1)) {
|
---|
| 258 | return true;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | parent = parent.getParent();
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | return false;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /**
|
---|
| 268 | *
|
---|
| 269 | */
|
---|
| 270 | private boolean isLeftMouseButtonInteraction(Event event) {
|
---|
| 271 | if (((event.getType() instanceof MouseClick) ||
|
---|
| 272 | (event.getType() instanceof MouseDoubleClick) ||
|
---|
| 273 | (event.getType() instanceof MouseButtonDown)) &&
|
---|
| 274 | (((MouseButtonInteraction) event.getType()).getButton() ==
|
---|
| 275 | MouseButtonInteraction.Button.LEFT))
|
---|
| 276 | {
|
---|
| 277 | return true;
|
---|
| 278 | }
|
---|
| 279 | else {
|
---|
| 280 | return false;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /**
|
---|
| 285 | *
|
---|
| 286 | */
|
---|
| 287 | private boolean isValueSelection(Event event) {
|
---|
| 288 | return event.getType() instanceof ValueSelection;
|
---|
| 289 | }
|
---|
| 290 | }
|
---|