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