Class: ECoreXMLInstantiator

Inherits:
AbstractXMLInstantiator show all
Includes:
RGen::ECore
Defined in:
lib/rgen/instantiator/ecore_xml_instantiator.rb

Defined Under Namespace

Classes: ResolverDescription

Constant Summary collapse

INFO =
0
WARN =
1
ERROR =
2

Constants included from RGen::ECore

RGen::ECore::EBoolean, RGen::ECore::EFloat, RGen::ECore::EInt, RGen::ECore::EJavaClass, RGen::ECore::EJavaObject, RGen::ECore::ERubyClass, RGen::ECore::ERubyObject, RGen::ECore::EString

Instance Method Summary collapse

Methods included from RGen::MetamodelBuilder::ModuleExtension

#_annotations, #annotation, #final_method, #method_added

Methods included from RGen::ECore::ECoreInstantiator

clear_ecore_cache, #ecore

Methods inherited from AbstractXMLInstantiator

#text

Constructor Details

#initialize(env, loglevel = ERROR) ⇒ ECoreXMLInstantiator

Returns a new instance of ECoreXMLInstantiator.



13
14
15
16
17
18
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 13

def initialize(env, loglevel=ERROR)
  @env = env
  @rolestack = []
  @elementstack = []
  @loglevel = loglevel
end

Instance Method Details

#eAllAttributes(element) ⇒ Object



104
105
106
107
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 104

def eAllAttributes(element)
  @eAllAttributes ||= {}
  @eAllAttributes[element.class] ||= element.class.ecore.eAllAttributes
end

#eAllReferences(element) ⇒ Object



99
100
101
102
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 99

def eAllReferences(element)
  @eAllReferences ||= {}
  @eAllReferences[element.class] ||= element.class.ecore.eAllReferences
end

#eAllStructuralFeatures(element) ⇒ Object



109
110
111
112
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 109

def eAllStructuralFeatures(element)
  @eAllStructuralFeatures ||= {}
  @eAllStructuralFeatures[element.class] ||= element.class.ecore.eAllStructuralFeatures
end

#end_tag(prefix, tag) ⇒ Object



56
57
58
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 56

def end_tag(prefix, tag)
  @elementstack.pop
end

#find_in_context(context, desc_elements) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 131

def find_in_context(context, desc_elements)
  if context.is_a?(EPackage)
    r = (context.eClassifiers + context.eSubpackages).find{|c| c.name == desc_elements.first}
  elsif context.is_a?(EClass)
    r = context.eStructuralFeatures.find{|s| s.name == desc_elements.first}
  else
    raise StandardError.new("Don't know how to find #{desc_elements.join('/')} in context #{context}")
  end
  if r
    if desc_elements.size > 1
      find_in_context(r, desc_elements[1..-1])
    else
      r
    end
  else
    log WARN, "Can not follow path, element #{desc_elements.first} not found within #{context}(#{context.name})"
  end
end

#find_referenced(context, desc) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 114

def find_referenced(context, desc)
  desc.split(/\s+/).collect do |r|
    if r =~ /^#\/\d*\/([\w\/]+)/
      find_in_context(context, $1.split('/'))
    elsif r =~ /#\/\/(\w+)$/
      case $1
        when "EString";     RGen::ECore::EString
        when "EInt";        RGen::ECore::EInt
        when "EBoolean";    RGen::ECore::EBoolean
        when "EFloat";      RGen::ECore::EFloat
        when "EJavaObject"; RGen::ECore::EJavaObject
        when "EJavaClass";  RGen::ECore::EJavaClass
      end
    end
  end.compact
end

#instantiate(str) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 81

def instantiate(str)
  @resolver_descs = []
#    puts "Instantiating ..."
  super(str, 1000)
  rootpackage = @env.find(:class => EPackage).first
#    puts "Resolving ..."
  @resolver_descs.each do |rd|
    refed = find_referenced(rootpackage, rd.value)
    feature = eAllStructuralFeatures(rd.object).find{|f| f.name == rd.attribute}
    raise StandardError.new("StructuralFeature not found: #{rd.attribute}") unless feature
    if feature.many
      rd.object.setGeneric(feature.name, refed)
    else
      rd.object.setGeneric(feature.name, refed.first)
    end
  end
end

#log(level, msg) ⇒ Object



150
151
152
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 150

def log(level, msg)
  puts %w(INFO WARN ERROR)[level] + ": " + msg if level >= @loglevel
end

#set_attribute(attr, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 62

def set_attribute(attr, value)
  return unless @elementstack.last
  eFeat = eAllStructuralFeatures(@elementstack.last).find{|a| a.name == attr}
  if eFeat.is_a?(EReference)
    rd = ResolverDescription.new
    rd.object = @elementstack.last
    rd.attribute = attr
    rd.value = value
    @resolver_descs << rd
  elsif eFeat
    value = true if value == "true" && eFeat.eType == EBoolean
    value = false if value == "false" && eFeat.eType == EBoolean
    value = value.to_i if eFeat.eType == EInt
    @elementstack.last.setGeneric(attr, value)
  else
    log WARN, "Feature not found: #{attr} on #{@elementstack.last}"
  end
end

#start_tag(prefix, tag, namespaces, attributes) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rgen/instantiator/ecore_xml_instantiator.rb', line 20

def start_tag(prefix, tag, namespaces, attributes)
  eRef = nil
  if @elementstack.last
    eRef = eAllReferences(@elementstack.last).find{|r|r.name == tag}
    if eRef
      if attributes["xsi:type"] && attributes["xsi:type"] =~ /ecore:(\w+)/
        class_name = $1
        attributes.delete("xsi:type")
      else 
        class_name = eRef.eType.name
      end
    else
      raise "Reference not found: #{tag} on #{@elementstack.last}"
    end
  else
    class_name = tag
  end
  
  eClass = RGen::ECore.ecore.eClassifiers.find{|c| c.name == class_name}
  if eClass
    obj = RGen::ECore.const_get(class_name).new
    if eRef
      if eRef.many
        @elementstack.last.addGeneric(eRef.name, obj)
      else
        @elementstack.last.setGeneric(eRef.name, obj)
      end
    end
    @env << obj
    @elementstack.push obj
  else
    log WARN, "Class not found: #{class_name}"
    @elementstack.push nil
  end
end