[1107] | 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.tasktrees.temporalrelation; |
---|
| 16 | |
---|
[1656] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
[1655] | 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
| 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
[1656] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1294] | 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1655] | 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1146] | 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
[1107] | 29 | |
---|
| 30 | /** |
---|
| 31 | * <p> |
---|
[1281] | 32 | * provides some convenience methods for rule application |
---|
[1107] | 33 | * </p> |
---|
| 34 | * |
---|
| 35 | * @author Patrick Harms |
---|
| 36 | */ |
---|
| 37 | class RuleUtils { |
---|
| 38 | |
---|
[1655] | 39 | /** |
---|
| 40 | * <p> |
---|
| 41 | * replaces a sub sequence for a specified range of elements in the provided |
---|
| 42 | * task instances list by a sub task instance |
---|
| 43 | * </p> |
---|
| 44 | * |
---|
| 45 | * @param parent |
---|
| 46 | * the list of which the range shall be replaced |
---|
| 47 | * @param startIndex |
---|
| 48 | * the start index of the range |
---|
| 49 | * @param endIndex |
---|
| 50 | * the end index of the range (inclusive) |
---|
| 51 | * @param model |
---|
| 52 | * the task model (required for instantiating the sub sequence) |
---|
| 53 | * @param taskFactory |
---|
| 54 | * the task factory used for instantiating the sub sequence |
---|
| 55 | * @param taskBuilder |
---|
| 56 | * the task builder to perform changes in the task structure |
---|
| 57 | * |
---|
| 58 | * @return the replacement for the range |
---|
[1733] | 59 | * @throws |
---|
[1655] | 60 | */ |
---|
| 61 | static ISequenceInstance createNewSubSequenceInRange( |
---|
| 62 | ITaskInstanceList parent, int startIndex, int endIndex, |
---|
[1733] | 63 | ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 64 | final ISequenceInstance subsequence = taskFactory |
---|
[1655] | 65 | .createNewTaskInstance(model); |
---|
[1733] | 66 | |
---|
[1700] | 67 | // TODO: Debug output |
---|
[1702] | 68 | /* |
---|
[1733] | 69 | * System.out.println("PRINTING MODEL: "); for (int i = 0; i < |
---|
| 70 | * subsequence.getSequence().getChildren().size(); i++) { |
---|
| 71 | * System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
| 72 | * |
---|
| 73 | * if (subsequence.getSequence().getChildren().get(i).getType() == |
---|
| 74 | * "selection") { for (int j = 0; j < ((ISelection) |
---|
| 75 | * subsequence.getSequence().getChildren().get(i)) |
---|
| 76 | * .getChildren().size(); j++) { if(((IStructuringTemporalRelationship) |
---|
| 77 | * subsequence |
---|
| 78 | * .getSequence().getChildren().get(i)).getChildren().get(j).getType() |
---|
| 79 | * =="sequence") { ISequence foo = (ISequence) ((ISelection) |
---|
| 80 | * (subsequence |
---|
| 81 | * .getSequence().getChildren().get(i))).getChildren().get(j); |
---|
| 82 | * System.out.println("\t" + foo); for(int k=0; k< |
---|
| 83 | * foo.getChildren().size();k++) { System.out.println("\t\t" |
---|
| 84 | * +foo.getChildren().get(k)); } System.out.println(); } else{ |
---|
| 85 | * System.out.println("\t" + ((ISelection) |
---|
| 86 | * subsequence.getSequence().getChildren().get(i)) |
---|
| 87 | * .getChildren().get(j)); } |
---|
| 88 | * |
---|
| 89 | * } } |
---|
| 90 | * |
---|
| 91 | * } System.out.println(); |
---|
| 92 | */ |
---|
[1700] | 93 | |
---|
[1733] | 94 | // TODO: This is dirty! |
---|
| 95 | missedOptionals = 0; |
---|
| 96 | int modelindex = 0; |
---|
[1655] | 97 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1733] | 98 | |
---|
| 99 | if (modelindex == model.getChildren().size()) { |
---|
[1657] | 100 | break; |
---|
| 101 | } |
---|
[1733] | 102 | final ITask tempTask = model.getChildren().get(modelindex); |
---|
| 103 | // System.out.println("Trying to add " + parent.get(startIndex) |
---|
| 104 | // + " to the model instance " + tempTask); |
---|
[1655] | 105 | if (tempTask.getType() == "optionality") { |
---|
[1733] | 106 | |
---|
| 107 | if (((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent |
---|
| 108 | .get(startIndex).getTask()) { |
---|
| 109 | // System.out.println("Adding OptionalInstance " + |
---|
| 110 | // parent.get(startIndex) + " to " + tempTask.getType()); |
---|
| 111 | final IOptionalInstance optional = taskFactory |
---|
| 112 | .createNewTaskInstance((IOptional) tempTask); |
---|
| 113 | taskBuilder.setChild(optional, parent.get(startIndex)); |
---|
| 114 | taskBuilder.addChild(subsequence, optional); |
---|
| 115 | } else { |
---|
| 116 | // System.out.println("Adding Empty optional, not deleting anything from the input sequence"); |
---|
| 117 | final IOptionalInstance optional = taskFactory |
---|
| 118 | .createNewTaskInstance((IOptional) tempTask); |
---|
| 119 | taskBuilder.addChild(subsequence, optional); |
---|
| 120 | modelindex++; |
---|
| 121 | missedOptionals++; |
---|
| 122 | continue; |
---|
| 123 | } |
---|
[1655] | 124 | } else if (tempTask.getType() == "selection") { |
---|
[1733] | 125 | final ISelectionInstance selection = taskFactory |
---|
| 126 | .createNewTaskInstance((ISelection) tempTask); |
---|
| 127 | final ISelection tmpSel = (ISelection) tempTask; |
---|
| 128 | if ((tmpSel.getChildren().get(0).getType() == "sequence") |
---|
| 129 | && (tmpSel.getChildren().get(1).getType() == "sequence")) { |
---|
[1693] | 130 | ISequenceInstance selseq = null; |
---|
[1733] | 131 | // The selection I create can just have 2 children |
---|
| 132 | if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel |
---|
| 133 | .getChildren().get(0)).getChildren().get(0).getId()) { |
---|
| 134 | selseq = taskFactory |
---|
| 135 | .createNewTaskInstance((ISequence) tmpSel |
---|
| 136 | .getChildren().get(0)); |
---|
| 137 | } else if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel |
---|
| 138 | .getChildren().get(1)).getChildren().get(0).getId()) { |
---|
| 139 | selseq = taskFactory |
---|
| 140 | .createNewTaskInstance((ISequence) tmpSel |
---|
| 141 | .getChildren().get(1)); |
---|
| 142 | } else if ((parent.get(startIndex).getTask().getId() == tmpSel |
---|
| 143 | .getChildren().get(0).getId()) |
---|
| 144 | || (parent.get(startIndex).getTask().getId() == tmpSel |
---|
| 145 | .getChildren().get(1).getId())) { |
---|
| 146 | // System.out.println("Session ID: " + |
---|
| 147 | // parent.get(startIndex).getTask().getId() + |
---|
| 148 | // " tmpSel(0): " + tmpSel.getChildren().get(0).getId() |
---|
| 149 | // + " tmpSel(1): " +tmpSel.getChildren().get(1).getId() |
---|
| 150 | // ); |
---|
[1694] | 151 | continue; |
---|
| 152 | } |
---|
[1733] | 153 | |
---|
| 154 | for (int k = 0; k < selseq.getSequence().getChildren() |
---|
| 155 | .size(); k++) { |
---|
| 156 | // System.out.println("Trying to add " + |
---|
| 157 | // parent.get(startIndex) + " to " + selseq); |
---|
| 158 | taskBuilder.addChild(selseq, parent.get(startIndex)); |
---|
[1693] | 159 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 160 | i++; |
---|
[1733] | 161 | // System.out.println("I:" + i); |
---|
[1693] | 162 | } |
---|
[1733] | 163 | // System.out.println("Trying to add " + selseq + " to " + |
---|
| 164 | // tmpSel); |
---|
[1693] | 165 | taskBuilder.setChild(selection, selseq); |
---|
[1700] | 166 | taskBuilder.addChild(subsequence, selection); |
---|
| 167 | modelindex++; |
---|
| 168 | continue; |
---|
[1733] | 169 | } else { |
---|
| 170 | // System.out.println("Trying to adding SelectionInstance " |
---|
| 171 | // + parent.get(startIndex) + " to " + tempTask); |
---|
[1693] | 172 | taskBuilder.setChild(selection, parent.get(startIndex)); |
---|
[1733] | 173 | taskBuilder.addChild(subsequence, selection); |
---|
[1693] | 174 | } |
---|
[1655] | 175 | } else if (tempTask.getType() == "sequence") { |
---|
[1733] | 176 | // System.out.println("Adding SequenceInstance " + |
---|
| 177 | // parent.get(startIndex) + " to " + tempTask); |
---|
[1694] | 178 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 179 | } else if (tempTask.getType() == "iteration") { |
---|
[1733] | 180 | // System.out.println("Adding IterationInstance " + |
---|
| 181 | // parent.get(startIndex) + " to " + tempTask); |
---|
[1682] | 182 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 183 | } else { |
---|
[1733] | 184 | // System.out.println("Adding EventInstance " + |
---|
| 185 | // parent.get(startIndex) + " to " + tempTask); |
---|
| 186 | // System.out.println("Foo"); |
---|
[1656] | 187 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 188 | } |
---|
| 189 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 190 | modelindex++; |
---|
| 191 | } |
---|
[1733] | 192 | |
---|
[1655] | 193 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
| 194 | |
---|
| 195 | return subsequence; |
---|
| 196 | } |
---|
| 197 | |
---|
[1733] | 198 | /** |
---|
| 199 | * <p> |
---|
| 200 | * returns the next available id (uses the id counter) |
---|
| 201 | * </p> |
---|
| 202 | * |
---|
| 203 | * @return the next available id |
---|
| 204 | */ |
---|
| 205 | static synchronized String getNewId() { |
---|
| 206 | return Integer.toString(idCounter++); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * <p> |
---|
| 211 | * generates a sub sequence for a specified range of elements in the |
---|
| 212 | * provided task instances list. |
---|
| 213 | * </p> |
---|
| 214 | * |
---|
| 215 | * @param parent |
---|
| 216 | * the list of which the range shall be extracted |
---|
| 217 | * @param startIndex |
---|
| 218 | * the start index of the range |
---|
| 219 | * @param endIndex |
---|
| 220 | * the end index of the range (inclusive) |
---|
| 221 | * @param model |
---|
| 222 | * the task model (required for instantiating the sub sequence) |
---|
| 223 | * @param taskFactory |
---|
| 224 | * the task factory used for instantiating the sub sequence |
---|
| 225 | * @param taskBuilder |
---|
| 226 | * the task builder to perform changes in the task structure |
---|
| 227 | * |
---|
| 228 | * @return a task instance representing the requested sub sequence |
---|
| 229 | */ |
---|
| 230 | static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, |
---|
| 231 | int startIndex, int endIndex, ISequence model, |
---|
| 232 | ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 233 | final ISequenceInstance subsequence = taskFactory |
---|
| 234 | .createNewTaskInstance(model); |
---|
| 235 | |
---|
| 236 | for (int i = startIndex; i <= endIndex; i++) { |
---|
| 237 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | return subsequence; |
---|
| 241 | } |
---|
| 242 | |
---|
[1731] | 243 | // Print out the progress |
---|
[1733] | 244 | static void printProgressPercentage(String message, int count, int size) { |
---|
[1731] | 245 | if (size > 100) { |
---|
[1733] | 246 | if (((count % (size / 100)) == 0)) { |
---|
[1731] | 247 | // Console.traceln(Level.INFO,("Thread" + |
---|
| 248 | // Thread.currentThread().getName() + ": " + Math.round((float) |
---|
| 249 | // count/size*100))+ "%"); |
---|
[1733] | 250 | System.out.println(message + " in thread" |
---|
| 251 | + Thread.currentThread().getName() + ": " |
---|
| 252 | + Math.round(((float) count / size) * 100) + "%"); |
---|
[1731] | 253 | } |
---|
| 254 | } else { |
---|
| 255 | // Console.traceln(Level.INFO,("Thread" + |
---|
| 256 | // Thread.currentThread().getName() + ": " +Math.round((float) |
---|
| 257 | // count/size*100))+ "%"); |
---|
[1733] | 258 | System.out.println(message + " in thread" |
---|
| 259 | + Thread.currentThread().getName() + ": " |
---|
| 260 | + Math.round(((float) count / size) * 100) + "%"); |
---|
[1731] | 261 | |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[1655] | 265 | /** |
---|
| 266 | * <p> |
---|
[1733] | 267 | * counter for generating unique ids. Starts at 0 for each new program start |
---|
[1655] | 268 | * </p> |
---|
| 269 | */ |
---|
[1733] | 270 | private static int idCounter = 0; |
---|
[1655] | 271 | |
---|
[1733] | 272 | public static int missedOptionals = 0; |
---|
| 273 | |
---|
[1655] | 274 | /** |
---|
| 275 | * <p> |
---|
| 276 | * prevent instantiation |
---|
| 277 | * </p> |
---|
| 278 | */ |
---|
| 279 | private RuleUtils() { |
---|
| 280 | // prevent instantiation |
---|
| 281 | } |
---|
| 282 | |
---|
[1107] | 283 | } |
---|