File: metamodel_generator.tpl

package info (click to toggle)
ruby-rgen 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: ruby: 11,344; xml: 1,368; yacc: 72; makefile: 10
file content (172 lines) | stat: -rw-r--r-- 6,961 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

<% define 'GenerateMetamodel', :for => EPackage do |filename| %>
  <% file filename do %>
    require 'rgen/metamodel_builder'
    <%nl%>
    <% if needClassReorder? %>
      <% expand 'GeneratePackagesOnly' %>
      <% expand 'GenerateClassesReordered' %>
    <% else %>
      <% expand 'GeneratePackage' %>
    <% end %>
    <%nl%>
    <% expand 'GenerateAssocs' %>
  <% end %>
<% end %>
    
<% define 'GeneratePackage', :for => EPackage do %>
  module <%= moduleName %><% iinc %>
    extend RGen::MetamodelBuilder::ModuleExtension
    include RGen::MetamodelBuilder::DataTypes
    <% expand 'annotations::Annotations' %>
    <%nl%>
    <% expand 'EnumTypes' %>
    <% for c in ownClasses %><%nl%>
      <% expand 'ClassHeader', this, :for => c %><%iinc%>
        <% if c.abstract %>abstract<% end %>
        <% expand 'annotations::Annotations', :for => c %>
        <% expand 'Attribute', this, :foreach => c.eAttributes %>
        <%idec%>
      end
    <% end %><%nl%>
    <% for p in eSubpackages %>
      <%nl%><% expand 'GeneratePackage', :for => p %>
    <% end %><%idec%>
  end
<% end %>

<% define 'GenerateClassesReordered', :for => EPackage do %>
  <% for c in allClassesSorted %><%nl%>
    <% expand 'ClassHeaderFullyQualified', this, :for => c %><%iinc%>
      <% if c.abstract %>abstract<% end %>
      <% expand 'annotations::Annotations', :for => c %>
      <% expand 'Attribute', this, :foreach => c.eAttributes %>
      <%idec%>
    end
  <% end %><%nl%>
<% end %>

<% define 'GeneratePackagesOnly', :for => EPackage do %>
  module <%= moduleName %><% iinc %>
    extend RGen::MetamodelBuilder::ModuleExtension
    include RGen::MetamodelBuilder::DataTypes
    <% expand 'annotations::Annotations' %>
    <%nl%>
    <% expand 'EnumTypes' %>
    <% for p in eSubpackages %>
      <%nl%><% expand 'GeneratePackagesOnly', :for => p %>
    <% end %><%idec%>
  end
<% end %>

<% define 'Attribute', :for => EAttribute do |rootp| %>
  <% if upperBound == 1%>has_attr<% else %>has_many_attr<% end %> '<%= name %>', <%nows%>
  <% if eType.is_a?(EEnum) %><%nows%>
    <%= eType.qualifiedClassifierName(rootp) %><%nows%>
  <% else %><%nows%>
    <%= eType && eType.instanceClass.to_s %><%nows%>
  <% end %><%nows%>
  <% for p in RGen::MetamodelBuilder::Intermediate::Attribute.properties %>
    <% unless p == :name || (p == :upperBound && (upperBound == 1 || upperBound == -1)) || 
      RGen::MetamodelBuilder::Intermediate::Attribute.default_value(p) == getGeneric(p) %>
        , :<%=p%> => <%nows%>
        <% if getGeneric(p).is_a?(String) %>
          "<%= getGeneric(p) %>"<%nows%>
        <% elsif getGeneric(p).is_a?(Symbol) %>
          :<%= getGeneric(p) %><%nows%>
        <% else %>
          <%= getGeneric(p) %><%nows%>
        <% end %>
    <% end %>
  <% end %>
  <%ws%><% expand 'annotations::Annotations' %><%nl%>
<% end %>

<% define 'EnumTypes', :for => EPackage do %>
  <% for enum in eClassifiers.select{|c| c.is_a?(EEnum)} %>
    <%= enum.name %> = Enum.new(:name => '<%= enum.name %>', :literals =>[ <%nows%>
    <%= enum.eLiterals.collect { |lit| ":"+(lit.name =~ /^\d|\W/ ? "'"+lit.name+"'" : lit.name) }.join(', ') %> ])
  <% end %>
<% end %>

