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

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

Added automatically created javadoc, still needs to be commented properly though

File size: 8.2 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/**
32 * <p>
33 * provides some convenience methods for rule application
34 * </p>.
35 *
36 * @author Patrick Harms
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         * @param parent            the list of which the range shall be replaced
47         * @param startIndex            the start index of the range
48         * @param endIndex            the end index of the range (inclusive)
49         * @param model            the task model (required for instantiating the sub sequence)
50         * @param taskFactory            the task factory used for instantiating the sub sequence
51         * @param taskBuilder            the task builder to perform changes in the task structure
52         * @return the replacement for the range
53         */
54        static ISequenceInstance createNewSubSequenceInRange(
55                        ITaskInstanceList parent, int startIndex, int endIndex,
56                        ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
57                final ISequenceInstance subsequence = taskFactory
58                                .createNewTaskInstance(model);
59
60               
61                // TODO: This is dirty, return this in addition with the sequence instance instead
62                missedOptionals = 0;
63                int modelindex = 0;
64                for (int i = startIndex; i <= endIndex; i++) {
65
66                        if (modelindex == model.getChildren().size()) {
67                                break;
68                        }
69                        final ITask tempTask = model.getChildren().get(modelindex);
70                        // System.out.println("Trying to add " + parent.get(startIndex)
71                        // + " to the model instance " + tempTask);
72                        if (tempTask.getType() == "optionality") {
73
74                                if (((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent
75                                                .get(startIndex).getTask()) {
76                                        // System.out.println("Adding OptionalInstance " +
77                                        // parent.get(startIndex) + " to " + tempTask.getType());
78                                        final IOptionalInstance optional = taskFactory
79                                                        .createNewTaskInstance((IOptional) tempTask);
80                                        taskBuilder.setChild(optional, parent.get(startIndex));
81                                        taskBuilder.addChild(subsequence, optional);
82                                } else {
83                                        // System.out.println("Adding Empty optional, not deleting anything from the input sequence");
84                                        final IOptionalInstance optional = taskFactory
85                                                        .createNewTaskInstance((IOptional) tempTask);
86                                        taskBuilder.addChild(subsequence, optional);
87                                        modelindex++;
88                                        missedOptionals++;
89                                        continue;
90                                }
91                        } else if (tempTask.getType() == "selection") {
92                                final ISelectionInstance selection = taskFactory
93                                                .createNewTaskInstance((ISelection) tempTask);
94                                final ISelection tmpSel = (ISelection) tempTask;
95                                if ((tmpSel.getChildren().get(0).getType() == "sequence")
96                                                && (tmpSel.getChildren().get(1).getType() == "sequence")) {
97                                        ISequenceInstance selseq = null;
98                                        // The selection I create can just have 2 children
99                                        if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel
100                                                        .getChildren().get(0)).getChildren().get(0).getId()) {
101                                                selseq = taskFactory
102                                                                .createNewTaskInstance((ISequence) tmpSel
103                                                                                .getChildren().get(0));
104                                        } else if (parent.get(startIndex).getTask().getId() == ((ISequence) tmpSel
105                                                        .getChildren().get(1)).getChildren().get(0).getId()) {
106                                                selseq = taskFactory
107                                                                .createNewTaskInstance((ISequence) tmpSel
108                                                                                .getChildren().get(1));
109                                        } else if ((parent.get(startIndex).getTask().getId() == tmpSel
110                                                        .getChildren().get(0).getId())
111                                                        || (parent.get(startIndex).getTask().getId() == tmpSel
112                                                                        .getChildren().get(1).getId())) {
113                                                continue;
114                                        }
115
116                                        for (int k = 0; k < selseq.getSequence().getChildren()
117                                                        .size(); k++) {
118                                                taskBuilder.addChild(selseq, parent.get(startIndex));
119                                                taskBuilder.removeTaskInstance(parent, startIndex);
120                                                i++;
121                                        }
122                                        taskBuilder.setChild(selection, selseq);
123                                        taskBuilder.addChild(subsequence, selection);
124                                        modelindex++;
125                                        continue;
126                                } else {
127                                        taskBuilder.setChild(selection, parent.get(startIndex));
128                                        taskBuilder.addChild(subsequence, selection);
129                                }
130                        } else if (tempTask.getType() == "sequence") {
131                                taskBuilder.addChild(subsequence, parent.get(startIndex));
132                        } else if (tempTask.getType() == "iteration") {
133                                taskBuilder.addChild(subsequence, parent.get(startIndex));
134                        } else {
135                                taskBuilder.addChild(subsequence, parent.get(startIndex));
136                        }
137                        taskBuilder.removeTaskInstance(parent, startIndex);
138                        modelindex++;
139                }
140
141                taskBuilder.addTaskInstance(parent, startIndex, subsequence);
142
143                return subsequence;
144        }
145
146        /**
147         * <p>
148         * returns the next available id (uses the id counter)
149         * </p>.
150         *
151         * @return the next available id
152         */
153        static synchronized String getNewId() {
154                return Integer.toString(idCounter++);
155        }
156
157        /**
158         * <p>
159         * generates a sub sequence for a specified range of elements in the
160         * provided task instances list.
161         * </p>
162         *
163         * @param parent
164         *            the list of which the range shall be extracted
165         * @param startIndex
166         *            the start index of the range
167         * @param endIndex
168         *            the end index of the range (inclusive)
169         * @param model
170         *            the task model (required for instantiating the sub sequence)
171         * @param taskFactory
172         *            the task factory used for instantiating the sub sequence
173         * @param taskBuilder
174         *            the task builder to perform changes in the task structure
175         *
176         * @return a task instance representing the requested sub sequence
177         */
178        static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent,
179                        int startIndex, int endIndex, ISequence model,
180                        ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
181                final ISequenceInstance subsequence = taskFactory
182                                .createNewTaskInstance(model);
183
184                for (int i = startIndex; i <= endIndex; i++) {
185                        taskBuilder.addChild(subsequence, parent.get(i));
186                }
187
188                return subsequence;
189        }
190
191        /**
192         * Prints the progress percentage.
193         *
194         * @param message the message
195         * @param count the count
196         * @param size the size
197         */
198        static void printProgressPercentage(String message, int count, int size) {
199                if (size > 100) {
200                        if (((count % (size / 100)) == 0)) {
201                                // Console.traceln(Level.INFO,("Thread" +
202                                // Thread.currentThread().getName() + ": " + Math.round((float)
203                                // count/size*100))+ "%");
204                                System.out.println(message + " in thread"
205                                                + Thread.currentThread().getName() + ": "
206                                                + Math.round(((float) count / size) * 100) + "%");
207                        }
208                } else {
209                        // Console.traceln(Level.INFO,("Thread" +
210                        // Thread.currentThread().getName() + ": " +Math.round((float)
211                        // count/size*100))+ "%");
212                        System.out.println(message + " in thread"
213                                        + Thread.currentThread().getName() + ": "
214                                        + Math.round(((float) count / size) * 100) + "%");
215
216                }
217        }
218
219        /**
220         * <p>
221         * counter for generating unique ids. Starts at 0 for each new program start
222         * </p>
223         */
224        private static int idCounter = 0;
225
226        /** The missed optionals. */
227        public static int missedOptionals = 0;
228
229        /**
230         * <p>
231         * prevent instantiation
232         * </p>.
233         */
234        private RuleUtils() {
235                // prevent instantiation
236        }
237
238}
Note: See TracBrowser for help on using the repository browser.