Module: EMF

Defined in:
lib/emf/emf_nav.rb,
lib/emf/xmi.rb

Overview

This module permits to manipulate EObjects serialized as Hash

Class Method Summary collapse

Class Method Details

.attrs(root) ⇒ Object



21
22
23
# File 'lib/emf/emf_nav.rb', line 21

def self.attrs(root)
	root.keys.select {|k| k.start_with? 'attr_'}
end


47
48
49
50
51
52
53
54
55
# File 'lib/emf/emf_nav.rb', line 47

def self.print_tree(root,depth=0)
	traverse(root) do |n,d|
		s = ""
		d.times { s = s + "  " }
		s = s + n['type'] if n
		s = s + '<NIL>' unless n
		puts s
	end
end

.rel_conts(root) ⇒ Object



13
14
15
# File 'lib/emf/emf_nav.rb', line 13

def self.rel_conts(root)
	root.keys.select {|k| k.start_with? 'relcont_'}
end

.rel_non_conts(root) ⇒ Object



17
18
19
# File 'lib/emf/emf_nav.rb', line 17

def self.rel_non_conts(root)
	root.keys.select {|k| k.start_with? 'relcont_'}
end

.to_xmi_str(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/emf/xmi.rb', line 12

def self.to_xmi_str(data)
	if data.is_a? EObject
		resource_set = ResourceSetImpl.new 
		resource = XMIResourceImpl.new
		resource_set.resources.add resource
		resource.contents.add e_object
		to_xmi_str(resource)			
	elsif data.is_a? Resource
		writer = java.io.StringWriter.new
		data.save(writer,nil)
		writer.to_s
	else
		raise "I do not know how to save a #{data.class}"
	end			
end

.traverse(root, depth = 0, &op) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/emf/emf_nav.rb', line 31

def self.traverse(root,depth=0,&op)
	return traverse(root['root'],depth,&op) if root and (root.key? 'root')
	op.call(root,depth)
	return unless root		
	rel_conts(root).each do |r|
		if root[r].is_a? Array
			root[r].each do |c|
				raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
				traverse(c,depth+1,&op)
			end
		else
			traverse(root[r],depth+1,&op)
		end
	end
end

.values(root, feat) ⇒ Object



25
26
27
28
29
# File 'lib/emf/emf_nav.rb', line 25

def self.values(root,feat)
	raw = root[feat]
	return raw if raw.is_a? Array
	return [raw]
end