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.



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

def initialize(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, nil)
			@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

	@associations = Array.new
	XmiHelper.all_associations(xmi_content).each do |xml|
		@associations << Association.new(xml, self)
	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

Instance Method Details

#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)


84
85
86
87
88
89
90
91
92
93
# File 'lib/xmimodel.rb', line 84

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)


100
101
102
103
104
105
106
107
108
109
# File 'lib/xmimodel.rb', line 100

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:



115
116
117
118
119
120
121
122
# File 'lib/xmimodel.rb', line 115

def classes
	return @classes unless @classes.nil?
	@classes = Array.new
	packages.each do |p|
		@classes.concat p.classes.sort
	end
	@classes
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)


129
130
131
132
133
134
135
136
137
138
# File 'lib/xmimodel.rb', line 129

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:



144
145
146
147
148
149
150
151
152
# File 'lib/xmimodel.rb', line 144

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

	add_package(@packages)
	
	@all_packages.sort!
	@all_packages
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)


159
160
161
162
163
# File 'lib/xmimodel.rb', line 159

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:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/xmimodel.rb', line 170

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



191
192
193
# File 'lib/xmimodel.rb', line 191

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