lightmodels 0.1.1-java → 0.1.2-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e868ed9d49df684c24f6d46cf9d35cfce53799f6
4
+ data.tar.gz: de01a21493b5d2086f6c8541b5d0332af366b769
5
+ SHA512:
6
+ metadata.gz: 5107453b1cc675f8ddf165b09b27ece34c01c4f75d44a7379acbdc9f389047fe1e13c3e3897c7d8539492936bae32285e633cd55f781f17a89e98515929db447
7
+ data.tar.gz: 0fd9d3b21944811c6e73763c24b925b54e9b8edd5fd4879553489b5097592a77c71cb8304509e63c6ae3720890636ebfc6e1d9a0e6df715dccb62560e63ac5fd
@@ -3,29 +3,28 @@
3
3
 
4
4
  module LightModels
5
5
 
6
- module Json
7
-
8
6
  module Query
9
7
 
10
- def rel_conts(root)
8
+ def self.rel_conts(root)
11
9
  root.keys.select {|k| k.start_with? 'relcont_'}
12
10
  end
13
11
 
14
- def rel_non_conts(root)
12
+ def self.rel_non_conts(root)
15
13
  root.keys.select {|k| k.start_with? 'relcont_'}
16
14
  end
17
15
 
18
- def attrs(root)
16
+ def self.attrs(root)
19
17
  root.keys.select {|k| k.start_with? 'attr_'}
20
18
  end
21
19
 
22
- def values(root,feat)
20
+ def self.values(root,feat)
23
21
  raw = root[feat]
22
+ return [] if raw==nil
24
23
  return raw if raw.is_a? Array
25
24
  return [raw]
26
25
  end
27
26
 
28
- def traverse(root,depth=0,&op)
27
+ def self.traverse(root,depth=0,&op)
29
28
  return traverse(root['root'],depth,&op) if root and (root.key? 'root')
30
29
  op.call(root,depth)
31
30
  return unless root
@@ -41,7 +40,7 @@ module Query
41
40
  end
42
41
  end
43
42
 
44
- def print_tree(root,depth=0)
43
+ def self.print_tree(root,depth=0)
45
44
  traverse(root) do |n,d|
46
45
  s = ""
47
46
  d.times { s = s + " " }
@@ -53,6 +52,4 @@ module Query
53
52
 
54
53
  end
55
54
 
56
- end
57
-
58
55
  end
@@ -0,0 +1,52 @@
1
+ require 'lightmodels/serialization'
2
+
3
+ module LightModels
4
+
5
+ module ModelBuilding
6
+
7
+ class << self
8
+ attr_accessor :verbose
9
+ end
10
+
11
+ def self.generate_models_in_dir(src,dest,src_extension,dest_extension,max_nesting=500,error_handler=nil,&model_creator)
12
+ puts "== #{src} -> #{dest} ==" if LightModels::ModelBuilding.verbose
13
+ Dir["#{src}/*"].each do |fd|
14
+ if File.directory? fd
15
+ basename = File.basename(fd)
16
+ generate_models_in_dir("#{src}/#{basename}","#{dest}/#{basename}",src_extension,dest_extension,max_nesting,error_handler,&model_creator)
17
+ else
18
+ if File.extname(fd)==".#{src_extension}"
19
+ translated_simple_name = "#{File.basename(fd, ".#{src_extension}")}.#{dest_extension}"
20
+ translated_name = "#{dest}/#{translated_simple_name}"
21
+ puts "* #{fd} --> #{translated_name}" if LightModels::ModelBuilding.verbose
22
+ generate_model_per_file(fd,translated_name,max_nesting,error_handler,&model_creator)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.generate_model_per_file(src,dest,max_nesting=500,error_handler=nil,&models_generator)
29
+ if not File.exist? dest
30
+ puts "<Model from #{src}>"
31
+
32
+ if error_handler
33
+ begin
34
+ m = models_generator.call(src)
35
+ LightModels::Serialization.save_model(m,dest,max_nesting)
36
+ rescue Exception => e
37
+ error_handler.call(src,e)
38
+ rescue
39
+ error_handler.call(src,nil)
40
+ end
41
+ else
42
+ m = models_generator.call(src)
43
+ LightModels::Serialization.save_model(m,dest,max_nesting)
44
+ end
45
+ else
46
+ puts "skipping #{src} because #{dest} found" if LightModels::ModelBuilding.verbose
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,36 @@
1
+ require 'set'
2
+ require 'lightmodels/jsonser_nav'
3
+
4
+ module LightModels
5
+
6
+ module Query
7
+
8
+ # the set of values appearing in the object and its children
9
+ def self.collect_values(el)
10
+ values = Set.new
11
+ rel_conts(el).each do |r|
12
+ values(el,r).each {|c| values.merge(collect_values(c))}
13
+ end
14
+ attrs(el).each do |a|
15
+ values(el,a).each {|v| values.add(v)}
16
+ end
17
+ values
18
+ end
19
+
20
+ # a counting map values appearing in the object and its children
21
+ def self.collect_values_with_count(el)
22
+ values = Hash.new {|h,k| h[k]=0}
23
+ rel_conts(el).each do |r|
24
+ LightModels::Query.values(el,r).each do |ch|
25
+ collect_values_with_count(ch).each {|v,count| values[v]+=count}
26
+ end
27
+ end
28
+ attrs(el).each do |a|
29
+ LightModels::Query.values(el,a).each {|v| values[v]+=1 }
30
+ end
31
+ values
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -2,6 +2,7 @@
2
2
  # containing lists and single values
