source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java @ 1285

Last change on this file since 1285 was 1281, checked in by pharms, 11 years ago
  • handled some TODOs
File size: 4.5 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.ITask;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
22
23/**
24 * <p>
25 * provides some convenience methods for rule application
26 * </p>
27 *
28 * @author Patrick Harms
29 */
30class RuleUtils {
31   
32    /**
33     * <p>
34     * counter for generating unique ids. Starts at 0 for each new program start
35     * </p>
36     */
37    private static int idCounter = 0;
38
39    /**
40     * <p>
41     * generates a sub sequence for a specified range of elements in the provided task instances
42     * list.
43     * </p>
44     *
45     * @param parent      the list of which the range shall be extracted
46     * @param startIndex  the start index of the range
47     * @param endIndex    the end index of the range (inclusive)
48     * @param model       the task model (required for instantiating the sub sequence)
49     * @param taskFactory the task factory used for instantiating the sub sequence
50     * @param taskBuilder the task builder to perform changes in the task structure
51     *
52     * @return a task instance representing the requested sub sequence
53     */
54    static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent,
55                                               int               startIndex,
56                                               int               endIndex,
57                                               ITask             model,
58                                               ITaskFactory      taskFactory,
59                                               ITaskBuilder      taskBuilder)
60    {
61        ITaskInstance subsequence = taskFactory.createNewTaskInstance(model);
62
63        for (int i = startIndex; i <= endIndex; i++) {
64            taskBuilder.addChild(subsequence, parent.get(i));
65        }
66
67        return subsequence;
68    }
69
70    /**
71     * <p>
72     * replaces a sub sequence for a specified range of elements in the provided task instances
73     * list by a sub task instance
74     * </p>
75     *
76     * @param parent      the list of which the range shall be replaced
77     * @param startIndex  the start index of the range
78     * @param endIndex    the end index of the range (inclusive)
79     * @param model       the task model (required for instantiating the sub sequence)
80     * @param taskFactory the task factory used for instantiating the sub sequence
81     * @param taskBuilder the task builder to perform changes in the task structure
82     *
83     * @return the replacement for the range
84     */
85    static ITaskInstance createNewSubSequenceInRange(ITaskInstanceList parent,
86                                                     int               startIndex,
87                                                     int               endIndex,
88                                                     ITask             model,
89                                                     ITaskFactory      taskFactory,
90                                                     ITaskBuilder      taskBuilder)
91    {
92        ITaskInstance subsequence = taskFactory.createNewTaskInstance(model);
93
94        for (int i = startIndex; i <= endIndex; i++) {
95            taskBuilder.addChild(subsequence, parent.get(startIndex));
96            taskBuilder.removeTaskInstance(parent, startIndex);
97        }
98
99
100        taskBuilder.addTaskInstance(parent, startIndex, subsequence);
101
102        return subsequence;
103    }
104
105    /**
106     * <p>
107     * returns the next available id (uses the id counter)
108     * </p>
109     *
110     * @return the next available id
111     */
112    static synchronized String getNewId() {
113        return Integer.toString(idCounter++);
114    }
115
116    /**
117     * <p>
118     * prevent instantiation
119     * </p>
120     */
121    private RuleUtils() {
122        // prevent instantiation
123    }
124
125}
Note: See TracBrowser for help on using the repository browser.