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.



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

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#data(xml, key, value) ⇒ Object



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

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

#data_type(xml, key, type) ⇒ Object



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

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

#docs(xml, docs) ⇒ Object



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

def docs(xml, docs)
  docs.each do |doc|
    data(xml, "documentation", doc)
  end
end

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



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

def edge(xml, id, source, target, type, name = nil, docs = [], 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



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

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

#layers(xml, layers) ⇒ Object



97
98
99
100
101
# File 'lib/archimate/export/graph_ml.rb', line 97

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

#name(xml, name_str) ⇒ Object



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

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

#next_edge_idObject



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

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

#next_prop_idObject



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

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

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



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

def node(xml, id, name, type, docs = [], properties = [], other_data = {})
  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



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

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

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

#properties(xml, source_id, properties) ⇒ Object



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

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



103
104
105
106
107
# File 'lib/archimate/export/graph_ml.rb', line 103

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



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
# File 'lib/archimate/export/graph_ml.rb', line 14

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