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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
# RGen Framework
# (c) Martin Thiede, 2006
require 'erb'
require 'rgen/metamodel_builder/intermediate/feature'
module RGen
module MetamodelBuilder
# This module provides methods which can be used to setup a metamodel element.
# The module is used to +extend+ MetamodelBuilder::MMBase, i.e. add the module's
# methods as class methods.
#
# MetamodelBuilder::MMBase should be used as a start for new metamodel elements.
# See MetamodelBuilder for an example.
#
module BuilderExtensions
include Util::NameHelper
class FeatureBlockEvaluator
def self.eval(block, props1, props2=nil)
return unless block
e = self.new(props1, props2)
e.instance_eval(&block)
end
def initialize(props1, props2)
@props1, @props2 = props1, props2
end
def annotation(hash)
@props1.annotations << Intermediate::Annotation.new(hash)
end
def opposite_annotation(hash)
raise "No opposite available" unless @props2
@props2.annotations << Intermediate::Annotation.new(hash)
end
end
# Add an attribute which can hold a single value.
# 'role' specifies the name which is used to access the attribute.
# 'target_class' specifies the type of objects which can be held by this attribute.
# If no target class is given, String will be default.
#
# This class method adds the following instance methods, where 'role' is to be
# replaced by the given role name:
# class#role # getter
# class#role=(value) # setter
def has_attr(role, target_class=nil, raw_props={}, &block)
props = Intermediate::Attribute.new(target_class, _ownProps(raw_props).merge({
:name=>role, :upperBound=>1}))
raise "No opposite available" unless _oppositeProps(raw_props).empty?
FeatureBlockEvaluator.eval(block, props)
_build_internal(props)
end
# Add an attribute which can hold multiple values.
# 'role' specifies the name which is used to access the attribute.
# 'target_class' specifies the type of objects which can be held by this attribute.
# If no target class is given, String will be default.
#
# This class method adds the following instance methods, where 'role' is to be
# replaced by the given role name:
# class#addRole(value, index=-1)
# class#removeRole(value)
# class#role # getter, returns an array
# class#role= # setter, sets multiple values at once
# Note that the first letter of the role name is turned into an uppercase
# for the add and remove methods.
def has_many_attr(role, target_class=nil, raw_props={}, &block)
props = Intermediate::Attribute.new(target_class, _setManyUpperBound(_ownProps(raw_props).merge({
:name=>role})))
raise "No opposite available" unless _oppositeProps(raw_props).empty?
FeatureBlockEvaluator.eval(block, props)
_build_internal(props)
end
# Add a single unidirectional association.
# 'role' specifies the name which is used to access the association.
# 'target_class' specifies the type of objects which can be held by this association.
#
# This class method adds the following instance methods, where 'role' is to be
# replaced by the given role name:
# class#role # getter
# class#role=(value) # setter
#
def has_one(role, target_class=nil, raw_props={}, &block)
props = Intermediate::Reference.new(target_class, _ownProps(raw_props).merge({
:name=>role, :upperBound=>1, :containment=>false}))
raise "No opposite available" unless _oppositeProps(raw_props).empty?
FeatureBlockEvaluator.eval(block, props)
_build_internal(props)
end
# Add an unidirectional _many_ association.
# 'role' specifies the name which is used to access the attribute.
# 'target_class' is optional and can be used to fix the type of objects which
# can be referenced by this association.
#
# This class method adds the following instance methods, where 'role' is to be
# replaced by the given role name:
# class#addRole(value, index=-1)
# class#removeRole(value)
# class#role # getter, returns an array
# Note that the first letter of the role name is turned into an uppercase
# for the add and remove methods.
#
def has_many(role, target_class=nil, raw_props={}, &block)
props = Intermediate::Reference.new(target_class, _setManyUpperBound(_ownProps(raw_props).merge({
:name=>role, :containment=>false})))
raise "No opposite available" unless _oppositeProps(raw_props).empty?
FeatureBlockEvaluator.eval(block, props)
_build_internal(props)
end
def contains_one_uni(role, target_class=nil, raw_props={}, &block)
props = Intermediate::Reference.new(target_class, _ownProps(raw_props).merge({
:name=>role, :upperBound=>1, :containment=>true}))
raise "No opposite available" unless _oppositeProps(raw_props).empty?
FeatureBlockEvaluator.eval(block, props)
_build_internal(props)
end
def contains_many_uni(role, target_class=nil, raw_props={}, &block)
props = Intermediate::Reference.new(target_class, _setManyUpperBound(_ownProps(raw_props).merge({
:name=>role, :containment=>true})))
raise "No opposite available" unless _oppositeProps(raw_props).empty?
FeatureBlockEvaluator.eval(block, props)
_build_internal(props)
end
# Add a bidirectional one-to-many association between two classes.
# The class this method is called on is refered to as _own_class_ in
# the following.
#
# Instances of own_class can use 'own_role' to access _many_ associated instances
# of type 'target_class'. Instances of 'target_class' can use 'target_role' to
# access _one_ associated instance of own_class.
#
# This class method adds the following instance methods where 'ownRole' and
# 'targetRole' are to be replaced by the given role names:
# own_class#addOwnRole(value, index=-1)
# own_class#removeOwnRole(value)
# own_class#ownRole
# target_class#targetRole
# target_class#targetRole=(value)
# Note that the first letter of the role name is turned into an uppercase
# for the add and remove methods.
#
# When an element is added/set on either side, this element also receives the element
# is is added to as a new element.
#
def one_to_many(target_role, target_class, own_role, raw_props={}, &block)
props1 = Intermediate::Reference.new(target_class, _setManyUpperBound(_ownProps(raw_props).merge({
:name=>target_role, :containment=>false})))
props2 = Intermediate::Reference.new(self, _oppositeProps(raw_props).merge({
:name=>own_role, :upperBound=>1, :containment=>false}))
FeatureBlockEvaluator.eval(block, props1, props2)
_build_internal(props1, props2)
end
def contains_many(target_role, target_class, own_role, raw_props={}, &block)
props1 = Intermediate::Reference.new(target_class, _setManyUpperBound(_ownProps(raw_props).merge({
:name=>target_role, :containment=>true})))
props2 = Intermediate::Reference.new(self, _oppositeProps(raw_props).merge({
:name=>own_role, :upperBound=>1, :containment=>false}))
FeatureBlockEvaluator.eval(block, props1, props2)
_build_internal(props1, props2)
end
# This is the inverse of one_to_many provided for convenience.
def many_to_one(target_role, target_class, own_role, raw_props={}, &block)
props1 = Intermediate::Reference.new(target_class, _ownProps(raw_props).merge({
:name=>target_role, :upperBound=>1, :containment=>false}))
props2 = Intermediate::Reference.new(self, _setManyUpperBound(_oppositeProps(raw_props).merge({
:name=>own_role, :containment=>false})))
FeatureBlockEvaluator.eval(block, props1, props2)
_build_internal(props1, props2)
end
# Add a bidirectional many-to-many association between two classes.
# The class this method is called on is refered to as _own_class_ in
# the following.
#
# Instances of own_class can use 'own_role' to access _many_ associated instances
# of type 'target_class'. Instances of 'target_class' can use 'target_role' to
# access _many_ associated instances of own_class.
#
# This class method adds the following instance methods where 'ownRole' and
# 'targetRole' are to be replaced by the given role names:
# own_class#addOwnRole(value, index=-1)
# own_class#removeOwnRole(value)
# own_class#ownRole
# target_class#addTargetRole
# target_class#removeTargetRole=(value)
# target_class#targetRole
# Note that the first letter of the role name is turned into an uppercase
# for the add and remove methods.
#
# When an element is added on either side, this element also receives the element
# is is added to as a new element.
#
def many_to_many(target_role, target_class, own_role, raw_props={}, &block)
props1 = Intermediate::Reference.new(target_class, _setManyUpperBound(_ownProps(raw_props).merge({
:name=>target_role, :containment=>false})))
props2 = Intermediate::Reference.new(self, _setManyUpperBound(_oppositeProps(raw_props).merge({
:name=>own_role, :containment=>false})))
FeatureBlockEvaluator.eval(block, props1, props2)
_build_internal(props1, props2)
end
# Add a bidirectional one-to-one association between two classes.
# The class this method is called on is refered to as _own_class_ in
# the following.
#
# Instances of own_class can use 'own_role' to access _one_ associated instance
# of type 'target_class'. Instances of 'target_class' can use 'target_role' to
# access _one_ associated instance of own_class.
#
# This class method adds the following instance methods where 'ownRole' and
# 'targetRole' are to be replaced by the given role names:
# own_class#ownRole
# own_class#ownRole=(value)
# target_class#targetRole
# target_class#targetRole=(value)
#
# When an element is set on either side, this element also receives the element
# is is added to as the new element.
#
def one_to_one(target_role, target_class, own_role, raw_props={}, &block)
props1 = Intermediate::Reference.new(target_class, _ownProps(raw_props).merge({
:name=>target_role, :upperBound=>1, :containment=>false}))
props2 = Intermediate::Reference.new(self, _oppositeProps(raw_props).merge({
:name=>own_role, :upperBound=>1, :containment=>false}))
FeatureBlockEvaluator.eval(block, props1, props2)
_build_internal(props1, props2)
end
def contains_one(target_role, target_class, own_role, raw_props={}, &block)
props1 = Intermediate::Reference.new(target_class, _ownProps(raw_props).merge({
:name=>target_role, :upperBound=>1, :containment=>true}))
props2 = Intermediate::Reference.new(self, _oppositeProps(raw_props).merge({
:name=>own_role, :upperBound=>1, :containment=>false}))
FeatureBlockEvaluator.eval(block, props1, props2)
_build_internal(props1, props2)
end
def _metamodel_description # :nodoc:
@metamodel_description ||= []
end
def _add_metamodel_description(desc) # :nodoc
@metamodel_description ||= []
@metamodelDescriptionByName ||= {}
@metamodel_description.delete(@metamodelDescriptionByName[desc.value(:name)])
@metamodel_description << desc
@metamodelDescriptionByName[desc.value(:name)] = desc
end
def abstract
@abstract = true
end
def _abstract_class
@abstract || false
end
def inherited(c)
c.send(:include, c.const_set(:ClassModule, Module.new))
MetamodelBuilder::ConstantOrderHelper.classCreated(c)
end
protected
# Central builder method
#
def _build_internal(props1, props2=nil)
_add_metamodel_description(props1)
if props1.many?
_build_many_methods(props1, props2)
else
_build_one_methods(props1, props2)
end
if props2
# this is a bidirectional reference
props1.opposite, props2.opposite = props2, props1
other_class = props1.impl_type
other_class._add_metamodel_description(props2)
raise "Internal error: second description must be a reference description" \
unless props2.reference?
if props2.many?
other_class._build_many_methods(props2, props1)
else
other_class._build_one_methods(props2, props1)
end
end
end
# To-One association methods
#
def _build_one_methods(props, other_props=nil)
name = props.value(:name)
other_role = other_props && other_props.value(:name)
if props.value(:derived)
build_derived_method(name, props, :one)
else
@@one_read_builder ||= ERB.new <<-CODE
def get<%= firstToUpper(name) %>
<% if !props.reference? && props.value(:defaultValueLiteral) %>
<% defVal = props.value(:defaultValueLiteral) %>
<% check_default_value_literal(defVal, props) %>
<% defVal = '"'+defVal+'"' if props.impl_type == String %>
<% defVal = ':'+defVal if props.impl_type.is_a?(DataTypes::Enum) && props.impl_type != DataTypes::Boolean %>
(defined? @<%= name %>) ? @<%= name %> : <%= defVal %>
<% else %>
@<%= name %>
<% end %>
end
<% if name != "class" %>
alias <%= name %> get<%= firstToUpper(name) %>
<% else %>
def getGeneric(role)
send("get\#{firstToUpper(role.to_s)}")
end
<% end %>
CODE
self::ClassModule.module_eval(@@one_read_builder.result(binding))
end
if props.value(:changeable)
@@one_write_builder ||= ERB.new <<-CODE
def set<%= firstToUpper(name) %>(val)
return if (defined? @<%= name %>) && val == @<%= name %>
<%= type_check_code("val", props) %>
oldval = @<%= name %>
@<%= name %> = val
<% if other_role %>
oldval._unregister<%= firstToUpper(other_role) %>(self) unless oldval.nil? || oldval.is_a?(MMGeneric)
val._register<%= firstToUpper(other_role) %>(self) unless val.nil? || val.is_a?(MMGeneric)
<% end %>
<% if props.reference? && props.value(:containment) %>
val._set_container(self, :<%= name %>) unless val.nil?
oldval._set_container(nil, nil) unless oldval.nil?
<% end %>
end
alias <%= name %>= set<%= firstToUpper(name) %>
def _register<%= firstToUpper(name) %>(val)
<% if other_role %>
@<%= name %>._unregister<%= firstToUpper(other_role) %>(self) unless @<%= name %>.nil? || @<%= name %>.is_a?(MMGeneric)
<% end %>
<% if props.reference? && props.value(:containment) %>
@<%= name %>._set_container(nil, nil) unless @<%= name %>.nil?
val._set_container(self, :<%= name %>) unless val.nil?
<% end %>
@<%= name %> = val
end
def _unregister<%= firstToUpper(name) %>(val)
<% if props.reference? && props.value(:containment) %>
@<%= name %>._set_container(nil, nil) unless @<%= name %>.nil?
<% end %>
@<%= name %> = nil
end
CODE
self::ClassModule.module_eval(@@one_write_builder.result(binding))
end
end
# To-Many association methods
#
def _build_many_methods(props, other_props=nil)
name = props.value(:name)
other_role = other_props && other_props.value(:name)
if props.value(:derived)
build_derived_method(name, props, :many)
else
@@many_read_builder ||= ERB.new <<-CODE
def get<%= firstToUpper(name) %>
( defined?(@<%= name %>) ? @<%= name %>.dup : [] )
end
<% if name != "class" %>
alias <%= name %> get<%= firstToUpper(name) %>
<% else %>
def getGeneric(role)
send("get\#{firstToUpper(role.to_s)}")
end
<% end %>
CODE
self::ClassModule.module_eval(@@many_read_builder.result(binding))
end
if props.value(:changeable)
@@many_write_builder ||= ERB.new <<-CODE
def add<%= firstToUpper(name) %>(val, index=-1)
@<%= name %> = [] unless defined?(@<%= name %>)
return if val.nil? || (val.is_a?(MMBase) || val.is_a?(MMGeneric)) && @<%= name %>.any? {|e| e.equal?(val)}
<%= type_check_code("val", props) %>
@<%= name %>.insert(index, val)
<% if other_role %>
val._register<%= firstToUpper(other_role) %>(self) unless val.is_a?(MMGeneric)
<% end %>
<% if props.reference? && props.value(:containment) %>
val._set_container(self, :<%= name %>)
<% end %>
end
def remove<%= firstToUpper(name) %>(val)
@<%= name %> = [] unless defined?(@<%= name %>)
@<%= name %>.each_with_index do |e,i|
if e.equal?(val)
@<%= name %>.delete_at(i)
<% if props.reference? && props.value(:containment) %>
val._set_container(nil, nil)
<% end %>
<% if other_role %>
val._unregister<%= firstToUpper(other_role) %>(self) unless val.is_a?(MMGeneric)
<% end %>
return
end
end
end
def set<%= firstToUpper(name) %>(val)
return if val.nil?
raise _assignmentTypeError(self, val, Enumerable) unless val.is_a? Enumerable
get<%= firstToUpper(name) %>.each {|e|
remove<%= firstToUpper(name) %>(e)
}
@<%= name %> = [] unless defined?(@<%= name %>)
<% if props.reference? %>
val.uniq {|elem| elem.object_id }.each {|elem|
next if elem.nil?
<%= type_check_code("elem", props) %>
@<%= name %> << elem
<% if other_role %>
elem._register<%= firstToUpper(other_role) %>(self) unless elem.is_a?(MMGeneric)
<% end %>
<% if props.value(:containment) %>
elem._set_container(self, :<%= name %>)
<% end %>
}
<% else %>
val.each {|elem|
<%= type_check_code("elem", props) %>
@<%= name %> << elem
}
<% end %>
end
alias <%= name %>= set<%= firstToUpper(name) %>
def _register<%= firstToUpper(name) %>(val)
@<%= name %> = [] unless defined?(@<%= name %>)
@<%= name %>.push val
<% if props.reference? && props.value(:containment) %>
val._set_container(self, :<%= name %>)
<% end %>
end
def _unregister<%= firstToUpper(name) %>(val)
@<%= name %>.delete val
<% if props.reference? && props.value(:containment) %>
val._set_container(nil, nil)
<% end %>
end
CODE
self::ClassModule.module_eval(@@many_write_builder.result(binding))
end
end
private
def build_derived_method(name, props, kind)
raise "Implement method #{name}_derived instead of method #{name}" \
if (public_instance_methods+protected_instance_methods+private_instance_methods).include?(name)
@@derived_builder ||= ERB.new <<-CODE
def get<%= firstToUpper(name) %>
raise "Derived feature requires public implementation of method <%= name %>_derived" \
unless respond_to?(:<%= name+"_derived" %>)
val = <%= name %>_derived
<% if kind == :many %>
raise _assignmentTypeError(self,val,Enumerable) unless val && val.is_a?(Enumerable)
val.each do |v|
<%= type_check_code("v", props) %>
end
<% else %>
<%= type_check_code("val", props) %>
<% end %>
val
end
<% if name != "class" %>
alias <%= name %> get<%= firstToUpper(name) %>
<% end %>
#TODO final_method :<%= name %>
CODE
self::ClassModule.module_eval(@@derived_builder.result(binding))
end
def check_default_value_literal(literal, props)
return if literal.nil? || props.impl_type == String
if props.impl_type == Integer || props.impl_type == RGen::MetamodelBuilder::DataTypes::Long
unless literal =~ /^\d+$/
raise StandardError.new("Property #{props.value(:name)} can not take value #{literal}, expected an Integer")
end
elsif props.impl_type == Float
unless literal =~ /^\d+\.\d+$/
raise StandardError.new("Property #{props.value(:name)} can not take value #{literal}, expected a Float")
end
elsif props.impl_type == RGen::MetamodelBuilder::DataTypes::Boolean
unless ["true", "false"].include?(literal)
raise StandardError.new("Property #{props.value(:name)} can not take value #{literal}, expected true or false")
end
elsif props.impl_type.is_a?(RGen::MetamodelBuilder::DataTypes::Enum)
unless props.impl_type.literals.include?(literal.to_sym)
raise StandardError.new("Property #{props.value(:name)} can not take value #{literal}, expected one of #{props.impl_type.literals_as_strings.join(', ')}")
end
else
raise StandardError.new("Unkown type "+props.impl_type.to_s)
end
end
def type_check_code(varname, props)
code = ""
if props.impl_type == RGen::MetamodelBuilder::DataTypes::Long
code << "unless #{varname}.nil? || #{varname}.is_a?(Integer) || #{varname}.is_a?(MMGeneric)"
code << "\n"
expected = "Integer"
elsif props.impl_type.is_a?(Class)
code << "unless #{varname}.nil? || #{varname}.is_a?(ObjectSpace._id2ref(#{props.impl_type.object_id})) || #{varname}.is_a?(MMGeneric)"
code << " || #{varname}.is_a?(BigDecimal)" if props.impl_type == Float && defined?(BigDecimal)
code << "\n"
expected = props.impl_type.to_s
elsif props.impl_type.is_a?(RGen::MetamodelBuilder::DataTypes::Enum)
code << "unless #{varname}.nil? || [#{props.impl_type.literals_as_strings.join(',')}].include?(#{varname}) || #{varname}.is_a?(MMGeneric)\n"
expected = "["+props.impl_type.literals_as_strings.join(',')+"]"
else
raise StandardError.new("Unkown type "+props.impl_type.to_s)
end
code << "raise _assignmentTypeError(self,#{varname},\"#{expected}\")\n"
code << "end"
code
end
def _ownProps(props)
Hash[*(props.select{|k,v| !(k.to_s =~ /^opposite_/)}.flatten)]
end
def _oppositeProps(props)
r = {}
props.each_pair do |k,v|
if k.to_s =~ /^opposite_(.*)$/
r[$1.to_sym] = v
end
end
r
end
def _setManyUpperBound(props)
props[:upperBound] = -1 unless props[:upperBound].is_a?(Integer) && props[:upperBound] > 1
props
end
end
end
end
|