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

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

Memory improvement and bugs

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