This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparator.js
264 lines (206 loc) · 10.1 KB
/
comparator.js
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
'use strict'
var jsmf = require('jsmf-core'),
Class = jsmf.Class, Model = jsmf.Model;
var _ =require('lodash');
//Compare models with same/similar (i.e., with same name, not necessarily full similar signature) metamodels
function compare(ModelSource,ModelTarget) {
//Compute values
//console.log(ModelSource);
const mSourceElements = ModelSource.modellingElements;
const mTargetElements = ModelTarget.modellingElements;
var mSourceMetrics = new Map();
var mTargetMetrics = new Map();
var keys = [];
var sameSimilar = [];
//comparison class by class
_.each(mSourceElements,function(elements,id){
//assuming they have the same classes;
mSourceMetrics.set(id,elements.length);
_.each(elements,function(elem){
// var currentlnormal = normalizeModelElements(elem);
_.each(mTargetElements[id], function(target) {
//Assumption: Objects have the same metamodel... should be tested before
var diff = modelElementDifference(elem,target,1);
var diffAtt = _.filter(diff,{type:"attribute"});
var diffRef = _.filter(diff,{type:"reference"});
const diflen= diff.length;
if(diflen!==0) {
//console.log('diff ',elem.name, ' : ', target.name, '/ ', diff);
//compute too "much" difference (heuristic) -> see with bayesian approach
const attributeKeys = Object.keys(elem.conformsTo().getAllAttributes());
const referenceKeys = Object.keys(elem.conformsTo().getAllReferences());
//WARNING: Heuristic, if some attributes are common =>threshold 50%?
if(diffAtt.length<=(attributeKeys.length/2) && diffRef.length<=(referenceKeys.length/2)) {
//console.log('Similar');
sameSimilar.push({type:'diff',src:elem,tgt:target,meta:id,diff:diff});
} else {
//if more than 50% of attributes are different => different modelling elements
//console.log('too much difference: probably not the same elements');
}
} else { //no differences between objects
//console.log('Same Objects');
sameSimilar.push({type:'same',src: elem, tgt: target, meta:id,diff:[]});
//Similar checked : add tuple source/target to list as similar
}
});
});
});
_.each(mTargetElements,function(elements,id){
mTargetMetrics.set(id,elements.length);
});
return ({"sourceMetrics": mSourceMetrics,"targetMetrics":mTargetMetrics});
}
function buildExample() {
var m1 = new Model('source');
var m2 = new Model('target');
var MM = Class.newInstance('MM');
MM.setAttribute('name',String);
MM.setAttribute('permission',Boolean);
var MMbis = Class.newInstance('MMbis');
MMbis.setAttribute('name',String);
MMbis.setAttribute('inv',Number);
MM.setReference('refMb', MMbis, -1);
//Model 1
var ms = MM.newInstance();
ms.name='sourceEl'
ms.permission=false;
var mrav = MM.newInstance();
mrav.name='sourceRAV';
mrav.permission=false;
var bis = MMbis.newInstance();
bis.name='bis';
bis.inv=12;
var tierce = MMbis.newInstance();
tierce.name='tierce';
tierce.inv=123;
ms.refMb=[bis,tierce];
// Model 2 compared
var mt1 = MM.newInstance();
mt1.name='targetEL'
mt1.permission=false;
var mt2= MM.newInstance();
mt2.name='sourceEl';
mt2.permission=false; // // console.log( _.difference(attributeKeys,targetKeys))console.log( _.difference(attributeKeys,targetKeys))
var mt3=MM.newInstance();
mt3.name='sourceEl';
mt3.permission=true;
var b1 = MMbis.newInstance();
b1.name='bis';
b1.inv=12;
mt2.refMb=b1;
m1.setModellingElements([ms,bis,tierce]);
m2.setModellingElements([mt1,mt2,b1]);
return {sm: m1, tm:m2};
}
/**
* Pairwise comparison of two model elements conforms to the same metamodel element.
@ param source : source model element to be compared to
@ param target : target model element to be compared.
@ param depth : relation traversal depth (to avoid circular to avoid complexe deep comparison);
@pre-condition: the two elements have common metamodel
@return an object containing the difference (can/should be a JSMF model!). Undefined if any of source or target are undefined
*/
function modelElementDifference(source,target,depth) {
//init the depth to one = checking references
var depth = depth==undefined? 1 : depth;
if(target!==undefined && source!==undefined) {
var diffObject = [];
//precond : source / target !== undefined
var attributeKeys = Object.keys(source.conformsTo().getAllAttributes());
//var targetKeys = Object.keys(source.conformsTo().getAllAttributes());
//do the same for references
var referenceKeys = Object.keys(source.conformsTo().getAllReferences());
//var referenceTargetKeys = Object.keys(source.conformsTo().getAllReferences());
_.each(attributeKeys,function(attName){
//console.log(attName, " : ",source[attName], "vs", target[attName]);
if(!(_.isEqual(source[attName],target[attName]))){
diffObject.push({name:attName,
targetValue:target[attName],
sourceValue:source[attName],
type:"attribute"
});
}
});
//do not go to deep in the object comparisons
if(depth > 0) {
_.each(referenceKeys, function(refName) {
const refSource = source[refName];
const refTarget = target[refName];
if(refSource.length!=0) {
//TODO: check the targetted types
//Compare elements one by one
var Msource = new Model();
var Mtarget = new Model();
Msource.add(refSource);
Mtarget.add(refTarget);
var oID= orderIndependentDiff(Msource,Mtarget,depth-1);
//See if relevant to have same card or not?
if(!(refSource.length==refTarget.length)) {
// console.log('Different effective cardinalities', refTarget.length);
// console.log('oid',oID);
var diffref = _.reject(oID,{type:'same'});
diffObject.push({name:refName,src:source,tgt:target, diff:diffref, type:"reference"})
//remplace the smaller one by place holder/undefined -> use the orderIndependentDiff
//
} else {
//check the elements targetted
//console.log('Same cardinality');
//console.log("oid2 ",oID);
var diffref = _.reject(oID,{type:'same'});
if(diffref.length!=0){
diffObject.push({name:refName,src:source,tgt:target, diff:diffref, type:"reference"})
}
}
} //endif : if source[refName] is defined
});
} //endif check reference (depth !== 0)
}
return diffObject;
}
//Check Similarity/difference of two models conforms to the same metamodel
function orderIndependentDiff(ModelSource,ModelTarget,depth) {
const mSourceElements = ModelSource.modellingElements;
const mTargetElements = ModelTarget.modellingElements;
var sameSimilar = [];
//comparison class by class
_.each(mSourceElements,function(elements,id){
//assuming they have the same classes
// console.log(id);
_.each(elements,function(elem){
// var currentlnormal = normalizeModelElements(elem);
_.each(mTargetElements[id], function(target) {
//Assumption: Objects have the same metamodel... should be tested before
//TODO Check the search depth..
var diff = modelElementDifference(elem,target,depth);
var diffAtt = _.filter(diff,{type:"attribute"});
// console.log('diff: ',diffAtt);
const diflen= diff.length;
if(diflen!==0) {
//console.log('diff ',elem.name, ' : ', target.name, '/ ', diff);
//compute too "much" difference (heuristic) -> see with bayesian approach
var attributeKeys = Object.keys(elem.conformsTo().getAllAttributes());
// if some attributes are common =>threshold 50%?
if(diffAtt.length<=(attributeKeys.length/2)) {
// console.log('OI-Similar');
sameSimilar.push({type:'diff',src:elem,tgt:target,meta:id,diff:diff});
} else {
//if all attributes are different => different modelling elements
// console.log('OI-DiSimilar');
sameSimilar.push({type:'largediff', src:elem, tgt:target,meta:id,diff:diff})
//console.log('too much difference: probably not the same elements');
}
} else {
// console.log('OI-same Objects');
sameSimilar.push({type:'same',src: elem, tgt: target, meta:id,diff:[]});
//Similar checked : add tuple source/target to list as similar
}
});
});
});
return sameSimilar;
}
//var comparator = buildExample();
//compare(comparator.sm, comparator.tm);
module.exports = {
compare: compare
}