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