emf_jruby 0.1.5-java → 0.1.6-java

Sign up to get free protection for your applications and to get access to all the features.
data/lib/emf/emf_nav.rb CHANGED
@@ -1,54 +1,57 @@
1
- module JSON
2
- def self.load_file(path,max_nesting=100)
3
- parse(File.read(path),{max_nesting: max_nesting})
4
- end
5
- end
6
-
7
- # This module permits to manipulate EObjects serialized
8
- # as Hash
9
- module EMF
10
-
11
- def self.rel_conts(root)
12
- root.keys.select {|k| k.start_with? 'relcont_'}
13
- end
14
-
15
- def self.rel_non_conts(root)
16
- root.keys.select {|k| k.start_with? 'relcont_'}
17
- end
18
-
19
- def self.attrs(root)
20
- root.keys.select {|k| k.start_with? 'attr_'}
21
- end
22
-
23
- def self.values(root,feat)
24
- raw = root[feat]
25
- return raw if raw.is_a? Array
26
- return [raw]
27
- end
28
-
29
- def self.traverse(root,depth=0,&op)
30
- op.call(root,depth)
31
- return unless root
32
- rel_conts(root).each do |r|
33
- if root[r].is_a? Array
34
- root[r].each do |c|
35
- raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
36
- traverse(c,depth+1,&op)
37
- end
38
- else
39
- traverse(root[r],depth+1,&op)
40
- end
41
- end
42
- end
43
-
44
- def self.print_tree(root,depth=0)
45
- traverse(root) do |n,d|
46
- s = ""
47
- d.times { s = s + " " }
48
- s = s + n['type'] if n
49
- s = s + '<NIL>' unless n
50
- puts s
51
- end
52
- end
53
-
1
+ require 'json'
2
+
3
+ module JSON
4
+ def self.load_file(path,max_nesting=100)
5
+ parse(File.read(path),{max_nesting: max_nesting})
6
+ end
7
+ end
8
+
9
+ # This module permits to manipulate EObjects serialized
10
+ # as Hash
11
+ module EMF
12
+
13
+ def self.rel_conts(root)
14
+ root.keys.select {|k| k.start_with? 'relcont_'}
15
+ end
16
+
17
+ def self.rel_non_conts(root)
18
+ root.keys.select {|k| k.start_with? 'relcont_'}
19
+ end
20
+
21
+ def self.attrs(root)
22
+ root.keys.select {|k| k.start_with? 'attr_'}
23
+ end
24
+
25
+ def self.values(root,feat)
26
+ raw = root[feat]
27
+ return raw if raw.is_a? Array
28
+ return [raw]
29
+ end
30
+
31
+ def self.traverse(root,depth=0,&op)
32
+ return traverse(root['root'],depth,&op) if root and (root.key? 'root')
33
+ op.call(root,depth)
34
+ return unless root
35
+ rel_conts(root).each do |r|
36
+ if root[r].is_a? Array
37
+ root[r].each do |c|
38
+ raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
39
+ traverse(c,depth+1,&op)
40
+ end
41
+ else
42
+ traverse(root[r],depth+1,&op)
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.print_tree(root,depth=0)
48
+ traverse(root) do |n,d|
49
+ s = ""
50
+ d.times { s = s + " " }
51
+ s = s + n['type'] if n
52
+ s = s + '<NIL>' unless n
53
+ puts s
54
+ end
55
+ end
56
+
54
57
  end
