Class: XmiModel
- Inherits:
-
Object
- Object
- XmiModel
- Defined in:
- lib/xmimodel.rb
Overview
A helper class for working with XMI Models.
Instance Attribute Summary collapse
-
#associations ⇒ Array<Association>
readonly
All model associations.
- #document ⇒ Nokogiri::XML::Document readonly
-
#exporter ⇒ String
readonly
The contents of the tag ‘XMI.header/XMI.documentation/XMI.exporter’.
-
#exporter_version ⇒ String
readonly
The contents of the tag ‘XMI.header/XMI.documentation/XMI.exporterVersion’.
-
#generalizations ⇒ Array<Generalization>
readonly
All model generalizations.
-
#metamodel_version ⇒ String
readonly
The value of property xmi.version of the tag ‘XMI.header/XMI.metamodel’.
-
#model_file_name ⇒ Object
readonly
Returns the value of attribute model_file_name.
Instance Method Summary collapse
-
#association_by_id(id) ⇒ Association
Get the object of type ‘Association’ by id.
-
#class_by_full_name(full_class_name) ⇒ Clazz
Get the object of type ‘Clazz’ by full name of class.
-
#class_by_id(class_id) ⇒ Clazz
Get the object of type ‘Clazz’ by id.
-
#classes ⇒ Array<Clazz>
Get all model classes.
- #id_exists?(id) ⇒ Boolean
-
#initialize(model_file_name) ⇒ XmiModel
constructor
Constructor.
-
#package_by_full_name(full_package_name) ⇒ Package
Get the object of type ‘Package’ by full name of package.
-
#packages ⇒ Array<Package>
Get all model packages.
- #save(model_file_name = @model_file_name) ⇒ Object
-
#state_by_id(id) ⇒ ActionState, ...
Get the object of type ‘State’ by id.
-
#states ⇒ Array<ActionState, FinalState, PseudoState>
Get all model states.
- #to_s ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(model_file_name) ⇒ XmiModel
Constructor.
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 |
# 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) = XmiHelper.(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 @associations = Array.new XmiHelper.all_associations(xmi_content).each do |xml| association = Association.new(xml, self) association.end_a.participant.associations << association.end_b association.end_b.participant.associations << association.end_a @associations << association end true end |
Instance Attribute Details
#associations ⇒ Array<Association> (readonly)
Returns All model associations.
27 28 29 |
# File 'lib/xmimodel.rb', line 27 def associations @associations end |
#document ⇒ Nokogiri::XML::Document (readonly)
12 13 14 |
# File 'lib/xmimodel.rb', line 12 def document @document end |
#exporter ⇒ String (readonly)
Returns 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_version ⇒ String (readonly)
Returns 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 |
#generalizations ⇒ Array<Generalization> (readonly)
Returns All model generalizations.
24 25 26 |
# File 'lib/xmimodel.rb', line 24 def generalizations @generalizations end |
#metamodel_version ⇒ String (readonly)
Returns The value of property xmi.version of the tag ‘XMI.header/XMI.metamodel’.
21 22 23 |
# File 'lib/xmimodel.rb', line 21 def end |
#model_file_name ⇒ Object (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.
94 95 96 97 98 |
# File 'lib/xmimodel.rb', line 94 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 |
#class_by_full_name(full_class_name) ⇒ Clazz
Get the object of type ‘Clazz’ by full name of class.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/xmimodel.rb', line 105 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.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/xmimodel.rb', line 121 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 |
#classes ⇒ Array<Clazz>
Get all model classes.
136 137 138 139 140 141 142 143 |
# File 'lib/xmimodel.rb', line 136 def classes return @classes unless @classes.nil? @classes = Array.new packages.each do |p| @classes.concat p.classes.sort end @classes end |
#id_exists?(id) ⇒ Boolean
213 214 215 216 |
# File 'lib/xmimodel.rb', line 213 def id_exists?(id) tag = XmiHelper.tag_by_id(@document, '*', id) return !tag.nil? end |
#package_by_full_name(full_package_name) ⇒ Package
Get the object of type ‘Package’ by full name of package.
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/xmimodel.rb', line 150 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 |
#packages ⇒ Array<Package>
Get all model packages.
165 166 167 168 169 170 171 172 173 |
# File 'lib/xmimodel.rb', line 165 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
226 227 228 229 230 |
# File 'lib/xmimodel.rb', line 226 def save(model_file_name=@model_file_name) f = File.open(model_file_name, 'w') f.write(@document.to_xml) f.close end |
#state_by_id(id) ⇒ ActionState, ...
Get the object of type ‘State’ by id.
180 181 182 183 184 |
# File 'lib/xmimodel.rb', line 180 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 |
#states ⇒ Array<ActionState, FinalState, PseudoState>
Get all model states.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/xmimodel.rb', line 191 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_s ⇒ Object
218 219 220 |
# File 'lib/xmimodel.rb', line 218 def to_s "'XmiModel #{exporter} #{exporter_version} [Packages: #{packages.size}, Classes: #{classes.size}]'" end |
#to_xml ⇒ Object
222 223 224 |
# File 'lib/xmimodel.rb', line 222 def to_xml @document.to_xml end |