lutaml-diagram 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Formatter
5
+ class Graphviz < Base
6
+ module NodeFormatter
7
+ ACCESS_SYMBOLS = {
8
+ 'public' => '+',
9
+ 'protected' => '#',
10
+ 'private' => '-',
11
+ 'package' => '~'
12
+ }.freeze
13
+
14
+ def format_attribute(node)
15
+ symbol = ACCESS_SYMBOLS[node.visibility]
16
+ result = "#{symbol}#{node.name}"
17
+ if node.type
18
+ stereotype = node.stereotype.empty? ? '' : node.stereotype.map { |s| "«#{s}»" }.join
19
+ result += " : #{stereotype}#{node.type}"
20
+ end
21
+ result += format_cardinality_bounds(node.cardinality) if node.cardinality
22
+ result = escape_html_chars(result)
23
+ result = "<U>#{result}</U>" if node.static
24
+ result
25
+ end
26
+
27
+ def format_cardinality_bounds(cardinality)
28
+ min = cardinality.min || '*'
29
+ max = cardinality.max || '*'
30
+ "[#{min}..#{max}]"
31
+ end
32
+
33
+ def format_operation(node)
34
+ symbol = ACCESS_SYMBOLS[node.visibility]
35
+ params = format_operation_params(node.owned_parameter)
36
+ result = "#{symbol} #{node.name}(#{params})"
37
+ result << " : #{node.return_type}" if node.return_type
38
+ result = escape_html_chars(result)
39
+ result = "<U>#{result}</U>" if node.is_static
40
+ result = "<I>#{result}</I>" if node.is_abstract
41
+ result
42
+ end
43
+
44
+ def format_operation_params(params)
45
+ return '' unless params&.any?
46
+
47
+ params.map do |param|
48
+ param.type ? "#{param.name} : #{param.type}" : param.name.to_s
49
+ end.join(', ')
50
+ end
51
+
52
+ # Kind labels rendered above the name: guillemet stereotypes first,
53
+ # then the entity-kind label (enums/data types/primitives carry no
54
+ # user stereotypes of their own in the grammar).
55
+ KIND_LABELS = {
56
+ enums: "enumeration",
57
+ data_types: "dataType",
58
+ primitives: "primitive"
59
+ }.freeze
60
+
61
+ def format_class(node, hide_members = nil)
62
+ name = ["<B>#{escape_html_chars(node.name)}</B>"]
63
+ node.stereotype.each do |stereotype|
64
+ name.unshift("«#{escape_html_chars(stereotype)}»")
65
+ end
66
+ if (kind = KIND_LABELS[node.class.entity_type])
67
+ name.unshift("«#{kind}»")
68
+ end
69
+ name_html = build_name_table(name)
70
+
71
+ member_formatter = node.is_a?(Lml::Enum) ? method(:format_enum_member) : nil
72
+ field_table = format_member_rows(node.attributes, hide_members, &member_formatter)
73
+ method_table = format_member_rows(node.operations, hide_members) if node.operations&.any?
74
+ table_body = build_table_body(name_html, field_table, method_table)
75
+
76
+ <<~HEREDOC.chomp
77
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="10">
78
+ #{table_body}
79
+ </TABLE>
80
+ HEREDOC
81
+ end
82
+
83
+ def format_enum_literal(node)
84
+ "<I>#{escape_html_chars(node.name.to_s)}</I>"
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Formatter
5
+ class Graphviz < Base
6
+ module RelationshipFormatter
7
+ ARROW_TYPES = {
8
+ 'composition' => 'diamond',
9
+ 'aggregation' => 'odiamond',
10
+ 'direct' => 'vee'
11
+ }.freeze
12
+
13
+ DASHED_TYPES = %w[dependency realizes].freeze
14
+
15
+ DIRECTION_LABELS = {
16
+ 'target' => '%s ▶',
17
+ 'source' => '◀ %s'
18
+ }.freeze
19
+
20
+ def format_relationship(node)
21
+ graph_parent_name = generate_graph_name(node.owner_end)
22
+ graph_node_name = generate_graph_name(node.member_end)
23
+ attributes = build_edge_attributes(node)
24
+ graph_attributes = " [#{attributes}]" unless attributes.empty?
25
+
26
+ "#{graph_parent_name} -> #{graph_node_name}#{graph_attributes}"
27
+ end
28
+
29
+ def build_edge_attributes(node)
30
+ attrs = Attributes.new
31
+
32
+ apply_edge_style(attrs, node)
33
+ apply_edge_direction(attrs, node)
34
+ apply_edge_labels(attrs, node)
35
+ apply_arrow_types(attrs, node)
36
+ maybe_swap_labels(attrs)
37
+
38
+ attrs
39
+ end
40
+
41
+ def format_label(name, cardinality = nil)
42
+ res = "+#{name}"
43
+ return res if cardinality.nil? || cardinality.min.nil? || cardinality.max.nil?
44
+
45
+ "#{res} #{cardinality.min}..#{cardinality.max}"
46
+ end
47
+
48
+ private
49
+
50
+ def sort_by_document_grouping(groups, associations)
51
+ result = []
52
+ seen = Set.new
53
+
54
+ groups.each do |group|
55
+ group.values.each do |group_name|
56
+ matches = associations.select { |a| a.owner_end == group_name }
57
+ matches.each { |a| add_unseen(a, result, seen) }
58
+ end
59
+ end
60
+ associations.each { |a| add_unseen(a, result, seen) }
61
+ result
62
+ end
63
+
64
+ def add_unseen(association, result, seen)
65
+ return if seen.include?(association)
66
+
67
+ result.push(association)
68
+ seen.add(association)
69
+ end
70
+
71
+ def apply_edge_style(attrs, node)
72
+ attrs['style'] = 'dashed' if DASHED_TYPES.include?(node.member_end_type)
73
+ end
74
+
75
+ def apply_edge_direction(attrs, node)
76
+ attrs['dir'] = if node.owner_end_type && node.member_end_type
77
+ 'both'
78
+ elsif node.owner_end_type
79
+ 'back'
80
+ else
81
+ 'direct'
82
+ end
83
+ end
84
+
85
+ def apply_edge_labels(attrs, node)
86
+ apply_verb_label(attrs, node)
87
+ apply_direction_label(attrs, node)
88
+ apply_endpoint_labels(attrs, node)
89
+ end
90
+
91
+ def apply_verb_label(attrs, node)
92
+ attrs['label'] = node.action.verb if node&.action&.verb
93
+ end
94
+
95
+ def apply_direction_label(attrs, node)
96
+ return unless node&.action&.direction
97
+
98
+ template = DIRECTION_LABELS[node.action.direction]
99
+ attrs['label'] = template % attrs['label'] if template
100
+ end
101
+
102
+ def apply_endpoint_labels(attrs, node)
103
+ if node.owner_end_attribute_name
104
+ attrs['headlabel'] = format_label(
105
+ node.owner_end_attribute_name, node.owner_end_cardinality
106
+ )
107
+ end
108
+
109
+ return unless node.member_end_attribute_name
110
+
111
+ attrs['taillabel'] = format_label(
112
+ node.member_end_attribute_name, node.member_end_cardinality
113
+ )
114
+ end
115
+
116
+ def apply_arrow_types(attrs, node)
117
+ attrs['arrowtail'] = ARROW_TYPES.fetch(node.owner_end_type, 'onormal')
118
+ attrs['arrowhead'] = ARROW_TYPES.fetch(node.member_end_type, 'onormal')
119
+ end
120
+
121
+ def maybe_swap_labels(attrs)
122
+ return unless attrs['dir'] == 'back' && attrs['arrowtail'] != 'vee'
123
+
124
+ attrs['arrowhead'], attrs['arrowtail'] = [attrs['arrowtail'], attrs['arrowhead']]
125
+ attrs['headlabel'], attrs['taillabel'] = [attrs['taillabel'], attrs['headlabel']]
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module Lutaml
6
+ module Formatter
7
+ # Emits Graphviz DOT source for LML diagrams. Pure text generation —
8
+ # no graphviz binary, no ruby-graphviz: rendering the emitted DOT
9
+ # rides etassia's pure-Ruby DOT support (etassia#497) or an external
10
+ # renderer at the consumer's discretion.
11
+ class Graphviz < Base
12
+ autoload :HtmlBuilder, "lutaml/lml/formatter/graphviz/html_builder"
13
+ autoload :NodeFormatter, "lutaml/lml/formatter/graphviz/node_formatter"
14
+ autoload :RelationshipFormatter, "lutaml/lml/formatter/graphviz/relationship_formatter"
15
+ autoload :DocumentFormatter, "lutaml/lml/formatter/graphviz/document_formatter"
16
+
17
+ include HtmlBuilder
18
+ include NodeFormatter
19
+ include RelationshipFormatter
20
+ include DocumentFormatter
21
+
22
+ class Attributes < Hash
23
+ def to_s
24
+ to_a
25
+ .reject { |(_k, val)| val.nil? }
26
+ .map { |(a, b)| "#{a}=#{b.inspect}" }
27
+ .join(' ')
28
+ end
29
+ end
30
+
31
+ DEFAULT_CLASS_FONT = 'Helvetica'
32
+
33
+ # Output formats: DOT source today; SVG (and friends) once etassia
34
+ # lands DOT rendering (etassia#497).
35
+ VALID_TYPES = %i[dot].freeze
36
+
37
+ def initialize(attributes = {})
38
+ super
39
+ setup_default_attributes
40
+ @type = :dot
41
+ end
42
+
43
+ attr_reader :graph, :edge, :node
44
+
45
+ def type=(value)
46
+ super
47
+ @type = :dot unless VALID_TYPES.include?(@type)
48
+ end
49
+
50
+ def format(node)
51
+ dot = super.lines.map(&:rstrip).join("\n")
52
+ generate_from_dot(dot)
53
+ end
54
+
55
+ def setup_default_attributes
56
+ @graph = build_graph_attrs
57
+ @edge = build_edge_attrs
58
+ @node = build_node_attrs
59
+ end
60
+
61
+ def build_graph_attrs
62
+ Attributes.new.tap do |g|
63
+ g['splines'] = 'ortho'
64
+ g['pad'] = '0.5'
65
+ g['ranksep'] = '1.2'
66
+ g['nodesep'] = '1.2'
67
+ g['rankdir'] = 'BT'
68
+ end
69
+ end
70
+
71
+ def build_edge_attrs
72
+ Attributes.new.tap { |e| e['color'] = 'gray50' }
73
+ end
74
+
75
+ def build_node_attrs
76
+ Attributes.new.tap { |n| n['shape'] = 'box' }
77
+ end
78
+
79
+ private :setup_default_attributes, :build_graph_attrs,
80
+ :build_edge_attrs, :build_node_attrs
81
+
82
+ protected
83
+
84
+ def generate_from_dot(input)
85
+ input
86
+ end
87
+
88
+ def generate_graph_name(name)
89
+ name.gsub(/[^0-9a-zA-Z]/i, '')
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Formatter
5
+ class SvgPdf
6
+ # Minimal SVG path data parser: absolute and relative M/m, L/l,
7
+ # H/h, V/v, C/c, Z/z — enough for ELK geometry and arrow shapes.
8
+ class PathParser
9
+ Point = Struct.new(:x, :y)
10
+ Line = Struct.new(:point) do
11
+ def last_point
12
+ point
13
+ end
14
+ end
15
+ Curve = Struct.new(:c1, :c2, :point) do
16
+ def last_point
17
+ point
18
+ end
19
+ end
20
+ Close = Struct.new(:last_point)
21
+
22
+ attr_reader :subpaths
23
+
24
+ def initialize(d, dx, dy)
25
+ @dx = dx
26
+ @dy = dy
27
+ parse(d)
28
+ end
29
+
30
+ private
31
+
32
+ def parse(d)
33
+ tokens = d.scan(/[MmLlHhVvCcZz]|-?\d*\.?\d+(?:[eE][-+]?\d+)?/)
34
+ current = Point.new(0.0, 0.0)
35
+ start = current
36
+ @subpaths = [[]]
37
+ i = 0
38
+ while i < tokens.size
39
+ cmd = tokens[i]
40
+ i += 1
41
+ case cmd
42
+ when 'Z', 'z'
43
+ @subpaths.last << Close.new(start)
44
+ current = start.dup
45
+ when 'M', 'm'
46
+ current, start = move_to(tokens, i, current, cmd == 'm')
47
+ i += 2
48
+ @subpaths << [] if cmd == 'm'
49
+ when 'L', 'l'
50
+ current = line_to(tokens, i, current, cmd == 'l')
51
+ i += 2
52
+ when 'H', 'h'
53
+ current = h_line(tokens, i, current, cmd == 'h')
54
+ i += 1
55
+ when 'V', 'v'
56
+ current = v_line(tokens, i, current, cmd == 'v')
57
+ i += 1
58
+ when 'C', 'c'
59
+ current = curve_to(tokens, i, current, cmd == 'c')
60
+ i += 6
61
+ end
62
+ end
63
+ finish
64
+ end
65
+
66
+ def move_to(tokens, i, current, relative)
67
+ x = num(tokens, i)
68
+ y = num(tokens, i + 1)
69
+ point = relative ? Point.new(current.x + x, current.y + y) : Point.new(x, y)
70
+ [point, point.dup]
71
+ end
72
+
73
+ def line_to(tokens, i, current, relative)
74
+ x = num(tokens, i)
75
+ y = num(tokens, i + 1)
76
+ point = relative ? Point.new(current.x + x, current.y + y) : Point.new(x, y)
77
+ @subpaths.last << Line.new(point.dup)
78
+ point
79
+ end
80
+
81
+ def h_line(tokens, i, current, relative)
82
+ x = num(tokens, i) + (relative ? current.x : 0)
83
+ point = Point.new(x, current.y)
84
+ @subpaths.last << Line.new(point.dup)
85
+ point
86
+ end
87
+
88
+ def v_line(tokens, i, current, relative)
89
+ y = num(tokens, i) + (relative ? current.y : 0)
90
+ point = Point.new(current.x, y)
91
+ @subpaths.last << Line.new(point.dup)
92
+ point
93
+ end
94
+
95
+ def curve_to(tokens, i, current, relative)
96
+ c1 = Point.new(num(tokens, i), num(tokens, i + 1))
97
+ c2 = Point.new(num(tokens, i + 2), num(tokens, i + 3))
98
+ to = Point.new(num(tokens, i + 4), num(tokens, i + 5))
99
+ if relative
100
+ c1 = Point.new(c1.x + current.x, c1.y + current.y)
101
+ c2 = Point.new(c2.x + current.x, c2.y + current.y)
102
+ to = Point.new(to.x + current.x, to.y + current.y)
103
+ end
104
+ @subpaths.last << Curve.new(c1, c2, to)
105
+ to
106
+ end
107
+
108
+ def num(tokens, i)
109
+ tokens[i].to_f
110
+ end
111
+
112
+ def finish
113
+ @subpaths.map { |segments| segments.map { |s| offset(s) } }
114
+ .reject(&:empty?)
115
+ end
116
+
117
+ def offset(segment)
118
+ case segment
119
+ when Line
120
+ Line.new(shift(segment.point))
121
+ when Curve
122
+ Curve.new(shift(segment.c1), shift(segment.c2), shift(segment.point))
123
+ else
124
+ segment
125
+ end
126
+ end
127
+
128
+ def shift(point)
129
+ Point.new(point.x + @dx, point.y + @dy)
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end