@@ -1,68 +1,68 @@
1
- # This code permit to transform EObjects in Hash objects
2
- # containing lists and single values
3
-
4
- require 'emf/ast_serialization'
5
-
6
- def qname(e_object)
7
- e_class = e_object.eClass
8
- e_package = e_class.ePackage
9
- "#{e_package.nsURI}##{e_class.name}"
10
- end
11
-
12
- def jsonize_attr_value(map,e_object,e_attr)
13
- value = e_object.eGet e_attr
14
- if e_attr.upperBound==1
15
- map["attr_#{e_attr.name}"] = value
16
- else
17
- l = []
18
- (0..(value.size-1)).each do |i|
19
- l << value.get(i)
20
- end
21
- map["attr_#{e_attr.name}"] = l
22
- end
23
- end
24
-
25
- def jsonize_ref_single_el(single_value,containment,adapters)
26
- if containment
27
- jsonize_obj(single_value,adapters)
28
- else
29
- serialization_id(single_value)
30
- end
31
- end
32
-
33
- def jsonize_ref_value(map,e_object,e_ref,adapters)
34
- value = e_object.eGet e_ref
35
-
36
- propname = "relcont_#{e_ref.name}" if e_ref.containment
37
- propname = "relnoncont_#{e_ref.name}" if not e_ref.containment
38
-
39
- if e_ref.upperBound==1
40
- map[propname] = jsonize_ref_single_el(value,e_ref.containment,adapters)
41
- else
42
- l = []
43
- (0..(value.size-1)).each do |i|
44
- l << jsonize_ref_single_el(value.get(i),e_ref.containment,adapters)
45
- end
46
- map[propname] = l
47
- end
48
- end
49
-
50
- def jsonize_obj(e_object, adapters={})
51
- if not e_object
52
- nil
53
- else
54
- map = { 'type' => qname(e_object), 'id' => serialization_id(e_object) }
55
- e_class = e_object.eClass
56
- e_class.eAllAttributes.each do |a|
57
- jsonize_attr_value(map,e_object,a)
58
- end
59
- e_class.eAllReferences.each do |r|
60
- #puts "ref #{r.name} #{r.containment}"
61
- jsonize_ref_value(map,e_object,r,adapters)
62
- end
63
- if adapters.has_key? qname(e_object)
64
- adapters[qname(e_object)].adapt(e_object,map)
65
- end
66
- map
67
- end
1
+ # This code permit to transform EObjects in Hash objects
2
+ # containing lists and single values
3
+
4
+ require 'emf/ast_serialization'
5
+
6
+ def qname(e_object)
7
+ e_class = e_object.eClass
8
+ e_package = e_class.ePackage
9
+ "#{e_package.nsURI}##{e_class.name}"
10
+ end
11
+
12
+ def jsonize_attr_value(map,e_object,e_attr)
13
+ value = e_object.eGet e_attr
14
+ if e_attr.upperBound==1
15
+ map["attr_#{e_attr.name}"] = value
16
+ else
17
+ l = []
18
+ (0..(value.size-1)).each do |i|
19
+ l << value.get(i)
20
+ end
21
+ map["attr_#{e_attr.name}"] = l
22
+ end
23
+ end
24
+
25
+ def jsonize_ref_single_el(single_value,containment,adapters)
26
+ if containment
27
+ jsonize_obj(single_value,adapters)
28
+ else
29
+ serialization_id(single_value)
30
+ end
31
+ end
32
+
33
+ def jsonize_ref_value(map,e_object,e_ref,adapters)
34
+ value = e_object.eGet e_ref
35
+
36
+ propname = "relcont_#{e_ref.name}" if e_ref.containment
37
+ propname = "relnoncont_#{e_ref.name}" if not e_ref.containment
38
+
39
+ if e_ref.upperBound==1
40
+ map[propname] = jsonize_ref_single_el(value,e_ref.containment,adapters)
41
+ else
42
+ l = []
43
+ (0..(value.size-1)).each do |i|
44
+ l << jsonize_ref_single_el(value.get(i),e_ref.containment,adapters)
45
+ end
46
+ map[propname] = l
47
+ end
48
+ end
49
+
50
+ def jsonize_obj(e_object, adapters={})
51
+ if not e_object
52
+ nil
53
+ else
54
+ map = { 'type' => qname(e_object), 'id' => serialization_id(e_object) }
55
+ e_class = e_object.eClass
56
+ e_class.eAllAttributes.each do |a|
57
+ jsonize_attr_value(map,e_object,a)
58
+ end
59
+ e_class.eAllReferences.each do |r|
60
+ #puts "ref #{r.name} #{r.containment}"
61
+ jsonize_ref_value(map,e_object,r,adapters)
62
+ end
63
+ if adapters.has_key? qname(e_object)
64
+ adapters[qname(e_object)].adapt(e_object,map)
65
+ end
66
+ map
67
+ end
68
68
  end
@@ -1,15 +1,15 @@
1
- module EObjectUtil
2
-
3
- def self.all_contents(root)
4
- contents = []
5
- root.getAllContents.each {|e| contents << e}
6
- contents
7
- end
8
-
9
- def self.only_content_of_eclass(root,eclass)
10
- selected = all_contents(root).select {|o| o.eClass.isSuperTypeOf eclass}
11
- raise "One expected, #{selected.count} found" unless selected.count == 1
12
- selected.first
13
- end
14
-
1
+ module EObjectUtil
2
+
3
+ def self.all_contents(root)
4
+ contents = []
5
+ root.getAllContents.each {|e| contents << e}
6
+ contents
7
+ end
8
+
9
+ def self.only_content_of_eclass(root,eclass)
10
+ selected = all_contents(root).select {|o| o.eClass.isSuperTypeOf eclass}
11
+ raise "One expected, #{selected.count} found" unless selected.count == 1
12
+ selected.first
13
+ end
14
+
15
15
  end
