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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
16 | |
---|
17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
23 | |
---|
24 | /** |
---|
25 | * <p> |
---|
26 | * provides some convenience methods for rule application |
---|
27 | * </p> |
---|
28 | * |
---|
29 | * @author Patrick Harms |
---|
30 | */ |
---|
31 | class RuleUtils { |
---|
32 | |
---|
33 | /** |
---|
34 | * <p> |
---|
35 | * counter for generating unique ids. Starts at 0 for each new program start |
---|
36 | * </p> |
---|
37 | */ |
---|
38 | private static int idCounter = 0; |
---|
39 | |
---|
40 | /** |
---|
41 | * <p> |
---|
42 | * generates a sub sequence for a specified range of elements in the provided task instances |
---|
43 | * list. |
---|
44 | * </p> |
---|
45 | * |
---|
46 | * @param parent the list of which the range shall be extracted |
---|
47 | * @param startIndex the start index of the range |
---|
48 | * @param endIndex the end index of the range (inclusive) |
---|
49 | * @param model the task model (required for instantiating the sub sequence) |
---|
50 | * @param taskFactory the task factory used for instantiating the sub sequence |
---|
51 | * @param taskBuilder the task builder to perform changes in the task structure |
---|
52 | * |
---|
53 | * @return a task instance representing the requested sub sequence |
---|
54 | */ |
---|
55 | static ITaskInstance getSubSequenceInRange(ITaskInstanceList parent, |
---|
56 | int startIndex, |
---|
57 | int endIndex, |
---|
58 | ISequence model, |
---|
59 | ITaskFactory taskFactory, |
---|
60 | ITaskBuilder taskBuilder) |
---|
61 | { |
---|
62 | ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model); |
---|
63 | |
---|
64 | for (int i = startIndex; i <= endIndex; i++) { |
---|
65 | taskBuilder.addChild(subsequence, parent.get(i)); |
---|
66 | } |
---|
67 | |
---|
68 | return subsequence; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * <p> |
---|
73 | * replaces a sub sequence for a specified range of elements in the provided task instances |
---|
74 | * list by a sub task instance |
---|
75 | * </p> |
---|
76 | * |
---|
77 | * @param parent the list of which the range shall be replaced |
---|
78 | * @param startIndex the start index of the range |
---|
79 | * @param endIndex the end index of the range (inclusive) |
---|
80 | * @param model the task model (required for instantiating the sub sequence) |
---|
81 | * @param taskFactory the task factory used for instantiating the sub sequence |
---|
82 | * @param taskBuilder the task builder to perform changes in the task structure |
---|
83 | * |
---|
84 | * @return the replacement for the range |
---|
85 | */ |
---|
86 | static ISequenceInstance createNewSubSequenceInRange(ITaskInstanceList parent, |
---|
87 | int startIndex, |
---|
88 | int endIndex, |
---|
89 | ISequence model, |
---|
90 | ITaskFactory taskFactory, |
---|
91 | ITaskBuilder taskBuilder) |
---|
92 | { |
---|
93 | ISequenceInstance subsequence = taskFactory.createNewTaskInstance(model); |
---|
94 | |
---|
95 | for (int i = startIndex; i <= endIndex; i++) { |
---|
96 | taskBuilder.addChild(subsequence, parent.get(startIndex)); |
---|
97 | taskBuilder.removeTaskInstance(parent, startIndex); |
---|
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 | } |
---|