3
3
 
4
4
  require 'json'
5
+ require 'fileutils'
5
6
 
6
7
  module LightModels
7
8
 
@@ -24,13 +25,21 @@ def self.serialization_id(obj)
24
25
  end
25
26
 
26
27
  def self.qname(e_object)
27
- e_class = e_object.eClass
28
- e_package = e_class.ePackage
29
- "#{e_package.nsURI}##{e_class.name}"
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
30
35
  end
31
36
 
32
37
  def self.jsonize_attr_value(map,e_object,e_attr)
33
- value = e_object.eGet e_attr
38
+ if e_object.respond_to? :eGet
39
+ value = e_object.eGet e_attr
40
+ else
41
+ value = e_object.send e_attr.name.to_sym
42
+ end
34
43
  if e_attr.upperBound==1
35
44
  map["attr_#{e_attr.name}"] = value
36
45
  else
@@ -51,7 +60,11 @@ def self.jsonize_ref_single_el(single_value,containment,adapters)
51
60
  end
52
61
 
53
62
  def self.jsonize_ref_value(map,e_object,e_ref,adapters)
54
- value = e_object.eGet e_ref
63
+ if e_object.respond_to? :eGet
64
+ value = e_object.eGet e_ref
65
+ else
66
+ value = e_object.send e_ref.name.to_sym
67
+ end
55
68
 
56
69
  propname = "relcont_#{e_ref.name}" if e_ref.containment
57
70
  propname = "relnoncont_#{e_ref.name}" if not e_ref.containment
@@ -61,7 +74,11 @@ def self.jsonize_ref_value(map,e_object,e_ref,adapters)
61
74
  else
62
75
  l = []
63
76
  (0..(value.size-1)).each do |i|
64
- l << jsonize_ref_single_el(value.get(i),e_ref.containment,adapters)
77
+ if value.is_a? Array
78
+ l << jsonize_ref_single_el(value.at(i),e_ref.containment,adapters)
79
+ else
80
+ l << jsonize_ref_single_el(value.get(i),e_ref.containment,adapters)
81
+ end
65
82
  end
66
83
  map[propname] = l
67
84
  end
@@ -72,7 +89,11 @@ def self.jsonize_obj(e_object, adapters={})
72
89
  nil
73
90
  else
74
91
  map = { 'type' => qname(e_object), 'id' => serialization_id(e_object) }
75
- e_class = e_object.eClass
92
+ if e_object.respond_to? :eClass
93
+ e_class = e_object.eClass
94
+ else
95
+ e_class = e_object.class.ecore
96
+ end
76
97
  e_class.eAllAttributes.each do |a|
77
98
  jsonize_attr_value(map,e_object,a)
78
99
  end
@@ -87,8 +108,8 @@ def self.jsonize_obj(e_object, adapters={})
87
108
  end
88
109
  end
89
110
 
90
- def self.load_file(path,max_nesting=100)
91
- parse(File.read(path),{max_nesting: max_nesting})
111
+ def self.load_file(path,max_nesting=500)
112
+ JSON.parse(File.read(path),{max_nesting: max_nesting})
92
113
  end
93
114
 
94
115
  def self.load_models_from_dir(dir,verbose=false,max=-1)
@@ -118,7 +139,10 @@ def self.load_models_from_dir(dir,verbose=false,max=-1)
118
139
  per_type_values_map
