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.IMarkingTemporalRelationship; |
---|
18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
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; |
---|
29 | |
---|
30 | /** |
---|
31 | * <p> |
---|
32 | * provides some convenience methods for rule application |
---|
33 | * </p> |
---|
34 | * |
---|
35 | * @author Patrick Harms |
---|
36 | */ |
---|
37 | class RuleUtils { |
---|
38 | |
---|
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; |
---|
45 | |
---|
46 | public static int missedOptionals = 0; |
---|
47 | |
---|
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); |
---|
74 | |
---|
75 | for (int i = startIndex; i <= endIndex; i++) { |
---|
76 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
77 | } |
---|
78 | |
---|
79 | return subsequence; |
---|
80 | } |
---|
81 | |
---|
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); |
---|
108 | |
---|
109 | //for (int i=0; i<subsequence.getSequence().getChildren().size();i++) { |
---|
110 | // System.out.println(subsequence.getSequence().getChildren().get(i)); |
---|
111 | //} |
---|
112 | //System.out.println(); |
---|
113 | //TODO: This is dirty! |
---|
114 | missedOptionals=0; |
---|
115 | int modelindex=0; |
---|
116 | for (int i = startIndex; i <= endIndex; i++) { |
---|
117 | |
---|
118 | if(modelindex == model.getChildren().size()) { |
---|
119 | break; |
---|
120 | } |
---|
121 | ITask tempTask = model.getChildren().get(modelindex); |
---|
122 | //System.out.println("Trying to add " + parent.get(startIndex) |
---|
123 | // + " to the model instance " + tempTask.getType()); |
---|
124 | if (tempTask.getType() == "optionality") { |
---|
125 | |
---|
126 | if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) { |
---|
127 | //System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
128 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
129 | taskBuilder.setChild(optional, parent.get(startIndex)); |
---|
130 | taskBuilder.addChild(subsequence, optional); |
---|
131 | } |
---|
132 | else { |
---|
133 | //System.out.println("Adding Empty optional, not deleting anything from the input sequence"); |
---|
134 | IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask); |
---|
135 | taskBuilder.addChild(subsequence, optional); |
---|
136 | modelindex++; |
---|
137 | missedOptionals++; |
---|
138 | continue; |
---|
139 | } |
---|
140 | } else if (tempTask.getType() == "selection") { |
---|
141 | //System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
142 | ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask); |
---|
143 | taskBuilder.setChild(selection, parent.get(startIndex) ); |
---|
144 | taskBuilder.addChild(subsequence,selection); |
---|
145 | |
---|
146 | } else if (tempTask.getType() == "sequence") { |
---|
147 | //System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
148 | taskBuilder.addChild(subsequence,parent.get(startIndex)); |
---|
149 | } else if (tempTask.getType() == "iteration") { |
---|
150 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
151 | //System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
152 | } else { |
---|
153 | //System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType()); |
---|
154 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
155 | } |
---|
156 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
157 | modelindex++; |
---|
158 | } |
---|
159 | |
---|
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 | |
---|
185 | } |
---|