[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 | |
---|
[1294] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1146] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
[1107] | 23 | |
---|
| 24 | /** |
---|
| 25 | * <p> |
---|
[1281] | 26 | * provides some convenience methods for rule application |
---|
[1107] | 27 | * </p> |
---|
| 28 | * |
---|
| 29 | * @author Patrick Harms |
---|
| 30 | */ |
---|
| 31 | class RuleUtils { |
---|
| 32 | |
---|
[1281] | 33 | /** |
---|
| 34 | * <p> |
---|
| 35 | * counter for generating unique ids. Starts at 0 for each new program start |
---|
| 36 | * </p> |
---|
| 37 | */ |
---|
[1107] | 38 | private static int idCounter = 0; |
---|
| 39 | |
---|
| 40 | /** |
---|
[1281] | 41 | * <p> |
---|
| 42 | * generates a sub sequence for a specified range of elements in the provided task instances |
---|
| 43 | * list. |
---|
| 44 | * </p> |
---|
| 45 | * |
---|
| 46 | * @param parent the list of which the range shall be extracted |
---|
| 47 | * @param startIndex the start index of the range |
---|
| 48 | * @param endIndex the end index of the range (inclusive) |
---|
| 49 | * @param model the task model (required for instantiating the sub sequence) |
---|
| 50 | * @param taskFactory the task factory used for instantiating the sub sequence |
---|
| 51 | * @param taskBuilder the task builder to perform changes in the task structure |
---|
[1107] | 52 | * |
---|
[1281] | 53 | * @return a task instance representing the requested sub sequence |
---|
[1107] | 54 | */ |
---|
[1146] | 55 | static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, |
---|
| 56 | int startIndex, |
---|
| 57 | int endIndex, |
---|
[1294] | 58 | ISequence model, |
---|
[1146] | 59 | ITaskFactory taskFactory, |
---|
| 60 | ITaskBuilder taskBuilder) |
---|
[1127] | 61 | { |
---|
[1294] | 62 | ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model); |
---|
[1127] | 63 | |
---|
| 64 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1146] | 65 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
[1127] | 66 | } |
---|
| 67 | |
---|
[1146] | 68 | return subsequence; |
---|
[1127] | 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
[1281] | 72 | * <p> |
---|
| 73 | * replaces a sub sequence for a specified range of elements in the provided task instances |
---|
| 74 | * list by a sub task instance |
---|
| 75 | * </p> |
---|
| 76 | * |
---|
| 77 | * @param parent the list of which the range shall be replaced |
---|
| 78 | * @param startIndex the start index of the range |
---|
| 79 | * @param endIndex the end index of the range (inclusive) |
---|
| 80 | * @param model the task model (required for instantiating the sub sequence) |
---|
| 81 | * @param taskFactory the task factory used for instantiating the sub sequence |
---|
| 82 | * @param taskBuilder the task builder to perform changes in the task structure |
---|
[1127] | 83 | * |
---|
[1281] | 84 | * @return the replacement for the range |
---|
[1127] | 85 | */ |
---|
[1294] | 86 | static ISequenceInstance createNewSubSequenceInRange(ITaskInstanceList parent, |
---|
| 87 | int startIndex, |
---|
| 88 | int endIndex, |
---|
| 89 | ISequence model, |
---|
| 90 | ITaskFactory taskFactory, |
---|
| 91 | ITaskBuilder taskBuilder) |
---|
[1107] | 92 | { |
---|
[1294] | 93 | ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model); |
---|
[1107] | 94 | |
---|
| 95 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1146] | 96 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
| 97 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
[1107] | 98 | } |
---|
| 99 | |
---|
[1146] | 100 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
| 101 | |
---|
| 102 | return subsequence; |
---|
[1107] | 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
[1281] | 106 | * <p> |
---|
| 107 | * returns the next available id (uses the id counter) |
---|
| 108 | * </p> |
---|
| 109 | * |
---|
| 110 | * @return the next available id |
---|
[1107] | 111 | */ |
---|
[1127] | 112 | static synchronized String getNewId() { |
---|
[1107] | 113 | return Integer.toString(idCounter++); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
[1281] | 117 | * <p> |
---|
| 118 | * prevent instantiation |
---|
| 119 | * </p> |
---|
[1107] | 120 | */ |
---|
| 121 | private RuleUtils() { |
---|
| 122 | // prevent instantiation |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | } |
---|