source: josm/trunk/src/org/openstreetmap/josm/data/osm/DataSetMerger.java@ 2615

Last change on this file since 2615 was 2609, checked in by stoecker, 14 years ago

applied #4146 - patch by mjulius - fix downloading incomplete relation

  • Property svn:eol-style set to native
File size: 16.7 KB
Line 
1package org.openstreetmap.josm.data.osm;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.util.Collection;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12import java.util.logging.Logger;
13
14import org.openstreetmap.josm.data.conflict.ConflictCollection;
15
16/**
17 * A dataset merger which takes a target and a source dataset and merges the source data set
18 * onto the target dataset.
19 *
20 */
21public class DataSetMerger {
22 private static Logger logger = Logger.getLogger(DataSetMerger.class.getName());
23
24 /** the collection of conflicts created during merging */
25 private ConflictCollection conflicts;
26
27 /** the target dataset for merging */
28 private final DataSet targetDataSet;
29 /** the source dataset where primitives are merged from */
30 private final DataSet sourceDataSet;
31
32 /**
33 * A map of all primitives that got replaced with other primitives.
34 * Key is the primitive id in their dataset, the value is the id in my dataset
35 */
36 private Map<Long, Long> mergedMap;
37 /** a set of primitive ids for which we have to fix references (to nodes and
38 * to relation members) after the first phase of merging
39 */
40 private Set<PrimitiveId> objectsWithChildrenToMerge;
41 private Set<OsmPrimitive> deletedObjectsToUnlink;
42
43 /**
44 * constructor
45 *
46 * The visitor will merge <code>theirDataSet</code> onto <code>myDataSet</code>
47 *
48 * @param targetDataSet dataset with my primitives. Must not be null.
49 * @param sourceDataSet dataset with their primitives. Ignored, if null.
50 * @throws IllegalArgumentException thrown if myDataSet is null
51 */
52 public DataSetMerger(DataSet targetDataSet, DataSet sourceDataSet) throws IllegalArgumentException {
53 if (targetDataSet == null)
54 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "targetDataSet"));
55 this.targetDataSet = targetDataSet;
56 this.sourceDataSet = sourceDataSet;
57 conflicts = new ConflictCollection();
58 mergedMap = new HashMap<Long, Long>();
59 objectsWithChildrenToMerge = new HashSet<PrimitiveId>();
60 deletedObjectsToUnlink = new HashSet<OsmPrimitive>();
61 }
62
63 /**
64 * Merges a primitive <code>other</code> of type <P> onto my primitives.
65 *
66 * If other.id != 0 it tries to merge it with an corresponding primitive from
67 * my dataset with the same id. If this is not possible a conflict is remembered
68 * in {@see #conflicts}.
69 *
70 * If other.id == 0 it tries to find a primitive in my dataset with id == 0 which
71 * is semantically equal. If it finds one it merges its technical attributes onto
72 * my primitive.
73 *
74 * @param <P> the type of the other primitive
75 * @param source the other primitive
76 */
77 protected void mergePrimitive(OsmPrimitive source) {
78 if (!source.isNew() ) {
79 // try to merge onto a matching primitive with the same
80 // defined id
81 //
82 if (mergeById(source))
83 return;
84 //if (!source.isVisible())
85 // ignore it
86 // return;
87 } else {
88 // try to merge onto a primitive which has no id assigned
89 // yet but which is equal in its semantic attributes
90 //
91 Collection<? extends OsmPrimitive> candidates = null;
92 switch(source.getType()) {
93 case NODE: candidates = targetDataSet.getNodes(); break;
94 case WAY: candidates =targetDataSet.getWays(); break;
95 case RELATION: candidates = targetDataSet.getRelations(); break;
96 }
97 for (OsmPrimitive target : candidates) {
98 if (!target.isNew()) {
99 continue;
100 }
101 if (target.hasEqualSemanticAttributes(source)) {
102 mergedMap.put(source.getUniqueId(), target.getUniqueId());
103 if (target.isDeleted() != source.isDeleted()) {
104 // differences in deleted state have to be merged manually
105 //
106 conflicts.add(target, source);
107 } else {
108 // copy the technical attributes from other
109 // version
110 target.setVisible(source.isVisible());
111 target.setUser(source.getUser());
112 target.setTimestamp(source.getTimestamp());
113 target.setModified(source.isModified());
114 objectsWithChildrenToMerge.add(source.getPrimitiveId());
115 }
116 return;
117 }
118 }
119 }
120
121 // If we get here we didn't find a suitable primitive in
122 // the target dataset. Create a clone and add it to the target dataset.
123 //
124 OsmPrimitive target = null;
125 switch(source.getType()) {
126 case NODE: target = source.isNew() ? new Node() : new Node(source.getId()); break;
127 case WAY: target = source.isNew() ? new Way() : new Way(source.getId()); break;
128 case RELATION: target = source.isNew() ? new Relation() : new Relation(source.getId()); break;
129 }
130 target.mergeFrom(source);
131 targetDataSet.addPrimitive(target);
132 mergedMap.put(source.getUniqueId(), target.getUniqueId());
133 objectsWithChildrenToMerge.add(source.getPrimitiveId());
134 }
135
136 protected OsmPrimitive getMergeTarget(OsmPrimitive mergeSource) throws IllegalStateException{
137 Long targetId = mergedMap.get(mergeSource.getUniqueId());
138 if (targetId == null)
139 return null;
140 return targetDataSet.getPrimitiveById(targetId, mergeSource.getType());
141 }
142
143 protected void fixIncomplete(Way other) {
144 Way myWay = (Way)getMergeTarget(other);
145 if (myWay == null)
146 throw new RuntimeException(tr("Missing merge target for way with id {0}", other.getUniqueId()));
147 }
148
149 /**
150 * A way in the target dataset might be incomplete because at least one of its nodes is incomplete.
151 * The nodes might have become complete because a complete node was merged into in the
152 * merge operation.
153 *
154 * This method loops over all parent ways of such nodes and turns them into complete ways
155 * if necessary.
156 *
157 * @param other
158 */
159 protected void fixIncompleteParentWays(Node other) {
160 Node myNode = (Node)getMergeTarget(other);
161 if (myNode == null)
162 throw new RuntimeException(tr("Missing merge target for node with id {0}", other.getUniqueId()));
163 if (myNode.isIncomplete() || myNode.isDeleted() || !myNode.isVisible()) return;
164 }
165
166 /**
167 * Postprocess the dataset and fix all merged references to point to the actual
168 * data.
169 */
170 public void fixReferences() {
171 for (Way w : sourceDataSet.getWays()) {
172 if (!conflicts.hasConflictForTheir(w) && objectsWithChildrenToMerge.contains(w.getPrimitiveId())) {
173 mergeNodeList(w);
174 fixIncomplete(w);
175 }
176 }
177 for (Relation r : sourceDataSet.getRelations()) {
178 if (!conflicts.hasConflictForTheir(r) && objectsWithChildrenToMerge.contains(r.getPrimitiveId())) {
179 mergeRelationMembers(r);
180 }
181 }
182 for (OsmPrimitive source: deletedObjectsToUnlink) {
183 OsmPrimitive target = getMergeTarget(source);
184 if (target == null)
185 throw new RuntimeException(tr("Missing merge target for object with id {0}", source.getUniqueId()));
186 targetDataSet.unlinkReferencesToPrimitive(target);
187 }
188 // objectsWithChildrenToMerge also includes complete nodes which have
189 // been merged into their incomplete equivalents.
190 //
191 for (PrimitiveId id: objectsWithChildrenToMerge) {
192 if (!id.getType().equals(OsmPrimitiveType.NODE)) {
193 continue;
194 }
195 Node n = (Node)sourceDataSet.getPrimitiveById(id);
196 if (!conflicts.hasConflictForTheir(n)) {
197 fixIncompleteParentWays(n);
198 }
199 }
200
201 }
202
203 /**
204 * Merges the node list of a source way onto its target way.
205 *
206 * @param source the source way
207 * @throws IllegalStateException thrown if no target way can be found for the source way
208 * @throws IllegalStateException thrown if there isn't a target node for one of the nodes in the source way
209 *
210 */
211 private void mergeNodeList(Way source) throws IllegalStateException {
212 Way target = (Way)getMergeTarget(source);
213 if (target == null)
214 throw new IllegalStateException(tr("Missing merge target for way with id {0}", source.getUniqueId()));
215
216 List<Node> newNodes = new LinkedList<Node>();
217 for (Node sourceNode : source.getNodes()) {
218 Node targetNode = (Node)getMergeTarget(sourceNode);
219 if (targetNode != null) {
220 if (!targetNode.isDeleted() && targetNode.isVisible()) {
221 newNodes.add(targetNode);
222 } else {
223 target.setModified(true);
224 }
225 } else
226 throw new IllegalStateException(tr("Missing merge target for node with id {0}", sourceNode.getUniqueId()));
227 }
228 target.setNodes(newNodes);
229 }
230
231 /**
232 * Merges the relation members of a source relation onto the corresponding target relation.
233 * @param source the source relation
234 * @throws IllegalStateException thrown if there is no corresponding target relation
235 * @throws IllegalStateException thrown if there isn't a corresponding target object for one of the relation
236 * members in source
237 */
238 private void mergeRelationMembers(Relation source) throws IllegalStateException {
239 Relation target = (Relation) getMergeTarget(source);
240 if (target == null)
241 throw new IllegalStateException(tr("Missing merge target for relation with id {0}", source.getUniqueId()));
242 LinkedList<RelationMember> newMembers = new LinkedList<RelationMember>();
243 for (RelationMember sourceMember : source.getMembers()) {
244 OsmPrimitive targetMember = getMergeTarget(sourceMember.getMember());
245 if (targetMember == null)
246 throw new IllegalStateException(tr("Missing merge target of type {0} with id {1}", targetMember.getType(), targetMember.getUniqueId()));
247 if (! targetMember.isDeleted() && targetMember.isVisible()) {
248 RelationMember newMember = new RelationMember(sourceMember.getRole(), targetMember);
249 newMembers.add(newMember);
250 } else {
251 target.setModified(true);
252 }
253 }
254 target.setMembers(newMembers);
255 }
256
257 /**
258 * Tries to merge a primitive <code>source</code> into an existing primitive with the same id.
259 *
260 * @param source the source primitive which is to be merged into a target primitive
261 * @return true, if this method was able to merge <code>source</code> into a target object; false, otherwise
262 */
263 private boolean mergeById(OsmPrimitive source) {
264 OsmPrimitive target = targetDataSet.getPrimitiveById(source.getId(), source.getType());
265 // merge other into an existing primitive with the same id, if possible
266 //
267 if (target == null)
268 return false;
269 // found a corresponding target, remember it
270 mergedMap.put(source.getUniqueId(), target.getUniqueId());
271
272 if (target.getVersion() > source.getVersion())
273 // target.version > source.version => keep target version
274 return true;
275 if (! target.isVisible() && source.isVisible()) {
276 // should not happen
277 //
278 logger.warning(tr("Target object with id {0} and version {1} is visible although "
279 + "source object with lower version {2} is not visible. "
280 + "Can''t deal with this inconsistency. Keeping target object. ",
281 Long.toString(target.getId()),Long.toString(target.getVersion()), Long.toString(source.getVersion())
282 ));
283 } else if (target.isVisible() && ! source.isVisible()) {
284 // this is always a conflict because the user has to decide whether
285 // he wants to create a clone of its target primitive or whether he
286 // wants to purge the target from the local dataset. He can't keep it unchanged
287 // because it was deleted on the server.
288 //
289 conflicts.add(target,source);
290 } else if (target.isIncomplete() && !source.isIncomplete()) {
291 // target is incomplete, source completes it
292 // => merge source into target
293 //
294 target.mergeFrom(source);
295 objectsWithChildrenToMerge.add(source.getPrimitiveId());
296 } else if (!target.isIncomplete() && source.isIncomplete()) {
297 // target is complete and source is incomplete
298 // => keep target, it has more information already
299 //
300 } else if (target.isIncomplete() && source.isIncomplete()) {
301 // target and source are incomplete. Doesn't matter which one to
302 // take. We take target.
303 //
304 } else if (target.isDeleted() && ! source.isDeleted() && target.getVersion() == source.getVersion()) {
305 // same version, but target is deleted. Assume target takes precedence
306 // otherwise too many conflicts when refreshing from the server
307 } else if (target.isDeleted() != source.isDeleted()) {
308 // differences in deleted state have to be resolved manually. This can
309 // happen if one layer is merged onto another layer
310 //
311 conflicts.add(target,source);
312 } else if (! target.isModified() && source.isModified()) {
313 // target not modified. We can assume that source is the most recent version.
314 // clone it into target. But check first, whether source is deleted. if so,
315 // make sure that target is not referenced any more in myDataSet.
316 //
317 if (source.isDeleted()) {
318 deletedObjectsToUnlink.add(source);
319 }
320 target.mergeFrom(source);
321 objectsWithChildrenToMerge.add(source.getPrimitiveId());
322 } else if (! target.isModified() && !source.isModified() && target.getVersion() == source.getVersion()) {
323 // both not modified. Keep mine
324 //
325 } else if (! target.isModified() && !source.isModified() && target.getVersion() < source.getVersion()) {
326 // my not modified but other is newer. clone other onto mine.
327 //
328 target.mergeFrom(source);
329 objectsWithChildrenToMerge.add(source.getPrimitiveId());
330 } else if (target.isModified() && ! source.isModified() && target.getVersion() == source.getVersion()) {
331 // target is same as source but target is modified
332 // => keep target
333 } else if (! target.hasEqualSemanticAttributes(source)) {
334 // target is modified and is not semantically equal with source. Can't automatically
335 // resolve the differences
336 // => create a conflict
337 conflicts.add(target,source);
338 } else {
339 // clone from other, but keep the modified flag. mergeFrom will mainly copy
340 // technical attributes like timestamp or user information. Semantic
341 // attributes should already be equal if we get here.
342 //
343 target.mergeFrom(source);
344 target.setModified(true);
345 objectsWithChildrenToMerge.add(source.getPrimitiveId());
346 }
347 return true;
348 }
349
350 /**
351 * Runs the merge operation. Successfully merged {@see OsmPrimitive}s are in
352 * {@see #getMyDataSet()}.
353 *
354 * See {@see #getConflicts()} for a map of conflicts after the merge operation.
355 */
356 public void merge() {
357 if (sourceDataSet == null)
358 return;
359 targetDataSet.beginUpdate();
360 try {
361 for (Node node: sourceDataSet.getNodes()) {
362 mergePrimitive(node);
363 }
364 for (Way way: sourceDataSet.getWays()) {
365 mergePrimitive(way);
366 }
367 for (Relation relation: sourceDataSet.getRelations()) {
368 mergePrimitive(relation);
369 }
370 fixReferences();
371 } finally {
372 targetDataSet.endUpdate();
373 }
374 }
375
376 /**
377 * replies my dataset
378 *
379 * @return
380 */
381 public DataSet getTargetDataSet() {
382 return targetDataSet;
383 }
384
385 /**
386 * replies the map of conflicts
387 *
388 * @return the map of conflicts
389 */
390 public ConflictCollection getConflicts() {
391 return conflicts;
392 }
393}
Note: See TracBrowser for help on using the repository browser.