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

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

HarmonizeIterationinstancemodel? DOES need to be called after iteration detection....

File size: 6.9 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.IIterationInstance;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
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 * @author Patrick Harms
38 */
39class RuleUtils {
40
41        /**
42         * <p>
43         * counter for generating unique ids. Starts at 0 for each new program start
44         * </p>
45         */
46        private static int idCounter = 0;
47
48        public static int missedOptionals = 0;
49       
50        /**
51         * <p>
52         * generates a sub sequence for a specified range of elements in the
53         * provided task instances list.
54         * </p>
55         *
56         * @param parent
57         *            the list of which the range shall be extracted
58         * @param startIndex
59         *            the start index of the range
60         * @param endIndex
61         *            the end index of the range (inclusive)
62         * @param model
63         *            the task model (required for instantiating the sub sequence)
64         * @param taskFactory
65         *            the task factory used for instantiating the sub sequence
66         * @param taskBuilder
67         *            the task builder to perform changes in the task structure
68         *
69         * @return a task instance representing the requested sub sequence
70         */
71        static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent,
72                        int startIndex, int endIndex, ISequence model,
73                        ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
74                ISequenceInstance subsequence = taskFactory
75                                .createNewTaskInstance(model);
76
77                for (int i = startIndex; i <= endIndex; i++) {
78                        taskBuilder.addChild(subsequence, parent.get(i));
79                }
80
81                return subsequence;
82        }
83
84        /**
85         * <p>
86         * replaces a sub sequence for a specified range of elements in the provided
87         * task instances list by a sub task instance
88         * </p>
89         *
90         * @param parent
91         *            the list of which the range shall be replaced
92         * @param startIndex
93         *            the start index of the range
94         * @param endIndex
95         *            the end index of the range (inclusive)
96         * @param model
97         *            the task model (required for instantiating the sub sequence)
98         * @param taskFactory
99         *            the task factory used for instantiating the sub sequence
100         * @param taskBuilder
101         *            the task builder to perform changes in the task structure
102         *
103         * @return the replacement for the range
104         */
105        static ISequenceInstance createNewSubSequenceInRange(
106                        ITaskInstanceList parent, int startIndex, int endIndex,
107                        ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
108                ISequenceInstance subsequence = taskFactory
109                                .createNewTaskInstance(model);
110               
111                //for (int i=0; i<subsequence.getSequence().getChildren().size();i++) {
112                //      System.out.println(subsequence.getSequence().getChildren().get(i));     
113                //}
114                //System.out.println();
115                //TODO: This is dirty!
116                missedOptionals=0;
117                int modelindex=0;
118                for (int i = startIndex; i <= endIndex; i++) {
119                       
120                        if(modelindex == model.getChildren().size()) {
121                                break;
122                        }
123                        ITask tempTask = model.getChildren().get(modelindex);
124                        //System.out.println("Trying to add " + parent.get(startIndex)
125                        //      + " to the model instance " + tempTask.getType());
126                        if (tempTask.getType() == "optionality") {
127                                               
128                                        if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) {
129                                                //System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType());
130                                                IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
131                                                taskBuilder.setChild(optional, parent.get(startIndex));
132                                                taskBuilder.addChild(subsequence, optional);
133                                        }
134                                        else {
135                                                //System.out.println("Adding Empty optional, not deleting anything from the input sequence");
136                                                IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
137                                                taskBuilder.addChild(subsequence, optional);
138                                                modelindex++;
139                                                missedOptionals++;
140                                                continue;
141                                        }                               
142                        } else if (tempTask.getType() == "selection") {
143                                //System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType());
144                                ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask);
145                                taskBuilder.setChild(selection, parent.get(startIndex) );
146                                taskBuilder.addChild(subsequence,selection);
147
148                        } else if (tempTask.getType() == "sequence") {
149                                //ISequenceInstance sequence = taskFactory.createNewTaskInstance((ISequence) tempTask);
150                                //taskBuilder.addChild(sequence, parent.get(i));
151                                //taskBuilder.addChild(subsequence,sequence);
152                                taskBuilder.addChild(subsequence, parent.get(startIndex));
153                        } else if (tempTask.getType() == "iteration") {
154                                //IIterationInstance iteration = taskFactory.createNewTaskInstance((IIteration) tempTask);
155                                //taskBuilder.addChild(iteration, parent.get(startIndex));
156                                //taskBuilder.addChild(subsequence, iteration);
157                                taskBuilder.addChild(subsequence, parent.get(startIndex));
158                                //System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType());
159                        } else {
160                                //System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType());
161                                taskBuilder.addChild(subsequence, parent.get(startIndex));
162                        }
163                        taskBuilder.removeTaskInstance(parent, startIndex);
164                        modelindex++;
165                }
166               
167                taskBuilder.addTaskInstance(parent, startIndex, subsequence);
168
169                return subsequence;
170        }
171
172        /**
173         * <p>
174         * returns the next available id (uses the id counter)
175         * </p>
176         *
177         * @return the next available id
178         */
179        static synchronized String getNewId() {
180                return Integer.toString(idCounter++);
181        }
182
183        /**
184         * <p>
185         * prevent instantiation
186         * </p>
187         */
188        private RuleUtils() {
189                // prevent instantiation
190        }
191
192}
Note: See TracBrowser for help on using the repository browser.