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

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

Fixed few parallel task scheduling bugs.

File size: 9.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.IMarkingTemporalRelationship;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
29
30/**
31 * <p>
32 * provides some convenience methods for rule application
33 * </p>
34 * .
35 *
36 * @author Patrick Harms, Ralph Krimmel
37 */
38class RuleUtils {
39
40        /**
41         * <p>
42         * replaces a sub sequence for a specified range of elements in the provided
43         * task instances list by a sub task instance
44         * </p>
45         * .
46         *
47         * @param parent
48         *            the list of which the range shall be replaced
49         * @param startIndex
50         *            the start index of the range
51         * @param endIndex
52         *            the end index of the range (inclusive)
53         * @param model
54         *            the task model (required for instantiating the sub sequence)
55         * @param taskFactory
56         *            the task factory used for instantiating the sub sequence
57         * @param taskBuilder
58         *            the task builder to perform changes in the task structure
59         * @return the replacement for the range
60         */
61        static ISequenceInstance createNewSubSequenceInRange(
62                        ITaskInstanceList parent, int startIndex, int endIndex,
63                        ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
64                final ISequenceInstance subsequence = taskFactory
65                                .createNewTaskInstance(model);
66                @SuppressWarnings("unused")
67                int modelid = model.getId();
68                if(modelid == 5412) {
69                        System.out.println("Printing session: ");
70                        for (int i = 0; i < parent.size();i++) {
71                                System.out.println(parent.get(i));
72                        }
73                        System.out.println("startIndex: " + startIndex + " endIndex: " + endIndex + "\n");
74                        System.out.println("Printing model: ");
75                        for(int i = 0; i < ((ISequence)model).getChildren().size();i++) {
76                                System.out.println((ISequence)model.getChildren().get(i));             
77                        }
78                       
79                }
80                // TODO: This is dirty, return this in addition with the sequence
81                // instance instead
82                missedOptionals = 0;
83                int modelindex = 0;
84                for (int i = startIndex; i <= endIndex; i++) {
85
86                        if (modelindex == model.getChildren().size()) {
87                                break;
88                        }
89                        final ITask tempTask = model.getChildren().get(modelindex);
90                        // System.out.println("Trying to add " + parent.get(startIndex)
91                        // + " to the model instance " + tempTask);
92                        if (tempTask.getType() == "optionality") {
93
94                                if (((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent
95                                                .get(startIndex).getTask()) {
96                                        // System.out.println("Adding OptionalInstance " +
97                                        // parent.get(startIndex) + " to " + tempTask.getType());
98                                        final IOptionalInstance optional = taskFactory
99                                                        .createNewTaskInstance((IOptional) tempTask);
100                                        taskBuilder.setChild(optional, parent.get(startIndex));
101                                        taskBuilder.addChild(subsequence, optional);
102                                } else {
103                                        // System.out.println("Adding Empty optional, not deleting anything from the input sequence");
104                                        final IOptionalInstance optional = taskFactory
105                                                        .createNewTaskInstance((IOptional) tempTask);
106                                        taskBuilder.addChild(subsequence, optional);
107                                        modelindex++;
108                                        missedOptionals++;
109                                        continue;
110                                }
111                        } else if (tempTask instanceof ISelection) {
112                                final ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask);
113                                final ISelection tmpSel = (ISelection) tempTask;
114                                //Check if the selection has 2 sequences as children
115                                if ((tmpSel.getChildren().get(0) instanceof ISequence) && (tmpSel.getChildren().get(1) instanceof ISequence)) {
116                                        ISequenceInstance selseq = null;
117                                        // The selection I create can just have 2 children, we need to check here, to which sequence of the model this occurence belongs
118                                        //This if checks of the occurrence is equal to the first element of the first sequence in the model
119                                        if (parent.get(startIndex).getTask().equals(((ISequence) tmpSel.getChildren().get(0)).getChildren().get(0))) {
120                                                selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(0));
121                                        //This if checks of the occurrence is equal to the first element of the second sequence in the model                                           
122                                        } else if (parent.get(startIndex).getTask().equals(((ISequence) tmpSel.getChildren().get(1)).getChildren().get(0))) {
123                                                selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(1));
124                                        //If the occurence is already a sequence we don't need to do anything, the next iteration will detect this as a sequence and add it
125                                        } else if ((parent.get(startIndex).getTask().equals(tmpSel.getChildren().get(0).getId()))
126                                                        || (parent.get(startIndex).getTask().equals(tmpSel.getChildren().get(1).getId()))) {
127                                                //taskBuilder.setChild(selection, parent.get(startIndex));
128                                                //taskBuilder.addChild(subsequence, selection);
129                                                //taskBuilder.removeTaskInstance(parent, startIndex);
130                                                //modelindex++;
131                                                continue;
132                                               
133                                        }
134                                        //TODO: Sometimes nullpointer exception here :(
135                                        for (int k = 0; k < ((ISequence)tmpSel.getChildren().get(0)).getChildren().size(); k++) {
136                                                taskBuilder.addChild(selseq, parent.get(startIndex));
137                                                taskBuilder.removeTaskInstance(parent, startIndex);
138                                                i++;
139                                        }
140                                        taskBuilder.setChild(selection, selseq);
141                                        taskBuilder.addChild(subsequence, selection);
142                                        modelindex++;
143                                        continue;
144                                //It is just a plain selection
145                                } else {
146                                        taskBuilder.setChild(selection, parent.get(startIndex));
147                                        taskBuilder.addChild(subsequence, selection);
148                                }
149                        } else if (tempTask.getType() == "sequence") {
150                                taskBuilder.addChild(subsequence, parent.get(startIndex));
151                        } else if (tempTask.getType() == "iteration") {
152                                taskBuilder.addChild(subsequence, parent.get(startIndex));
153                        } else {
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
163                return subsequence;
164        }
165
166        /**
167         * <p>
168         * returns the next available id (uses the id counter)
169         * </p>
170         * .
171         *
172         * @return the next available id
173         */
174        static synchronized String getNewId() {
175                return Integer.toString(idCounter++);
176        }
177
178        /**
179         * <p>
180         * generates a sub sequence for a specified range of elements in the
181         * provided task instances list.
182         * </p>
183         *
184         * @param parent
185         *            the list of which the range shall be extracted
186         * @param startIndex
187         *            the start index of the range
188         * @param endIndex
189         *            the end index of the range (inclusive)
190         * @param model
191         *            the task model (required for instantiating the sub sequence)
192         * @param taskFactory
193         *            the task factory used for instantiating the sub sequence
194         * @param taskBuilder
195         *            the task builder to perform changes in the task structure
196         *
197         * @return a task instance representing the requested sub sequence
198         */
199        static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent,
200                        int startIndex, int endIndex, ISequence model,
201                        ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
202                final ISequenceInstance subsequence = taskFactory
203                                .createNewTaskInstance(model);
204
205                for (int i = startIndex; i <= endIndex; i++) {
206                        taskBuilder.addChild(subsequence, parent.get(i));
207                }
208
209                return subsequence;
210        }
211
212        /**
213         * Prints the progress percentage.
214         *
215         * @param message
216         *            the message
217         * @param count
218         *            the count
219         * @param size
220         *            the size
221         */
222        static void printProgressPercentage(String message, int count, int size) {
223                if (size > 100) {
224                        if (((count % (size / 100)) == 0)) {
225                                // Console.traceln(Level.INFO,("Thread" +
226                                // Thread.currentThread().getName() + ": " + Math.round((float)
227                                // count/size*100))+ "%");
228                                // System.out.println(message + " in thread"
229                                // + Thread.currentThread().getName() + ": "
230                                // + Math.round(((float) count / size) * 100) + "%");
231                        }
232                } else {
233                        // Console.traceln(Level.INFO,("Thread" +
234                        // Thread.currentThread().getName() + ": " +Math.round((float)
235                        // count/size*100))+ "%");
236                        // System.out.println(message + " in thread"
237                        // + Thread.currentThread().getName() + ": "
238                        // + Math.round(((float) count / size) * 100) + "%");
239
240                }
241        }
242
243        /**
244         * <p>
245         * counter for generating unique ids. Starts at 0 for each new program start
246         * </p>
247         */
248        private static int idCounter = 0;
249
250        /** The missed optionals. */
251        public static int missedOptionals = 0;
252
253        /**
254         * <p>
255         * prevent instantiation
256         * </p>
257         * .
258         */
259        private RuleUtils() {
260                // prevent instantiation
261        }
262
263}
Note: See TracBrowser for help on using the repository browser.