Class: Lutaml::Cli::Uml::DiagramCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/cli/uml/diagram_command.rb

Overview

CLI command for diagram rendering

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DiagramCommand

Returns a new instance of DiagramCommand.



10
11
12
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 10

def initialize(options = {})
  @options = options.transform_keys(&:to_sym)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 8

def options
  @options
end

Class Method Details

.add_options_to(thor_class, _method_name) ⇒ Object

rubocop:disable Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 14

def self.add_options_to(thor_class, _method_name) # rubocop:disable Metrics/MethodLength
  thor_class.long_desc <<-DESC
  Render EA diagrams to SVG format.

  This command converts Enterprise Architect diagram data into clean,
  interactive SVG files suitable for web display. The diagrams can be
  rendered from LUR packages or directly from diagram data.

  The output SVG files include proper styling, interactive elements,
  and can be embedded in web applications or documentation.

  Examples:
    lutaml uml diagram render diagram001 -o diagram001.svg
    lutaml uml diagram render diagram001 -o diagram001.svg --interactive
    lutaml uml diagram list mymodel.lur
  DESC

  thor_class.option :output, aliases: "-o", type: :string,
                             desc: "Output SVG file path"
  thor_class.option :format, type: :string, default: "svg",
                             desc: "Output format (svg|png)"
  thor_class.option :interactive, type: :boolean, default: true,
                                  desc: "Include interactive elements"
  thor_class.option :width, type: :numeric, desc: "Diagram width"
  thor_class.option :height, type: :numeric, desc: "Diagram height"
  thor_class.option :padding, type: :numeric, default: 20,
                              desc: "Padding around diagram"
  thor_class.option :background, type: :string, default: "#ffffff",
                                 desc: "Background color"
  thor_class.option :grid, type: :boolean, default: false,
                           desc: "Show grid lines"
end

Instance Method Details

#add_association_metadata(data, connector) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 206

def (data, connector)
  return unless connector.is_a?(Lutaml::Uml::Association)

  if connector.owner_end_attribute_name
    data[:source_role] =
      connector.owner_end_attribute_name
  end
  if connector.member_end_attribute_name
    data[:target_role] =
      connector.member_end_attribute_name
  end
  if connector.owner_end_cardinality
    data[:source_multiplicity] =
      format_cardinality(connector.owner_end_cardinality)
  end
  if connector.member_end_cardinality
    data[:target_multiplicity] =
      format_cardinality(connector.member_end_cardinality)
  end
end

#add_classifier_members(data, uml_element) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 151

def add_classifier_members(data, uml_element)
  return unless uml_element.is_a?(Lutaml::Uml::Classifier)

  if uml_element.attributes
    data[:attributes] =
      serialize_element_attributes(uml_element.attributes)
  end
  if uml_element.operations
    data[:operations] =
      serialize_element_operations(uml_element.operations)
  end
end

#add_connector_coordinates(data, connector, diagram) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 227

def add_connector_coordinates(data, connector, diagram)
  return unless connector.is_a?(Lutaml::Uml::Association)

  if connector.owner_end && connector.source
    add_endpoint_coords(data, connector.source.xmi_id, diagram,
                        :source_x, :source_y)
  end

  return unless connector.member_end && connector.target

  add_endpoint_coords(data, connector.target.xmi_id, diagram,
                      :target_x, :target_y)
end

#add_element_stereotype(data, uml_element) ⇒ Object



144
145
146
147
148
149
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 144

def add_element_stereotype(data, uml_element)
  return unless uml_element.is_a?(Lutaml::Uml::TopElement)
  return unless uml_element.stereotype && !uml_element.stereotype.empty?

  data[:stereotype] = uml_element.stereotype.first
end

#add_endpoint_coords(data, xmi_id, diagram, x_key, y_key) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 241

def add_endpoint_coords(data, xmi_id, diagram, x_key, y_key)
  obj = find_diagram_object_for_element(xmi_id, diagram)
  return unless obj

  data[x_key] = obj.left + ((obj.right - obj.left) / 2)
  data[y_key] = obj.top + ((obj.bottom - obj.top) / 2)
end

#base_element_data(obj, uml_element) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 131

def base_element_data(obj, uml_element)
  {
    id: obj.diagram_object_id || obj.object_xmi_id,
    type: determine_element_type(uml_element),
    name: uml_element.name,
    x: obj.left || 0,
    y: obj.top || 0,
    width: (obj.right - obj.left) || 120,
    height: (obj.bottom - obj.top) || 80,
    style: obj.style,
  }