data/lib/emf/model.rb CHANGED
@@ -1,5 +1,5 @@
1
- class Model
2
- attr_accessor :root
3
- attr_accessor :name
4
- attr_accessor :external_objects
1
+ class Model
2
+ attr_accessor :root
3
+ attr_accessor :name
4
+ attr_accessor :external_objects
5
5
  end
data/lib/emf/stats.rb ADDED
@@ -0,0 +1,97 @@
1
+ require 'emf/emf_nav'
2
+
3
+ module CodeModels
4
+
5
+ class CountingMap
6
+
7
+ def initialize
8
+ @map = {}
9
+ @sum_values = 0
10
+ end
11
+
12
+ def inc(key)
13
+ @map[key] = 0 unless @map[key]
14
+ @map[key] = @map[key]+1
15
+ @sum_values += 1
16
+ end
17
+
18
+ def value(key)
19
+ @map[key] = 0 unless @map[key]
20
+ @map[key]
21
+ end
22
+
23
+ # number of times the value appeared divived by total frequency
24
+ def p(key)
25
+ @map[key].to_f/total_frequency.to_f
26
+ end
27
+
28
+ def each(&block)
29
+ @map.each(&block)
30
+ end
31
+
32
+ def total_frequency
33
+ @sum_values
34
+ end
35
+
36
+ def n_values
37
+ @map.count
38
+ end
39
+
40
+ end
41
+
42
+ def self.entropy(counting_map)
43
+ s = 0.0
44
+ counting_map.each do |k,v|
45
+ p = counting_map.p(k)
46
+ s += p*Math.log(p)
47
+ end
48
+ -s
49
+ end
50
+
51
+ def idf(n,n_docs)
52
+ Math.log(n_docs.to_f/n.to_f)
53
+ end
54
+
55
+ def combine_self(arr,&op)
56
+ for i in 0..(arr.count-2)
57
+ for j in (i+1)..(arr.count-1)
58
+ op.call(arr[i],arr[j])
59
+ end
60
+ end
61
+ end
62
+
63
+ def combine(arr1,arr2,&op)
64
+ arr1.each do |el1|
65
+ arr2.each {|el2| op.call(el1,el2)}
66
+ end
67
+ end
68
+
69
+
70
+ def self.load_models_from_dir(dir,verbose=false,max=-1)
71
+ per_type_values_map = Hash.new do |pt_hash,pt_key|
72
+ pt_hash[pt_key] = Hash.new do |pa_hash,pa_key|
73
+ pa_hash[pa_key] = CountingMap.new
74
+ end
75
+ end
76
+
77
+ n = 0
78
+ files = Dir[dir+'/**/*.json']
79
+ files = files[0..(max-1)] if max!=-1
80
+ files.each do |f|
81
+ n+=1
82
+ puts "...#{n}) #{f}" if verbose
83
+ model = ::JSON.load_file(f,max_nesting=500)
84
+ EMF.traverse(model) do |n|
85
+ if n
86
+ puts "\tnode: #{n['type']}" if verbose
87
+ EMF.attrs(n).each do |a|
88
+ puts "\t\tattr: #{a}" if verbose
89
+ per_type_values_map[n['type']][a].inc(n[a])
90
+ end
91
+ end
92
+ end
93
+ end
94
+ per_type_values_map
95
+ end
96
+
97
+ end
data/lib/emf/xmi.rb CHANGED
@@ -1,28 +1,28 @@
1
- # java_import org.eclipse.emf.ecore.resource.URIConverter
2
- java_import org.eclipse.emf.ecore.resource.ResourceSet
3
- java_import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
4
- java_import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
5
- # java_import import org.eclipse.emf.common.util.URI
6
- java_import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl
7
- java_import org.eclipse.emf.ecore.EObject
8
- java_import org.eclipse.emf.ecore.resource.Resource
9
-
10
- module EMF
11
-
12
- def self.to_xmi_str(data)
13
- if data.is_a? EObject
14
- resource_set = ResourceSetImpl.new
15
- resource = XMIResourceImpl.new
16
- resource_set.resources.add resource
17
- resource.contents.add e_object
18
- to_xmi_str(resource)
19
- elsif data.is_a? Resource
20
- writer = java.io.StringWriter.new
21
- data.save(writer,nil)
22
- writer.to_s
23
- else
24
- raise "I do not know how to save a #{data.class}"
25
- end
26
- end
27
-
1
+ # java_import org.eclipse.emf.ecore.resource.URIConverter
2
+ java_import org.eclipse.emf.ecore.resource.ResourceSet
3
+ java_import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
4
+ java_import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
5
+ # java_import import org.eclipse.emf.common.util.URI
6
+ java_import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl
7
+ java_import org.eclipse.emf.ecore.EObject
8
+ java_import org.eclipse.emf.ecore.resource.Resource
9
+
10
+ module EMF
11
+
12
+ def self.to_xmi_str(data)
13
+ if data.is_a? EObject
14
+ resource_set = ResourceSetImpl.new
15
+ resource = XMIResourceImpl.new
16
+ resource_set.resources.add resource
17
+ resource.contents.add e_object
18
+ to_xmi_str(resource)
19
+ elsif data.is_a? Resource
20
+ writer = java.io.StringWriter.new
21
+ data.save(writer,nil)
22
+ writer.to_s
23
+ else
24
+ raise "I do not know how to save a #{data.class}"
25
+ end
26
+ end
27
+
28
28
  end
