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 de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
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; |
---|
32 | |
---|
33 | /** |
---|
34 | * <p> |
---|
35 | * provides some convenience methods for rule application |
---|
36 | * </p> |
---|
37 | * |
---|
38 | * @author Patrick Harms |
---|
39 | */ |
---|
40 | class RuleUtils { |
---|
41 | |
---|
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; |
---|
48 | |
---|
49 | public static int missedOptionals = 0; |
---|
50 | |
---|
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); |
---|
77 | |
---|
78 | for (int i = startIndex; i <= endIndex; i++) { |
---|
79 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
80 | } |
---|
81 | |
---|
82 | return subsequence; |
---|
83 | } |
---|
84 | |
---|
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 | * @throws |
---|
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 | |
---|
114 | // TODO: Debug output |
---|
115 | /* |
---|
116 | System.out.println("PRINTING MODEL: "); |
---|
117 | for (int i = 0; i < subsequence.getSequence().getChildren().size(); i++) { |
---|
118 | System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
119 | |
---|
120 | if (subsequence.getSequence().getChildren().get(i).getType() == "selection") { |
---|
121 | for (int j = 0; j < ((ISelection) subsequence.getSequence().getChildren().get(i)) |
---|
122 | .getChildren().size(); j++) { |
---|
123 | if(((IStructuringTemporalRelationship) subsequence.getSequence().getChildren().get(i)).getChildren().get(j).getType() =="sequence") |
---|
124 | { |
---|
125 | ISequence foo = (ISequence) ((ISelection) (subsequence.getSequence().getChildren().get(i))).getChildren().get(j); |
---|
126 | System.out.println("\t" + foo); |
---|
127 | for(int k=0; k< foo.getChildren().size();k++) { |
---|
128 | System.out.println("\t\t" +foo.getChildren().get(k)); |
---|
129 | } |
---|
130 | System.out.println(); |
---|
131 | } |
---|
132 | else{ |
---|
133 | System.out.println("\t" |
---|
134 | + ((ISelection) subsequence.getSequence().getChildren().get(i)) |
---|
135 | .getChildren().get(j)); |
---|
136 | } |
---|
137 | |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | } |
---|
142 | System.out.println(); |
---|
143 | */ |
---|
144 | |
---|
145 | //TODO: This is dirty! |
---|
146 | missedOptionals=0; |
---|
147 | int modelindex=0; |
---|
148 | for (int i = startIndex; i <= endIndex; i++) { |
---|
149 | |
---|
150 | if(modelindex == model.getChildren().size()) { |
---|
151 | break; |
---|
152 | } |
---|
153 | ITask tempTask = model.getChildren().get(modelindex); |
---|
154 | //System.out.println("Trying to add " + parent.get(startIndex) |
---|
155 | // + " to the model instance " + tempTask); |
---|
156 | if (tempTask.getType() == "optionality") { |
---|
157 | |
---|
158 | if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) { |
---|
159 | //System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
160 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
161 | taskBuilder.setChild(optional, parent.get(startIndex)); |
---|
162 | taskBuilder.addChild(subsequence, optional); |
---|
163 | } |
---|
164 | else { |
---|
165 | //System.out.println("Adding Empty optional, not deleting anything from the input sequence"); |
---|
166 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
167 | taskBuilder.addChild(subsequence, optional); |
---|
168 | modelindex++; |
---|
169 | missedOptionals++; |
---|
170 | continue; |
---|
171 | } |
---|
172 | } else if (tempTask.getType() == "selection") { |
---|
173 | ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask); |
---|
174 | ISelection tmpSel = (ISelection)tempTask; |
---|
175 | if(tmpSel.getChildren().get(0).getType() == "sequence" && tmpSel.getChildren().get(1).getType()=="sequence") { |
---|
176 | ISequenceInstance selseq = null; |
---|
177 | //The selection I create can just have 2 children |
---|
178 | if(parent.get(startIndex).getTask().getId() == ((ISequence)tmpSel.getChildren().get(0)).getChildren().get(0).getId()) { |
---|
179 | selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(0)); |
---|
180 | } |
---|
181 | else if(parent.get(startIndex).getTask().getId() == ((ISequence)tmpSel.getChildren().get(1)).getChildren().get(0).getId()) { |
---|
182 | selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(1)); |
---|
183 | } |
---|
184 | else if(parent.get(startIndex).getTask().getId() == tmpSel.getChildren().get(0).getId() || parent.get(startIndex).getTask().getId() == tmpSel.getChildren().get(1).getId() ) { |
---|
185 | //System.out.println("Session ID: " + parent.get(startIndex).getTask().getId() + " tmpSel(0): " + tmpSel.getChildren().get(0).getId() + " tmpSel(1): " +tmpSel.getChildren().get(1).getId() ); |
---|
186 | continue; |
---|
187 | } |
---|
188 | |
---|
189 | for (int k=0;k<selseq.getSequence().getChildren().size();k++) { |
---|
190 | //System.out.println("Trying to add " + parent.get(startIndex) + " to " + selseq); |
---|
191 | taskBuilder.addChild(selseq,parent.get(startIndex)); |
---|
192 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
193 | i++; |
---|
194 | //System.out.println("I:" + i); |
---|
195 | } |
---|
196 | //System.out.println("Trying to add " + selseq + " to " + tmpSel); |
---|
197 | taskBuilder.setChild(selection, selseq); |
---|
198 | taskBuilder.addChild(subsequence, selection); |
---|
199 | modelindex++; |
---|
200 | continue; |
---|
201 | } |
---|
202 | else |
---|
203 | { |
---|
204 | //System.out.println("Trying to adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask); |
---|
205 | taskBuilder.setChild(selection, parent.get(startIndex)); |
---|
206 | taskBuilder.addChild(subsequence,selection); |
---|
207 | } |
---|
208 | } else if (tempTask.getType() == "sequence") { |
---|
209 | //System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask); |
---|
210 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
211 | } else if (tempTask.getType() == "iteration") { |
---|
212 | //System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask); |
---|
213 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
214 | } else { |
---|
215 | //System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask); |
---|
216 | //System.out.println("Foo"); |
---|
217 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
218 | } |
---|
219 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
220 | modelindex++; |
---|
221 | } |
---|
222 | |
---|
223 | taskBuilder.addTaskInstance(parent, startIndex, subsequence); |
---|
224 | |
---|
225 | return subsequence; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * <p> |
---|
230 | * returns the next available id (uses the id counter) |
---|
231 | * </p> |
---|
232 | * |
---|
233 | * @return the next available id |
---|
234 | */ |
---|
235 | static synchronized String getNewId() { |
---|
236 | return Integer.toString(idCounter++); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * <p> |
---|
241 | * prevent instantiation |
---|
242 | * </p> |
---|
243 | */ |
---|
244 | private RuleUtils() { |
---|
245 | // prevent instantiation |
---|
246 | } |
---|
247 | |
---|
248 | } |
---|