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.Collection;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.logging.Level;
|
---|
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.gui.KeyInteractionCorrector;
|
---|
26 | import de.ugoe.cs.autoquest.eventcore.gui.KeyInteractionCorrector.CleanupMode;
|
---|
27 | import de.ugoe.cs.util.console.Command;
|
---|
28 | import de.ugoe.cs.util.console.Console;
|
---|
29 | import de.ugoe.cs.util.console.GlobalDataContainer;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * <p>
|
---|
33 | * Command to sort the key interactions in a sequence of events. An example, the sequence to write
|
---|
34 | * the upper case D
|
---|
35 | * </p>
|
---|
36 | * <ul>
|
---|
37 | * <li>press shift key</li>
|
---|
38 | * <li>press D key</li>
|
---|
39 | * <li>release shift key</li>
|
---|
40 | * <li>release D key</li>
|
---|
41 | * </ul>
|
---|
42 | *
|
---|
43 | * <p>
|
---|
44 | * is transformed to the sequence
|
---|
45 | * </p>
|
---|
46 | *
|
---|
47 | * <ul>
|
---|
48 | * <li>press shift key</li>
|
---|
49 | * <li>press D key</li>
|
---|
50 | * <li>release D key</li>
|
---|
51 | * <li>release shift key</li>
|
---|
52 | * </ul>
|
---|
53 | *
|
---|
54 | * <p>
|
---|
55 | * in which the first pressed key (shift in this case) is always released last. The same is done for
|
---|
56 | * the alt and the ctrl keys.
|
---|
57 | * </p>
|
---|
58 | *
|
---|
59 | * @author Patrick Harms
|
---|
60 | * @version 1.0
|
---|
61 | */
|
---|
62 | public class CMDsortKeyInteractions implements Command {
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * (non-Javadoc)
|
---|
66 | *
|
---|
67 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
68 | */
|
---|
69 | @Override
|
---|
70 | public String help() {
|
---|
71 | return "sortKeyInteractions <sequences> {<new sequences>}";
|
---|
72 | }
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * (non-Javadoc)
|
---|
76 | *
|
---|
77 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
78 | */
|
---|
79 | @SuppressWarnings("unchecked")
|
---|
80 | @Override
|
---|
81 | public void run(List<Object> parameters) {
|
---|
82 | String sequencesName;
|
---|
83 | String newSequencesName;
|
---|
84 | String modeString = "ADDITION";
|
---|
85 | try {
|
---|
86 | sequencesName = (String) parameters.get(0);
|
---|
87 | if (parameters.size() > 1) {
|
---|
88 | newSequencesName = (String) parameters.get(1);
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | newSequencesName = sequencesName;
|
---|
92 | }
|
---|
93 | if (parameters.size() > 2) {
|
---|
94 | modeString = (String) parameters.get(2);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | catch (Exception e) {
|
---|
98 | throw new IllegalArgumentException("must provide a sequences name");
|
---|
99 | }
|
---|
100 |
|
---|
101 | Collection<List<Event>> sequences = null;
|
---|
102 | Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
|
---|
103 | if (dataObject == null) {
|
---|
104 | CommandHelpers.objectNotFoundMessage(sequencesName);
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
|
---|
108 | CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | CleanupMode mode = null;
|
---|
113 | try {
|
---|
114 | mode = CleanupMode.valueOf(modeString);
|
---|
115 | }
|
---|
116 | catch (IllegalArgumentException e) {
|
---|
117 | Console.printerrln("Invalid mode. Only REMOVAL and ADDITION are allowed values!");
|
---|
118 | return;
|
---|
119 | }
|
---|
120 |
|
---|
121 | sequences = (Collection<List<Event>>) dataObject;
|
---|
122 |
|
---|
123 | Collection<List<Event>> newSequences = new LinkedList<List<Event>>();
|
---|
124 |
|
---|
125 | int i = 1;
|
---|
126 | for (List<Event> sequence : sequences) {
|
---|
127 | Console.traceln(Level.INFO, "Processing sequence " + i++);
|
---|
128 | newSequences.add(new KeyInteractionCorrector(mode).sortKeyInteractions(sequence));
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (GlobalDataContainer.getInstance().addData(newSequencesName, newSequences)) {
|
---|
132 | CommandHelpers.dataOverwritten(newSequencesName);
|
---|
133 | }
|
---|
134 |
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|