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 | |
---|
17 | import java.util.Iterator; |
---|
18 | |
---|
19 | import org.apache.commons.lang.NotImplementedException; |
---|
20 | |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
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; |
---|
33 | |
---|
34 | /** |
---|
35 | * <p> |
---|
36 | * provides some convenience methods for rule application |
---|
37 | * </p> |
---|
38 | * |
---|
39 | * @author Patrick Harms |
---|
40 | */ |
---|
41 | class RuleUtils { |
---|
42 | |
---|
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; |
---|
49 | |
---|
50 | public static int missedOptionals = 0; |
---|
51 | |
---|
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); |
---|
78 | |
---|
79 | for (int i = startIndex; i <= endIndex; i++) { |
---|
80 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
81 | } |
---|
82 | |
---|
83 | return subsequence; |
---|
84 | } |
---|
85 | |
---|
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); |
---|
112 | |
---|
113 | //for (int i=0; i<subsequence.getSequence().getChildren().size();i++) { |
---|
114 | // System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
115 | //} |
---|
116 | //System.out.println(); |
---|
117 | //TODO: This is dirty! |
---|
118 | missedOptionals=0; |
---|
119 | int modelindex=0; |
---|
120 | for (int i = startIndex; i <= endIndex; i++) { |
---|
121 | |
---|
122 | if(modelindex == model.getChildren().size()) { |
---|
123 | break; |
---|
124 | } |
---|
125 | ITask tempTask = model.getChildren().get(modelindex); |
---|
126 | //System.out.println("Trying to add " + parent.get(startIndex) |
---|
127 | // + " to the model instance " + tempTask.getType()); |
---|
128 | if (tempTask.getType() == "optionality") { |
---|
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++; |
---|
141 | missedOptionals++; |
---|
142 | continue; |
---|
143 | } |
---|
144 | } else if (tempTask.getType() == "selection") { |
---|
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); |
---|
149 | |
---|
150 | } else if (tempTask.getType() == "sequence") { |
---|
151 | //System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
152 | //TODO: Implement this |
---|
153 | throw new NotImplementedException(); |
---|
154 | |
---|
155 | } else if (tempTask.getType() == "iteration") { |
---|
156 | //TODO: Implement this |
---|
157 | //System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
158 | throw new NotImplementedException(); |
---|
159 | } else { |
---|
160 | //System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
161 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
162 | } |
---|
163 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
164 | modelindex++; |
---|
165 | } |
---|
166 | /* |
---|
167 | System.out.println(); |
---|
168 | System.out.println("Sequence instance:"); |
---|
169 | for(Iterator<ITaskInstance> it = subsequence.iterator();it.hasNext();) { |
---|
170 | System.out.println(it.next()); |
---|
171 | }*/ |
---|
172 | //System.out.println(); |
---|
173 | //System.out.println(); |
---|
174 | |
---|
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 | |
---|
200 | } |
---|