lightmodels 0.1.2-java → 0.2.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ # This module permits to manipulate Objects serialized
2
+ # as Hash
3
+
4
+ module LightModels
5
+
6
+ module QuerySerialized
7
+
8
+ def self.rel_conts(root)
9
+ root.keys.select {|k| k.start_with? 'relcont_'}
10
+ end
11
+
12
+ def self.rel_non_conts(root)
13
+ root.keys.select {|k| k.start_with? 'relnoncont_'}
14
+ end
15
+
16
+ def self.attrs(root)
17
+ root.keys.select {|k| k.start_with? 'attr_'}
18
+ end
19
+
20
+ def self.print_tree(root,depth=0)
21
+ traverse(root) do |n,d|
22
+ s = ""
23
+ d.times { s = s + " " }
24
+ s = s + n['type'] if n
25
+ s = s + '<NIL>' unless n
26
+ puts s
27
+ end
28
+ end
29
+
30
+ def self.values(root,feat)
31
+ raw = root[feat]
32
+ return [] if raw==nil
33
+ return raw if raw.is_a? Array
34
+ return [raw]
35
+ end
36
+
37
+ def self.traverse(root,depth=0,&op)
38
+ return traverse(root['root'],depth,&op) if root and (root.key? 'root')
39
+ op.call(root,depth)
40
+ return unless root
41
+ rel_conts(root).each do |r|
42
+ if root[r].is_a? Array
43
+ root[r].each do |c|
44
+ raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
45
+ traverse(c,depth+1,&op)
46
+ end
47
+ else
48
+ traverse(root[r],depth+1,&op)
49
+ end
50
+ end
51
+ end
52
+
53
+ # the set of values appearing in the object and its children
54
+ def self.collect_values(el)
55
+ values = Set.new
56
+ rel_conts(el).each do |r|
57
+ values(el,r).each {|c| values.merge(collect_values(c))}
58
+ end
59
+ attrs(el).each do |a|
60
+ values(el,a).each {|v| values.add(v)}
61
+ end
62
+ values
63
+ end
64
+
65
+ # a counting map values appearing in the object and its children
66
+ def self.collect_values_with_count(el)
67
+ values = Hash.new {|h,k| h[k]=0}
68
+ rel_conts(el).each do |r|
69
+ values(el,r).each do |ch|
70
+ collect_values_with_count(ch).each {|v,count| values[v]+=count}
71
+ end
72
+ end
73
+ attrs(el).each do |a|
74
+ values(el,a).each {|v| values[v]+=1 }
75
+ end
76
+ values
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,153 @@
1
+ # Extensions to RGen objects
2
+
3
+ require 'rgen/metamodel_builder'
4
+
5
+ class RGen::MetamodelBuilder::MMBase
6
+
7
+ module ClassAddOn
8
+
9
+ def build(values={})
10
+ instance = self.new
11
+ if values.is_a? Hash
12
+ values.each do |k,v|
13
+ attribute = self.ecore.eAllAttributes.find {|x| x.name==k.to_s}
14
+ reference = self.ecore.eAllReferences.find {|x| x.name==k.to_s}
15
+ raise EMF::UnexistingFeature.new(k.to_s) unless (attribute or reference)
16
+ setter = (k.to_s+'=').to_sym
17
+ instance.send setter, v
18
+ end
19
+ else
20
+ has_dynamic = false
21
+ self.ecore.eAllAttributes.each {|a| has_dynamic|=a.name=='dynamic'}
22
+ d = 0
23
+ d = 1 if has_dynamic
24
+
25
+ raise EMF::SingleAttributeRequired.new(self.ecore.name,self.ecore.eAllAttributes) if self.ecore.eAllAttributes.count!=1+d
26
+ attribute = self.ecore.eAllAttributes[0]
27
+ set_attr(instance,attribute,values)
28
+ end
29
+ instance
30
+ end
31
+
32
+ private
33
+
34
+ def set_attr(instance,attribute,value)
35
+ setter = (attribute.name+'=').to_sym
36
+ instance.send setter, value
37
+ end
38
+ end
39
+
40
+ module SingletonAddOn
41
+
42
+ # It does not check references, it is needed to avoid infinite recursion
43
+ def shallow_eql?(other)
44
+ return false if other==nil
45
+ return false unless self.class==other.class
46
+ self.class.ecore.eAllAttributes.each do |attrib|
47
+ raise "Attrib <nil> for class #{self.class.ecore.name}" unless attrib
48
+ if attrib.name != 'dynamic' # I have to understand this...
49
+ self_value = self.get(attrib)
50
+ other_value = other.get(attrib)
51
+ #puts "returning false on #{attrib.name}" unless self_value.eql?(other_value)
52
+ return false unless self_value == other_value
53
+ end
54
+ end
55
+ true
56
+ end
57
+
58
+ def eql?(other)
59
+ # it should ignore relations which has as opposite a containement
60
+ return false unless self.shallow_eql?(other)
61
+ self.class.ecore.eAllReferences.each do |ref|
62
+ self_value = self.get(ref)
63
+ other_value = other.get(ref)
64
+ to_ignore = ref.getEOpposite and ref.getEOpposite.containment
65
+ unless to_ignore
66
+ if ref.containment
67
+ return false unless self_value == other_value
68
+ else
69
+ if (self_value.is_a? Array) or (other_value.is_a? Array)
70
+ return false unless self_value.count==other_value.count
71
+ for i in 0..(self_value.count-1)
72
+ return false unless self_value[i].shallow_eql?(other_value[i])
73
+ end
74
+ else
75
+ if self_value==nil
76
+ return false unless other_value==nil
77
+ else
78
+ return false unless self_value.shallow_eql?(other_value)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ true
85
+ end
86
+
87
+ def ==(other)
88
+ eql? other
89
+ end
90
+
91
+ def get(attr_or_ref)
92
+ getter = (attr_or_ref.name).to_sym
93
+ send getter
94
+ end
95
+
96
+ def children
97
+ arr = []
98
+ ecore = self.class.ecore
99
+ ecore.eAllReferences.select {|r| r.containment}.each do |ref|
100
+ res = self.send(ref.name.to_sym)
101
+ if res.is_a? Array
102
+ arr.concat(res)
103
+ elsif res
104
+ arr << res
105
+ end
106
+ end
107
+ arr
108
+ end
109
+
110
+ def children_deep
111
+ arr = []
112
+ children.each do |c|
113
+ arr << c
114
+ arr.concat(c.children_deep)
115
+ end
116
+ arr
117
+ end
118
+
119
+ def traverse(&op)
120
+ op.call(self)
121
+ children_deep.each do |c|
122
+ op.call(c)
123
+ end
124
+ end
125
+
126
+ def children_of_type(type)
127
+ children.select {|c| c and c.is_a?(type)}
128
+ end
129
+
130
+ def children_deep_of_type(type)
131
+ children_deep.select {|c| c and c.is_a?(type)}
132
+ end
133
+
134
+ def only_child_of_type(type)
135
+ selected = children_of_type(type)
136
+ raise "Exactly one child of type #{type} expected, #{selected.count} found on #{self}" unless selected.count==1
137
+ selected[0]
138
+ end
139
+
140
+ def only_child_deep_of_type(type)
141
+ selected = children_deep_of_type(type)
142
+ raise "Exactly one child of type #{type} expected, #{selected.count} found on #{self}" unless selected.count==1
143
+ selected[0]
144
+ end
145
+
146
+ end
147
+
148
+ class << self
149
+ include ClassAddOn
150
+ end
151
+
152
+ include SingletonAddOn
153
+ end
@@ -25,13 +25,7 @@ def self.serialization_id(obj)
25
25
  end
