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

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

Changed substitution matrix back to float, we store alot of them...

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