119
140
  end
120
141
 
121
- def self.to_model(root)
142
+ def self.eobject_to_model(root,adapters={})
143
+ @serialization_ids = {}
144
+ @next_serialization_id = 1
145
+
122
146
  model = {}
123
147
  external_elements = if root.eResource
124
148
  root.eResource.contents.select {|e| e!=root}
@@ -126,7 +150,22 @@ def self.to_model(root)
126
150
  []
127
151
  end
128
152
 
129
- model['root'] = jsonize_obj(root)
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
+ def self.rgenobject_to_model(root,adapters={})
162
+ @serialization_ids = {}
163
+ @next_serialization_id = 1
164
+
165
+ model = {}
166
+ external_elements = []
167
+
168
+ model['root'] = jsonize_obj(root,adapters)
130
169
  model['external_elements'] = []
131
170
  external_elements.each do |ee|
132
171
  model['external_elements'] << jsonize_obj(ee)
@@ -136,11 +175,15 @@ end
136
175
 
137
176
  def self.save_as_model(root,model_path)
138
177
  model = to_model(root)
178
+ save_model(model,model_path)
179
+ end
180
+
181
+ def self.save_model(model,model_path, max_nesting=500)
139
182
  dir = File.dirname(model_path)
140
183
  FileUtils.mkdir_p(dir)
141
184
 
142
185
  File.open(model_path, 'w') do |file|
143
- file.write(JSON.pretty_generate(model))
186
+ file.write(JSON.pretty_generate(model, :max_nesting => max_nesting))
144
187
  end
145
188
  end
146
189
 
metadata CHANGED
@@ -1,48 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightmodels
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.1.2
6
5
  platform: java
7
6
  authors:
8
7
  - Federico Tomassetti
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-26 00:00:00.000000000 Z
11
+ date: 2013-08-27 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: emf_jruby
16
- version_requirements: !ruby/object:Gem::Requirement
15
+ requirement: !ruby/object:Gem::Requirement
17
16
  requirements:
18
17
  - - '>='
19
18
  - !ruby/object:Gem::Version
20
19
  version: '0'
21
- none: false
22
- requirement: !ruby/object:Gem::Requirement
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- none: false
28
- prerelease: false
29
- type: :runtime
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: json
32
- version_requirements: !ruby/object:Gem::Requirement
29
+ requirement: !ruby/object:Gem::Requirement
33
30
  requirements:
34
31
  - - '>='
35
32
  - !ruby/object:Gem::Version
36
33
  version: '0'
37
- none: false
38
- requirement: !ruby/object:Gem::Requirement
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
39
37
  requirements:
40
38
  - - '>='
41
39
  - !ruby/object:Gem::Version
42
40
  version: '0'
43
- none: false
44
- prerelease: false
45
- type: :runtime
46
41
  description: Light format to store models. Mostly they are stored in Hash and Array.
47
42
  email: f.tomassetti@gmail.com
48
43
  executables: []
@@ -50,13 +45,16 @@ extensions: []
50
45
  extra_rdoc_files: []
51
46
  files:
52
47
  - lib/lightmodels.rb
53
- - ./lib/lightmodels/serialization.rb
54
- - ./lib/lightmodels/model.rb
55
48
  - ./lib/lightmodels/jsonser_nav.rb
49
+ - ./lib/lightmodels/model.rb
50
+ - ./lib/lightmodels/model_building.rb
51
+ - ./lib/lightmodels/query.rb
52
+ - ./lib/lightmodels/serialization.rb
56
53
  - ./lib/lightmodels/stats.rb
57
54
  homepage: http://federico-tomassetti.it
58
55
  licenses: []
59
- post_install_message:
56
+ metadata: {}
57
+ post_install_message:
60
58
  rdoc_options: []
61
59
  require_paths:
62
60
  - lib
@@ -65,17 +63,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
63
  - - '>='
66
64
  - !ruby/object:Gem::Version
67
65
  version: '0'
68
- none: false
69
66
  required_rubygems_version: !ruby/object:Gem::Requirement
70
67
  requirements:
71
68
  - - '>='
72
69
  - !ruby/object:Gem::Version
73
70
  version: '0'
74
- none: false
75
71
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 1.8.24
78
- signing_key:
79
- specification_version: 3
72
+ rubyforge_project:
73
+ rubygems_version: 2.0.3
74
+ signing_key:
75
+ specification_version: 4
80
76
  summary: Light format to store models
81
77
  test_files: []