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