data/lib/emf_jruby.rb CHANGED
@@ -1,12 +1,13 @@
1
- emf_libs = $:.select{|e| e.gsub('emf_jruby').count>0}
2
- raise "One lib containing 'emf_jruby' expected, found: #{emf_libs}" unless emf_libs.count==1
3
-
4
- Dir[emf_libs[0]+"/jars/*.jar"].each do |jar|
5
- require jar
6
- end
7
-
8
- require 'emf/ast_serialization'
9
- require 'emf/emf_to_json'
10
- require 'emf/eobject_util'
11
- require 'emf/model'
12
- require 'emf/xmi'
1
+ emf_libs = $:.select{|e| e.gsub('emf_jruby').count>0}
2
+ raise "One lib containing 'emf_jruby' expected, found: #{emf_libs}" unless emf_libs.count==1
3
+
4
+ Dir[emf_libs[0]+"/jars/*.jar"].each do |jar|
5
+ require jar
6
+ end
7
+
8
+ require 'emf/ast_serialization'
9
+ require 'emf/emf_to_json'
10
+ require 'emf/eobject_util'
11
+ require 'emf/model'
12
+ require 'emf/xmi'
13
+ require 'emf/stats'
metadata CHANGED
@@ -2,15 +2,33 @@
2
2
  name: emf_jruby
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.5
5
+ version: 0.1.6
6
6
  platform: java
7
7
  authors:
8
8
  - Federico Tomassetti
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-01 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: !binary |-
21
+ MA==
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: !binary |-
28
+ MA==
29
+ none: false
30
+ prerelease: false
31
+ type: :runtime
14
32
  description: EMF support for JRuby. Support for loading and saving models in JSon.
15
33
  email: f.tomassetti@gmail.com
16
34
  executables: []
@@ -24,6 +42,7 @@ files:
24
42
  - lib/emf/eobject_util.rb
25
43
  - lib/emf/model.rb
26
44
  - lib/emf/xmi.rb
45
+ - lib/emf/stats.rb
27
46
  - lib/jars/org.antlr.runtime_3.0.0.v200803061811.jar
28
47
  - lib/jars/org.eclipse.emf.common_2.8.0.v20130125-0546.jar
29
48
  - lib/jars/org.eclipse.emf.ecore.xmi_2.8.1.v20130125-0546.jar
@@ -36,15 +55,17 @@ require_paths:
36
55
  - lib
37
56
  required_ruby_version: !ruby/object:Gem::Requirement
38
57
  requirements:
39
- - - '>='
58
+ - - ">="
40
59
  - !ruby/object:Gem::Version
41
- version: '0'
60
+ version: !binary |-
61
+ MA==
42
62
  none: false
43
63
  required_rubygems_version: !ruby/object:Gem::Requirement
44
64
  requirements:
45
- - - '>='
65
+ - - ">="
46
66
  - !ruby/object:Gem::Version
47
- version: '0'
67
+ version: !binary |-
68
+ MA==
48
69
  none: false
49
70
  requirements: []
50
71
  rubyforge_project: