[2512] | 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 |
|
---|
[2598] | 15 | import org.openstreetmap.josm.actions.upload.CyclicUploadDependencyException;
|
---|
[2979] | 16 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
| 17 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
[2512] | 18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[4534] | 19 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
[2512] | 20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[4113] | 22 | import org.openstreetmap.josm.data.osm.OsmPrimitiveComparator;
|
---|
[2979] | 23 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
[2512] | 24 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 25 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[4100] | 27 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 28 |
|
---|
| 29 | /**
|
---|
[5266] | 30 | * Represents a collection of {@link OsmPrimitive}s which should be uploaded to the
|
---|
[2512] | 31 | * API.
|
---|
[5266] | 32 | * The collection is derived from the modified primitives of an {@link DataSet} and it provides methods
|
---|
[2598] | 33 | * for sorting the objects in upload order.
|
---|
[2512] | 34 | *
|
---|
| 35 | */
|
---|
| 36 | public class APIDataSet {
|
---|
| 37 | private LinkedList<OsmPrimitive> toAdd;
|
---|
| 38 | private LinkedList<OsmPrimitive> toUpdate;
|
---|
| 39 | private LinkedList<OsmPrimitive> toDelete;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * creates a new empty data set
|
---|
| 43 | */
|
---|
| 44 | public APIDataSet() {
|
---|
| 45 | toAdd = new LinkedList<OsmPrimitive>();
|
---|
| 46 | toUpdate = new LinkedList<OsmPrimitive>();
|
---|
| 47 | toDelete = new LinkedList<OsmPrimitive>();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
| 52 | *
|
---|
| 53 | * @param ds the data set. Ignored, if null.
|
---|
| 54 | */
|
---|
| 55 | public void init(DataSet ds) {
|
---|
| 56 | if (ds == null) return;
|
---|
[5589] | 57 | init(ds.allPrimitives());
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public void init(Collection<OsmPrimitive> primitives) {
|
---|
[2512] | 61 | toAdd.clear();
|
---|
| 62 | toUpdate.clear();
|
---|
| 63 | toDelete.clear();
|
---|
| 64 |
|
---|
[5589] | 65 | for (OsmPrimitive osm :primitives) {
|
---|
[2512] | 66 | if (osm.get("josm/ignore") != null) {
|
---|
| 67 | continue;
|
---|
| 68 | }
|
---|
[3336] | 69 | if (osm.isNewOrUndeleted() && !osm.isDeleted()) {
|
---|
[2512] | 70 | toAdd.add(osm);
|
---|
| 71 | } else if (osm.isModified() && !osm.isDeleted()) {
|
---|
| 72 | toUpdate.add(osm);
|
---|
[3336] | 73 | } else if (osm.isDeleted() && !osm.isNew() && osm.isModified() && osm.isVisible()) {
|
---|
[2512] | 74 | toDelete.add(osm);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[4113] | 77 | OsmPrimitiveComparator c = new OsmPrimitiveComparator();
|
---|
| 78 | c.relationsFirst = true;
|
---|
[4874] | 79 | Collections.sort(toDelete, c);
|
---|
| 80 | Collections.sort(toAdd, c);
|
---|
| 81 | Collections.sort(toUpdate, c);
|
---|
[2512] | 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * initializes the API data set with the modified primitives in <code>ds</code>
|
---|
| 86 | *
|
---|
| 87 | * @param ds the data set. Ignored, if null.
|
---|
| 88 | */
|
---|
| 89 | public APIDataSet(DataSet ds) {
|
---|
| 90 | this();
|
---|
| 91 | init(ds);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
[2979] | 95 | * Replies true if one of the primitives to be updated or to be deleted
|
---|
| 96 | * participates in the conflict <code>conflict</code>
|
---|
[3530] | 97 | *
|
---|
[2979] | 98 | * @param conflict the conflict
|
---|
| 99 | * @return true if one of the primitives to be updated or to be deleted
|
---|
| 100 | * participates in the conflict <code>conflict</code>
|
---|
| 101 | */
|
---|
| 102 | public boolean participatesInConflict(Conflict<?> conflict) {
|
---|
| 103 | if (conflict == null) return false;
|
---|
| 104 | for (OsmPrimitive p: toUpdate) {
|
---|
| 105 | if (conflict.isParticipating(p)) return true;
|
---|
| 106 | }
|
---|
| 107 | for (OsmPrimitive p: toDelete) {
|
---|
| 108 | if (conflict.isParticipating(p)) return true;
|
---|
| 109 | }
|
---|
| 110 | return false;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Replies true if one of the primitives to be updated or to be deleted
|
---|
| 115 | * participates in at least one conflict in <code>conflicts</code>
|
---|
[3530] | 116 | *
|
---|
[2979] | 117 | * @param conflicts the collection of conflicts
|
---|
| 118 | * @return true if one of the primitives to be updated or to be deleted
|
---|
| 119 | * participates in at least one conflict in <code>conflicts</code>
|
---|
| 120 | */
|
---|
| 121 | public boolean participatesInConflict(ConflictCollection conflicts) {
|
---|
| 122 | if (conflicts == null || conflicts.isEmpty()) return false;
|
---|
| 123 | Set<PrimitiveId> idsParticipatingInConflicts = new HashSet<PrimitiveId>();
|
---|
| 124 | for (OsmPrimitive p: conflicts.getMyConflictParties()) {
|
---|
| 125 | idsParticipatingInConflicts.add(p.getPrimitiveId());
|
---|
| 126 | }
|
---|
| 127 | for (OsmPrimitive p: conflicts.getTheirConflictParties()) {
|
---|
| 128 | idsParticipatingInConflicts.add(p.getPrimitiveId());
|
---|
| 129 | }
|
---|
| 130 | for (OsmPrimitive p: toUpdate) {
|
---|
| 131 | if (idsParticipatingInConflicts.contains(p.getPrimitiveId())) return true;
|
---|
| 132 | }
|
---|
| 133 | for (OsmPrimitive p: toDelete) {
|
---|
| 134 | if (idsParticipatingInConflicts.contains(p.getPrimitiveId())) return true;
|
---|
| 135 | }
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
[2512] | 140 | * initializes the API data set with the primitives in <code>primitives</code>
|
---|
| 141 | *
|
---|
| 142 | * @param primitives the collection of primitives
|
---|
| 143 | */
|
---|
| 144 | public APIDataSet(Collection<OsmPrimitive> primitives) {
|
---|
| 145 | this();
|
---|
[5589] | 146 | init(primitives);
|
---|
[2512] | 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * Replies true if there are no primitives to upload
|
---|
| 151 | *
|
---|
| 152 | * @return true if there are no primitives to upload
|
---|
| 153 | */
|
---|
| 154 | public boolean isEmpty() {
|
---|
| 155 | return toAdd.isEmpty() && toUpdate.isEmpty() && toDelete.isEmpty();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * Replies the primitives which should be added to the OSM database
|
---|
| 160 | *
|
---|
| 161 | * @return the primitives which should be added to the OSM database
|
---|
| 162 | */
|
---|
| 163 | public List<OsmPrimitive> getPrimitivesToAdd() {
|
---|
| 164 | return toAdd;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * Replies the primitives which should be updated in the OSM database
|
---|
| 169 | *
|
---|
| 170 | * @return the primitives which should be updated in the OSM database
|
---|
| 171 | */
|
---|
| 172 | public List<OsmPrimitive> getPrimitivesToUpdate() {
|
---|
| 173 | return toUpdate;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Replies the primitives which should be deleted in the OSM database
|
---|
| 178 | *
|
---|
| 179 | * @return the primitives which should be deleted in the OSM database
|
---|
| 180 | */
|
---|
| 181 | public List<OsmPrimitive> getPrimitivesToDelete() {
|
---|
| 182 | return toDelete;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * Replies all primitives
|
---|
| 187 | *
|
---|
| 188 | * @return all primitives
|
---|
| 189 | */
|
---|
| 190 | public List<OsmPrimitive> getPrimitives() {
|
---|
| 191 | LinkedList<OsmPrimitive> ret = new LinkedList<OsmPrimitive>();
|
---|
| 192 | ret.addAll(toAdd);
|
---|
| 193 | ret.addAll(toUpdate);
|
---|
| 194 | ret.addAll(toDelete);
|
---|
| 195 | return ret;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
[2598] | 199 | * Replies the number of objects to upload
|
---|
[2711] | 200 | *
|
---|
[2598] | 201 | * @return the number of objects to upload
|
---|
| 202 | */
|
---|
| 203 | public int getSize() {
|
---|
| 204 | return toAdd.size() + toUpdate.size() + toDelete.size();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[4534] | 207 | public void removeProcessed(Collection<IPrimitive> processed) {
|
---|
[2598] | 208 | if (processed == null) return;
|
---|
| 209 | toAdd.removeAll(processed);
|
---|
| 210 | toUpdate.removeAll(processed);
|
---|
| 211 | toDelete.removeAll(processed);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
[2512] | 215 | * Adjusts the upload order for new relations. Child relations are uploaded first,
|
---|
| 216 | * parent relations second.
|
---|
| 217 | *
|
---|
| 218 | * This method detects cyclic dependencies in new relation. Relations with cyclic
|
---|
| 219 | * dependencies can't be uploaded.
|
---|
| 220 | *
|
---|
| 221 | * @throws CyclicUploadDependencyException thrown, if a cyclic dependency is detected
|
---|
| 222 | */
|
---|
| 223 | public void adjustRelationUploadOrder() throws CyclicUploadDependencyException{
|
---|
| 224 | LinkedList<OsmPrimitive> newToAdd = new LinkedList<OsmPrimitive>();
|
---|
[4100] | 225 | newToAdd.addAll(Utils.filteredCollection(toAdd, Node.class));
|
---|
| 226 | newToAdd.addAll(Utils.filteredCollection(toAdd, Way.class));
|
---|
[2512] | 227 |
|
---|
[4100] | 228 | List<Relation> relationsToAdd = new ArrayList<Relation>(Utils.filteredCollection(toAdd, Relation.class));
|
---|
[2512] | 229 | List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
|
---|
| 230 | newToAdd.addAll(noProblemRelations);
|
---|
| 231 | relationsToAdd.removeAll(noProblemRelations);
|
---|
| 232 |
|
---|
| 233 | RelationUploadDependencyGraph graph = new RelationUploadDependencyGraph(relationsToAdd);
|
---|
| 234 | newToAdd.addAll(graph.computeUploadOrder());
|
---|
| 235 | toAdd = newToAdd;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * Replies the subset of relations in <code>relations</code> which are not referring to any
|
---|
| 240 | * new relation
|
---|
| 241 | *
|
---|
| 242 | * @param relations a list of relations
|
---|
| 243 | * @return the subset of relations in <code>relations</code> which are not referring to any
|
---|
| 244 | * new relation
|
---|
| 245 | */
|
---|
| 246 | protected List<Relation> filterRelationsNotReferringToNewRelations(Collection<Relation> relations) {
|
---|
| 247 | List<Relation> ret = new LinkedList<Relation>();
|
---|
| 248 | for (Relation relation: relations) {
|
---|
| 249 | boolean refersToNewRelation = false;
|
---|
| 250 | for (RelationMember m : relation.getMembers()) {
|
---|
[3336] | 251 | if (m.isRelation() && m.getMember().isNewOrUndeleted()) {
|
---|
[2512] | 252 | refersToNewRelation = true;
|
---|
| 253 | break;
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | if (!refersToNewRelation) {
|
---|
| 257 | ret.add(relation);
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | return ret;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
[2915] | 264 | * Utility class to sort a collection of new relations with their dependencies
|
---|
[2512] | 265 | * topologically.
|
---|
| 266 | *
|
---|
| 267 | */
|
---|
[4874] | 268 | private static class RelationUploadDependencyGraph {
|
---|
[2512] | 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) {
|
---|
[3336] | 287 | if (!relation.isNewOrUndeleted() ) {
|
---|
[2512] | 288 | continue;
|
---|
| 289 | }
|
---|
| 290 | this.relations.add(relation);
|
---|
| 291 | for (RelationMember m: relation.getMembers()) {
|
---|
[3336] | 292 | if (m.isRelation() && m.getMember().isNewOrUndeleted()) {
|
---|
[2512] | 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 | }
|
---|
[4874] | 343 | );
|
---|
[2512] | 344 | return ret;
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | }
|
---|