Module: EMF

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

Overview

This module permits to manipulate EObjects serialized as Hash

Constant Summary collapse

EcoreLiterals =
JavaUtilities.get_proxy_class('org.eclipse.emf.ecore.EcorePackage$Literals')

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

.create_eattributeObject



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

def self.create_eattribute
  EcoreFactory.eINSTANCE.createEAttribute
end

.create_eclassObject



9
10
11
# File 'lib/emf/rgen_to_emf.rb', line 9

def self.create_eclass
  EcoreFactory.eINSTANCE.createEClass
end

.create_eobject(eclass) ⇒ Object



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

def self.create_eobject(eclass)
  DynamicEObjectImpl.new eclass
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

.rgen_to_eclass(rgen_class) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/emf/rgen_to_emf.rb', line 32

def self.rgen_to_eclass(rgen_class)
  emf_eclass = create_eclass
  rgen_class.ecore.getEAttributes.each do |a|      
    emf_a = create_eattribute
    emf_a.name = a.name
    emf_a.setEType(rgen_to_edatatype(a.eType))
    emf_eclass.getEStructuralFeatures.add emf_a
  end
  emf_eclass
end

.rgen_to_edatatype(rgen_datatype) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/emf/rgen_to_emf.rb', line 24

def self.rgen_to_edatatype(rgen_datatype)
  if rgen_datatype.name=='EString'
    return EcoreLiterals.ESTRING
  else
    raise "I don't know how to deal with datatype #{rgen_datatype} (name=#{rgen_datatype.name})"
  end
end

.rgen_to_eobject(rgen_obj) ⇒ Object



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

def self.rgen_to_eobject(rgen_obj)
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 data
    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