Class: ValueGraphTransformation::DotCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/value_graph_transformation/dot_compiler.rb

Overview

Compiles Context with Value and Function objects into dot code.

Defined Under Namespace

Classes: Selection

Constant Summary

DEFAULT_VALUE_COLOR =

Returns the default background color of Value nodes

Returns:

  • (String)

    the default background color of Value nodes

"white"

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DotCompiler) initialize(context)

Returns a new instance of DotCompiler



14
15
16
17
# File 'lib/value_graph_transformation/dot_compiler.rb', line 14

def initialize(context)
  @context = context
  @selections = []
end

Class Method Details

+ (String) to_svg(dot)

Returns the given string compiled into an SVG element.

Parameters:

  • dot (String)

    the string in dot format to compile.

Returns:

  • (String)

    the given string compiled into an SVG element.

Raises:

  • (Error)

    if GraphViz is not installed on host.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/value_graph_transformation/dot_compiler.rb', line 38

def self.to_svg(dot)
  svg = nil
  fail_msg = nil
  begin
    Open3.popen3("dot -Tsvg") { |stdin, stdout, stderr, wait_thr|
      stdin.print(dot)
      stdin.close
      svg = stdout.read
      fail_msg = stderr.read
      exit_status = wait_thr.value # Process::Status object returned.
    }
  rescue => err
    raise "Could not execute dot, perhaps GraphViz is not installed?"
  end

  raise fail_msg unless fail_msg == ""

  return svg
end

Instance Method Details

- (String) caption

Returns the labels for the color descriptions.

Returns:

  • (String)

    the labels for the color descriptions.

Specifications:

When there are no vertex selections

It returns an empty string



156
157
158
159
160
161
162
163
164
# File 'lib/value_graph_transformation/dot_compiler.rb', line 156

def caption
  return "" if @selections.empty?

  labels = @selections.collect{|selection|
    "<tr><td align=\"left\" bgcolor=\"#{selection.color}\">#{selection.text}</td></tr>"
  }

  "label=<<table border=\"0\">#{labels.join}</table>>"
end

- (Hash<Vertex,String>) color_map

Returns a mapping between each vertex in its dot color string.

Returns:

  • (Hash<Vertex,String>)

    a mapping between each vertex in its dot color string.

Specifications:

it returns a Hash.

When there are no vertex selections

each vertex in Context is mapped to default color string

When some vertices are selected

the selected vertices are mapped to the given color string

When a vertex is selected more than once

it is mapped to a string containing all colors separated by a colon

red:green:blue


88
89
90
91
92
93
94
95
96
97
# File 'lib/value_graph_transformation/dot_compiler.rb', line 88

def color_map
  colors = Hash.new
  selected = selection_colors

  @context.vertices.each{|vertex|
    colors[vertex] = (selected[vertex].empty?) ? DEFAULT_VALUE_COLOR
                                               : selected[vertex].join(':')
  }
  colors
end

- (Hash<String,String>) graph_properties

Returns a hash of the properties of the digraph element.

Returns:

  • (Hash<String,String>)

    a hash of the properties of the digraph element.

Specifications:

returns a Hash with the properties of the digraph element

{"rankdir"=>"LR", "bgcolor"=>"#FFFFFF00"}


148
149
150
151
152
153
# File 'lib/value_graph_transformation/dot_compiler.rb', line 148

def graph_properties
  {
    'rankdir' => 'LR',
    'bgcolor' => '#FFFFFF00'
  }
end

- (Hash<String,String>) node_properties(vertex)

Returns a hash of the dot properties of the vertex.

Parameters:

  • vertex (Vertex)

Returns:

  • (Hash<String,String>)

    a hash of the dot properties of the vertex.

Specifications:

returns a Hash with the properties of a vertex

A value node

{"label"=>"a", "shape"=>"oval", "style"=>"filled", "gradientangle"=>45, "width"=>0.3, "height"=>0.3}

A function node

{"label"=>"+", "shape"=>"square", "style"=>"filled", "width"=>0.3, "height"=>0.3}


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/value_graph_transformation/dot_compiler.rb', line 122

def node_properties(vertex)
  case vertex
  when Value
    {
      'label' => vertex.identifiers.join(','),
      'shape' => (vertex.identifiers.empty?) ? 'circle' : 'oval',
      'style' => 'filled',
      'gradientangle' => 45,
      'width' => 0.3,
      'height' => 0.3
    }
  when Function
    {
      'label' => vertex.symbol,
      'shape' => 'square',
      'style' => 'filled',
      'width' => 0.3,
      'height' => 0.3
    }
  else
    fail
  end
