lightmodels 0.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ curr_dir = File.dirname(__FILE__)
2
+
3
+ Dir[curr_dir+"/lightmodels/*.rb"].each do |rb|
4
+ require rb
5
+ end
@@ -0,0 +1,58 @@
1
+ # This module permits to manipulate Objects serialized
2
+ # as Hash
3
+
4
+ module LightModels
5
+
6
+ module Json
7
+
8
+ module Query
9
+
10
+ def rel_conts(root)
11
+ root.keys.select {|k| k.start_with? 'relcont_'}
12
+ end
13
+
14
+ def rel_non_conts(root)
15
+ root.keys.select {|k| k.start_with? 'relcont_'}
16
+ end
17
+
18
+ def attrs(root)
19
+ root.keys.select {|k| k.start_with? 'attr_'}
20
+ end
21
+
22
+ def values(root,feat)
23
+ raw = root[feat]
24
+ return raw if raw.is_a? Array
25
+ return [raw]
26
+ end
27
+
28
+ def traverse(root,depth=0,&op)
29
+ return traverse(root['root'],depth,&op) if root and (root.key? 'root')
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 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
+
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,11 @@
1
+ module LightModels
2
+
3
+ # A light model. External objects contains a basic description (only type and attributes)
4
+ # of nodes referred by nodes in the proper model tree.
5
+ class Model
6
+ attr_accessor :root
7
+ attr_accessor :name
8
+ attr_accessor :external_objects
9
+ end
10
+
11
+ end
@@ -0,0 +1,149 @@
1
+ # This code permit to transform EObjects in Hash objects
2
+ # containing lists and single values
3
+
4
+ require 'json'
5
+
6
+ module LightModels
7
+
8
+ module Serialization
9
+
10
+ @serialization_ids = {} unless @serialization_ids
11
+ @next_serialization_id = 1 unless @next_serialization_id
12
+
13
+ class << self
14
+ attr_reader :serialization_ids
15
+ attr_accessor :next_serialization_id
16
+ end
17
+
18
+ def self.serialization_id(obj)
19
+ unless LightModels::Serialization.serialization_ids[obj]
20
+ LightModels::Serialization.serialization_ids[obj] = LightModels::Serialization.next_serialization_id
21
+ LightModels::Serialization.next_serialization_id += 1
22
+ end
23
+ LightModels::Serialization.serialization_ids[obj]
24
+ end
25
+
26
+ def self.qname(e_object)
27
+ e_class = e_object.eClass
28
+ e_package = e_class.ePackage
29
+ "#{e_package.nsURI}##{e_class.name}"
30
+ end
31
+
32
+ def self.jsonize_attr_value(map,e_object,e_attr)
33
+ value = e_object.eGet e_attr
34
+ if e_attr.upperBound==1
35
+ map["attr_#{e_attr.name}"] = value
36
+ else
37
+ l = []
38
+ (0..(value.size-1)).each do |i|
39
+ l << value.get(i)
40
+ end
41
+ map["attr_#{e_attr.name}"] = l
42
+ end
43
+ end
44
+
45
+ def self.jsonize_ref_single_el(single_value,containment,adapters)
46
+ if containment
47
+ jsonize_obj(single_value,adapters)
48
+ else
49
+ serialization_id(single_value)
50
+ end
51
+ end
52
+
53
+ def self.jsonize_ref_value(map,e_object,e_ref,adapters)
54
+ value = e_object.eGet e_ref
55
+
56
+ propname = "relcont_#{e_ref.name}" if e_ref.containment
57
+ propname = "relnoncont_#{e_ref.name}" if not e_ref.containment
58
+
59
+ if e_ref.upperBound==1
60
+ map[propname] = jsonize_ref_single_el(value,e_ref.containment,adapters)
61
+ else
62
+ l = []
63
+ (0..(value.size-1)).each do |i|
64
+ l << jsonize_ref_single_el(value.get(i),e_ref.containment,adapters)
65
+ end
66
+ map[propname] = l
67
+ end
68
+ end
69
+
70
+ def self.jsonize_obj(e_object, adapters={})
71
+ if not e_object
72
+ nil
73
+ else
74
+ map = { 'type' => qname(e_object), 'id' => serialization_id(e_object) }
75
+ e_class = e_object.eClass
76
+ e_class.eAllAttributes.each do |a|
77
+ jsonize_attr_value(map,e_object,a)
78
+ end
79
+ e_class.eAllReferences.each do |r|
80
+ #puts "ref #{r.name} #{r.containment}"
81
+ jsonize_ref_value(map,e_object,r,adapters)
82
+ end
83
+ if adapters.has_key? qname(e_object)
84
+ adapters[qname(e_object)].adapt(e_object,map)
85
+ end
86
+ map
87
+ end
88
+ end
89
+
90
+ def self.load_file(path,max_nesting=100)
91
+ parse(File.read(path),{max_nesting: max_nesting})
92
+ end
93
+
94
+ def self.load_models_from_dir(dir,verbose=false,max=-1)
95
+ per_type_values_map = Hash.new do |pt_hash,pt_key|
96
+ pt_hash[pt_key] = Hash.new do |pa_hash,pa_key|
97
+ pa_hash[pa_key] = CountingMap.new
98
+ end
99
+ end
100
+
101
+ n = 0
102
+ files = Dir[dir+'/**/*.json']
103
+ files = files[0..(max-1)] if max!=-1
104
+ files.each do |f|
105
+ n+=1
106
+ puts "...#{n}) #{f}" if verbose
107
+ model = ::JSON.load_file(f,max_nesting=500)
108
+ EMF.traverse(model) do |n|
109
+ if n
110
+ puts "\tnode: #{n['type']}" if verbose
111
+ EMF.attrs(n).each do |a|
112
+ puts "\t\tattr: #{a}" if verbose
113
+ per_type_values_map[n['type']][a].inc(n[a])
114
+ end
115
+ end
116
+ end
117
+ end
118
+ per_type_values_map
119
+ end
120
+
121
+ def self.to_model(root)
122
+ model = {}
123
+ external_elements = if root.eResource
124
+ root.eResource.contents.select {|e| e!=root}
125
+ else
126
+ []
127
+ end
128
+
129
+ model['root'] = jsonize_obj(root)
130
+ model['external_elements'] = []
131
+ external_elements.each do |ee|
132
+ model['external_elements'] << jsonize_obj(ee)
133
+ end
134
+ model
135
+ end
136
+
137
+ def self.save_as_model(root,model_path)
138
+ model = to_model(root)
139
+ dir = File.dirname(model_path)
140
+ FileUtils.mkdir_p(dir)
141
+
142
+ File.open(model_path, 'w') do |file|
143
+ file.write(JSON.pretty_generate(model))
144
+ end
145
+ end
146
+
147
+ end # module
148
+
149
+ end # module
@@ -0,0 +1,69 @@
1
+ require 'emf_jruby'
2
+
3
+ module LightModels
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
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightmodels
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: java
7
+ authors:
8
+ - Federico Tomassetti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: emf_jruby
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ prerelease: false
45
+ type: :runtime
46
+ description: Light format to store models. Mostly they are stored in Hash and Array.
47
+ email: f.tomassetti@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/lightmodels.rb
53
+ - ./lib/lightmodels/serialization.rb
54
+ - ./lib/lightmodels/model.rb
55
+ - ./lib/lightmodels/jsonser_nav.rb
56
+ - ./lib/lightmodels/stats.rb
57
+ homepage: http://federico-tomassetti.it
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ none: false
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ none: false
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.24
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Light format to store models
81
+ test_files: []