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

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

Fixed model replacment, index updates still to go.

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