emf_jruby 0.1.7-java → 0.2.0-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,57 +1,107 @@
1
- require 'json'
1
+ # Utils methods to naviate real EMF objects
2
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
3
+ java_import org.eclipse.emf.ecore.EObject
8
4
 
9
- # This module permits to manipulate EObjects serialized
10
- # as Hash
11
- module EMF
5
+ module Java::OrgEclipseEmfEcore::EObject
12
6
 
13
- def self.rel_conts(root)
14
- root.keys.select {|k| k.start_with? 'relcont_'}
15
- end
7
+ module NavigationMethods
16
8
 
17
- def self.rel_non_conts(root)
18
- root.keys.select {|k| k.start_with? 'relcont_'}
19
- end
9
+ if not instance_methods.include? :contents
20
10
 
21
- def self.attrs(root)
22
- root.keys.select {|k| k.start_with? 'attr_'}
23
- end
11
+ def contents
12
+ method = nil
13
+ method = :eContents if respond_to?(:eContents)
14
+ method = :old_contents if respond_to?(:old_contents)
15
+ method = :getContents if respond_to?(:getContents)
16
+ raise "No method for getting contents, class: #{self.class}" unless method!=nil
17
+ send method
18
+ end
24
19
 
25
- def self.values(root,feat)
26
- raw = root[feat]
27
- return raw if raw.is_a? Array
28
- return [raw]
29
20
  end
30
21
 
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)
22
+ def contents_deep
23
+ l = []
24
+ contents.each do |c|
25
+ l << c
26
+ begin
27
+ grand_children = c.contents_deep
28
+ rescue Exception => e
29
+ raise "Problem getting children of #{c} (#{c.class}): #{e}"
30
+ end
31
+ grand_children.each do |sc|
32
+ l << sc
40
33
  end
34
+ end
35
+ l
36
+ end
37
+
38
+ def only_content_of_eclass(eclass)
39
+ Java::OrgEclipseEmfEcore::EObject::NavigationMethods.only_of_class(contents,eclass)
40
+ end
41
+
42
+ def only_content_deep_of_eclass(eclass)
43
+ Java::OrgEclipseEmfEcore::EObject::NavigationMethods.only_of_class(contents_deep,eclass)
44
+ end
45
+
46
+ def self.only_of_class(collection,eclass)
47
+ selected = collection.select {|o| o.eClass.isSuperTypeOf eclass}
48
+ case selected.count
49
+ when 0
50
+ raise EMF::LessThanExpectedFound.new
51
+ when 1
52
+ return selected.first
41
53
  else
42
- traverse(root[r],depth+1,&op)
54
+ raise EMF::MoreThanExpectedFound.new
43
55
  end
44
56
  end
57
+
45
58
  end
46
59
 
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
60
+ module ModificationMethods
61
+
62
+ def set_attr_value(attr_name,value)
63
+ a = get_attr(attr_name)
64
+ raise 'Attr not found' unless a
65
+ eSet(a,value)
66
+ end
67
+
68
+ def get_attr_value(attr_name)
69
+ eGet(get_attr(attr_name))
54
70
  end
71
+
72
+ def get_attr(attr_name)
73
+ (eClass.getEAllAttributes.select {|a| a.name==attr_name}).first
74
+ end
75
+
76
+ def get_ref(name)
77
+ (eClass.getEAllReferences.select {|r| r.name==name}).first
78
+ end
79
+
80
+ def get_ref_value(name)
81
+ eGet(get_ref(name))
82
+ end
83
+
84
+ def add_to_ref(ref_name,el)
85
+ l = eGet(get_ref(ref_name))
86
+ l.add el
87
+ end
88
+
55
89
  end
56
90
 
91
+ include NavigationMethods
92
+ include ModificationMethods
93
+
94
+ end
95
+
96
+ class Java::OrgEclipseEmfEcoreImpl::BasicEObjectImpl
97
+
98
+ include Java::OrgEclipseEmfEcore::EObject::NavigationMethods
99
+ include Java::OrgEclipseEmfEcore::EObject::ModificationMethods
100
+
101
+ end
102
+
103
+ class Java::OrgEclipseEmfEcoreResourceImpl::ResourceImpl
104
+
105
+ include Java::OrgEclipseEmfEcore::EObject::NavigationMethods
106
+
57
107
  end
