Class: UrlGraph

Inherits:
RGL::ImplicitGraph
  • Object
show all
Defined in:
lib/graphiclious/url_graph.rb

Overview

UrlGraph models relations between tags

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUrlGraph

Returns a new instance of UrlGraph.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphiclious/url_graph.rb', line 15

def initialize
  @fixed_dot_attributes = DotAttributes.new
  @wrapped_graph = RGL::AdjacencyGraph.new
  @filter = proc { |v| @filter_set.nil? || @filter_set.include?(v) }
  @filter_set = nil
  super { |g|
      g.vertex_iterator { |b|
        @wrapped_graph.each_vertex { |v| b.call(v) if @filter.call(v) }
      }
      g.adjacent_iterator { |v, b|
        @wrapped_graph.each_adjacent(v) { |u| b.call(u) if @filter.call(u) }
      }
   }
end

Instance Attribute Details

#filter_setObject

Returns the value of attribute filter_set.



13
14
15
# File 'lib/graphiclious/url_graph.rb', line 13

def filter_set
  @filter_set
end

#fixed_dot_attributesObject

Returns the value of attribute fixed_dot_attributes.



13
14
15
# File 'lib/graphiclious/url_graph.rb', line 13

def fixed_dot_attributes
  @fixed_dot_attributes
end

Class Method Details

.ankor(url, name) ⇒ Object



46
47
48
# File 'lib/graphiclious/url_graph.rb', line 46

def self.ankor(url, name)
  "<a class=\"extern\" href=\"#{url}\" target=\"_top\">#{name}</a>"
end

.edge_ankor(tag1, tag2, style = nil) ⇒ Object



76
77
78
# File 'lib/graphiclious/url_graph.rb', line 76

def self.edge_ankor(tag1, tag2, style=nil)
  intern_ankor(edge_url(tag1, tag2), edge_name(tag1, tag2), style)
end

.edge_name(tag1, tag2) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/graphiclious/url_graph.rb', line 64

def self.edge_name(tag1, tag2)
  if tag1 <= tag2
    tag1 + '-' + tag2
  else
    tag2 + '-' + tag1    
  end
end

.edge_url(tag1, tag2) ⇒ Object



72
73
74
# File 'lib/graphiclious/url_graph.rb', line 72

def self.edge_url(tag1, tag2)
  node_url(edge_name(tag1, tag2))
end

.intern_ankor(url, name, style = nil) ⇒ Object



50
51
52
53
54
# File 'lib/graphiclious/url_graph.rb', line 50

def self.intern_ankor(url, name, style=nil)
  link = "<a href=\"#{tag2file(url)}\">#{name}</a>"
  link = "<span class=\"#{style}\">#{link}</span>" unless style.nil?
  link
end

.node_ankor(tag, style = nil) ⇒ Object



60
61
62
# File 'lib/graphiclious/url_graph.rb', line 60

def self.node_ankor(tag, style=nil)
  intern_ankor(node_url(tag), tag, style)
end

.node_url(tag) ⇒ Object



56
57
58
# File 'lib/graphiclious/url_graph.rb', line 56

def self.node_url(tag)
  "#{tag2file(tag)}.htm"
end

.tag2file(tag) ⇒ Object

some characters are not allowed in filenames and need to be replaced



39
40
41
42
43
44
# File 'lib/graphiclious/url_graph.rb', line 39

def self.tag2file(tag)
  # Windows: \/:*?"<>|
  # Linux: ()
  # Call to Graphviz: Space 
  tag.gsub(/[\\\/:*?"<>|() ]/, '_')
end

Instance Method Details

#add_edge(tag, otherTag) ⇒ Object



34
35
36
# File 'lib/graphiclious/url_graph.rb', line 34

def add_edge(tag, otherTag)
  @wrapped_graph.add_edge(tag, otherTag)
end

#add_vertex(vertex) ⇒ Object



30
31
32
# File 'lib/graphiclious/url_graph.rb', line 30

def add_vertex(vertex)
  @wrapped_graph.add_vertex(vertex)
end

#d_quote(string) ⇒ Object



92
93
94
# File 'lib/graphiclious/url_graph.rb', line 92

def d_quote(string)
  "\"#{string}\""
end

#fixed_dot_edge_attributesObject



84
85
86
# File 'lib/graphiclious/url_graph.rb', line 84

def fixed_dot_edge_attributes
  fixed_dot_attributes.edge_attributes
end

#fixed_dot_graph_attributesObject



88
89
90
# File 'lib/graphiclious/url_graph.rb', line 88

def fixed_dot_graph_attributes
  fixed_dot_attributes.graph_attributes
end

#fixed_dot_node_attributesObject



80
81
82
# File 'lib/graphiclious/url_graph.rb', line 80

def fixed_dot_node_attributes
  fixed_dot_attributes.node_attributes
end

Output the DOT-graph to a file.



124
125
126
127
128
# File 'lib/graphiclious/url_graph.rb', line 124

def print_dotted_on_file(filename = 'graph.dot', params = {})
  File.open(filename, "w") {|f|
    print_dotted_on(params, f)
  }
end

#to_dot_graph(params = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/graphiclious/url_graph.rb', line 96

def to_dot_graph(params = {})
  fixed_dot_graph_attributes.each { |k, v|
    params[k] ||= v
  }
  graph = (directed? ? DOT::DOTDigraph : DOT::DOTSubgraph).new(params)
  edge_class = directed? ? DOT::DOTDirectedEdge : DOT::DOTEdge
  dot_node_attributes = fixed_dot_node_attributes.clone
  each_vertex do |v|
    name = v.to_s
    dot_node_attributes['name'] = d_quote(name)
    dot_node_attributes['URL'] = d_quote(UrlGraph.node_url(name))
    dot_node_attributes['label'] = name
    graph << DOT::DOTNode.new(dot_node_attributes)
  end
  dot_edge_attributes = fixed_dot_edge_attributes.clone
  each_edge do |u,v|
    tag1 = u.to_s
    tag2 = v.to_s
    dot_edge_attributes['from'] = d_quote(tag1)
    dot_edge_attributes['to'] = d_quote(tag2)
    dot_edge_attributes['URL'] = d_quote(UrlGraph.edge_url(tag1, tag2))
    graph << edge_class.new(dot_edge_attributes)
  end
  graph
end