-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
172 lines (158 loc) · 6.53 KB
/
index.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
/**
* The MIT License (MIT)
*
* Copyright ©2015-2016 Luxembourg Institute of Science and Technology All Rights Reserved
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @author J.S. Sottet
*/
'use strict'
const _ = require('lodash')
const JSMF = require('jsmf-core')
const Class = JSMF.Class
const Model = JSMF.Model
/** Extracts a metamodel (class) from an specific (archetypal) instance and update meta class if provided
* @param {String} name : Name of the new discovered Class
* @param {Object} element : the object either JSMF or other archetype of the system.
@param metaElement {Class} (optional): a metaClass that can be updated according to the element
*/
function archetypalDiscovery(name,element, metaElement) {
// The new created Class
const result = Class.newInstance(name);
_(element).toPairs()
.map( x => {
let key = x[0]
let val = x[1];
//console.log(key, typeof(val))
switch(JSMFtypeof(val)) {
case 'string' :
result.setAttribute(key,String);
break;
case 'number' :
result.setAttribute(key,Number); //warning boolean encoded by 0.1 are Number
break;
case 'boolean' :
result.setAttribute(key,Boolean);
break;
case 'date' :
result.setAttribute(key,JSMF.Date);
break;
case 'singleReference' :
result.setReference('to'+val.conformsTo().__name,val.conformsTo(),-1)
break;
case 'anyReference' :
result.setReference('toAnyClass',JSMF.JSMFAny,-1)
break;
case 'multipleReference':
result.setReference('to'+val[0].conformsTo().__name,val[0].conformsTo(),-1)
break;
case 'undefined':
//console.log('undefined element');
break;
default :
console.log(JSMFtypeof(val),'undisclosed type')
break;
}
})
.value();
if(JSMF.isJSMFClass(metaElement)) {
updateClass(metaElement,result)
}
return result;
}
/**
@param metaElement {Class} (optional): a metaClass that can be updated according to the element
*/
function updateClass(metaClass,newMetaClass) {
metaClass.attributes = {}
_(newMetaClass.attributes).each((x,y) => {
metaClass.setAttribute(y,
x.type,
x.mandatory,
x.errorCallback)
})
metaClass.references={}
_(newMetaClass.references).each((x,y) => { console.log(x.type,x.cardinality)
metaClass.addReference(y,
x.type,
x.cardinality,
x.opposite,
x.oppositeCardinality,
x.associated,
x.errorCallback,
x.oppositeErrorCallback)
})
}
/*----------------
Utility functions
------------------*/
/** Return if an array contains only a single type of JSMF elements
* @param {Array} Arr : Array any elements
*/
function isArraySingleReference(arr) {
if(_.every(arr,JSMF.isJSMFElement)) {
const init = arr[0].conformsTo()
return _(arr).map(x => x.conformsTo())
.every(y => y===init)
} else return false;
}
/** Return a String that corresponds to the type of a value in parameter (including references targets)
* @param {Array} Arr : Array any elements
*/
function JSMFtypeof(val) {
var result = 'undefined';
switch(typeof(val)) {
case 'string' :
result ='string'
break;
case 'number' :
result = 'number'
break;
case 'boolean' :
result = 'boolean'
break;
case 'object' :
//if is JSMFObject
if(val instanceof Date) {
result = 'date'
}
if(JSMF.isJSMFElement(val)) {
result = 'singleReference'
} else {
if(_.isArray(val)) {
result = isArraySingleReference(val)?
'multipleReference'
: 'anyReference'
}
}
break;
case 'undefined':
break;
default :
break;
}
return result;
}
module.exports = {archetypalDiscovery, updateClass}