@@ -0,0 +1,65 @@
1
+ # Generic utilities related to real EMF objects
2
+
3
+ java_import org.eclipse.emf.ecore.EcoreFactory
4
+ java_import org.eclipse.emf.ecore.impl.DynamicEObjectImpl
5
+ java_import org.eclipse.emf.ecore.EcorePackage
6
+
7
+ module EMF
8
+
9
+ EcoreLiterals = JavaUtilities.get_proxy_class('org.eclipse.emf.ecore.EcorePackage$Literals')
10
+
11
+ def self.create_eclass(p=nil)
12
+ if p
13
+ c = p.createEClass p.next_id
14
+ c
15
+ else
16
+ EcoreFactory.eINSTANCE.createEClass
17
+ end
18
+ end
19
+
20
+ def self.create_eattribute(name, datatype)
21
+ a = EcoreFactory.eINSTANCE.createEAttribute
22
+ a.name = name
23
+ a.etype = datatype
24
+ a
25
+ end
26
+
27
+ def self.create_eattribute_str(name)
28
+ create_eattribute(name,EcoreLiterals::ESTRING)
29
+ end
30
+
31
+ def self.create_ereference(type=nil, name=nil, params=[])
32
+ r = EcoreFactory.eINSTANCE.createEReference #(type, type.ePackage.next_id)
33
+ r.set_etype(type)
34
+ r.name = name
35
+ raise 'Cannot be both single and many' if params.include? :many and params.include? :single
36
+ raise 'Cannot be both containment and not containment' if params.include? :containment and params.include? :not_containment
37
+ containment = params.include? :containment
38
+ many = params.include? :many
39
+ r.containment = containment
40
+ r.set_upper_bound(-1) if many
41
+ r.set_upper_bound(1) unless many
42
+ r
43
+ end
44
+
45
+ def self.create_eobject(eclass)
46
+ eo = EcoreFactory.eINSTANCE.createEObject
47
+ eo.eSetClass eclass
48
+ eo
49
+ end
50
+
51
+ def self.create_epackage(name, uri)
52
+ p = EcoreFactory.eINSTANCE.createEPackage
53
+ class << p
54
+ def next_id
55
+ @next_id = 0 unless @next_id
56
+ @next_id += 1
57
+ @next_id
58
+ end
59
+ end
60
+ p.setName name
61
+ p.setNsURI uri
62
+ p
63
+ end
64
+
65
+ end
@@ -0,0 +1,33 @@
1
+ # Various exceptions used in the lib
2
+
3
+ module EMF
4
+
5
+ class MoreThanExpectedFound < Exception
6
+ end
7
+
8
+ class LessThanExpectedFound < Exception
9
+ end
10
+
11
+ class UnexistingFeature < Exception
12
+ attr_reader :feat_name
13
+ def initialize(feat_name)
14
+ @feat_name = feat_name
15
+ end
16
+ def to_s
17
+ "UnexistingFeature: '#{@feat_name}'"
18
+ end
19
+ end
20
+
21
+ class SingleAttributeRequired < Exception
22
+ def initialize(class_name,attributes)
23
+ @class_name = class_name
24
+ @attributes = attributes
25
+ end
26
+ def to_s
27
+ names = []
28
+ @attributes.each {|a| names << a.name}
29
+ "SingleAttributeRequired: '#{@class_name}', attributes: #{names.join(', ')}"
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,104 @@
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
+ if attrib.name != 'dynamic' # I have to understand this...
48
+ self_value = self.get(attrib)
49
+ other_value = other.get(attrib)
50
+ #puts "returning false on #{attrib.name}" unless self_value.eql?(other_value)
51
+ return false unless self_value == other_value
52
+ end
53
+ end
54
+ true
55
+ end
56
+
57
+ def eql?(other)
58
+ # it should ignore relations which has as opposite a containement
59
+ return false unless self.shallow_eql?(other)
60
+ self.class.ecore.eAllReferences.each do |ref|
61
+ self_value = self.get(ref)
62
+ other_value = other.get(ref)
63
+ to_ignore = ref.getEOpposite and ref.getEOpposite.containment
64
+ #puts "ignore #{self.class.name}.#{ref.name}" if to_ignore
65
+ #puts "returning false on #{attrib.name}" unless self_value.eql?(other_value)
66
+ unless to_ignore
67
+ if ref.containment
68
+ return false unless self_value == other_value
69
+ else
70
+ if (self_value.is_a? Array) or (other_value.is_a? Array)
71
+ return false unless self_value.count==other_value.count
72
+ for i in 0..(self_value.count-1)
73
+ return false unless self_value[i].shallow_eql?(other_value[i])
74
+ end
75
+ else
76
+ if self_value==nil
77
+ return false unless other_value==nil
78
+ else
79
+ return false unless self_value.shallow_eql?(other_value)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ true
86
+ end
87
+
88
+ def ==(other)
89
+ eql? other
90
+ end
91
+
92
+ def get(attr_or_ref)
93
+ getter = (attr_or_ref.name).to_sym
94
+ send getter
95
+ end
96
+
97
+ end
98
+
99
+ class << self
100
+ include ClassAddOn
101
+ end
102
+
103
+ include SingletonAddOn
104
+ end
@@ -1,24 +1,27 @@
1
- java_import org.eclipse.emf.ecore.EcoreFactory
2
- java_import org.eclipse.emf.ecore.impl.DynamicEObjectImpl
3
- java_import org.eclipse.emf.ecore.EcorePackage
1
+ # Conversion of RGen object to real EMF objects
4
2
 
