1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Set;
|
---|
13 | import java.util.Stack;
|
---|
14 | import java.util.logging.Logger;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.actions.upload.CyclicUploadDependencyException;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Represents a collection of {@see OsmPrimitive}s which should be uploaded to the
|
---|
27 | * API.
|
---|
28 | * The collection is derived from the modified primitives of an {@see DataSet}.
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public class APIDataSet {
|
---|
32 | private LinkedList<OsmPrimitive> toAdd;
|
---|
33 | private LinkedList<OsmPrimitive> toUpdate;
|
---|
34 | private LinkedList<OsmPrimitive> toDelete;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * creates a new empty data set
|
---|
38 | */
|
---|
39 | public APIDataSet() {
|
---|
40 | toAdd = new LinkedList<OsmPrimitive>();
|
---|
41 | toUpdate = new LinkedList<OsmPrimitive>();
|
---|
42 | toDelete = new LinkedList<OsmPrimitive>();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
47 | *
|
---|
48 | * @param ds the data set. Ignored, if null.
|
---|
49 | */
|
---|
50 | public void init(DataSet ds) {
|
---|
51 | if (ds == null) return;
|
---|
52 | toAdd.clear();
|
---|
53 | toUpdate.clear();
|
---|
54 | toDelete.clear();
|
---|
55 |
|
---|
56 | for (OsmPrimitive osm :ds.allPrimitives()) {
|
---|
57 | if (osm.get("josm/ignore") != null) {
|
---|
58 | continue;
|
---|
59 | }
|
---|
60 | if (osm.isNew() && !osm.isDeleted()) {
|
---|
61 | toAdd.add(osm);
|
---|
62 | } else if (osm.isModified() && !osm.isDeleted()) {
|
---|
63 | toUpdate.add(osm);
|
---|
64 | } else if (osm.isDeleted() && !osm.isNew() && osm.isModified()) {
|
---|
65 | toDelete.add(osm);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | sortDeleted();
|
---|
69 | sortNew();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Ensures that primitives are deleted in the following order: Relations, then Ways,
|
---|
74 | * then Nodes.
|
---|
75 | *
|
---|
76 | */
|
---|
77 | protected void sortDeleted() {
|
---|
78 | Collections.sort(
|
---|
79 | toDelete,
|
---|
80 | new Comparator<OsmPrimitive>() {
|
---|
81 | public int compare(OsmPrimitive o1, OsmPrimitive o2) {
|
---|
82 | if (o1 instanceof Node && o2 instanceof Node)
|
---|
83 | return 0;
|
---|
84 | else if (o1 instanceof Node)
|
---|
85 | return 1;
|
---|
86 | else if (o2 instanceof Node)
|
---|
87 | return -1;
|
---|
88 |
|
---|
89 | if (o1 instanceof Way && o2 instanceof Way)
|
---|
90 | return 0;
|
---|
91 | else if (o1 instanceof Way && o2 instanceof Relation)
|
---|
92 | return 1;
|
---|
93 | else if (o2 instanceof Way && o1 instanceof Relation)
|
---|
94 | return -1;
|
---|
95 |
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | );
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Ensures that primitives are added in the following order: Nodes, then Ways,
|
---|
104 | * then Relations.
|
---|
105 | *
|
---|
106 | */
|
---|
107 | protected void sortNew() {
|
---|
108 | Collections.sort(
|
---|
109 | toAdd,
|
---|
110 | new Comparator<OsmPrimitive>() {
|
---|
111 | public int compare(OsmPrimitive o1, OsmPrimitive o2) {
|
---|
112 | if (o1 instanceof Node && o2 instanceof Node)
|
---|
113 | return 0;
|
---|
114 | else if (o1 instanceof Node)
|
---|
115 | return -1;
|
---|
116 | else if (o2 instanceof Node)
|
---|
117 | return 1;
|
---|
118 |
|
---|
119 | if (o1 instanceof Way && o2 instanceof Way)
|
---|
120 | return 0;
|
---|
121 | else if (o1 instanceof Way && o2 instanceof Relation)
|
---|
122 | return -1;
|
---|
123 | else if (o2 instanceof Way && o1 instanceof Relation)
|
---|
124 | return 1;
|
---|
125 |
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | );
|
---|
130 | }
|
---|
131 | /**
|
---|
132 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
133 | *
|
---|
134 | * @param ds the data set. Ignored, if null.
|
---|
135 | */
|
---|
136 | public APIDataSet(DataSet ds) {
|
---|
137 | this();
|
---|
138 | init(ds);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * initializes the API data set with the primitives in <code>primitives</code>
|
---|
143 | *
|
---|
144 | * @param primitives the collection of primitives
|
---|
145 | */
|
---|
146 | public APIDataSet(Collection<OsmPrimitive> primitives) {
|
---|
147 | this();
|
---|
148 | toAdd.clear();
|
---|
149 | toUpdate.clear();
|
---|
150 | toDelete.clear();
|
---|
151 | for (OsmPrimitive osm: primitives) {
|
---|
152 | if (osm.isNew() && !osm.isDeleted()) {
|
---|
153 | toAdd.addLast(osm);
|
---|
154 | } else if (osm.isModified() && !osm.isDeleted()) {
|
---|
155 | toUpdate.addLast(osm);
|
---|
156 | } else if (osm.isDeleted() && !osm.isNew() && osm.isModified()) {
|
---|
157 | toDelete.addFirst(osm);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | sortNew();
|
---|
161 | sortDeleted();
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Replies true if there are no primitives to upload
|
---|
166 | *
|
---|
167 | * @return true if there are no primitives to upload
|
---|
168 | */
|
---|
169 | public boolean isEmpty() {
|
---|
170 | return toAdd.isEmpty() && toUpdate.isEmpty() && toDelete.isEmpty();
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Replies the primitives which should be added to the OSM database
|
---|
175 | *
|
---|
176 | * @return the primitives which should be added to the OSM database
|
---|
177 | */
|
---|
178 | public List<OsmPrimitive> getPrimitivesToAdd() {
|
---|
179 | return toAdd;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Replies the primitives which should be updated in the OSM database
|
---|
184 | *
|
---|
185 | * @return the primitives which should be updated in the OSM database
|
---|
186 | */
|
---|
187 | public List<OsmPrimitive> getPrimitivesToUpdate() {
|
---|
188 | return toUpdate;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Replies the primitives which should be deleted in the OSM database
|
---|
193 | *
|
---|
194 | * @return the primitives which should be deleted in the OSM database
|
---|
195 | */
|
---|
196 | public List<OsmPrimitive> getPrimitivesToDelete() {
|
---|
197 | return toDelete;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Replies all primitives
|
---|
202 | *
|
---|
203 | * @return all primitives
|
---|
204 | */
|
---|
205 | public List<OsmPrimitive> getPrimitives() {
|
---|
206 | LinkedList<OsmPrimitive> ret = new LinkedList<OsmPrimitive>();
|
---|
207 | ret.addAll(toAdd);
|
---|
208 | ret.addAll(toUpdate);
|
---|
209 | ret.addAll(toDelete);
|
---|
210 | return ret;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Adjusts the upload order for new relations. Child relations are uploaded first,
|
---|
215 | * parent relations second.
|
---|
216 | *
|
---|
217 | * This method detects cyclic dependencies in new relation. Relations with cyclic
|
---|
218 | * dependencies can't be uploaded.
|
---|
219 | *
|
---|
220 | * @throws CyclicUploadDependencyException thrown, if a cyclic dependency is detected
|
---|
221 | */
|
---|
222 | public void adjustRelationUploadOrder() throws CyclicUploadDependencyException{
|
---|
223 | LinkedList<OsmPrimitive> newToAdd = new LinkedList<OsmPrimitive>();
|
---|
224 | newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Node.class));
|
---|
225 | newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Way.class));
|
---|
226 |
|
---|
227 | List<Relation> relationsToAdd = OsmPrimitive.getFilteredList(toAdd, Relation.class);
|
---|
228 | List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
|
---|
229 | newToAdd.addAll(noProblemRelations);
|
---|
230 | relationsToAdd.removeAll(noProblemRelations);
|
---|
231 |
|
---|
232 | RelationUploadDependencyGraph graph = new RelationUploadDependencyGraph(relationsToAdd);
|
---|
233 | newToAdd.addAll(graph.computeUploadOrder());
|
---|
234 | toAdd = newToAdd;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Replies the subset of relations in <code>relations</code> which are not referring to any
|
---|
239 | * new relation
|
---|
240 | *
|
---|
241 | * @param relations a list of relations
|
---|
242 | * @return the subset of relations in <code>relations</code> which are not referring to any
|
---|
243 | * new relation
|
---|
244 | */
|
---|
245 | protected List<Relation> filterRelationsNotReferringToNewRelations(Collection<Relation> relations) {
|
---|
246 | List<Relation> ret = new LinkedList<Relation>();
|
---|
247 | for (Relation relation: relations) {
|
---|
248 | boolean refersToNewRelation = false;
|
---|
249 | for (RelationMember m : relation.getMembers()) {
|
---|
250 | if (m.isRelation() && m.getMember().isNew()) {
|
---|
251 | refersToNewRelation = true;
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | if (!refersToNewRelation) {
|
---|
256 | ret.add(relation);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | return ret;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Utility class to sort a collection of of new relations with their dependencies
|
---|
264 | * topologically.
|
---|
265 | *
|
---|
266 | */
|
---|
267 | private class RelationUploadDependencyGraph {
|
---|
268 | private final Logger logger = Logger.getLogger(RelationUploadDependencyGraph.class.getName());
|
---|
269 | private HashMap<Relation, Set<Relation>> children;
|
---|
270 | private Collection<Relation> relations;
|
---|
271 | private Set<Relation> visited;
|
---|
272 | private List<Relation> uploadOrder;
|
---|
273 |
|
---|
274 | public RelationUploadDependencyGraph() {
|
---|
275 | this.children = new HashMap<Relation, Set<Relation>>();
|
---|
276 | this.visited = new HashSet<Relation>();
|
---|
277 | }
|
---|
278 |
|
---|
279 | public RelationUploadDependencyGraph(Collection<Relation> relations) {
|
---|
280 | this();
|
---|
281 | build(relations);
|
---|
282 | }
|
---|
283 |
|
---|
284 | public void build(Collection<Relation> relations) {
|
---|
285 | this.relations = new HashSet<Relation>();
|
---|
286 | for(Relation relation: relations) {
|
---|
287 | if (!relation.isNew() ) {
|
---|
288 | continue;
|
---|
289 | }
|
---|
290 | this.relations.add(relation);
|
---|
291 | for (RelationMember m: relation.getMembers()) {
|
---|
292 | if (m.isRelation() && m.getMember().isNew()) {
|
---|
293 | addDependency(relation, (Relation)m.getMember());
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | public Set<Relation> getChildren(Relation relation) {
|
---|
300 | Set<Relation> p = children.get(relation);
|
---|
301 | if (p == null) {
|
---|
302 | p = new HashSet<Relation>();
|
---|
303 | children.put(relation, p);
|
---|
304 | }
|
---|
305 | return p;
|
---|
306 | }
|
---|
307 |
|
---|
308 | public void addDependency(Relation relation, Relation child) {
|
---|
309 | getChildren(relation).add(child);
|
---|
310 | }
|
---|
311 |
|
---|
312 | protected void visit(Stack<Relation> path, Relation current) throws CyclicUploadDependencyException{
|
---|
313 | if (path.contains(current)) {
|
---|
314 | path.push(current);
|
---|
315 | throw new CyclicUploadDependencyException(path);
|
---|
316 | }
|
---|
317 | if (!visited.contains(current)) {
|
---|
318 | path.push(current);
|
---|
319 | visited.add(current);
|
---|
320 | for (Relation dependent : getChildren(current)) {
|
---|
321 | visit(path,dependent);
|
---|
322 | }
|
---|
323 | uploadOrder.add(current);
|
---|
324 | path.pop();
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | public List<Relation> computeUploadOrder() throws CyclicUploadDependencyException {
|
---|
329 | visited = new HashSet<Relation>();
|
---|
330 | uploadOrder = new LinkedList<Relation>();
|
---|
331 | Stack<Relation> path = new Stack<Relation>();
|
---|
332 | for (Relation relation: relations) {
|
---|
333 | visit(path, relation);
|
---|
334 | }
|
---|
335 | ArrayList<Relation> ret = new ArrayList<Relation>(relations);
|
---|
336 | Collections.sort(
|
---|
337 | ret,
|
---|
338 | new Comparator<Relation>() {
|
---|
339 | public int compare(Relation o1, Relation o2) {
|
---|
340 | return Integer.valueOf(uploadOrder.indexOf(o1)).compareTo(uploadOrder.indexOf(o2));
|
---|
341 | }
|
---|
342 | }
|
---|
343 | );
|
---|
344 | return ret;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|