[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 | |
---|
[1692] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
[1656] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
[1655] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
[1656] | 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1294] | 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1655] | 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1146] | 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
[1107] | 31 | |
---|
| 32 | /** |
---|
| 33 | * <p> |
---|
[1281] | 34 | * provides some convenience methods for rule application |
---|
[1107] | 35 | * </p> |
---|
| 36 | * |
---|
| 37 | * @author Patrick Harms |
---|
| 38 | */ |
---|
| 39 | class RuleUtils { |
---|
| 40 | |
---|
[1655] | 41 | /** |
---|
| 42 | * <p> |
---|
| 43 | * counter for generating unique ids. Starts at 0 for each new program start |
---|
| 44 | * </p> |
---|
| 45 | */ |
---|
| 46 | private static int idCounter = 0; |
---|
[1127] | 47 | |
---|
[1657] | 48 | public static int missedOptionals = 0; |
---|
| 49 | |
---|
[1655] | 50 | /** |
---|
| 51 | * <p> |
---|
| 52 | * generates a sub sequence for a specified range of elements in the |
---|
| 53 | * provided task instances list. |
---|
| 54 | * </p> |
---|
| 55 | * |
---|
| 56 | * @param parent |
---|
| 57 | * the list of which the range shall be extracted |
---|
| 58 | * @param startIndex |
---|
| 59 | * the start index of the range |
---|
| 60 | * @param endIndex |
---|
| 61 | * the end index of the range (inclusive) |
---|
| 62 | * @param model |
---|
| 63 | * the task model (required for instantiating the sub sequence) |
---|
| 64 | * @param taskFactory |
---|
| 65 | * the task factory used for instantiating the sub sequence |
---|
| 66 | * @param taskBuilder |
---|
| 67 | * the task builder to perform changes in the task structure |
---|
| 68 | * |
---|
| 69 | * @return a task instance representing the requested sub sequence |
---|
| 70 | */ |
---|
| 71 | static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, |
---|
| 72 | int startIndex, int endIndex, ISequence model, |
---|
| 73 | ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 74 | ISequenceInstance subsequence = taskFactory |
---|
| 75 | .createNewTaskInstance(model); |
---|
[1127] | 76 | |
---|
[1655] | 77 | for (int i = startIndex; i <= endIndex; i++) { |
---|
| 78 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
| 79 | } |
---|
[1127] | 80 | |
---|
[1655] | 81 | return subsequence; |
---|
| 82 | } |
---|
[1107] | 83 | |
---|
[1655] | 84 | /** |
---|
| 85 | * <p> |
---|
| 86 | * replaces a sub sequence for a specified range of elements in the provided |
---|
| 87 | * task instances list by a sub task instance |
---|
| 88 | * </p> |
---|
| 89 | * |
---|
| 90 | * @param parent |
---|
| 91 | * the list of which the range shall be replaced |
---|
| 92 | * @param startIndex |
---|
| 93 | * the start index of the range |
---|
| 94 | * @param endIndex |
---|
| 95 | * the end index of the range (inclusive) |
---|
| 96 | * @param model |
---|
| 97 | * the task model (required for instantiating the sub sequence) |
---|
| 98 | * @param taskFactory |
---|
| 99 | * the task factory used for instantiating the sub sequence |
---|
| 100 | * @param taskBuilder |
---|
| 101 | * the task builder to perform changes in the task structure |
---|
| 102 | * |
---|
| 103 | * @return the replacement for the range |
---|
| 104 | */ |
---|
| 105 | static ISequenceInstance createNewSubSequenceInRange( |
---|
| 106 | ITaskInstanceList parent, int startIndex, int endIndex, |
---|
| 107 | ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 108 | ISequenceInstance subsequence = taskFactory |
---|
| 109 | .createNewTaskInstance(model); |
---|
[1656] | 110 | |
---|
[1657] | 111 | //for (int i=0; i<subsequence.getSequence().getChildren().size();i++) { |
---|
| 112 | // System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
| 113 | //} |
---|
[1666] | 114 | //System.out.println(); |
---|
[1657] | 115 | //TODO: This is dirty! |
---|
| 116 | missedOptionals=0; |
---|
[1655] | 117 | int modelindex=0; |
---|
| 118 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1656] | 119 | |
---|
[1657] | 120 | if(modelindex == model.getChildren().size()) { |
---|
| 121 | break; |
---|
| 122 | } |
---|
[1655] | 123 | ITask tempTask = model.getChildren().get(modelindex); |
---|
[1656] | 124 | //System.out.println("Trying to add " + parent.get(startIndex) |
---|
| 125 | // + " to the model instance " + tempTask.getType()); |
---|
[1655] | 126 | if (tempTask.getType() == "optionality") { |
---|
[1656] | 127 | |
---|
| 128 | if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) { |
---|
[1690] | 129 | //System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1656] | 130 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
| 131 | taskBuilder.setChild(optional, parent.get(startIndex)); |
---|
| 132 | taskBuilder.addChild(subsequence, optional); |
---|
| 133 | } |
---|
| 134 | else { |
---|
[1690] | 135 | //System.out.println("Adding Empty optional, not deleting anything from the input sequence"); |
---|
[1656] | 136 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
| 137 | taskBuilder.addChild(subsequence, optional); |
---|
| 138 | modelindex++; |
---|
[1657] | 139 | missedOptionals++; |
---|
[1656] | 140 | continue; |
---|
| 141 | } |
---|
[1655] | 142 | } else if (tempTask.getType() == "selection") { |
---|
[1690] | 143 | //System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1656] | 144 | ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask); |
---|
| 145 | taskBuilder.setChild(selection, parent.get(startIndex) ); |
---|
| 146 | taskBuilder.addChild(subsequence,selection); |
---|
[1107] | 147 | |
---|
[1655] | 148 | } else if (tempTask.getType() == "sequence") { |
---|
[1692] | 149 | //ISequenceInstance sequence = taskFactory.createNewTaskInstance((ISequence) tempTask); |
---|
| 150 | //taskBuilder.addChild(sequence, parent.get(i)); |
---|
| 151 | //taskBuilder.addChild(subsequence,sequence); |
---|
| 152 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 153 | } else if (tempTask.getType() == "iteration") { |
---|
[1692] | 154 | //IIterationInstance iteration = taskFactory.createNewTaskInstance((IIteration) tempTask); |
---|
| 155 | //taskBuilder.addChild(iteration, parent.get(startIndex)); |
---|
| 156 | //taskBuilder.addChild(subsequence, iteration); |
---|
[1682] | 157 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1690] | 158 | //System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1655] | 159 | } else { |
---|
[1690] | 160 | //System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1656] | 161 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 162 | } |
---|
| 163 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 164 | modelindex++; |
---|
| 165 | } |
---|
[1656] | 166 | |
---|
[1655] | 167 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
| 168 | |
---|
| 169 | return subsequence; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * <p> |
---|
| 174 | * returns the next available id (uses the id counter) |
---|
| 175 | * </p> |
---|
| 176 | * |
---|
| 177 | * @return the next available id |
---|
| 178 | */ |
---|
| 179 | static synchronized String getNewId() { |
---|
| 180 | return Integer.toString(idCounter++); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * <p> |
---|
| 185 | * prevent instantiation |
---|
| 186 | * </p> |
---|
| 187 | */ |
---|
| 188 | private RuleUtils() { |
---|
| 189 | // prevent instantiation |
---|
| 190 | } |
---|
| 191 | |
---|
[1107] | 192 | } |
---|