5
- module EMF
3
+ require 'rgen/metamodel_builder'
6
4
 
7
- EcoreLiterals = JavaUtilities.get_proxy_class('org.eclipse.emf.ecore.EcorePackage$Literals')
5
+ module EMF
8
6
 
9
- def self.create_eclass
10
- EcoreFactory.eINSTANCE.createEClass
11
- end
7
+ # Track already converted classes
8
+ # Known the namespace and the URI
9
+ class ConversionContext
10
+ def initialize
11
+ @classes_cache = {}
12
+ end
12
13
 
13
- def self.create_eattribute
14
- EcoreFactory.eINSTANCE.createEAttribute
14
+ def convert(rgen_class)
15
+ unless @classes_cache[rgen_class]
16
+ emf_class = EMF.rgen_to_eclass(rgen_class,self)
17
+ @classes_cache[rgen_class] = emf_class
18
+ end
19
+ @classes_cache[rgen_class]
20
+ end
15
21
  end
16
22
 
17
- def self.create_eobject(eclass)
18
- DynamicEObjectImpl.new eclass
19
- end
20
23
 
21
- def self.rgen_to_eobject(rgen_obj)
24
+ def self.rgen_to_eobject(rgen_obj,context=ConversionContext.new)
22
25
  end
23
26
 
24
27
  def self.rgen_to_edatatype(rgen_datatype)
@@ -29,15 +32,52 @@ module EMF
29
32
  end
30
33
  end
31
34
 
32
- def self.rgen_to_eclass(rgen_class)
35
+ def self.rgen_to_eclass(rgen_class,context=ConversionContext.new)
36
+ if rgen_class.respond_to? :ecore
37
+ ecore = rgen_class.ecore
38
+ else
39
+ ecore = rgen_class
40
+ end
33
41
  emf_eclass = create_eclass
34
- rgen_class.ecore.getEAttributes.each do |a|
35
- emf_a = create_eattribute
36
- emf_a.name = a.name
37
- emf_a.setEType(rgen_to_edatatype(a.eType))
42
+ ecore.getEAttributes.each do |a|
43
+ emf_a = create_eattribute(a.name,rgen_to_edatatype(a.eType))
38
44
  emf_eclass.getEStructuralFeatures.add emf_a
39
45
  end
46
+ ecore.getEReferences.each do |r|
47
+ #puts "Ref #{r} #{r.name}"
48
+ emf_r = create_ereference
49
+ emf_r.name = r.name
50
+ emf_r.containment = r.containment
51
+ #emf_r.resolve_proxies = r.getResolveProxies
52
+ #emf_r.setEType(context.convert r.getEType)
53
+ emf_eclass.getEStructuralFeatures.add emf_r
54
+ end
40
55
  emf_eclass
41
56
  end
42
57
 
58
+ class RGen::MetamodelBuilder::MMBase
59
+
60
+ module EmfConversionClassMethods
61
+
62
+ def to_eclass(context=ConversionContext.new)
63
+ EMF.rgen_to_eclass(self,context)
64
+ end
65
+
66
+ end
67
+
68
+ module EmfConversionInstanceMethods
69
+
70
+ def to_eobject(context=ConversionContext.new)
71
+ EMF.rgen_to_eobject(self,context)
72
+ end
73
+
74
+ end
75
+
76
+ class << self
77
+ include EmfConversionClassMethods
78
+ end
79
+
80
+ include EmfConversionInstanceMethods
81
+ end
82
+
43
83
  end
data/lib/emf/xmi.rb CHANGED
@@ -1,28 +1,55 @@
1
- # java_import org.eclipse.emf.ecore.resource.URIConverter
1
+ # Methods to managa XMI serialization of real EMF objects
2
+
3
+ require 'rgen/metamodel_builder'
4
+
2
5
  java_import org.eclipse.emf.ecore.resource.ResourceSet
3
6
  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
7
  java_import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl
7
8
  java_import org.eclipse.emf.ecore.EObject
8
9
  java_import org.eclipse.emf.ecore.resource.Resource
9
10
 
10
11
  module EMF
11
12
 
