Class: Xumlidot::Diagram::Xmi

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/xumlidot/diagram/xmi.rb,
lib/xumlidot/diagram/xmi/id.rb,
lib/xumlidot/diagram/xmi/klass.rb,
lib/xumlidot/diagram/xmi/method.rb,
lib/xumlidot/diagram/xmi/argument.rb,
lib/xumlidot/diagram/xmi/constant.rb,
lib/xumlidot/diagram/xmi/attribute.rb,
lib/xumlidot/diagram/xmi/superklass.rb

Defined Under Namespace

Modules: Argument, Attribute, Constant, ID, Klass, MethodSignature, Superklass Classes: DiagramAssociationElements, DiagramElements, Elements, ModelAssociationElements, ModelElements, NamespaceToId

Instance Method Summary collapse

Constructor Details

#initialize(stack) ⇒ Xmi

Returns a new instance of Xmi.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xumlidot/diagram/xmi.rb', line 93

def initialize(stack)
  @stack = stack

  @namespace_to_id = NamespaceToId.new

  @model = ModelElements.new
  @diagram = DiagramElements.new

  @model_associations = ModelAssociationElements.new
  @diagram_associations = DiagramAssociationElements.new
end

Instance Method Details

#drawObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/xumlidot/diagram/xmi.rb', line 105

def draw # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  # First traversal we're just assigning ids to everything so that when
  # we come to draw things we can do a lookup on the ids for any composition
  # aggregation or subclass relationships.
  @stack.traverse do |klass|
    klass.extend(::Xumlidot::Diagram::Xmi::Klass)
    klass.superklass.extend(::Xumlidot::Diagram::Xmi::Superklass) unless klass.superklass.name.nil?

    klass.definition.inherited_modules.each do |m|
      next if m.empty?

      m.extend(::Xumlidot::Diagram::Xmi::Superklass)
    end

    next if @namespace_to_id.has?(klass.draw_identifier)

    @namespace_to_id[klass.draw_identifier] = klass.id
  end

  # Second traversal we are drawing everything
  @stack.traverse do |klass|
    # resolve the superclass id to that of an existing class with an id
    # we do this in the second loop so that all the classes are present
    unless klass.superklass.name.nil?
      id = @namespace_to_id[klass.superklass.draw_identifier]
      klass.superklass.force_id(id)
    end

    # do the same with the inherited modules
    klass.definition.inherited_modules.each do |m|
      next if m.empty?

      id = @namespace_to_id[m.draw_identifier]
      m.force_id(id)
    end

    # if we have not added an id for this element it is likely a duplicate
    # so do not draw it.
    next unless @namespace_to_id.has_value?(klass.id) # rubocop:disable Style/PreferredHashMethods

    @model << klass.draw_klass
    @diagram << klass.draw_diagram

    if ::Xumlidot::Options.composition
      klass.constants.each do |k|
        # Likewise to the above, unless we have an id for the class
        # we don't want to draw the relationships
        next unless @namespace_to_id.has_value?(k.id) # rubocop:disable Style/PreferredHashMethods

        @model_associations << klass.draw_model_composition(k)
        @diagram_associations << klass.draw_diagram_composition(k)
      end
    end
  end

  xml = header
  xml += @model.draw { @model_associations.draw }
  xml += @diagram.draw { @diagram_associations.draw }
  xml += footer

  pretty_print(xml)
end