<% define 'GenerateAssocs', :for => EPackage do %>
  <% refDone = {} %>
  <% for ref in eAllClassifiers.select{|c| c.is_a?(EClass)}.eReferences %>
    <% if !refDone[ref] && ref.eOpposite %>
      <% ref = ref.eOpposite if ref.eOpposite.containment %>
      <% refDone[ref] = refDone[ref.eOpposite] = true %>
      <% if !ref.many && !ref.eOpposite.many %>
        <% if ref.containment %>
          <% expand 'Reference', "contains_one", this, :for => ref %>
        <% else %>
          <% expand 'Reference', "one_to_one", this, :for => ref %>
        <% end %>
      <% elsif !ref.many && ref.eOpposite.many %>
        <% expand 'Reference', "many_to_one", this, :for => ref %>
      <% elsif ref.many && !ref.eOpposite.many %>
        <% if ref.containment %>
          <% expand 'Reference', "contains_many", this, :for => ref %>
        <% else %>
          <% expand 'Reference', "one_to_many", this, :for => ref %>
        <% end %>
      <% elsif ref.many && ref.eOpposite.many %>
        <% expand 'Reference', "many_to_many", this, :for => ref %>
      <% end %>
      <% expand 'annotations::Annotations', :for => ref %><%nl%>
    <% elsif !refDone[ref] %>
      <% refDone[ref] = true %>
      <% if !ref.many %>
        <% if ref.containment %>
          <% expand 'Reference', "contains_one_uni", this, :for => ref %>
        <% else %>
          <% expand 'Reference', "has_one", this, :for => ref %>
        <% end %>
      <% elsif ref.many %>
        <% if ref.containment %>
          <% expand 'Reference', "contains_many_uni", this, :for => ref %>
        <% else %>
          <% expand 'Reference', "has_many", this, :for => ref %>
        <% end %>
      <% end %>
      <% expand 'annotations::Annotations', :for => ref %><%nl%>
    <% end %>
  <% end %>
<% end %>

<% define 'Reference', :for => EReference do |cmd, rootpackage| %>
  <%= eContainingClass.qualifiedClassifierName(rootpackage) %>.<%= cmd %> '<%= name %>', <%= eType && eType.qualifiedClassifierName(rootpackage) %><%nows%>
  <% if eOpposite %><%nows%>
    , '<%= eOpposite.name%>'<%nows%>
  <% end %><%nows%>
  <% pset = RGen::MetamodelBuilder::Intermediate::Reference.properties.reject{|p| p == :name || p == :upperBound || p == :containment} %>
  <% for p in pset.reject{|p| RGen::MetamodelBuilder::Intermediate::Reference.default_value(p) == getGeneric(p)} %>
      , :<%=p%> => <%=getGeneric(p)%><%nows%>
  <% end %>
  <% if eOpposite %>
    <% for p in pset.reject{|p| RGen::MetamodelBuilder::Intermediate::Reference.default_value(p) == eOpposite.getGeneric(p)} %>
        , :opposite_<%=p%> => <%=eOpposite.getGeneric(p)%><%nows%>
    <% end %>
  <% end %><%ws%>
<% end %>

<% define 'ClassHeader', :for => EClass do |rootp| %>
  class <%= classifierName %> < <% nows %>
  <% if eSuperTypes.size > 1 %><% nows %>
    RGen::MetamodelBuilder::MMMultiple(<%= eSuperTypes.collect{|t| t.qualifiedClassifierNameIfRequired(rootp)}.join(', ') %>)
  <% elsif eSuperTypes.size > 0 %><% nows %>
    <%= eSuperTypes.first.qualifiedClassifierNameIfRequired(rootp) %>
  <% else %><% nows %>
    RGen::MetamodelBuilder::MMBase
  <% end %>
<% end %>

<% define 'ClassHeaderFullyQualified', :for => EClass do |rootp| %>
  class <%= qualifiedClassifierName(rootp) %> < <% nows %>
  <% if eSuperTypes.size > 1 %><% nows %>
    RGen::MetamodelBuilder::MMMultiple(<%= eSuperTypes.collect{|t| t.qualifiedClassifierName(rootp)}.join(', ') %>)
  <% elsif eSuperTypes.size > 0 %><% nows %>
    <%= eSuperTypes.first.qualifiedClassifierName(rootp) %>
  <% else %><% nows %>
    RGen::MetamodelBuilder::MMBase
  <% end %>
<% end %>