26
26
 
27
27
  def self.qname(e_object)
28
- if e_object.respond_to? :eClass
29
- e_class = e_object.eClass
30
- e_package = e_class.ePackage
31
- "#{e_package.nsURI}##{e_class.name}"
32
- else
33
- e_object.class.to_s
34
- end
28
+ e_object.class.to_s
35
29
  end
36
30
 
37
31
  def self.jsonize_attr_value(map,e_object,e_attr)
@@ -44,8 +38,16 @@ def self.jsonize_attr_value(map,e_object,e_attr)
44
38
  map["attr_#{e_attr.name}"] = value
45
39
  else
46
40
  l = []
47
- (0..(value.size-1)).each do |i|
48
- l << value.get(i)
41
+ if value.respond_to? :size
42
+ dim = value.size-1
43
+ value.each do |e|
44
+ l << e
45
+ end
46
+ else
47
+ dim = value.count-1
48
+ (0..(dim)).each do |i|
49
+ l << value.get(i)
50
+ end
49
51
  end
50
52
  map["attr_#{e_attr.name}"] = l
51
53
  end
@@ -139,25 +141,6 @@ def self.load_models_from_dir(dir,verbose=false,max=-1)
139
141
  per_type_values_map
140
142
  end
141
143
 
142
- def self.eobject_to_model(root,adapters={})
143
- @serialization_ids = {}
144
- @next_serialization_id = 1
145
-
146
- model = {}
147
- external_elements = if root.eResource
148
- root.eResource.contents.select {|e| e!=root}
149
- else
150
- []
151
- end
152
-
153
- model['root'] = jsonize_obj(root,adapters)
154
- model['external_elements'] = []
155
- external_elements.each do |ee|
156
- model['external_elements'] << jsonize_obj(ee)
157
- end
158
- model
159
- end
160
-
161
144
  def self.rgenobject_to_model(root,adapters={})