12
- def self.to_xmi_str(data)
13
- if data.is_a? EObject
13
+ module XmiConversion
14
+
15
+ def self.to_xmi(data)
16
+ if data.is_a? EObject
17
+ eobject_to_xmi(data)
18
+ elsif data.is_a? Resource
19
+ resource_to_xmi(data)
20
+ else
21
+ raise "I do not know how to save a #{data.class}"
22
+ end
23
+ end
24
+
25
+ def self.eobject_to_xmi(eobject)
14
26
  resource_set = ResourceSetImpl.new
15
27
  resource = XMIResourceImpl.new
16
28
  resource_set.resources.add resource
17
- resource.contents.add data
18
- to_xmi_str(resource)
19
- elsif data.is_a? Resource
29
+ resource.contents.add eobject
30
+ to_xmi(resource)
31
+ end
32
+
33
+ def self.resource_to_xmi(res)
20
34
  writer = java.io.StringWriter.new
21
- data.save(writer,nil)
35
+ res.save(writer,nil)
22
36
  writer.to_s
23
- else
24
- raise "I do not know how to save a #{data.class}"
25
- end
37
+ end
38
+
39
+ end
40
+
41
+ class Java::OrgEclipseEmfEcoreImpl::BasicEObjectImpl
42
+
43
+ module XmiMethods
44
+
45
+ def to_xmi
46
+ EMF::XmiConversion::eobject_to_xmi(self)
47
+ end
48
+
49
+ end
50
+
51
+ include XmiMethods
52
+
26
53
  end
27
54
 
28
55
  end
data/lib/emf_jruby.rb CHANGED
@@ -3,10 +3,6 @@ Dir[curr_dir+"/jars/*.jar"].each do |jar|
3
3
  require jar
4
4
  end
5
5
 
6
- require 'emf/ast_serialization'
7
- require 'emf/emf_to_json'
8
- require 'emf/eobject_util'
9
- require 'emf/model'
10
- require 'emf/xmi'
11
- require 'emf/stats'
12
- require 'emf/rgen_to_emf'
6
+ Dir[curr_dir+"/emf/*.rb"].each do |rb|
7
+ require rb
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emf_jruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: java
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-03 00:00:00.000000000 Z
12
+ date: 2013-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -34,18 +34,16 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
36
  - lib/emf_jruby.rb
37
- - lib/emf/emf_to_json.rb
38
- - lib/emf/emf_nav.rb
39
- - lib/emf/ast_serialization.rb
40
- - lib/emf/eobject_util.rb
41
- - lib/emf/model.rb
42
- - lib/emf/xmi.rb
43
- - lib/emf/stats.rb
44
- - lib/emf/rgen_to_emf.rb
45
- - lib/jars/org.antlr.runtime_3.0.0.v200803061811.jar
46
- - lib/jars/org.eclipse.emf.common_2.8.0.v20130125-0546.jar
47
- - lib/jars/org.eclipse.emf.ecore.xmi_2.8.1.v20130125-0546.jar
48
- - lib/jars/org.eclipse.emf.ecore_2.8.3.v20130125-0546.jar
37
+ - ./lib/emf/xmi.rb
38
+ - ./lib/emf/rgen_to_emf.rb
39
+ - ./lib/emf/emf_nav.rb
40
+ - ./lib/emf/exceptions.rb
41
+ - ./lib/emf/emf_utils.rb
42
+ - ./lib/emf/rgen_ext.rb
43
+ - ./lib/jars/org.eclipse.emf.ecore_2.8.3.v20130125-0546.jar
44
+ - ./lib/jars/org.eclipse.emf.ecore.xmi_2.8.1.v20130125-0546.jar
45
+ - ./lib/jars/org.antlr.runtime_3.0.0.v200803061811.jar
46
+ - ./lib/jars/org.eclipse.emf.common_2.8.0.v20130125-0546.jar
49
47
  homepage: http://federico-tomassetti.it
50
48
  licenses: []
51
49
  post_install_message:
@@ -1,10 +0,0 @@
1
- $serialization_ids = {}
2
- $next_serialization_id = 1
3
-
4
- def serialization_id(obj)
5
- unless $serialization_ids[obj]
6
- $serialization_ids[obj] = $next_serialization_id
7
- $next_serialization_id += 1
8
- end
9
- $serialization_ids[obj]
10
- end
@@ -1,68 +0,0 @@
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
- end
@@ -1,15 +0,0 @@
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
- end
data/lib/emf/model.rb DELETED
@@ -1,5 +0,0 @@
1
- class Model
2
- attr_accessor :root
3
- attr_accessor :name
4
- attr_accessor :external_objects
5
- end
data/lib/emf/stats.rb DELETED
@@ -1,97 +0,0 @@
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