[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 | |
---|
[1294] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1146] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
[1107] | 25 | |
---|
| 26 | /** |
---|
| 27 | * <p> |
---|
[1281] | 28 | * provides some convenience methods for rule application |
---|
[1107] | 29 | * </p> |
---|
| 30 | * |
---|
| 31 | * @author Patrick Harms |
---|
| 32 | */ |
---|
| 33 | class RuleUtils { |
---|
| 34 | |
---|
[1281] | 35 | /** |
---|
| 36 | * <p> |
---|
| 37 | * counter for generating unique ids. Starts at 0 for each new program start |
---|
| 38 | * </p> |
---|
| 39 | */ |
---|
[1107] | 40 | private static int idCounter = 0; |
---|
| 41 | |
---|
| 42 | /** |
---|
[1281] | 43 | * <p> |
---|
| 44 | * generates a sub sequence for a specified range of elements in the provided task instances |
---|
| 45 | * list. |
---|
| 46 | * </p> |
---|
| 47 | * |
---|
| 48 | * @param parent the list of which the range shall be extracted |
---|
| 49 | * @param startIndex the start index of the range |
---|
| 50 | * @param endIndex the end index of the range (inclusive) |
---|
| 51 | * @param model the task model (required for instantiating the sub sequence) |
---|
| 52 | * @param taskFactory the task factory used for instantiating the sub sequence |
---|
| 53 | * @param taskBuilder the task builder to perform changes in the task structure |
---|
[1107] | 54 | * |
---|
[1281] | 55 | * @return a task instance representing the requested sub sequence |
---|
[1107] | 56 | */ |
---|
[1146] | 57 | static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, |
---|
| 58 | int startIndex, |
---|
| 59 | int endIndex, |
---|
[1294] | 60 | ISequence model, |
---|
[1146] | 61 | ITaskFactory taskFactory, |
---|
| 62 | ITaskBuilder taskBuilder) |
---|
[1127] | 63 | { |
---|
[1294] | 64 | ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model); |
---|
[1127] | 65 | |
---|
| 66 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1146] | 67 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
[1127] | 68 | } |
---|
| 69 | |
---|
[1146] | 70 | return subsequence; |
---|
[1127] | 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
[1281] | 74 | * <p> |
---|
| 75 | * replaces a sub sequence for a specified range of elements in the provided task instances |
---|
| 76 | * list by a sub task instance |
---|
| 77 | * </p> |
---|
| 78 | * |
---|
| 79 | * @param parent the list of which the range shall be replaced |
---|
| 80 | * @param startIndex the start index of the range |
---|
| 81 | * @param endIndex the end index of the range (inclusive) |
---|
| 82 | * @param model the task model (required for instantiating the sub sequence) |
---|
| 83 | * @param taskFactory the task factory used for instantiating the sub sequence |
---|
| 84 | * @param taskBuilder the task builder to perform changes in the task structure |
---|
[1127] | 85 | * |
---|
[1281] | 86 | * @return the replacement for the range |
---|
[1127] | 87 | */ |
---|
[1294] | 88 | static ISequenceInstance createNewSubSequenceInRange(ITaskInstanceList parent, |
---|
| 89 | int startIndex, |
---|
| 90 | int endIndex, |
---|
| 91 | ISequence model, |
---|
| 92 | ITaskFactory taskFactory, |
---|
| 93 | ITaskBuilder taskBuilder) |
---|
[1107] | 94 | { |
---|
[1294] | 95 | ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model); |
---|
[1645] | 96 | |
---|
[1654] | 97 | for (Iterator<ITaskInstance> it = subsequence.iterator();it.hasNext();) { |
---|
| 98 | ITaskInstance foo = it.next(); |
---|
| 99 | System.out.println(foo); |
---|
| 100 | } |
---|
[1645] | 101 | |
---|
| 102 | System.out.println("Got model: " + model.toString()); |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | System.out.println(parent); |
---|
| 107 | //System.out.println(parent.get(startIndex)); |
---|
[1107] | 108 | for (int i = startIndex; i <= endIndex; i++) { |
---|
[1645] | 109 | System.out.println("Trying to add "+ parent.get(startIndex) + " to the model"); |
---|
[1146] | 110 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
[1645] | 111 | |
---|
[1146] | 112 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
[1107] | 113 | } |
---|
| 114 | |
---|
[1146] | 115 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
| 116 | |
---|
| 117 | return subsequence; |
---|
[1107] | 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
[1281] | 121 | * <p> |
---|
| 122 | * returns the next available id (uses the id counter) |
---|
| 123 | * </p> |
---|
| 124 | * |
---|
| 125 | * @return the next available id |
---|
[1107] | 126 | */ |
---|
[1127] | 127 | static synchronized String getNewId() { |
---|
[1107] | 128 | return Integer.toString(idCounter++); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[1281] | 132 | * <p> |
---|
| 133 | * prevent instantiation |
---|
| 134 | * </p> |
---|
[1107] | 135 | */ |
---|
| 136 | private RuleUtils() { |
---|
| 137 | // prevent instantiation |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | } |
---|