Class: Twb::Util::GML

Inherits:
Object
  • Object
show all
Includes:
TabTool
Defined in:
lib/twb/util/gml.rb

Constant Summary collapse

@@hasher =
Digest::SHA256.new
@@docfileName =
'./ttdoc/gml.log'
@@gmlHeader =
<<GMLHEADER
graph [
  directed 1
  rankdir "LR"
  splines "line"

GMLHEADER
@@gmlFooter =
"\n]"

Instance Attribute Summary collapse

Attributes included from TabTool

#alerts, #docDir, #docfiles, #funcdoc, #id, #licensed, #logfilename, #logger, #loglevel, #metrics, #properties, #ttdocdir, #type, #uuid

Instance Method Summary collapse

Methods included from TabTool

#addDocFile, #alert, #closeDocFiles, #config, #docFile, #docFileMaxNameLen, #docfilesdoc, #docfilesdocto_s, #emit, #emitCSV, #finis, #hasConfig, #init, #initCSV, #initDocDir, #initLogger, #license=, #licensed?, #loadConfig

Constructor Details

#initializeGML

Returns a new instance of GML.


42
43
44
45
46
47
48
49
# File 'lib/twb/util/gml.rb', line 42

def initialize
  # @docFile = File.open(@@docfileName,'w')
  # @docFile.puts "Starting up the GML process"
  @gmlHeader = @@gmlHeader
  @fileName  = 'gmlFile'      # will be reset in def render when file is generated
  @nodes     = SortedSet.new
  @edges     = SortedSet.new
end

Instance Attribute Details

#edgesObject

Returns the value of attribute edges


40
41
42
# File 'lib/twb/util/gml.rb', line 40

def edges
  @edges
end

#fileNameObject

Returns the value of attribute fileName


40
41
42
# File 'lib/twb/util/gml.rb', line 40

def fileName
  @fileName
end

#gmlHeaderObject

Returns the value of attribute gmlHeader


40
41
42
# File 'lib/twb/util/gml.rb', line 40

def gmlHeader
  @gmlHeader
end

#nodesObject

Returns the value of attribute nodes


40
41
42
# File 'lib/twb/util/gml.rb', line 40

def nodes
  @nodes
end

Instance Method Details

#prepareObject

def init fileName

# do stuff for new GML file here
@fileName = fileName
prepare

end


61
62
63
64
# File 'lib/twb/util/gml.rb', line 61

def prepare
  @nodes = SortedSet.new
  @edges = SortedSet.new
end

#renderObject


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/twb/util/gml.rb', line 66

def render
  # puts "GML.render - rendering into #{@fileName}"
  @fileName = "#{@fileName}.gml"
  file = File.open(docFile(@fileName),'w')
  file.puts @gmlHeader
  renderNodes file
  renderEdges file
  file.puts @@gmlFooter
  file.close
  return file
end

#renderEdges(file) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/twb/util/gml.rb', line 96

def renderEdges file
  edges = Set.new
  @edges.each do |edge|
    gmlSourceID = Digest::MD5.hexdigest(edge.from.id)
    gmlTargetID = Digest::MD5.hexdigest(edge.to.id)
    edges << "edge [\n        source \"#{gmlSourceID}\" \n        target \"#{gmlTargetID}\" \n    ]"
  end
  edges.each do |edge|
    file.puts edge
  end
end

#renderNodes(file) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/twb/util/gml.rb', line 78

def renderNodes file
  nodes = Set.new
  # puts 'def renderNodes'
  @nodes.each do |node|
    gmlID   = Digest::MD5.hexdigest(node.id)
    gmlName = node.name.gsub('&','&amp;').gsub('"','&quot;')
    nodestr = "node [\n        id    \"#{gmlID}\" \n        label \"#{gmlName}\" "
    unless node.colour.nil?
      nodestr += "\n    graphics \n   [ \n      fill  \"#{node.colour}\" \n   ] "
    end
    nodestr += "\n    ]"
    nodes << nodestr
  end
  nodes.each do |node|
    file.puts node
  end
end

#to_sObject


108
109
110
# File 'lib/twb/util/gml.rb', line 108

def to_s
  "file:#{@fileName}; #nodes:#{@nodes.length}; #edges:#{@edges.length}"
end