162
145
  @serialization_ids = {}
163
146
  @next_serialization_id = 1
@@ -0,0 +1,3 @@
1
+ module LightModels
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lightmodels/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.platform = 'java'
8
+ s.name = 'lightmodels'
9
+ s.version = LightModels::VERSION
10
+ s.date = '2013-08-27'
11
+ s.summary = "Light format to store models"
12
+ s.description = "Light format to store models. Mostly they are stored in Hash and Array."
13
+ s.authors = ["Federico Tomassetti"]
14
+ s.email = 'f.tomassetti@gmail.com'
15
+ s.homepage = 'https://github.com/ftomassetti/lightmodels'
16
+ s.license = "APACHE2"
17
+
18
+ s.files = `git ls-files`.split($/)
19
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency('json')
24
+ s.add_dependency('rgen')
25
+
26
+ s.add_development_dependency "bundler", "~> 1.3"
27
+ s.add_development_dependency "rake"
28
+ s.add_development_dependency "simplecov"
29
+ end
@@ -0,0 +1,443 @@
1
+ {
2
+ "type": "http://www.emftext.org/java/members#ClassMethod",
3
+ "id": 774,
4
+ "attr_name": "setCompleted",
5
+ "relcont_layoutInformations": [
6
+
7
+ ],
8
+ "relcont_typeReference": {
9
+ "type": "http://www.emftext.org/java/types#Void",
10
+ "id": 775,
11
+ "relcont_layoutInformations": [
12
+
13
+ ]
14
+ },
15
+ "relcont_arrayDimensionsBefore": [
16
+
17
+ ],
18
+ "relcont_arrayDimensionsAfter": [
19
+
20
+ ],
21
+ "relcont_typeParameters": [
22
+
23
+ ],
24
+ "relcont_parameters": [
25
+ {
26
+ "type": "http://www.emftext.org/java/parameters#OrdinaryParameter",
27
+ "id": 776,
28
+ "attr_name": "completed",
29
+ "relcont_layoutInformations": [
30
+
31
+ ],
32
+ "relcont_typeReference": {
33
+ "type": "http://www.emftext.org/java/types#Boolean",
34
+ "id": 777,
35
+ "relcont_layoutInformations": [
36
+
37
+ ]
38
+ },
39
+ "relcont_arrayDimensionsBefore": [
40
+
41
+ ],
42
+ "relcont_arrayDimensionsAfter": [
43
+
44
+ ],
45
+ "relcont_typeArguments": [
46
+
47
+ ],
48
+ "relcont_annotationsAndModifiers": [
49
+
50
+ ]
51
+ }
52
+ ],
53
+ "relcont_exceptions": [
54
+
55
+ ],
56
+ "relcont_annotationsAndModifiers": [
57
+ {
58
+ "type": "http://www.emftext.org/java/modifiers#Public",
59
+ "id": 778,
60
+ "relcont_layoutInformations": [
61
+
62
+ ]
63
+ }
64
+ ],
65
+ "relcont_statements": [
66
+ {
67
+ "type": "http://www.emftext.org/java/statements#ExpressionStatement",
68
+ "id": 779,
69
+ "relcont_layoutInformations": [
70
+
71
+ ],
72
+ "relcont_expression": {
73
+ "type": "http://www.emftext.org/java/expressions#AssignmentExpression",
74
+ "id": 780,
75
+ "relcont_layoutInformations": [
76
+
77
+ ],
78
+ "relcont_child": {
79
+ "type": "http://www.emftext.org/java/expressions#ConditionalExpression",
80
+ "id": 781,
81
+ "relcont_layoutInformations": [
82
+
83
+ ],
84
+ "relcont_child": {
85
+ "type": "http://www.emftext.org/java/expressions#ConditionalOrExpression",
86
+ "id": 782,
87
+ "relcont_layoutInformations": [
88
+
89
+ ],
90
+ "relcont_children": [
91
+ {
92
+ "type": "http://www.emftext.org/java/expressions#ConditionalAndExpression",
93
+ "id": 783,
94
+ "relcont_layoutInformations": [
95
+
96
+ ],
97
+ "relcont_children": [
98
+ {
99
+ "type": "http://www.emftext.org/java/expressions#InclusiveOrExpression",
100
+ "id": 784,
101
+ "relcont_layoutInformations": [
102
+
103
+ ],
104
+ "relcont_children": [
105
+ {
106
+ "type": "http://www.emftext.org/java/expressions#ExclusiveOrExpression",
107
+ "id": 785,
108
+ "relcont_layoutInformations": [
109
+
110
+ ],
111
+ "relcont_children": [
112
+ {
113
+ "type": "http://www.emftext.org/java/expressions#AndExpression",
114
+ "id": 786,
115
+ "relcont_layoutInformations": [
116
+
117
+ ],
118
+ "relcont_children": [
119
+ {
120
+ "type": "http://www.emftext.org/java/expressions#EqualityExpression",
121
+ "id": 787,
122
+ "relcont_layoutInformations": [
123
+
124
+ ],
125
+ "relcont_equalityOperators": [
126
+
127
+ ],
128
+ "relcont_children": [
129
+ {
130
+ "type": "http://www.emftext.org/java/expressions#InstanceOfExpression",
131
+ "id": 788,
132
+ "relcont_layoutInformations": [
133
+
134
+ ],
135
+ "relcont_arrayDimensionsBefore": [
136
+
137
+ ],
138
+ "relcont_arrayDimensionsAfter": [
139
+
140
+ ],
141
+ "relcont_typeReference": null,
142
+ "relcont_child": {
143
+ "type": "http://www.emftext.org/java/expressions#RelationExpression",
144
+ "id": 789,
145
+ "relcont_layoutInformations": [
146
+
147
+ ],
148
+ "relcont_children": [
149
+ {
150
+ "type": "http://www.emftext.org/java/expressions#ShiftExpression",
151
+ "id": 790,
152
+ "relcont_layoutInformations": [
153
+
154
+ ],
155
+ "relcont_children": [
156
+ {
157
+ "type": "http://www.emftext.org/java/expressions#AdditiveExpression",
158
+ "id": 791,
159
+ "relcont_layoutInformations": [
160
+
161
+ ],
162
+ "relcont_children": [
163
+ {
164
+ "type": "http://www.emftext.org/java/expressions#MultiplicativeExpression",
165
+ "id": 792,
166
+ "relcont_layoutInformations": [
167
+
168
+ ],
169
+ "relcont_children": [
170
+ {
171
+ "type": "http://www.emftext.org/java/expressions#UnaryExpression",
172
+ "id": 793,
173
+ "relcont_layoutInformations": [
174
+
175
+ ],
176
+ "relcont_operators": [
177
+
178
+ ],
179
+ "relcont_child": {
180
+ "type": "http://www.emftext.org/java/expressions#SuffixUnaryModificationExpression",
181
+ "id": 794,
182
+ "relcont_layoutInformations": [
183
+
184
+ ],
185
+ "relcont_child": {
186
+ "type": "http://www.emftext.org/java/references#SelfReference",
187
+ "id": 795,
188
+ "relcont_layoutInformations": [
189
+
190
+ ],
191
+ "relcont_typeArguments": [
192
+
193
+ ],
194
+ "relcont_next": {
195
+ "type": "http://www.emftext.org/java/references#IdentifierReference",
196
+ "id": 796,
197
+ "relcont_layoutInformations": [
198
+
199
+ ],
200
+ "relcont_typeArguments": [
201
+
202
+ ],
203
+ "relcont_next": null,
204
+ "relcont_arraySelectors": [
205
+
206
+ ],
207
+ "relnoncont_target": 79
208
+ },
209
+ "relcont_arraySelectors": [
210
+
211
+ ],
212
+ "relcont_self": {
213
+ "type": "http://www.emftext.org/java/literals#This",
214
+ "id": 797,
215
+ "relcont_layoutInformations": [
216
+
217
+ ]
218
+ }
219
+ },
220
+ "relcont_operator": null
221
+ }
222
+ }
223
+ ],
224
+ "relcont_multiplicativeOperators": [
225
+
226
+ ]
227
+ }
228
+ ],
229
+ "relcont_additiveOperators": [
230
+
231
+ ]
232
+ }
233
+ ],
234
+ "relcont_shiftOperators": [
235
+
236
+ ]
237
+ }
238
+ ],
239
+ "relcont_relationOperators": [
240
+
241
+ ]
242
+ }
243
+ }
244
+ ]
245
+ }
246
+ ]
247
+ }
248
+ ]
249
+ }
250
+ ]
251
+ }
252
+ ]
253
+ }
254
+ ]
255
+ },
256
+ "relcont_expressionIf": null,
257
+ "relcont_expressionElse": null
258
+ },
259
+ "relcont_assignmentOperator": {
260
+ "type": "http://www.emftext.org/java/operators#Assignment",
261
+ "id": 798,
262
+ "relcont_layoutInformations": [
263
+
264
+ ]
265
+ },
266
+ "relcont_value": {
267
+ "type": "http://www.emftext.org/java/expressions#AssignmentExpression",
268
+ "id": 799,
269
+ "relcont_layoutInformations": [
270
+
271
+ ],
272
+ "relcont_child": {
273
+ "type": "http://www.emftext.org/java/expressions#ConditionalExpression",
274
+ "id": 800,
275
+ "relcont_layoutInformations": [
276
+
277
+ ],
278
+ "relcont_child": {
279
+ "type": "http://www.emftext.org/java/expressions#ConditionalOrExpression",
280
+ "id": 801,
281
+ "relcont_layoutInformations": [
282
+
283
+ ],
284
+ "relcont_children": [
285
+ {
286
+ "type": "http://www.emftext.org/java/expressions#ConditionalAndExpression",
287
+ "id": 802,
288
+ "relcont_layoutInformations": [
289
+
290
+ ],
291
+ "relcont_children": [
292
+ {
293
+ "type": "http://www.emftext.org/java/expressions#InclusiveOrExpression",
294
+ "id": 803,
295
+ "relcont_layoutInformations": [
296
+
297
+ ],
298
+ "relcont_children": [
299
+ {
300
+ "type": "http://www.emftext.org/java/expressions#ExclusiveOrExpression",
301
+ "id": 804,
302
+ "relcont_layoutInformations": [
303
+
304
+ ],
305
+ "relcont_children": [
306
+ {
307
+ "type": "http://www.emftext.org/java/expressions#AndExpression",
308
+ "id": 805,
309
+ "relcont_layoutInformations": [
310
+
311
+ ],
312
+ "relcont_children": [
313
+ {
314
+ "type": "http://www.emftext.org/java/expressions#EqualityExpression",
315
+ "id": 806,
316
+ "relcont_layoutInformations": [
317
+
318
+ ],
319
+ "relcont_equalityOperators": [
320
+
321
+ ],
322
+ "relcont_children": [
323
+ {
324
+ "type": "http://www.emftext.org/java/expressions#InstanceOfExpression",
325
+ "id": 807,
326
+ "relcont_layoutInformations": [
327
+
328
+ ],
329
+ "relcont_arrayDimensionsBefore": [
330
+
331
+ ],
332
+ "relcont_arrayDimensionsAfter": [
333
+
334
+ ],
335
+ "relcont_typeReference": null,
336
+ "relcont_child": {
337
+ "type": "http://www.emftext.org/java/expressions#RelationExpression",
338
+ "id": 808,
339
+ "relcont_layoutInformations": [
340
+
341
+ ],
342
+ "relcont_children": [
343
+ {
344
+ "type": "http://www.emftext.org/java/expressions#ShiftExpression",
345
+ "id": 809,
346
+ "relcont_layoutInformations": [
347
+
348
+ ],
349
+ "relcont_children": [
350
+ {
351
+ "type": "http://www.emftext.org/java/expressions#AdditiveExpression",
352
+ "id": 810,
353
+ "relcont_layoutInformations": [
354
+
355
+ ],
356
+ "relcont_children": [
357
+ {
358
+ "type": "http://www.emftext.org/java/expressions#MultiplicativeExpression",
359
+ "id": 811,
360
+ "relcont_layoutInformations": [
361
+
362
+ ],
363
+ "relcont_children": [
364
+ {
365
+ "type": "http://www.emftext.org/java/expressions#UnaryExpression",
366
+ "id": 812,
367
+ "relcont_layoutInformations": [
368
+
369
+ ],
370
+ "relcont_operators": [
371
+
372
+ ],
373
+ "relcont_child": {
374
+ "type": "http://www.emftext.org/java/expressions#SuffixUnaryModificationExpression",
375
+ "id": 813,
376
+ "relcont_layoutInformations": [
377
+
378
+ ],
379
+ "relcont_child": {
380
+ "type": "http://www.emftext.org/java/references#IdentifierReference",
381
+ "id": 814,
382
+ "relcont_layoutInformations": [
383
+
384
+ ],
385
+ "relcont_typeArguments": [
386
+
387
+ ],
388
+ "relcont_next": null,
389
+ "relcont_arraySelectors": [
390
+
391
+ ],
392
+ "relnoncont_target": 776
393
+ },
394
+ "relcont_operator": null
395
+ }
396
+ }
397
+ ],
398
+ "relcont_multiplicativeOperators": [
399
+
400
+ ]
401
+ }
402
+ ],
403
+ "relcont_additiveOperators": [
404
+
405
+ ]
406
+ }
407
+ ],
408
+ "relcont_shiftOperators": [
409
+
410
+ ]
411
+ }
412
+ ],
413
+ "relcont_relationOperators": [
414
+
415
+ ]
416
+ }
417
+ }
418
+ ]
419
+ }
420
+ ]
421
+ }
422
+ ]
423
+ }
424
+ ]
425
+ }
426
+ ]
427
+ }
428
+ ]
429
+ },
430
+ "relcont_expressionIf": null,
431
+ "relcont_expressionElse": null
432
+ },
433
+ "relcont_assignmentOperator": null,
434
+ "relcont_value": null
435
+ }
436
+ }
437
+ }
438
+ ],
439
+ "attr_getter": false,
440
+ "relnoncont_getterFor": null,
441
+ "attr_setter": true,
442
+ "relnoncont_setterFor": 79
443
+ }