Class: Archimate::Export::GraphML

Inherits:
Object
  • Object
show all
Defined in:
lib/archimate/export/graph_ml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ GraphML

Returns a new instance of GraphML.



11
12
13
# File 'lib/archimate/export/graph_ml.rb', line 11

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/archimate/export/graph_ml.rb', line 9

def model
  @model
end

Instance Method Details

#data(xml, key, value) ⇒ Object



71
72
73
# File 'lib/archimate/export/graph_ml.rb', line 71

def data(xml, key, value)
  xml.data(key: key) { xml.text(value) unless value.nil? }
end

#data_type(xml, key, type) ⇒ Object



67
68
69
# File 'lib/archimate/export/graph_ml.rb', line 67

def data_type(xml, key, type)
  data(xml, key, type.nil? ? nil : type.sub("archimate:", ""))
end

#docs(xml, docs) ⇒ Object



84
85
86
87
88
89
# File 'lib/archimate/export/graph_ml.rb', line 84

def docs(xml, docs)
  return unless docs
  docs.langs.each do |lang|
    data(xml, "documentation", docs.by_lang(lang))
  end
end

#edge(xml, id, source, target, type, name = nil, docs = nil, properties = []) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/archimate/export/graph_ml.rb', line 123

def edge(xml, id, source, target, type, name = nil, docs = nil, properties = [])
  xml.edge(id: id, source: source, target: target, label: type) do
    data_type(xml, "relationship-type", type)
    name(xml, name)
    docs(xml, docs)
  end
  properties(xml, id, properties)
end

#graph_attrs(xml, model) ⇒ Object



57
58
59
60
61
# File 'lib/archimate/export/graph_ml.rb', line 57

def graph_attrs(xml, model)
  name(xml, model.name)
  docs(xml, model.documentation)
  properties(xml, model.id, model.properties)
end

#layers(xml, layers) ⇒ Object



99
100
101
102
103
# File 'lib/archimate/export/graph_ml.rb', line 99

def layers(xml, layers)
  layers.each_with_index do |layer, i|
    node(xml, "layer-#{i}", layer, "Layer")
  end
end

#name(xml, name_str) ⇒ Object



63
64
65
# File 'lib/archimate/export/graph_ml.rb', line 63

def name(xml, name_str)
  data(xml, "name", name_str) unless name_str.nil?
end

#next_edge_idObject



52
53
54
55
# File 'lib/archimate/export/graph_ml.rb', line 52

def next_edge_id
  @edge_id += 1
  "edge-#{@edge_id}"
end

#next_prop_idObject



47
48
49
50
# File 'lib/archimate/export/graph_ml.rb', line 47

def next_prop_id
  @prop_id += 1
  "property-#{@prop_id}"
end

#node(xml, id, name, type, other_data = {}, docs: nil, properties: []) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/archimate/export/graph_ml.rb', line 111

def node(xml, id, name, type, other_data = {}, docs: nil, properties: [])
  xml.node(id: id, label: name, labels: "#{type}:#{name}") do
    data_type(xml, "element-type", type)
    name(xml, name)
    docs(xml, docs)
    other_data.each do |k, v|
      xml.data(key: k) { xml.text(v) }
    end
  end
  properties(xml, id, properties)
end

#nodes(xml, elements) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/archimate/export/graph_ml.rb', line 91

def nodes(xml, elements)
  elements.each do |element|
    node(xml, element.id, element.name, element.type, docs: element.documentation, properties: element.properties)

    @layers[element.layer] << element.id
  end
end

#properties(xml, source_id, properties) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/archimate/export/graph_ml.rb', line 75

def properties(xml, source_id, properties)
  properties.each do |property|
    # TODO: if we want to be slick here, keep a set of property values and point to that instead
    prop_id = next_prop_id
    node(xml, prop_id, property.key, "Property", "property-value" => property.value)
    edge(xml, next_edge_id, source_id, prop_id, "HasProperty", property.key)
  end
end

#relationships(xml, relationships) ⇒ Object



105
106
107
108
109
# File 'lib/archimate/export/graph_ml.rb', line 105

def relationships(xml, relationships)
  relationships.each do |r|
    edge(xml, r.id, r.source, r.target, r.type, r.name, r.documentation, r.properties)
  end
end

#to_graphmlObject



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/archimate/export/graph_ml.rb', line 15

def to_graphml
  @prop_id = 1
  @edge_id = 1
  @layers = Hash.new do |hash, key|
    hash[key] = [] unless hash.key?(key)
    hash[key]
  end
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.graphml(
      "xmlns" => "http://graphml.graphdrawing.org/xmlns",
      "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
      "xsi:schemaLocation" => "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"
    ) do
      xml.key(id: "element-type", for: "node", "attr.name" => "type", "attr.type" => "string")
      xml.key(id: "relationship-type", for: "edge", "attr.name" => "type", "attr.type" => "string")
      xml.key(id: "name", for: "all", "attr.name" => "name", "attr.type" => "string")
      xml.key(id: "documentation", for: "all", "attr.name" => "documentation", "attr.type" => "string")
      xml.key(id: "property-value", for: "all", "attr.name" => "value", "attr.type" => "string")
      xml.graph(
        id: model.id,
        edgedefault: "directed"
      ) do
        graph_attrs(xml, model)
        nodes(xml, model.elements)
        layers(xml, @layers)
        relationships(xml, model.relationships)
      end
    end
  end
  builder.to_xml
end