Class: XmiModel

Inherits:
Object
  • Object
show all
Defined in:
lib/xmimodel.rb

Overview

A helper class for working with XMI Models.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_file_name) ⇒ XmiModel

Constructor.

Parameters:

  • Path (String, #read)

    of model.


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
# File 'lib/xmimodel.rb', line 35

def initialize(model_file_name)

	@model_file_name = model_file_name

	# Obtem a tag 'XMI.content' que contém todos os objetos que serão tratados
	f = File.open(model_file_name)		
	doc = Nokogiri::XML(f)
	@document = doc
	xmi_content = doc.at_xpath("./XMI/XMI.content")
	f.close

	# Header
	@exporter = XmiHelper.exporter(doc)
	@exporter_version = XmiHelper.exporter_version(doc)
	@metamodel_version = XmiHelper.metamodel_version(doc)

	# Constrói vetor de pacotes
	@packages = Array.new		
	XmiHelper.packages(xmi_content).each do |uml_package|
		if ! (uml_package.attribute('name').nil? || 
			uml_package.attribute('name').to_s.empty? || 
			uml_package.attribute('name').to_s.strip == "Component View" ||
			uml_package.attribute('name').to_s.strip == "Data types")
			p = Package.new(uml_package, self)
			@packages << p
		end
	end

	# Constrói vetor de heranças
	@generalizations = Array.new
	XmiHelper.all_generalizations(xmi_content).each do |xml|
		g = Generalization.new(xml, self)
		
		g.child_obj.parent  = g.parent_obj unless g.child_obj.nil?
		g.parent_obj.children << g.child_obj unless g.parent_obj.nil?

		#puts "#{g.child_obj.full_name} - #{g.parent_obj.full_name}"
		@generalizations << g
	end

	# Constroi vetor de associações
	@associations = Array.new
	XmiHelper.all_associations(xmi_content).each do |xml|

		association = Association.new(xml, self)
		
		if association.end_a.participant.nil?
			puts "#{association.end_a} does not have an participant."
		else
			association.end_a.participant.associations << association	
		end
		if association.end_b.participant.nil?
			puts "#{association.end_b} does not have an participant."
		else
			association.end_b.participant.associations << association
		end

		association.end_a.other_end = association.end_b
		association.end_b.other_end = association.end_a

		association.end_a.participant.associations_end << association.end_b	unless association.end_a.participant.nil?
		association.end_b.participant.associations_end << association.end_a	unless association.end_b.participant.nil?

		@associations << association
	end

	# Associa os signal events aos action states 
	use_cases().each do |use_case|
		use_case.activity_graphs.each do |activity_graph|
			activity_graph.states.each do |state|
				state.transitions.each do |transition|
					trigger_id = transition.trigger_id
					if !trigger_id.empty?
						
						signal_event = signal_event_by_id(trigger_id)
						transition.trigger = signal_event

						if transition.trigger.nil?
							call_event = call_event_by_id(trigger_id)
							transition.trigger = call_event
						end
					end
				end
			end
		end
	end

	call_events().each do |call_event|
		if !call_event.operation_id.nil? && !call_event.operation_id.empty?
			operation = operation_by_id(call_event.operation_id)
			call_event.operation = operation
		end
	end			

	# Associa o deferrable_event dos action states aos call events
	states().each do |state|
		if state.is_action_state? && (!state.deferrable_event_id.nil? && !state.deferrable_event_id.empty?)
			call_event = call_event_by_id(state.deferrable_event_id)
			state.deferrable_event = call_event
		end
	end

	true
end

Instance Attribute Details

#associationsArray<Association> (readonly)

Returns All model associations.

Returns:


27
28
29
# File 'lib/xmimodel.rb', line 27

def associations
  @associations
end

#documentNokogiri::XML::Document (readonly)

Returns:

  • (Nokogiri::XML::Document)

12
13
14
# File 'lib/xmimodel.rb', line 12

def document
  @document
end

#exporterString (readonly)

Returns The contents of the tag 'XMI.header/XMI.documentation/XMI.exporter'.

Returns:

  • (String)

    The contents of the tag 'XMI.header/XMI.documentation/XMI.exporter'.


15
16
17
# File 'lib/xmimodel.rb', line 15

def exporter
  @exporter
end

#exporter_versionString (readonly)

Returns The contents of the tag 'XMI.header/XMI.documentation/XMI.exporterVersion'.

Returns:

  • (String)

    The contents of the tag 'XMI.header/XMI.documentation/XMI.exporterVersion'.


18
19
20
# File 'lib/xmimodel.rb', line 18

def exporter_version
  @exporter_version
end

#generalizationsArray<Generalization> (readonly)

Returns All model generalizations.

Returns:


24
25
26
# File 'lib/xmimodel.rb', line 24

def generalizations
  @generalizations
end

#metamodel_versionString (readonly)

Returns The value of property xmi.version of the tag 'XMI.header/XMI.metamodel'.

Returns:

  • (String)

    The value of property xmi.version of the tag 'XMI.header/XMI.metamodel'.


21
22
23
# File 'lib/xmimodel.rb', line 21

def metamodel_version
  @metamodel_version
end

#model_file_nameObject (readonly)

Returns the value of attribute model_file_name


29
30
31
# File 'lib/xmimodel.rb', line 29

def model_file_name
  @model_file_name
end

Instance Method Details

#association_by_id(id) ⇒ Association

Get the object of type 'Association' by id.

Parameters:

  • Id (String, #read)

    of the state in model file.

Returns:

Raises:

  • (ArgumentError)

145
146
147
148
149
# File 'lib/xmimodel.rb', line 145

def association_by_id(id)
	raise ArgumentError.new("Parameter 'id' cannot be empty.") if id.nil? or id.empty?
	objs = @associations.select{|obj| obj.id == id}	
	(!objs.nil? && objs.size > 0) ? objs[0] : nil
end

#call_event_by_id(id) ⇒ CallEvent

Get the object of type 'CallEvent' by id.

Parameters:

  • Id (String, #read)

    of the CallEvent in model file.

Returns:

Raises:

  • (ArgumentError)

366
367
368
369
370
# File 'lib/xmimodel.rb', line 366

def call_event_by_id(id)		
	raise ArgumentError.new("Parameter 'id' cannot be empty.") if id.nil? or id.empty?
	objs = call_events().select{|obj| obj.id == id}	
	(!objs.nil? && objs.size > 0) ? objs[0] : nil
end

#call_eventsArray<CallEvent>

Get all Call Events.

Returns:


352
353
354
355
356
357
358
359
# File 'lib/xmimodel.rb', line 352

def call_events
	return @call_events unless @call_events.nil?
	@call_events = Array.new
	use_cases.each do |c|
		@call_events.concat c.call_events
	end
	@call_events
end

#class_by_full_name(full_class_name) ⇒ Clazz

Get the object of type 'Clazz' by full name of class.

Parameters:

  • Name (String, #read)

    of the class including package name.

Returns:

Raises:

  • (ArgumentError)

156
157
158
159
160
161
162
163
164
165
# File 'lib/xmimodel.rb', line 156

def class_by_full_name(full_class_name)
	raise ArgumentError.new("Parameter 'full_class_name' cannot be empty.") if full_class_name.nil? or full_class_name.empty?
	clazz = classes.select{|c| c.full_name == full_class_name}
	
	if !clazz.nil? && clazz.size > 0
		clazz[0]
	else
		nil
	end
end

#class_by_id(class_id) ⇒ Clazz

Get the object of type 'Clazz' by id.

Parameters:

  • Id (String, #read)

    of the class in model file.

Returns:

Raises:

  • (ArgumentError)

172
173
174
175
176
177
178
179
180
181
# File 'lib/xmimodel.rb', line 172

def class_by_id(class_id)
	raise ArgumentError.new("#{__method__}: 'class_id' cannot be empty.") if class_id.nil? or class_id.empty?
	clazz = classes.select{|c| c.id == class_id}
	
	if !clazz.nil? && clazz.size > 0
		clazz[0]
	else
		nil
	end
end

#classesArray<Clazz>

Get all model classes.

Returns:


187
188
189
190
191
192
193
194
# File 'lib/xmimodel.rb', line 187

def classes
	return @classes unless @classes.nil?
	@classes = Array.new
	packages.each do |p|
		@classes.concat p.classes.sort
	end
	@classes
end

#enumeration_by_id(id) ⇒ Enumeration

Get the object of type 'Clazz' by id.

Parameters:

  • Id (String, #read)

    of the class in model file.

Returns:

Raises:

  • (ArgumentError)

214
215
216
217
218
219
220
221
222
223
# File 'lib/xmimodel.rb', line 214

def enumeration_by_id(id)
	raise ArgumentError.new("#{__method__}: 'id' cannot be empty.") if id.nil? or id.empty?
	obj = enumerations.select{|o| o.id == id}
	
	if !obj.nil? && obj.size > 0
		obj[0]
	else
		nil
	end
end

#enumerationsArray<Enumeration>

Get all model enumerations.

Returns:


200
201
202
203
204
205
206
207
# File 'lib/xmimodel.rb', line 200

def enumerations
	return @enumerations unless @enumerations.nil?
	@enumerations = Array.new
	packages.each do |p|
		@enumerations.concat p.enumerations.sort
	end
	@enumerations
end

#id_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)

322
323
324
325
# File 'lib/xmimodel.rb', line 322

def id_exists?(id)
	tag = XmiHelper.tag_by_id(@document, '*', id)
	return !tag.nil?
end

#operation_by_id(id) ⇒ Operation

Get the object of type 'Operation' by id.

Parameters:

  • Id (String, #read)

    of the Operation in model file.

Returns:

Raises:

  • (ArgumentError)

390
391
392
393
394
# File 'lib/xmimodel.rb', line 390

def operation_by_id(id)		
	raise ArgumentError.new("Parameter 'id' cannot be empty.") if id.nil? or id.empty?
	objs = operations().select{|obj| obj.id == id}	
	(!objs.nil? && objs.size > 0) ? objs[0] : nil
end

#operationsArray<Operation>

Get all Operations

Returns:


376
377
378
379
380
381
382
383
# File 'lib/xmimodel.rb', line 376

def operations
	return @operations unless @operations.nil?
	@operations = Array.new
	classes.each do |c|
		@operations.concat c.operations
	end
	@operations
end

#package_by_full_name(full_package_name) ⇒ Package

Get the object of type 'Package' by full name of package.

Parameters:

  • Name (String, #read)

    of the package including sub packages name.

Returns:

Raises:

  • (ArgumentError)

230
231
232
233
234
235
236
237
238
239
# File 'lib/xmimodel.rb', line 230

def package_by_full_name(full_package_name)
	raise ArgumentError.new("Parameter 'full_package_name' cannot be empty.") if full_package_name.nil? or full_package_name.empty?
	package = packages.select{|p| p.full_name == full_package_name}
	
	if !package.nil? && package.size > 0
		package[0]
	else
		nil
	end
end

#packagesArray<Package>

Get all model packages.

Returns:


245
246
247
248
249
250
251
252
253
# File 'lib/xmimodel.rb', line 245

def packages
	return @all_packages unless @all_packages.nil?
	@all_packages = Array.new

	add_package(@packages)
	
	@all_packages.sort!
	@all_packages
end

#save(model_file_name = @model_file_name) ⇒ Object


396
397
398
399
400
# File 'lib/xmimodel.rb', line 396

def save(model_file_name=@model_file_name)
	f = File.open(model_file_name, 'w')
	f.write(@document.to_xml)
	f.close		
end

#signal_event_by_id(signal_event_id) ⇒ SignalEvent

Get the object of type 'SignalEvent' by id.

Parameters:

  • Id (String, #read)

    of the SignalEvent in model file.

Returns:

Raises:

  • (ArgumentError)

310
311
312
313
314
315
316
317
318
319
# File 'lib/xmimodel.rb', line 310

def signal_event_by_id(signal_event_id)
	raise ArgumentError.new("#{__method__}: 'signal_event_id' cannot be empty.") if signal_event_id.nil? or signal_event_id.empty?
	signal_event = signal_events.select{|obj| obj.id == signal_event_id}
	
	if !signal_event.nil? && signal_event.size > 0
		signal_event[0]
	else
		nil
	end
end

#signal_eventsObject


292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/xmimodel.rb', line 292

def signal_events
	return @signal_events unless @signal_events.nil?

	@signal_events = Array.new
	packages().each do |p|
		@signal_events.concat p.signal_events.sort
	end
	use_cases().each do |p|
		@signal_events.concat p.signal_events.sort
	end
	@signal_events		
end

#state_by_id(id) ⇒ ActionState, ...

Get the object of type 'State' by id.

Parameters:

  • Id (String, #read)

    of the state in model file.

Returns:

Raises:

  • (ArgumentError)

260
261
262
263
264
# File 'lib/xmimodel.rb', line 260

def state_by_id(id)
	raise ArgumentError.new("Parameter 'id' cannot be empty.") if id.nil? or id.empty?
	objs = states.select{|obj| obj.id == id}	
	(!objs.nil? && objs.size > 0) ? objs[0] : nil
end

#statesArray<ActionState, FinalState, PseudoState>

Get all model states.

Returns:


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/xmimodel.rb', line 271

def states
	return @states unless @states.nil?

	@states = Array.new
	packages.each do |p|
		p.use_cases.each do |u|
			u.activity_graphs.each do |a|
				@states.concat a.action_states
				@states.concat a.final_states
				@states.concat a.pseudo_states
			end
		end
		p.activity_graphs.each do |a|
			@states.concat a.action_states
			@states.concat a.final_states
			@states.concat a.pseudo_states
		end			
	end
	@states		
end

#to_sObject


327
328
329
# File 'lib/xmimodel.rb', line 327

def to_s
	"'XmiModel #{exporter} #{exporter_version} [Packages: #{packages.size}, Classes: #{classes.size}]'"
end

#to_xmlObject


331
332
333
# File 'lib/xmimodel.rb', line 331

def to_xml
	@document.to_xml
end

#use_casesArray<UseCase>

Get all Use Case.

Returns:


339
340
341
342
343
344
345
346
# File 'lib/xmimodel.rb', line 339

def use_cases
	return @use_cases unless @use_cases.nil?
	@use_cases = Array.new
	packages.each do |p|
		@use_cases.concat p.use_cases.sort
	end
	@use_cases
end