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