/
GraphComparer.cpp
274 lines (224 loc) · 9.39 KB
/
GraphComparer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "GraphComparer.h"
#include <algorithm>
#include "ientity.h"
#include "i18n.h"
#include "itextstream.h"
#include "iselectiongroup.h"
#include "icomparablenode.h"
#include "math/Hash.h"
#include "scenelib.h"
#include "string/string.h"
#include "command/ExecutionNotPossible.h"
#include "NodeUtils.h"
namespace scene
{
namespace merge
{
ComparisonResult::Ptr GraphComparer::Compare(const IMapRootNodePtr& source, const IMapRootNodePtr& base)
{
auto result = std::make_shared<ComparisonResult>(source, base);
auto sourceEntities = NodeUtils::CollectEntityFingerprints(source);
auto baseEntities = NodeUtils::CollectEntityFingerprints(base);
// Filter out all the matching nodes and store them in the result
if (sourceEntities.empty())
{
// Cannot merge the source without any entities in it
throw cmd::ExecutionNotPossible(_("The source map doesn't contain any entities, cannot merge"));
}
EntityMismatchByName sourceMismatches;
for (const auto& sourceEntity : sourceEntities)
{
// Check each source node for an equivalent node in the base
auto matchingBaseNode = baseEntities.find(sourceEntity.first);
if (matchingBaseNode != baseEntities.end())
{
// Found an equivalent node
result->equivalentEntities.emplace_back(ComparisonResult::Match{ sourceEntity.first, sourceEntity.second, matchingBaseNode->second });
}
else
{
auto entityName = NodeUtils::GetEntityName(sourceEntity.second);
sourceMismatches.emplace(entityName, EntityMismatch{ sourceEntity.first, sourceEntity.second, entityName });
}
}
EntityMismatchByName baseMismatches;
for (const auto& baseEntity : baseEntities)
{
// Check each source node for an equivalent node in the base
// Matching nodes have already been checked in the above loop
if (sourceEntities.count(baseEntity.first) == 0)
{
auto entityName = NodeUtils::GetEntityName(baseEntity.second);
baseMismatches.emplace(entityName, EntityMismatch{ baseEntity.first, baseEntity.second, entityName });
}
}
// Enter the second stage and try to match entities and detailing diffs
processDifferingEntities(*result, sourceMismatches, baseMismatches);
return result;
}
void GraphComparer::processDifferingEntities(ComparisonResult& result, const EntityMismatchByName& sourceMismatches, const EntityMismatchByName& baseMismatches)
{
// Find all entities that are missing in either source or base (by name)
std::list<EntityMismatchByName::value_type> missingInSource;
std::list<EntityMismatchByName::value_type> missingInBase;
std::list<EntityMismatchByName::value_type> matchingByName;
auto compareEntityNames = [](const EntityMismatchByName::value_type& left, const EntityMismatchByName::value_type& right)
{
return left.first < right.first;
};
// Calculate intersection and two-way exclusives
std::set_intersection(sourceMismatches.begin(), sourceMismatches.end(), baseMismatches.begin(), baseMismatches.end(),
std::back_inserter(matchingByName), compareEntityNames);
std::set_difference(sourceMismatches.begin(), sourceMismatches.end(), baseMismatches.begin(), baseMismatches.end(),
std::back_inserter(missingInBase), compareEntityNames);
std::set_difference(baseMismatches.begin(), baseMismatches.end(), sourceMismatches.begin(), sourceMismatches.end(),
std::back_inserter(missingInSource), compareEntityNames);
for (const auto& match : matchingByName)
{
auto sourceMismatch = sourceMismatches.find(match.second.entityName)->second;
auto baseMismatch = baseMismatches.find(match.second.entityName)->second;
auto& entityDiff = result.differingEntities.emplace_back(ComparisonResult::EntityDifference
{
sourceMismatch.node,
baseMismatch.node,
match.second.entityName,
sourceMismatch.fingerPrint,
baseMismatch.fingerPrint,
ComparisonResult::EntityDifference::Type::EntityPresentButDifferent
});
// Analyse the key values
entityDiff.differingKeyValues = compareKeyValues(sourceMismatch.node, baseMismatch.node);
// Analyse the child nodes
entityDiff.differingChildren = compareChildNodes(sourceMismatch.node, baseMismatch.node);
}
for (const auto& mismatch : missingInSource)
{
result.differingEntities.emplace_back(ComparisonResult::EntityDifference
{
INodePtr(), // source node is empty
mismatch.second.node,
mismatch.second.entityName,
std::string(),// source fingerprint is empty
mismatch.second.fingerPrint, // base fingerprint
ComparisonResult::EntityDifference::Type::EntityMissingInSource
});
}
for (const auto& mismatch : missingInBase)
{
result.differingEntities.emplace_back(ComparisonResult::EntityDifference
{
mismatch.second.node,
INodePtr(), // base node is empty
mismatch.second.entityName,
mismatch.second.fingerPrint, // source fingerprint
std::string(),// base fingerprint is empty
ComparisonResult::EntityDifference::Type::EntityMissingInBase
});
}
}
namespace
{
using KeyValueMap = std::map<std::string, std::string, string::ILess>;
inline KeyValueMap loadKeyValues(const INodePtr& entityNode)
{
KeyValueMap result;
auto entity = Node_getEntity(entityNode);
entity->forEachKeyValue([&](const std::string& key, const std::string& value)
{
result.emplace(key, value);
}, false);
return result;
}
}
std::list<ComparisonResult::KeyValueDifference> GraphComparer::compareKeyValues(
const INodePtr& sourceNode, const INodePtr& baseNode)
{
std::list<ComparisonResult::KeyValueDifference> result;
auto sourceKeyValues = loadKeyValues(sourceNode);
auto baseKeyValues = loadKeyValues(baseNode);
string::ILess icmp;
auto compareKeysNoCase = [&](const KeyValueMap::value_type& left, const KeyValueMap::value_type& right)
{
return icmp(left.first, right.first);
};
std::vector<KeyValueMap::value_type> missingInSource;
std::vector<KeyValueMap::value_type> missingInBase;
std::vector<KeyValueMap::value_type> presentInBoth;
std::set_intersection(sourceKeyValues.begin(), sourceKeyValues.end(),
baseKeyValues.begin(), baseKeyValues.end(), std::back_inserter(presentInBoth), compareKeysNoCase);
std::set_difference(sourceKeyValues.begin(), sourceKeyValues.end(),
baseKeyValues.begin(), baseKeyValues.end(), std::back_inserter(missingInBase), compareKeysNoCase);
std::set_difference(baseKeyValues.begin(), baseKeyValues.end(),
sourceKeyValues.begin(), sourceKeyValues.end(), std::back_inserter(missingInSource), compareKeysNoCase);
for (const auto& pair : missingInBase)
{
result.emplace_back(ComparisonResult::KeyValueDifference
{
pair.first,
pair.second,
ComparisonResult::KeyValueDifference::Type::KeyValueAdded
});
}
for (const auto& pair : missingInSource)
{
result.emplace_back(ComparisonResult::KeyValueDifference
{
pair.first,
pair.second,
ComparisonResult::KeyValueDifference::Type::KeyValueRemoved
});
}
// Compare each value which is present on both entities
for (const auto& pair : presentInBoth)
{
if (sourceKeyValues[pair.first] == baseKeyValues[pair.first])
{
continue;
}
result.emplace_back(ComparisonResult::KeyValueDifference
{
pair.first,
pair.second,
ComparisonResult::KeyValueDifference::Type::KeyValueChanged
});
}
return result;
}
std::list<ComparisonResult::PrimitiveDifference> GraphComparer::compareChildNodes(
const INodePtr& sourceNode, const INodePtr& baseNode)
{
std::list<ComparisonResult::PrimitiveDifference> result;
auto sourceChildren = NodeUtils::CollectPrimitiveFingerprints(sourceNode);
auto baseChildren = NodeUtils::CollectPrimitiveFingerprints(baseNode);
std::vector<Fingerprints::value_type> missingInSource;
std::vector<Fingerprints::value_type> missingInBase;
auto compareFingerprint = [](const Fingerprints::value_type& left, const Fingerprints::value_type& right)
{
return left.first < right.first;
};
std::set_difference(sourceChildren.begin(), sourceChildren.end(),
baseChildren.begin(), baseChildren.end(), std::back_inserter(missingInBase), compareFingerprint);
std::set_difference(baseChildren.begin(), baseChildren.end(),
sourceChildren.begin(), sourceChildren.end(), std::back_inserter(missingInSource), compareFingerprint);
for (const auto& pair : missingInBase)
{
result.emplace_back(ComparisonResult::PrimitiveDifference
{
pair.first,
pair.second,
ComparisonResult::PrimitiveDifference::Type::PrimitiveAdded
});
}
for (const auto& pair : missingInSource)
{
result.emplace_back(ComparisonResult::PrimitiveDifference
{
pair.first,
pair.second,
ComparisonResult::PrimitiveDifference::Type::PrimitiveRemoved
});
}
return result;
}
}
}