source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java @ 1749

Last change on this file since 1749 was 1749, checked in by rkrimmel, 10 years ago

Code cleanup, removed dublicate storage of task items

File size: 12.5 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
16
17import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
31
32/**
33 * <p>
34 * provides some convenience methods for rule application
35 * </p>
36 * .
37 *
38 * @author Patrick Harms, Ralph Krimmel
39 */
40class RuleUtils {
41
42        /**
43         * <p>
44         * replaces a sub sequence for a specified range of elements in the provided
45         * task instances list by a sub task instance
46         * </p>
47         * .
48         *
49         * @param parent
50         *            the list of which the range shall be replaced
51         * @param startIndex
52         *            the start index of the range
53         * @param endIndex
54         *            the end index of the range (inclusive)
55         * @param model
56         *            the task model (required for instantiating the sub sequence)
57         * @param taskFactory
58         *            the task factory used for instantiating the sub sequence
59         * @param taskBuilder
60         *            the task builder to perform changes in the task structure
61         * @return the replacement for the range
62         */
63        static synchronized ISequenceInstance createNewSubSequenceInRange(
64                        ITaskInstanceList parent, int startIndex, int endIndex,
65                        ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
66                final ISequenceInstance subsequence = taskFactory
67                                .createNewTaskInstance(model);
68                /*
69                System.out.println("STARTINDEX: " + startIndex);
70                System.out.println("PRINTING SEQUENCE: ");
71                for(int i =0; i< parent.size();i++) {
72                    System.out.println(i + ": " +parent.get(i));
73                }
74                System.out.println();
75               
76                System.out.println("PRINTING MODEL: ");
77                for (int i = 0; i < subsequence.getSequence().getChildren().size(); i++) {
78                        System.out.println(subsequence.getSequence().getChildren().get(i));
79
80                        if (subsequence.getSequence().getChildren().get(i).getType() == "selection") {
81                                for (int j = 0; j < ((ISelection) subsequence.getSequence()
82                                                .getChildren().get(i)).getChildren().size(); j++) {
83                                        if (((IStructuringTemporalRelationship) subsequence
84                                                        .getSequence().getChildren().get(i)).getChildren()
85                                                        .get(j).getType() == "sequence") {
86                                                ISequence foo = (ISequence) ((ISelection) (subsequence
87                                                                .getSequence().getChildren().get(i)))
88                                                                .getChildren().get(j);
89                                                System.out.println("\t" + foo);
90                                                for (int k = 0; k < foo.getChildren().size(); k++) {
91                                                        System.out.println("\t\t"
92                                                                        + foo.getChildren().get(k));
93                                                }
94                                                System.out.println();
95                                        } else {
96                                                System.out.println("\t"
97                                                                + ((ISelection) subsequence.getSequence()
98                                                                                .getChildren().get(i)).getChildren()
99                                                                                .get(j));
100                                        }
101
102                                }
103                        }
104
105                }
106                System.out.println();
107                */
108                // TODO: This is dirty, return this in addition with the sequence
109                // instance instead
110                missedOptionals = 0;
111                int modelindex = 0;
112                for (int i = startIndex; i <= endIndex; i++) {
113
114                        if (modelindex == model.getChildren().size()) {
115                                break;
116                        }
117                        final ITask tempTask = model.getChildren().get(modelindex);
118                        //System.out.println("Trying to add " + parent.get(startIndex)
119                        // + " to the model instance " + tempTask);
120                        if (tempTask instanceof IOptional) {
121
122                                if (((IMarkingTemporalRelationship) tempTask).getMarkedTask().equals(parent
123                                              .get(startIndex).getTask())) {
124                                         //System.out.println("Adding OptionalInstance " +
125                                        // parent.get(startIndex) + " to " + tempTask.getType());
126                                        final IOptionalInstance optional = taskFactory
127                                                        .createNewTaskInstance((IOptional) tempTask);
128                                        taskBuilder.setChild(optional, parent.get(startIndex));
129                                        taskBuilder.addChild(subsequence, optional);
130                                } else {
131                                         //System.out.println("Adding Empty optional, not deleting anything from the input sequence");
132                                        final IOptionalInstance optional = taskFactory
133                                                        .createNewTaskInstance((IOptional) tempTask);
134                                        taskBuilder.addChild(subsequence, optional);
135                                        modelindex++;
136                                        missedOptionals++;
137                                        continue;
138                                }
139                        } else if (tempTask instanceof ISelection) {
140                                final ISelectionInstance selection = taskFactory
141                                                .createNewTaskInstance((ISelection) tempTask);
142                                final ISelection tmpSel = (ISelection) tempTask;
143                                if (tmpSel.getChildren().size() == 0) {
144                                        try {
145                                                throw new Exception("Selection without children");
146                                        } catch (Exception e) {
147                                                e.printStackTrace();
148                                        }
149                                }
150                                // Check if the selection has 2 sequences as children
151                                // TODO: This segment is a mess, clean it up.
152                                if ((tmpSel.getChildren().get(0) instanceof ISequence)
153                                                && (tmpSel.getChildren().get(1) instanceof ISequence)) {
154                                        ISequenceInstance selseq = null;
155                                        // The selection I create can just have 2 children, we need
156                                        // to check here, to which sequence of the model this
157                                        // occurence belongs
158                                        // This if checks of the occurrence is equal to the first
159                                        // element of the first sequence in the model
160                                        if (parent
161                                                        .get(startIndex)
162                                                        .getTask()
163                                                        .equals(((ISequence) tmpSel.getChildren().get(0))
164                                                                        .getChildren().get(0))) {
165                                                selseq = taskFactory
166                                                                .createNewTaskInstance((ISequence) tmpSel
167                                                                                .getChildren().get(0));
168                                                for (int k = 0; k < selseq.getSequence().getChildren()
169                                                                .size(); k++) {
170                                                        taskBuilder
171                                                                        .addChild(selseq, parent.get(startIndex));
172                                                        taskBuilder.removeTaskInstance(parent, startIndex);
173                                                        i++;
174                                                }
175                                                taskBuilder.setChild(selection, selseq);
176                                                taskBuilder.addChild(subsequence, selection);
177                                                modelindex++;
178                                                continue;
179                                        }
180                                        // This if checks of the occurrence is equal to the first
181                                        // element of the second sequence in the model
182                                        else if (parent
183                                                        .get(startIndex)
184                                                        .getTask()
185                                                        .equals(((ISequence) tmpSel.getChildren().get(1))
186                                                                        .getChildren().get(0))) {
187                                                selseq = taskFactory
188                                                                .createNewTaskInstance((ISequence) tmpSel
189                                                                                .getChildren().get(1));
190                                                for (int k = 0; k < selseq.getSequence().getChildren()
191                                                                .size(); k++) {
192                                                        taskBuilder
193                                                                        .addChild(selseq, parent.get(startIndex));
194                                                        taskBuilder.removeTaskInstance(parent, startIndex);
195                                                        i++;
196                                                }
197                                                taskBuilder.setChild(selection, selseq);
198                                                taskBuilder.addChild(subsequence, selection);
199                                                modelindex++;
200                                                continue;
201                                        }
202                                        // If the occurrence is already a sequence wedon't need to
203                                        // do anything, the next iteration will detect this as a
204                                        // sequence and add it
205                                        else if ((parent.get(startIndex).getTask().equals(tmpSel
206                                                        .getChildren().get(0)))
207                                                        || (parent.get(startIndex).getTask().equals(tmpSel
208                                                                        .getChildren().get(1)))) {
209                                                taskBuilder.setChild(selection, parent.get(startIndex));
210                                                taskBuilder.addChild(subsequence, selection);
211                                                taskBuilder.removeTaskInstance(parent,startIndex);
212                                                modelindex++;
213                                                continue;
214                                        }
215                                }
216                                // It is just a plain selection
217                                else {
218                                        taskBuilder.setChild(selection, parent.get(startIndex));
219                                        taskBuilder.addChild(subsequence, selection);
220                                }
221                        } else if (tempTask instanceof ISequence) {
222                                taskBuilder.addChild(subsequence, parent.get(startIndex));
223                        } else if (tempTask instanceof IIteration) {
224                                taskBuilder.addChild(subsequence, parent.get(startIndex));
225                        } else {
226                                taskBuilder.addChild(subsequence, parent.get(startIndex));
227                        }
228                        taskBuilder.removeTaskInstance(parent, startIndex);
229                        modelindex++;
230                }
231
232                taskBuilder.addTaskInstance(parent, startIndex, subsequence);
233
234                return subsequence;
235        }
236
237       
238        /*
239        static boolean containsOptional(ISequence seq) {
240          for(int i = 0; i < seq.getChildren().size();i++) {
241              if(seq.getChildren().get(i) instanceof IOptional) {
242                  return true;
243              }
244          } 
245              return false;
246        }
247       
248        private boolean reachesEnd(ITaskInstanceList parent, ISequence model,int sequenceIndex,int modelIndex,int startIndex) {
249           
250            if(modelIndex == model.getChildren().size()) {
251                return true;
252            }
253            ITask modelTask = model.getChildren().get(modelIndex);
254            ITask sessionTask = parent.get(startIndex+sequenceIndex).getTask();
255            if(modelTask instanceof IOptional) {
256                    reachesEnd(parent,model,sequenceIndex++,modelIndex++,startIndex);
257                    reachesEnd(parent,model,sequenceIndex,modelIndex++,startIndex);
258            }
259           
260            boolean taskfits=false;
261            if(modelTask instanceof ISequence) {
262                if(sessionTask.equals(((ISequence)modelTask).getChildren().get(0))) {
263                    taskfits=true;
264                }
265            }
266            if(modelTask instanceof IIteration) {
267                if(sessionTask.equals(((ISequence)modelTask).getChildren().get(0))) {
268                    taskfits=true;
269                }
270            }
271            if(modelTask instanceof ISelection) {
272               
273            }
274           
275        return taskfits;
276           
277           
278           
279        }
280        */
281
282       
283        /**
284         * <p>
285         * returns the next available id (uses the id counter)
286         * </p>
287         * .
288         *
289         * @return the next available id
290         */
291        static synchronized String getNewId() {
292                return Integer.toString(idCounter++);
293        }
294
295        /**
296         * <p>
297         * generates a sub sequence for a specified range of elements in the
298         * provided task instances list.
299         * </p>
300         *
301         * @param parent
302         *            the list of which the range shall be extracted
303         * @param startIndex
304         *            the start index of the range
305         * @param endIndex
306         *            the end index of the range (inclusive)
307         * @param model
308         *            the task model (required for instantiating the sub sequence)
309         * @param taskFactory
310         *            the task factory used for instantiating the sub sequence
311         * @param taskBuilder
312         *            the task builder to perform changes in the task structure
313         *
314         * @return a task instance representing the requested sub sequence
315         */
316        static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent,
317                        int startIndex, int endIndex, ISequence model,
318                        ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
319                final ISequenceInstance subsequence = taskFactory
320                                .createNewTaskInstance(model);
321
322                for (int i = startIndex; i <= endIndex; i++) {
323                        taskBuilder.addChild(subsequence, parent.get(i));
324                }
325
326                return subsequence;
327        }
328
329        /**
330         * Prints the progress percentage.
331         *
332         * @param message
333         *            the message
334         * @param count
335         *            the count
336         * @param size
337         *            the size
338         */
339        static void printProgressPercentage(String message, int count, int size) {
340                if (size > 100) {
341                        if (((count % (size / 100)) == 0)) {
342                                // Console.traceln(Level.INFO,("Thread" +
343                                // Thread.currentThread().getName() + ": " + Math.round((float)
344                                // count/size*100))+ "%");
345                                // System.out.println(message + " in thread "
346                                // + Thread.currentThread().getName() + ": "
347                                // + Math.round(((float) count / size) * 100) + "%");
348                        }
349                } else {
350                        // Console.traceln(Level.INFO,("Thread" +
351                        // Thread.currentThread().getName() + ": " +Math.round((float)
352                        // count/size*100))+ "%");
353                        // System.out.println(message + " in thread "
354                        // + Thread.currentThread().getName() + ": "
355                        // + Math.round(((float) count / size) * 100) + "%");
356
357                }
358        }
359
360        /**
361         * <p>
362         * counter for generating unique ids. Starts at 0 for each new program start
363         * </p>
364         */
365        private static int idCounter = 0;
366
367        /** The missed optionals. */
368        public static int missedOptionals = 0;
369
370        /**
371         * <p>
372         * prevent instantiation
373         * </p>
374         * .
375         */
376        private RuleUtils() {
377                // prevent instantiation
378        }
379
380}
Note: See TracBrowser for help on using the repository browser.