end

#build_connector_data(link, connector, diagram) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 191

def build_connector_data(link, connector, diagram)
  data = {
    id: link.connector_id || link.connector_xmi_id,
    type: determine_connector_type(connector),
    style: link.style,
    geometry: link.geometry,
    path: link.path,
  }

  (data, connector)
  add_connector_coordinates(data, connector, diagram)

  data
end

#build_element_data(obj, uml_element) ⇒ Object



124
125
126
127
128
129
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 124

def build_element_data(obj, uml_element)
  data = base_element_data(obj, uml_element)
  add_element_stereotype(data, uml_element)
  add_classifier_members(data, uml_element)
  data
end

#convert_diagram_to_rendering_format(diagram, repository) ⇒ Object



60
61
62
63
64
65
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 60

def convert_diagram_to_rendering_format(diagram, repository)
  elements = extract_diagram_elements(diagram, repository)
  connectors = extract_diagram_connectors(diagram, repository)

  { elements: elements, connectors: connectors }
end

#determine_connector_type(connector) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 101

def determine_connector_type(connector)
  case connector
  when Lutaml::Uml::Association
    "association"
  when Lutaml::Uml::Generalization
    "generalization"
  when Lutaml::Uml::Dependency
    "dependency"
  else
    "connector"
  end
end

#determine_element_type(uml_element) ⇒ Object

rubocop:disable Metrics/MethodLength



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 86

def determine_element_type(uml_element) # rubocop:disable Metrics/MethodLength
  case uml_element
  when Lutaml::Uml::Class
    "class"
  when Lutaml::Uml::Package
    "package"
  when Lutaml::Uml::DataType
    "datatype"
  when Lutaml::Uml::Enumeration
    "enumeration"
  else
    "unknown"
  end
end

#extract_diagram_connectors(diagram, repository) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 181

def extract_diagram_connectors(diagram, repository)
  diagram.diagram_links.filter_map do |link|
    connector = find_connector_by_xmi_id(link.connector_xmi_id,
                                         repository)
    next unless connector

    build_connector_data(link, connector, diagram)
  end
end

#extract_diagram_elements(diagram, repository) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 114

def extract_diagram_elements(diagram, repository)
  diagram.diagram_objects.filter_map do |obj|
    uml_element = find_uml_element_by_xmi_id(obj.object_xmi_id,
                                             repository)
    next unless uml_element

    build_element_data(obj, uml_element)
  end
end

#find_connector_by_xmi_id(xmi_id, repository) ⇒ Object



75
76
77
78
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 75

def find_connector_by_xmi_id(xmi_id, repository)
  # Search through associations and other connectors
  repository.associations_index.find { |a| a.xmi_id == xmi_id }
end

#find_diagram_object_for_element(element_xmi_id, diagram) ⇒ Object



80
81
82
83
84
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 80

def find_diagram_object_for_element(element_xmi_id, diagram)
  diagram.diagram_objects.find do |obj|
    obj.object_xmi_id == element_xmi_id
  end
end

#find_uml_element_by_xmi_id(xmi_id, repository) ⇒ Object

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



67
68
69
70
71
72
73
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 67

def find_uml_element_by_xmi_id(xmi_id, repository) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  # Search through all element types
  repository.classes_index.find { |c| c.xmi_id == xmi_id } ||
    repository.packages_index.find { |p| p.xmi_id == xmi_id } ||
    repository.data_types_index.find { |d| d.xmi_id == xmi_id } ||
    repository.enums_index.find { |e| e.xmi_id == xmi_id }
end

#run(action, *args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 47

def run(action, *args)
  case action
  when "render"
    render_diagram(args.first)
  when "list"
    list_diagrams(args.first)
  else
    puts "Unknown action: #{action}"
    puts "Available actions: render, list"
    raise Thor::Error, "Invalid action"
  end
end

#serialize_element_attributes(attributes) ⇒ Object



164
165
166
167
168
169
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 164

def serialize_element_attributes(attributes)
  attributes.map do |attr|
    { name: attr.name, type: attr.type,
      visibility: attr.visibility || "public" }
  end
end

#serialize_element_operations(operations) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/lutaml/cli/uml/diagram_command.rb', line 171

def serialize_element_operations(operations)
  operations.map do |op|
    { name: op.name, return_type: op.return_type,
      visibility: op.visibility || "public",
      parameters: op.parameters&.map do |p|
        { name: p.name, type: p.type }
      end || [] }
  end
end