[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 | |
---|
[1675] | 17 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence; |
---|
| 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
[1656] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
[1655] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
[1656] | 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1294] | 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[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 |
---|
| 105 | */ |
---|
| 106 | static ISequenceInstance createNewSubSequenceInRange( |
---|
| 107 | ITaskInstanceList parent, int startIndex, int endIndex, |
---|
| 108 | ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 109 | ISequenceInstance subsequence = taskFactory |
---|
| 110 | .createNewTaskInstance(model); |
---|
[1656] | 111 | |
---|
[1657] | 112 | //for (int i=0; i<subsequence.getSequence().getChildren().size();i++) { |
---|
| 113 | // System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
| 114 | //} |
---|
[1666] | 115 | //System.out.println(); |
---|
[1657] | 116 | //TODO: This is dirty! |
---|
| 117 | missedOptionals=0; |
---|
[1655] | 118 | int modelindex=0; |
---|
| 119 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1656] | 120 | |
---|
[1657] | 121 | if(modelindex == model.getChildren().size()) { |
---|
| 122 | break; |
---|
| 123 | } |
---|
[1655] | 124 | ITask tempTask = model.getChildren().get(modelindex); |
---|
[1656] | 125 | //System.out.println("Trying to add " + parent.get(startIndex) |
---|
| 126 | // + " to the model instance " + tempTask.getType()); |
---|
[1655] | 127 | if (tempTask.getType() == "optionality") { |
---|
[1656] | 128 | |
---|
| 129 | if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) { |
---|
[1675] | 130 | System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1656] | 131 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
| 132 | taskBuilder.setChild(optional, parent.get(startIndex)); |
---|
| 133 | taskBuilder.addChild(subsequence, optional); |
---|
| 134 | } |
---|
| 135 | else { |
---|
[1675] | 136 | System.out.println("Adding Empty optional, not deleting anything from the input sequence"); |
---|
[1656] | 137 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
| 138 | taskBuilder.addChild(subsequence, optional); |
---|
| 139 | modelindex++; |
---|
[1657] | 140 | missedOptionals++; |
---|
[1656] | 141 | continue; |
---|
| 142 | } |
---|
[1655] | 143 | } else if (tempTask.getType() == "selection") { |
---|
[1675] | 144 | System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1656] | 145 | ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask); |
---|
| 146 | taskBuilder.setChild(selection, parent.get(startIndex) ); |
---|
| 147 | taskBuilder.addChild(subsequence,selection); |
---|
[1107] | 148 | |
---|
[1655] | 149 | } else if (tempTask.getType() == "sequence") { |
---|
[1675] | 150 | System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1680] | 151 | taskBuilder.addChild(subsequence,parent.get(startIndex)); |
---|
[1655] | 152 | } else if (tempTask.getType() == "iteration") { |
---|
[1682] | 153 | //IIterationInstance iteration = taskFactory.createNewTaskInstance((IIteration) tempTask); |
---|
| 154 | //taskBuilder.addChild(iteration,parent.get(startIndex)); |
---|
| 155 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1675] | 156 | System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1655] | 157 | } else { |
---|
[1675] | 158 | System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
[1656] | 159 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1655] | 160 | } |
---|
| 161 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
| 162 | modelindex++; |
---|
| 163 | } |
---|
[1656] | 164 | |
---|
[1655] | 165 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
| 166 | |
---|
| 167 | return subsequence; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * <p> |
---|
| 172 | * returns the next available id (uses the id counter) |
---|
| 173 | * </p> |
---|
| 174 | * |
---|
| 175 | * @return the next available id |
---|
| 176 | */ |
---|
| 177 | static synchronized String getNewId() { |
---|
| 178 | return Integer.toString(idCounter++); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * <p> |
---|
| 183 | * prevent instantiation |
---|
| 184 | * </p> |
---|
| 185 | */ |
---|
| 186 | private RuleUtils() { |
---|
| 187 | // prevent instantiation |
---|
| 188 | } |
---|
| 189 | |
---|
[1107] | 190 | } |
---|