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

  @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

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


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.

Parameters:

  • Name (String, #read)

    of the class including package name.

Returns:

Raises:

  • (ArgumentError)


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.

Parameters:

  • Id (String, #read)

    of the class in model file.

Returns:

Raises:

  • (ArgumentError)


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

#classesArray<Clazz>

Get all model classes.

Returns:



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

Returns:

  • (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.

Parameters:

  • Name (String, #read)

    of the package including sub packages name.

Returns:

Raises:

  • (ArgumentError)


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

#packagesArray<Package>

Get all model packages.

Returns:



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.

Parameters:

  • Id (String, #read)

    of the state in model file.

Returns:

Raises:

  • (ArgumentError)


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

#statesArray<ActionState, FinalState, PseudoState>

Get all model states.

Returns:



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_sObject



218
219
220
# File 'lib/xmimodel.rb', line 218

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

#to_xmlObject



222
223
224
# File 'lib/xmimodel.rb', line 222

def to_xml
  @document.to_xml
end