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.treeifc; |
---|
16 | |
---|
17 | import java.util.ArrayList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | /** |
---|
21 | * <p> |
---|
22 | * A task path is a path through a task tree. Conceptually, it is a list of task objects. But the |
---|
23 | * plain information about the tasks is not enough for a task path. The reason is, that multiple |
---|
24 | * paths through a task tree may be identical because the tasks in the paths are identical. This |
---|
25 | * happens, e.g., if a sequence has three children, where the first and the last are the same. Then |
---|
26 | * based on the plain task information, it would not be possible to define separate task paths for |
---|
27 | * the first and the last child of the sequence. Hence, we also add for each path element the index |
---|
28 | * of the element in the list of children of the parent. This index is also considered, when |
---|
29 | * comparing task paths. |
---|
30 | * </p> |
---|
31 | * |
---|
32 | * @author Patrick Harms |
---|
33 | */ |
---|
34 | public class TaskPath { |
---|
35 | |
---|
36 | /** */ |
---|
37 | private List<Entry> taskList; |
---|
38 | |
---|
39 | /** |
---|
40 | * |
---|
41 | * |
---|
42 | */ |
---|
43 | public TaskPath() { |
---|
44 | super(); |
---|
45 | taskList = new ArrayList<Entry>(); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * |
---|
50 | */ |
---|
51 | public TaskPath(TaskPath path) { |
---|
52 | this(); |
---|
53 | taskList.addAll(path.taskList); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * |
---|
58 | */ |
---|
59 | public TaskPath subPath(int fromIndex, int toIndex) { |
---|
60 | TaskPath result = new TaskPath(); |
---|
61 | result.taskList.addAll(taskList.subList(fromIndex, toIndex)); |
---|
62 | return result; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * |
---|
67 | */ |
---|
68 | public ITask removeLast() { |
---|
69 | Entry last = taskList.remove(taskList.size() - 1); |
---|
70 | if (last != null) { |
---|
71 | return last.getTask(); |
---|
72 | } |
---|
73 | else { |
---|
74 | return null; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * |
---|
80 | */ |
---|
81 | public ITask getLast() { |
---|
82 | Entry last = taskList.get(taskList.size() - 1); |
---|
83 | if (last != null) { |
---|
84 | return last.getTask(); |
---|
85 | } |
---|
86 | else { |
---|
87 | return null; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /* (non-Javadoc) |
---|
92 | * @see java.lang.Object#toString() |
---|
93 | */ |
---|
94 | @Override |
---|
95 | public String toString() { |
---|
96 | return taskList.toString(); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * |
---|
101 | */ |
---|
102 | public int size() { |
---|
103 | return taskList.size(); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * |
---|
108 | */ |
---|
109 | public boolean isEmpty() { |
---|
110 | return taskList.isEmpty(); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * |
---|
115 | */ |
---|
116 | public void add(ITask task, int index) { |
---|
117 | taskList.add(new Entry(task, index)); |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * |
---|
122 | */ |
---|
123 | public ITask getTask(int index) { |
---|
124 | Entry last = taskList.get(index); |
---|
125 | if (last != null) { |
---|
126 | return last.getTask(); |
---|
127 | } |
---|
128 | else { |
---|
129 | return null; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * |
---|
135 | */ |
---|
136 | public Entry get(int index) { |
---|
137 | return taskList.get(index); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /* (non-Javadoc) |
---|
142 | * @see java.lang.Object#hashCode() |
---|
143 | */ |
---|
144 | @Override |
---|
145 | public int hashCode() { |
---|
146 | ITask last = getLast(); |
---|
147 | if (last != null) { |
---|
148 | return last.hashCode(); |
---|
149 | } |
---|
150 | else { |
---|
151 | return 0; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /* (non-Javadoc) |
---|
156 | * @see java.lang.Object#equals(java.lang.Object) |
---|
157 | */ |
---|
158 | @Override |
---|
159 | public boolean equals(Object obj) { |
---|
160 | if (this == obj) { |
---|
161 | return true; |
---|
162 | } |
---|
163 | else if (obj instanceof TaskPath) { |
---|
164 | TaskPath other = (TaskPath) obj; |
---|
165 | if (this.size() == other.size()) { |
---|
166 | for (int i = 0; i < this.size(); i++) { |
---|
167 | if (!this.get(i).equals(other.get(i))) { |
---|
168 | return false; |
---|
169 | } |
---|
170 | } |
---|
171 | return true; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | return false; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | /** |
---|
180 | * |
---|
181 | */ |
---|
182 | public static class Entry { |
---|
183 | |
---|
184 | /** */ |
---|
185 | private ITask task; |
---|
186 | |
---|
187 | /** */ |
---|
188 | private int index; |
---|
189 | |
---|
190 | /** |
---|
191 | * |
---|
192 | */ |
---|
193 | private Entry(ITask task, int index) { |
---|
194 | this.task = task; |
---|
195 | this.index = index; |
---|
196 | } |
---|
197 | |
---|
198 | /* (non-Javadoc) |
---|
199 | * @see java.lang.Object#toString() |
---|
200 | */ |
---|
201 | @Override |
---|
202 | public String toString() { |
---|
203 | return "(" + task + "," + index + ")"; |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * @return the task |
---|
208 | */ |
---|
209 | public ITask getTask() { |
---|
210 | return task; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * @return the index |
---|
215 | */ |
---|
216 | public int getIndex() { |
---|
217 | return index; |
---|
218 | } |
---|
219 | |
---|
220 | /* (non-Javadoc) |
---|
221 | * @see java.lang.Object#hashCode() |
---|
222 | */ |
---|
223 | @Override |
---|
224 | public int hashCode() { |
---|
225 | return task.hashCode(); |
---|
226 | } |
---|
227 | |
---|
228 | /* (non-Javadoc) |
---|
229 | * @see java.lang.Object#equals(java.lang.Object) |
---|
230 | */ |
---|
231 | @Override |
---|
232 | public boolean equals(Object obj) { |
---|
233 | if (this == obj) { |
---|
234 | return true; |
---|
235 | } |
---|
236 | else if (obj instanceof TaskPath.Entry) { |
---|
237 | return (this.getIndex() == ((TaskPath.Entry) obj).getIndex()) && |
---|
238 | (this.getTask().equals(((TaskPath.Entry) obj).getTask())); |
---|
239 | } |
---|
240 | |
---|
241 | return false; |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | } |
---|
246 | |
---|
247 | } |
---|