Module: RGen::MetamodelBuilder::MMBase::SingletonAddOn

Included in:
RGen::MetamodelBuilder::MMBase
Defined in:
lib/lightmodels/rgen_ext.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



87
88
89
# File 'lib/lightmodels/rgen_ext.rb', line 87

def ==(other)
	eql? other
end

#childrenObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lightmodels/rgen_ext.rb', line 96

def children
	arr = []
	ecore = self.class.ecore
	ecore.eAllReferences.select {|r| r.containment}.each do |ref|
		res = self.send(ref.name.to_sym)
		if res.is_a? Array
			arr.concat(res)
		elsif res
			arr << res
		end
	end
	arr
end

#children_deepObject



110
111
112
113
114
115
116
117
# File 'lib/lightmodels/rgen_ext.rb', line 110

def children_deep
	arr = []
	children.each do |c|
		arr << c
		arr.concat(c.children_deep)
	end			
	arr
end

#children_deep_of_type(type) ⇒ Object



130
131
132
# File 'lib/lightmodels/rgen_ext.rb', line 130

def children_deep_of_type(type)
	children_deep.select {|c| c and c.is_a?(type)}
end

#children_of_type(type) ⇒ Object



126
127
128
# File 'lib/lightmodels/rgen_ext.rb', line 126

def children_of_type(type)
	children.select {|c| c and c.is_a?(type)}
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/lightmodels/rgen_ext.rb', line 58

def eql?(other)
	# it should ignore relations which has as opposite a containement
	return false unless self.shallow_eql?(other)
	self.class.ecore.eAllReferences.each do |ref|
		self_value = self.get(ref)
		other_value = other.get(ref)
		to_ignore = ref.getEOpposite and ref.getEOpposite.containment
		unless to_ignore
			if ref.containment
				return false unless self_value == other_value
			else
				if (self_value.is_a? Array) or (other_value.is_a? Array)
					return false unless self_value.count==other_value.count
					for i in 0..(self_value.count-1)
						return false unless self_value[i].shallow_eql?(other_value[i])
					end
				else  
					if self_value==nil
						return false unless other_value==nil
					else
						return false unless self_value.shallow_eql?(other_value)
					end
				end
			end
		end						
	end
	true
end

#get(attr_or_ref) ⇒ Object



91
92
93
94
# File 'lib/lightmodels/rgen_ext.rb', line 91

def get(attr_or_ref)
	getter = (attr_or_ref.name).to_sym
	send getter
end

#only_child_deep_of_type(type) ⇒ Object



140
141
142
143
144
# File 'lib/lightmodels/rgen_ext.rb', line 140

def only_child_deep_of_type(type)
	selected = children_deep_of_type(type)
	raise "Exactly one child of type #{type} expected, #{selected.count} found on #{self}" unless selected.count==1
	selected[0]
end

#only_child_of_type(type) ⇒ Object



134
135
136
137
138
# File 'lib/lightmodels/rgen_ext.rb', line 134

def only_child_of_type(type)
	selected = children_of_type(type)
	raise "Exactly one child of type #{type} expected, #{selected.count} found on #{self}" unless selected.count==1
	selected[0]
end

#shallow_eql?(other) ⇒ Boolean

It does not check references, it is needed to avoid infinite recursion

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lightmodels/rgen_ext.rb', line 43

def shallow_eql?(other)
	return false if other==nil
	return false unless self.class==other.class
	self.class.ecore.eAllAttributes.each do |attrib|
		raise "Attrib <nil> for class #{self.class.ecore.name}" unless attrib
		if attrib.name != 'dynamic' # I have to understand this...
			self_value  = self.get(attrib)
			other_value = other.get(attrib)
			#puts "returning false on #{attrib.name}" unless self_value.eql?(other_value)
			return false unless self_value == other_value
		end
	end
	true
end

#traverse(&op) ⇒ Object



119
120
121
122
123
124
# File 'lib/lightmodels/rgen_ext.rb', line 119

def traverse(&op)
	op.call(self)
	children_deep.each do |c|
		op.call(c)
	end
end