end

- (Object) propertify(map)

return [String] the keys/values of the hash converted to dot property format: i.e.:

'property1="foo" property2="bar"'

Parameters:

  • map (Hash)

Specifications:

It converts the given Hash to a string in dot property format

Given the hash:

{"property1"=>"foo", "property2"=>"bar"}

The expected string:

property1="foo" property2="bar"


116
117
118
# File 'lib/value_graph_transformation/dot_compiler.rb', line 116

def propertify(map)
  map.collect{|property,value| "#{property}=\"#{value}\""}.join(' ')
end

- (Selection) select(vertices, text, color)

Set a background color with caption to a specific set of vertices.

Parameters:

  • vertices (Array<Vertex>)

    the vertices to select.

  • text (String)

    the caption for the set.

  • color (String)

    the color string for the selection.

Returns:



25
26
27
# File 'lib/value_graph_transformation/dot_compiler.rb', line 25

def select(vertices, text, color)
  @selections << Selection.new(vertices, text, color)
end

- (Hash<Vertex,Array<String>>) selection_colors

Returns a mapping between each vertex contained within a selection and its colors

Returns:

  • (Hash<Vertex,Array<String>>)

    a mapping between each vertex contained within a selection and its colors

Specifications:

When there are no vertex selections

it returns an empty hash.

When some vertices are selected

the selected vertices should be keys in the hash.

each value is an Array containing the given color string.

When a vertex is selected more than once

the array should contain color strings from all its selections



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/value_graph_transformation/dot_compiler.rb', line 101

def selection_colors
  colors = Hash.new{|hash,key| hash[key] = [] }

  @selections.each{|selection|
    selection.vertices.each{|vertex|
      colors[vertex] << selection.color
    }
  }

  colors
end

- (String) to_dot

Returns the Context compiled into dot code

Returns:

  • (String)

    the Context compiled into dot code

Specifications:

It compiles the context into a String in dot format.

digraph {
	rankdir="LR" bgcolor="#FFFFFF00";
	node0 [label="a" shape="oval" style="filled" gradientangle="45" width="0.3" height="0.3" fillcolor="white"];
	node1 [label="b" shape="oval" style="filled" gradientangle="45" width="0.3" height="0.3" fillcolor="white"];
	node2 [label="c" shape="oval" style="filled" gradientangle="45" width="0.3" height="0.3" fillcolor="white"];
	node3 [label="+" shape="square" style="filled" width="0.3" height="0.3" fillcolor="white"];
	node0->node3;
	node1->node3;
	node3->node2;
}


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/value_graph_transformation/dot_compiler.rb', line 59

def to_dot
  ids = vertex_ids
  colors = color_map

  nodes = @context.vertices.collect{|vertex|
    properties = node_properties(vertex)
    properties['fillcolor'] = colors[vertex]
    "\t#{ids[vertex]} [#{propertify(properties)}];"
  }

  edges = @context.edges.collect{|edge|
    "\t#{ids[edge.source]}->#{ids[edge.target]};"
  }

  header = "\t#{caption}#{propertify(graph_properties)};\n"

  "digraph {\n#{header}#{(nodes+edges).join("\n")}\n}"
end

- (String) to_svg

Returns the Context compiled into an SVG element

Returns:

  • (String)

    the Context compiled into an SVG element

Raises:

  • (Error)

    if GraphViz is not installed on host.

Specifications:

It compiles the context into a String in svg format.

%3 node0 a node3 + node0->node3 node1 b node1->node3 node2 c node3->node2


31
32
33
# File 'lib/value_graph_transformation/dot_compiler.rb', line 31

def to_svg
  DotCompiler.to_svg(to_dot)
end

- (Hash<Vertex,String>) vertex_ids

Returns a mapping between each vertex and its dot id.

Returns:

  • (Hash<Vertex,String>)

    a mapping between each vertex and its dot id.

Specifications:

It returns a Hash

Each vertex in the context is mapped to a unique id string



79
80
81
82
83
84
85
# File 'lib/value_graph_transformation/dot_compiler.rb', line 79

def vertex_ids
  ids = {}
  @context.vertices.each_with_index{|vertex, index|
    ids[vertex] = "node#{index}"
  }
  ids
end