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

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

Parallel Replacement done

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