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

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

Fixed first iteration of sequence detection and replacement

File size: 6.9 KB
RevLine 
[1107]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
[1675]17import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
[1656]20import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
[1655]21import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
[1656]23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
[1294]25import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
[1655]27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
[1146]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;
[1107]32
33/**
34 * <p>
[1281]35 * provides some convenience methods for rule application
[1107]36 * </p>
37 *
38 * @author Patrick Harms
39 */
40class RuleUtils {
41
[1655]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;
[1127]48
[1657]49        public static int missedOptionals = 0;
50       
[1655]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);
[1127]77
[1655]78                for (int i = startIndex; i <= endIndex; i++) {
79                        taskBuilder.addChild(subsequence, parent.get(i));
80                }
[1127]81
[1655]82                return subsequence;
83        }
[1107]84
[1655]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         */
106        static ISequenceInstance createNewSubSequenceInRange(
107                        ITaskInstanceList parent, int startIndex, int endIndex,
108                        ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
109                ISequenceInstance subsequence = taskFactory
110                                .createNewTaskInstance(model);
[1656]111               
[1657]112                //for (int i=0; i<subsequence.getSequence().getChildren().size();i++) {
113                //      System.out.println(subsequence.getSequence().getChildren().get(i));     
114                //}
[1666]115                //System.out.println();
[1657]116                //TODO: This is dirty!
117                missedOptionals=0;
[1655]118                int modelindex=0;
119                for (int i = startIndex; i <= endIndex; i++) {
[1656]120                       
[1657]121                        if(modelindex == model.getChildren().size()) {
122                                break;
123                        }
[1655]124                        ITask tempTask = model.getChildren().get(modelindex);
[1656]125                        //System.out.println("Trying to add " + parent.get(startIndex)
126                        //      + " to the model instance " + tempTask.getType());
[1655]127                        if (tempTask.getType() == "optionality") {
[1656]128                                               
129                                        if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) {
[1675]130                                                System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType());
[1656]131                                                IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
132                                                taskBuilder.setChild(optional, parent.get(startIndex));
133                                                taskBuilder.addChild(subsequence, optional);
134                                        }
135                                        else {
[1675]136                                                System.out.println("Adding Empty optional, not deleting anything from the input sequence");
[1656]137                                                IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
138                                                taskBuilder.addChild(subsequence, optional);
139                                                modelindex++;
[1657]140                                                missedOptionals++;
[1656]141                                                continue;
142                                        }                               
[1655]143                        } else if (tempTask.getType() == "selection") {
[1675]144                                System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType());
[1656]145                                ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask);
146                                taskBuilder.setChild(selection, parent.get(startIndex) );
147                                taskBuilder.addChild(subsequence,selection);
[1107]148
[1655]149                        } else if (tempTask.getType() == "sequence") {
[1675]150                                System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask.getType());
151                                ISequenceInstance sequence = taskFactory.createNewTaskInstance((ISequence) tempTask);
152                                taskBuilder.addChild(sequence,parent.get(startIndex));
[1656]153                               
[1655]154                        } else if (tempTask.getType() == "iteration") {
[1675]155                                IIterationInstance iteration = taskFactory.createNewTaskInstance((IIteration) tempTask);
156                                taskBuilder.addChild(iteration,parent.get(startIndex));
157                                System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType());
[1655]158                        } else {
[1675]159                                System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType());
[1656]160                                taskBuilder.addChild(subsequence, parent.get(startIndex));
[1655]161                        }
162                        taskBuilder.removeTaskInstance(parent, startIndex);
163                        modelindex++;
164                }
[1656]165               
[1655]166                taskBuilder.addTaskInstance(parent, startIndex, subsequence);
167
168                return subsequence;
169        }
170
171        /**
172         * <p>
173         * returns the next available id (uses the id counter)
174         * </p>
175         *
176         * @return the next available id
177         */
178        static synchronized String getNewId() {
179                return Integer.toString(idCounter++);
180        }
181
182        /**
183         * <p>
184         * prevent instantiation
185         * </p>
186         */
187        private RuleUtils() {
188                // prevent instantiation
189        }
190
[1107]191}
Note: See TracBrowser for help on using the repository browser.