[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 | |
---|
[1747] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
[1656] | 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
[1655] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
| 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
[1656] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1294] | 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1655] | 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1146] | 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
[1107] | 30 | |
---|
| 31 | /** |
---|
| 32 | * <p> |
---|
[1281] | 33 | * provides some convenience methods for rule application |
---|
[1738] | 34 | * </p> |
---|
| 35 | * . |
---|
[1734] | 36 | * |
---|
[1738] | 37 | * @author Patrick Harms, Ralph Krimmel |
---|
[1107] | 38 | */ |
---|
| 39 | class RuleUtils { |
---|
| 40 | |
---|
[1655] | 41 | /** |
---|
| 42 | * <p> |
---|
| 43 | * replaces a sub sequence for a specified range of elements in the provided |
---|
| 44 | * task instances list by a sub task instance |
---|
[1738] | 45 | * </p> |
---|
| 46 | * . |
---|
[1655] | 47 | * |
---|
[1738] | 48 | * @param parent |
---|
| 49 | * the list of which the range shall be replaced |
---|
| 50 | * @param startIndex |
---|
| 51 | * the start index of the range |
---|
| 52 | * @param endIndex |
---|
| 53 | * the end index of the range (inclusive) |
---|
| 54 | * @param model |
---|
| 55 | * the task model (required for instantiating the sub sequence) |
---|
| 56 | * @param taskFactory |
---|
| 57 | * the task factory used for instantiating the sub sequence |
---|
| 58 | * @param taskBuilder |
---|
| 59 | * the task builder to perform changes in the task structure |
---|
[1655] | 60 | * @return the replacement for the range |
---|
| 61 | */ |
---|
[1742] | 62 | static synchronized ISequenceInstance createNewSubSequenceInRange( |
---|
[1655] | 63 | ITaskInstanceList parent, int startIndex, int endIndex, |
---|
[1733] | 64 | ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 65 | final ISequenceInstance subsequence = taskFactory |
---|
[1655] | 66 | .createNewTaskInstance(model); |
---|
[1749] | 67 | /* |
---|
| 68 | System.out.println("STARTINDEX: " + startIndex); |
---|
| 69 | System.out.println("PRINTING SEQUENCE: "); |
---|
| 70 | for(int i =0; i< parent.size();i++) { |
---|
| 71 | System.out.println(i + ": " +parent.get(i)); |
---|
| 72 | } |
---|
[1747] | 73 | System.out.println(); |
---|
| 74 | |
---|
[1743] | 75 | System.out.println("PRINTING MODEL: "); |
---|
| 76 | for (int i = 0; i < subsequence.getSequence().getChildren().size(); i++) { |
---|
| 77 | System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
| 78 | |
---|
| 79 | if (subsequence.getSequence().getChildren().get(i).getType() == "selection") { |
---|
| 80 | for (int j = 0; j < ((ISelection) subsequence.getSequence() |
---|
| 81 | .getChildren().get(i)).getChildren().size(); j++) { |
---|
| 82 | if (((IStructuringTemporalRelationship) subsequence |
---|
| 83 | .getSequence().getChildren().get(i)).getChildren() |
---|
| 84 | .get(j).getType() == "sequence") { |
---|
| 85 | ISequence foo = (ISequence) ((ISelection) (subsequence |
---|
| 86 | .getSequence().getChildren().get(i))) |
---|
| 87 | .getChildren().get(j); |
---|
| 88 | System.out.println("\t" + foo); |
---|
| 89 | for (int k = 0; k < foo.getChildren().size(); k++) { |
---|
| 90 | System.out.println("\t\t" |
---|
| 91 | + foo.getChildren().get(k)); |
---|
| 92 | } |
---|
| 93 | System.out.println(); |
---|
| 94 | } else { |
---|
| 95 | System.out.println("\t" |
---|
| 96 | + ((ISelection) subsequence.getSequence() |
---|
| 97 | .getChildren().get(i)).getChildren() |
---|
| 98 | .get(j)); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | } |
---|
[1738] | 102 | } |
---|
[1743] | 103 | |
---|
| 104 | } |
---|
| 105 | System.out.println(); |
---|
[1749] | 106 | */ |
---|
[1738] | 107 | // TODO: This is dirty, return this in addition with the sequence |
---|
| 108 | // instance instead |
---|
[1733] | 109 | missedOptionals = 0; |
---|
| 110 | int modelindex = 0; |
---|
[1655] | 111 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1733] | 112 | |
---|
| 113 | if (modelindex == model.getChildren().size()) { |
---|
[1657] | 114 | break; |
---|
| 115 | } |
---|
[1733] | 116 | final ITask tempTask = model.getChildren().get(modelindex); |
---|
[1743] | 117 | //System.out.println("Trying to add " + parent.get(startIndex) |
---|
[1733] | 118 | // + " to the model instance " + tempTask); |
---|
[1747] | 119 | if (tempTask instanceof IOptional) { |
---|
[1733] | 120 | |
---|
[1747] | 121 | if (((IMarkingTemporalRelationship) tempTask).getMarkedTask().equals(parent |
---|
| 122 | .get(startIndex).getTask())) { |
---|
| 123 | //System.out.println("Adding OptionalInstance " + |
---|
[1733] | 124 | // parent.get(startIndex) + " to " + tempTask.getType()); |
---|
| 125 | final IOptionalInstance optional = taskFactory |
---|
| 126 | .createNewTaskInstance((IOptional) tempTask); |
---|
| 127 | taskBuilder.setChild(optional, parent.get(startIndex)); |
---|
| 128 | taskBuilder.addChild(subsequence, optional); |
---|
| 129 | } else { |
---|
[1747] | 130 | //System.out.println("Adding Empty optional, not deleting anything from the input sequence"); |
---|
[1733] | 131 | final IOptionalInstance optional = taskFactory |
---|
| 132 | .createNewTaskInstance((IOptional) tempTask); |
---|
| 133 | taskBuilder.addChild(subsequence, optional); |
---|
| 134 | modelindex++; |
---|
| 135 | missedOptionals++; |
---|
| 136 | continue; |
---|
| 137 | } |
---|
[1738] | 138 | } else if (tempTask instanceof ISelection) { |
---|
[1743] | 139 | final ISelectionInstance selection = taskFactory |
---|
| 140 | .createNewTaskInstance((ISelection) tempTask); |
---|
[1733] | 141 | final ISelection tmpSel = (ISelection) tempTask; |
---|
[1743] | 142 | if (tmpSel.getChildren().size() == 0) { |
---|
| 143 | try { |
---|
| 144 | throw new Exception("Selection without children"); |
---|
| 145 | } catch (Exception e) { |
---|
| 146 | e.printStackTrace(); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | // Check if the selection has 2 sequences as children |
---|
| 150 | // TODO: This segment is a mess, clean it up. |
---|
| 151 | if ((tmpSel.getChildren().get(0) instanceof ISequence) |
---|
| 152 | && (tmpSel.getChildren().get(1) instanceof ISequence)) { |
---|
[1693] | 153 | ISequenceInstance selseq = null; |
---|
[1743] | 154 | // The selection I create can just have 2 children, we need |
---|
| 155 | // to check here, to which sequence of the model this |
---|
| 156 | // occurence belongs |
---|
| 157 | // This if checks of the occurrence is equal to the first |
---|
| 158 | // element of the first sequence in the model |
---|
| 159 | if (parent |
---|
| 160 | .get(startIndex) |
---|
| 161 | .getTask() |
---|
| 162 | .equals(((ISequence) tmpSel.getChildren().get(0)) |
---|
| 163 | .getChildren().get(0))) { |
---|
| 164 | selseq = taskFactory |
---|
| 165 | .createNewTaskInstance((ISequence) tmpSel |
---|
| 166 | .getChildren().get(0)); |
---|
| 167 | for (int k = 0; k < selseq.getSequence().getChildren() |
---|
| 168 | .size(); k++) { |
---|
| 169 | taskBuilder |
---|
| 170 | .addChild(selseq, parent.get(startIndex)); |
---|
| 171 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 172 | i++; |
---|
| 173 | } |
---|
| 174 | taskBuilder.setChild(selection, selseq); |
---|
| 175 | taskBuilder.addChild(subsequence, selection); |
---|
| 176 | modelindex++; |
---|
| 177 | continue; |
---|
| 178 | } |
---|
| 179 | // This if checks of the occurrence is equal to the first |
---|
| 180 | // element of the second sequence in the model |
---|
| 181 | else if (parent |
---|
| 182 | .get(startIndex) |
---|
| 183 | .getTask() |
---|
| 184 | .equals(((ISequence) tmpSel.getChildren().get(1)) |
---|
| 185 | .getChildren().get(0))) { |
---|
| 186 | selseq = taskFactory |
---|
| 187 | .createNewTaskInstance((ISequence) tmpSel |
---|
| 188 | .getChildren().get(1)); |
---|
| 189 | for (int k = 0; k < selseq.getSequence().getChildren() |
---|
| 190 | .size(); k++) { |
---|
| 191 | taskBuilder |
---|
| 192 | .addChild(selseq, parent.get(startIndex)); |
---|
| 193 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 194 | i++; |
---|
| 195 | } |
---|
| 196 | taskBuilder.setChild(selection, selseq); |
---|
| 197 | taskBuilder.addChild(subsequence, selection); |
---|
| 198 | modelindex++; |
---|
| 199 | continue; |
---|
| 200 | } |
---|
| 201 | // If the occurrence is already a sequence wedon't need to |
---|
| 202 | // do anything, the next iteration will detect this as a |
---|
| 203 | // sequence and add it |
---|
| 204 | else if ((parent.get(startIndex).getTask().equals(tmpSel |
---|
| 205 | .getChildren().get(0))) |
---|
| 206 | || (parent.get(startIndex).getTask().equals(tmpSel |
---|
| 207 | .getChildren().get(1)))) { |
---|
[1741] | 208 | taskBuilder.setChild(selection, parent.get(startIndex)); |
---|
| 209 | taskBuilder.addChild(subsequence, selection); |
---|
[1743] | 210 | taskBuilder.removeTaskInstance(parent,startIndex); |
---|
[1741] | 211 | modelindex++; |
---|
[1694] | 212 | continue; |
---|
| 213 | } |
---|
[1743] | 214 | } |
---|
| 215 | // It is just a plain selection |
---|
| 216 | else { |
---|
[1693] | 217 | taskBuilder.setChild(selection, parent.get(startIndex)); |
---|
[1733] | 218 | taskBuilder.addChild(subsequence, selection); |
---|
[1693] | 219 | } |
---|
[1747] | 220 | } else if (tempTask instanceof ISequence) { |
---|
[1694] | 221 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1747] | 222 | } else if (tempTask instanceof IIteration) { |
---|
[1682] | 223 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 224 | } else { |
---|
[1656] | 225 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 226 | } |
---|
| 227 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 228 | modelindex++; |
---|
| 229 | } |
---|
[1733] | 230 | |
---|
[1655] | 231 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
| 232 | |
---|
| 233 | return subsequence; |
---|
| 234 | } |
---|
| 235 | |
---|
[1747] | 236 | |
---|
| 237 | /* |
---|
| 238 | static boolean containsOptional(ISequence seq) { |
---|
| 239 | for(int i = 0; i < seq.getChildren().size();i++) { |
---|
| 240 | if(seq.getChildren().get(i) instanceof IOptional) { |
---|
| 241 | return true; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | return false; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | private boolean reachesEnd(ITaskInstanceList parent, ISequence model,int sequenceIndex,int modelIndex,int startIndex) { |
---|
| 248 | |
---|
| 249 | if(modelIndex == model.getChildren().size()) { |
---|
| 250 | return true; |
---|
| 251 | } |
---|
| 252 | ITask modelTask = model.getChildren().get(modelIndex); |
---|
| 253 | ITask sessionTask = parent.get(startIndex+sequenceIndex).getTask(); |
---|
| 254 | if(modelTask instanceof IOptional) { |
---|
| 255 | reachesEnd(parent,model,sequenceIndex++,modelIndex++,startIndex); |
---|
| 256 | reachesEnd(parent,model,sequenceIndex,modelIndex++,startIndex); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | boolean taskfits=false; |
---|
| 260 | if(modelTask instanceof ISequence) { |
---|
| 261 | if(sessionTask.equals(((ISequence)modelTask).getChildren().get(0))) { |
---|
| 262 | taskfits=true; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | if(modelTask instanceof IIteration) { |
---|
| 266 | if(sessionTask.equals(((ISequence)modelTask).getChildren().get(0))) { |
---|
| 267 | taskfits=true; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | if(modelTask instanceof ISelection) { |
---|
| 271 | |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | return taskfits; |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | } |
---|
| 279 | */ |
---|
| 280 | |
---|
| 281 | |
---|
[1733] | 282 | /** |
---|
| 283 | * <p> |
---|
| 284 | * returns the next available id (uses the id counter) |
---|
[1738] | 285 | * </p> |
---|
| 286 | * . |
---|
[1734] | 287 | * |
---|
[1733] | 288 | * @return the next available id |
---|
| 289 | */ |
---|
| 290 | static synchronized String getNewId() { |
---|
| 291 | return Integer.toString(idCounter++); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * <p> |
---|
| 296 | * generates a sub sequence for a specified range of elements in the |
---|
| 297 | * provided task instances list. |
---|
| 298 | * </p> |
---|
| 299 | * |
---|
| 300 | * @param parent |
---|
| 301 | * the list of which the range shall be extracted |
---|
| 302 | * @param startIndex |
---|
| 303 | * the start index of the range |
---|
| 304 | * @param endIndex |
---|
| 305 | * the end index of the range (inclusive) |
---|
| 306 | * @param model |
---|
| 307 | * the task model (required for instantiating the sub sequence) |
---|
| 308 | * @param taskFactory |
---|
| 309 | * the task factory used for instantiating the sub sequence |
---|
| 310 | * @param taskBuilder |
---|
| 311 | * the task builder to perform changes in the task structure |
---|
| 312 | * |
---|
| 313 | * @return a task instance representing the requested sub sequence |
---|
| 314 | */ |
---|
| 315 | static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, |
---|
| 316 | int startIndex, int endIndex, ISequence model, |
---|
| 317 | ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 318 | final ISequenceInstance subsequence = taskFactory |
---|
| 319 | .createNewTaskInstance(model); |
---|
| 320 | |
---|
| 321 | for (int i = startIndex; i <= endIndex; i++) { |
---|
| 322 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | return subsequence; |
---|
| 326 | } |
---|
| 327 | |
---|
[1734] | 328 | /** |
---|
| 329 | * Prints the progress percentage. |
---|
| 330 | * |
---|
[1738] | 331 | * @param message |
---|
| 332 | * the message |
---|
| 333 | * @param count |
---|
| 334 | * the count |
---|
| 335 | * @param size |
---|
| 336 | * the size |
---|
[1734] | 337 | */ |
---|
[1733] | 338 | static void printProgressPercentage(String message, int count, int size) { |
---|
[1731] | 339 | if (size > 100) { |
---|
[1733] | 340 | if (((count % (size / 100)) == 0)) { |
---|
[1731] | 341 | // Console.traceln(Level.INFO,("Thread" + |
---|
| 342 | // Thread.currentThread().getName() + ": " + Math.round((float) |
---|
| 343 | // count/size*100))+ "%"); |
---|
[1743] | 344 | // System.out.println(message + " in thread " |
---|
| 345 | // + Thread.currentThread().getName() + ": " |
---|
| 346 | // + Math.round(((float) count / size) * 100) + "%"); |
---|
[1731] | 347 | } |
---|
| 348 | } else { |
---|
| 349 | // Console.traceln(Level.INFO,("Thread" + |
---|
| 350 | // Thread.currentThread().getName() + ": " +Math.round((float) |
---|
| 351 | // count/size*100))+ "%"); |
---|
[1743] | 352 | // System.out.println(message + " in thread " |
---|
| 353 | // + Thread.currentThread().getName() + ": " |
---|
| 354 | // + Math.round(((float) count / size) * 100) + "%"); |
---|
[1731] | 355 | |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | |
---|
[1655] | 359 | /** |
---|
| 360 | * <p> |
---|
[1733] | 361 | * counter for generating unique ids. Starts at 0 for each new program start |
---|
[1655] | 362 | * </p> |
---|
| 363 | */ |
---|
[1733] | 364 | private static int idCounter = 0; |
---|
[1655] | 365 | |
---|
[1734] | 366 | /** The missed optionals. */ |
---|
[1733] | 367 | public static int missedOptionals = 0; |
---|
| 368 | |
---|
[1655] | 369 | /** |
---|
| 370 | * <p> |
---|
| 371 | * prevent instantiation |
---|
[1738] | 372 | * </p> |
---|
| 373 | * . |
---|
[1655] | 374 | */ |
---|
| 375 | private RuleUtils() { |
---|
| 376 | // prevent instantiation |
---|
| 377 | } |
---|
| 378 | |
---|
[1107] | 379 | } |
---|