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.addLast(osm);
|
---|
62 | } else if (osm.isModified() && !osm.isDeleted()) {
|
---|
63 | toUpdate.addLast(osm);
|
---|
64 | } else if (osm.isDeleted() && !osm.isNew() && osm.isModified()) {
|
---|
65 | toDelete.addFirst(osm);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | sortDeleted();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Ensures that primitives are deleted in the following order: Relations, then Ways,
|
---|
73 | * then Nodes.
|
---|
74 | *
|
---|
75 | */
|
---|
76 | protected void sortDeleted() {
|
---|
77 | Collections.sort(
|
---|
78 | toDelete,
|
---|
79 | new Comparator<OsmPrimitive>() {
|
---|
80 | public int compare(OsmPrimitive o1, OsmPrimitive o2) {
|
---|
81 | if (o1 instanceof Node && o2 instanceof Node)
|
---|
82 | return 0;
|
---|
83 | else if (o1 instanceof Node)
|
---|
84 | return 1;
|
---|
85 | else if (o2 instanceof Node)
|
---|
86 | return -1;
|
---|
87 |
|
---|
88 | if (o1 instanceof Way && o2 instanceof Way)
|
---|
89 | return 0;
|
---|
90 | else if (o1 instanceof Way && o2 instanceof Relation)
|
---|
91 | return 1;
|
---|
92 | else if (o2 instanceof Way && o1 instanceof Relation)
|
---|
93 | return -1;
|
---|
94 |
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | );
|
---|
99 | }
|
---|
100 | /**
|
---|
101 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
102 | *
|
---|
103 | * @param ds the data set. Ignored, if null.
|
---|
104 | */
|
---|
105 | public APIDataSet(DataSet ds) {
|
---|
106 | this();
|
---|
107 | init(ds);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * initializes the API data set with the primitives in <code>primitives</code>
|
---|
112 | *
|
---|
113 | * @param primitives the collection of primitives
|
---|
114 | */
|
---|
115 | public APIDataSet(Collection<OsmPrimitive> primitives) {
|
---|
116 | this();
|
---|
117 | toAdd.clear();
|
---|
118 | toUpdate.clear();
|
---|
119 | toDelete.clear();
|
---|
120 | for (OsmPrimitive osm: primitives) {
|
---|
121 | if (osm.isNew() && !osm.isDeleted()) {
|
---|
122 | toAdd.addLast(osm);
|
---|
123 | } else if (osm.isModified() && !osm.isDeleted()) {
|
---|
124 | toUpdate.addLast(osm);
|
---|
125 | } else if (osm.isDeleted() && !osm.isNew() && osm.isModified()) {
|
---|
126 | toDelete.addFirst(osm);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | sortDeleted();
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Replies true if there are no primitives to upload
|
---|
134 | *
|
---|
135 | * @return true if there are no primitives to upload
|
---|
136 | */
|
---|
137 | public boolean isEmpty() {
|
---|
138 | return toAdd.isEmpty() && toUpdate.isEmpty() && toDelete.isEmpty();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Replies the primitives which should be added to the OSM database
|
---|
143 | *
|
---|
144 | * @return the primitives which should be added to the OSM database
|
---|
145 | */
|
---|
146 | public List<OsmPrimitive> getPrimitivesToAdd() {
|
---|
147 | return toAdd;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Replies the primitives which should be updated in the OSM database
|
---|
152 | *
|
---|
153 | * @return the primitives which should be updated in the OSM database
|
---|
154 | */
|
---|
155 | public List<OsmPrimitive> getPrimitivesToUpdate() {
|
---|
156 | return toUpdate;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Replies the primitives which should be deleted in the OSM database
|
---|
161 | *
|
---|
162 | * @return the primitives which should be deleted in the OSM database
|
---|
163 | */
|
---|
164 | public List<OsmPrimitive> getPrimitivesToDelete() {
|
---|
165 | return toDelete;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Replies all primitives
|
---|
170 | *
|
---|
171 | * @return all primitives
|
---|
172 | */
|
---|
173 | public List<OsmPrimitive> getPrimitives() {
|
---|
174 | LinkedList<OsmPrimitive> ret = new LinkedList<OsmPrimitive>();
|
---|
175 | ret.addAll(toAdd);
|
---|
176 | ret.addAll(toUpdate);
|
---|
177 | ret.addAll(toDelete);
|
---|
178 | return ret;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Adjusts the upload order for new relations. Child relations are uploaded first,
|
---|
183 | * parent relations second.
|
---|
184 | *
|
---|
185 | * This method detects cyclic dependencies in new relation. Relations with cyclic
|
---|
186 | * dependencies can't be uploaded.
|
---|
187 | *
|
---|
188 | * @throws CyclicUploadDependencyException thrown, if a cyclic dependency is detected
|
---|
189 | */
|
---|
190 | public void adjustRelationUploadOrder() throws CyclicUploadDependencyException{
|
---|
191 | LinkedList<OsmPrimitive> newToAdd = new LinkedList<OsmPrimitive>();
|
---|
192 | newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Node.class));
|
---|
193 | newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Way.class));
|
---|
194 |
|
---|
195 | List<Relation> relationsToAdd = OsmPrimitive.getFilteredList(toAdd, Relation.class);
|
---|
196 | List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
|
---|
197 | newToAdd.addAll(noProblemRelations);
|
---|
198 | relationsToAdd.removeAll(noProblemRelations);
|
---|
199 |
|
---|
200 | RelationUploadDependencyGraph graph = new RelationUploadDependencyGraph(relationsToAdd);
|
---|
201 | newToAdd.addAll(graph.computeUploadOrder());
|
---|
202 | toAdd = newToAdd;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Replies the subset of relations in <code>relations</code> which are not referring to any
|
---|
207 | * new relation
|
---|
208 | *
|
---|
209 | * @param relations a list of relations
|
---|
210 | * @return the subset of relations in <code>relations</code> which are not referring to any
|
---|
211 | * new relation
|
---|
212 | */
|
---|
213 | protected List<Relation> filterRelationsNotReferringToNewRelations(Collection<Relation> relations) {
|
---|
214 | List<Relation> ret = new LinkedList<Relation>();
|
---|
215 | for (Relation relation: relations) {
|
---|
216 | boolean refersToNewRelation = false;
|
---|
217 | for (RelationMember m : relation.getMembers()) {
|
---|
218 | if (m.isRelation() && m.getMember().isNew()) {
|
---|
219 | refersToNewRelation = true;
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | if (!refersToNewRelation) {
|
---|
224 | ret.add(relation);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | return ret;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Utility class to sort a collection of of new relations with their dependencies
|
---|
232 | * topologically.
|
---|
233 | *
|
---|
234 | */
|
---|
235 | private class RelationUploadDependencyGraph {
|
---|
236 | private final Logger logger = Logger.getLogger(RelationUploadDependencyGraph.class.getName());
|
---|
237 | private HashMap<Relation, Set<Relation>> children;
|
---|
238 | private Collection<Relation> relations;
|
---|
239 | private Set<Relation> visited;
|
---|
240 | private List<Relation> uploadOrder;
|
---|
241 |
|
---|
242 | public RelationUploadDependencyGraph() {
|
---|
243 | this.children = new HashMap<Relation, Set<Relation>>();
|
---|
244 | this.visited = new HashSet<Relation>();
|
---|
245 | }
|
---|
246 |
|
---|
247 | public RelationUploadDependencyGraph(Collection<Relation> relations) {
|
---|
248 | this();
|
---|
249 | build(relations);
|
---|
250 | }
|
---|
251 |
|
---|
252 | public void build(Collection<Relation> relations) {
|
---|
253 | this.relations = new HashSet<Relation>();
|
---|
254 | for(Relation relation: relations) {
|
---|
255 | if (!relation.isNew() ) {
|
---|
256 | continue;
|
---|
257 | }
|
---|
258 | this.relations.add(relation);
|
---|
259 | for (RelationMember m: relation.getMembers()) {
|
---|
260 | if (m.isRelation() && m.getMember().isNew()) {
|
---|
261 | addDependency(relation, (Relation)m.getMember());
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | public Set<Relation> getChildren(Relation relation) {
|
---|
268 | Set<Relation> p = children.get(relation);
|
---|
269 | if (p == null) {
|
---|
270 | p = new HashSet<Relation>();
|
---|
271 | children.put(relation, p);
|
---|
272 | }
|
---|
273 | return p;
|
---|
274 | }
|
---|
275 |
|
---|
276 | public void addDependency(Relation relation, Relation child) {
|
---|
277 | getChildren(relation).add(child);
|
---|
278 | }
|
---|
279 |
|
---|
280 | protected void visit(Stack<Relation> path, Relation current) throws CyclicUploadDependencyException{
|
---|
281 | if (path.contains(current)) {
|
---|
282 | path.push(current);
|
---|
283 | throw new CyclicUploadDependencyException(path);
|
---|
284 | }
|
---|
285 | if (!visited.contains(current)) {
|
---|
286 | path.push(current);
|
---|
287 | visited.add(current);
|
---|
288 | for (Relation dependent : getChildren(current)) {
|
---|
289 | visit(path,dependent);
|
---|
290 | }
|
---|
291 | uploadOrder.add(current);
|
---|
292 | path.pop();
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | public List<Relation> computeUploadOrder() throws CyclicUploadDependencyException {
|
---|
297 | visited = new HashSet<Relation>();
|
---|
298 | uploadOrder = new LinkedList<Relation>();
|
---|
299 | Stack<Relation> path = new Stack<Relation>();
|
---|
300 | for (Relation relation: relations) {
|
---|
301 | visit(path, relation);
|
---|
302 | }
|
---|
303 | ArrayList<Relation> ret = new ArrayList<Relation>(relations);
|
---|
304 | Collections.sort(
|
---|
305 | ret,
|
---|
306 | new Comparator<Relation>() {
|
---|
307 | public int compare(Relation o1, Relation o2) {
|
---|
308 | return Integer.valueOf(uploadOrder.indexOf(o1)).compareTo(uploadOrder.indexOf(o2));
|
---|
309 | }
|
---|
310 | }
|
---|
311 | );
|
---|
312 | return ret;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|