Class: Orbacle::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/orbacle/graph.rb

Defined Under Namespace

Classes: Lambda, Metod, MetodGraph, Yield, ZSuper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/orbacle/graph.rb', line 13

def initialize
  @original = RGL::DirectedAdjacencyGraph.new
  @reversed = RGL::DirectedAdjacencyGraph.new

  @global_variables = {}
  @constants = {}
  @main_ivariables = {}
  @instance_ivariables = {}
  @class_ivariables = {}
  @cvariables = {}
  @metods = {}
  @lambdas = {}
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



27
28
29
# File 'lib/orbacle/graph.rb', line 27

def constants
  @constants
end

Instance Method Details

#add_edge(x, y) ⇒ Object



44
45
46
47
48
# File 'lib/orbacle/graph.rb', line 44

def add_edge(x, y)
  raise if x.nil? || y.nil?
  @original.add_edge(x, y)
  @reversed.add_edge(y, x)
end

#add_edges(nodes_source, nodes_target) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/orbacle/graph.rb', line 36

def add_edges(nodes_source, nodes_target)
  Array(nodes_source).each do |source|
    Array(nodes_target).each do |target|
      add_edge(source, target)
    end
  end
end

#add_vertex(node) ⇒ Object



29
30
31
32
33
34
# File 'lib/orbacle/graph.rb', line 29

def add_vertex(node)
  raise if node.nil?
  @original.add_vertex(node)
  @reversed.add_vertex(node)
  node
end

#adjacent_vertices(v) ⇒ Object



58
59
60
# File 'lib/orbacle/graph.rb', line 58

def adjacent_vertices(v)
  @original.adjacent_vertices(v)
end

#edgesObject



50
51
52
# File 'lib/orbacle/graph.rb', line 50

def edges
  @original.edges
end

#get_class_level_ivar_definition_node(scope, ivar_name) ⇒ Object



91
92
93
94
95
# File 'lib/orbacle/graph.rb', line 91

def get_class_level_ivar_definition_node(scope, ivar_name)
  class_ivariables[scope.absolute_str] ||= {}
  class_ivariables[scope.absolute_str][ivar_name] ||= add_vertex(Node.new(:clivar_definition, {}))
  return class_ivariables[scope.absolute_str][ivar_name]
end

#get_constant_definition_node(const_name) ⇒ Object



80
81
82
83
# File 'lib/orbacle/graph.rb', line 80

def get_constant_definition_node(const_name)
  constants[const_name] ||= add_vertex(Node.new(:const_definition, {}))
  return constants[const_name]
end

#get_cvar_definition_node(scope, ivar_name) ⇒ Object



97
98
99
100
101
# File 'lib/orbacle/graph.rb', line 97

def get_cvar_definition_node(scope, ivar_name)
  cvariables[scope.absolute_str] ||= {}
  cvariables[scope.absolute_str][ivar_name] ||= add_vertex(Node.new(:cvar_definition, {}))
  return cvariables[scope.absolute_str][ivar_name]
end

#get_gvar_definition_node(gvar_name) ⇒ Object



70
71
72
73
# File 'lib/orbacle/graph.rb', line 70

def get_gvar_definition_node(gvar_name)
  global_variables[gvar_name] ||= add_vertex(Node.new(:gvar_definition, {}))
  return global_variables[gvar_name]
end

#get_ivar_definition_node(scope, ivar_name) ⇒ Object



85
86
87
88
89
# File 'lib/orbacle/graph.rb', line 85

def get_ivar_definition_node(scope, ivar_name)
  instance_ivariables[scope.absolute_str] ||= {}
  instance_ivariables[scope.absolute_str][ivar_name] ||= add_vertex(Node.new(:ivar_definition, {}))
  return instance_ivariables[scope.absolute_str][ivar_name]
end

#get_lambda_nodes(lambda_id) ⇒ Object



132
133
134
# File 'lib/orbacle/graph.rb', line 132

def get_lambda_nodes(lambda_id)
  return lambdas[lambda_id]
end

#get_main_ivar_definition_node(ivar_name) ⇒ Object



75
76
77
78
# File 'lib/orbacle/graph.rb', line 75

def get_main_ivar_definition_node(ivar_name)
  main_ivariables[ivar_name] ||= add_vertex(Node.new(:ivar_definition, {}))
  return main_ivariables[ivar_name]
end

#get_metod_nodes(method_id) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/orbacle/graph.rb', line 103

def get_metod_nodes(method_id)
  metod = metods[method_id]
  if metod.instance_of?(Metod)
    metod
  else
    new_nodes = metod.all_nodes.map(&:clone)
    mapping = metod.all_nodes.zip(new_nodes).to_h
    new_nodes.each(&method(:add_vertex))
    metod.all_edges.each do |v1, v2|
      add_edge(mapping.fetch(v1), mapping.fetch(v2))
    end
    new_arguments_nodes = metod.args.each_with_object({}) do |(k, v), h|
      h[k] = mapping.fetch(v)
    end
    new_yields = metod.yields.map {|y| Yield.new(y.send_args.map {|a| mapping.fetch(a) }, mapping.fetch(y.send_result)) }
    Metod.new(new_arguments_nodes, mapping.fetch(metod.result), new_yields, [], mapping.fetch(metod.caller_node))
  end
end

#has_edge?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/orbacle/graph.rb', line 66

def has_edge?(x, y)
  @original.has_edge?(x, y)
end

#parent_vertices(v) ⇒ Object



62
63
64
# File 'lib/orbacle/graph.rb', line 62

def parent_vertices(v)
  @reversed.adjacent_vertices(v)
end

#store_lambda_nodes(lambda_id, arguments_nodes, result_node) ⇒ Object



136
137
138
# File 'lib/orbacle/graph.rb', line 136

def store_lambda_nodes(lambda_id, arguments_nodes, result_node)
  lambdas[lambda_id] ||= Lambda.new(arguments_nodes, result_node)
end

#store_metod_nodes(metod_id, arguments_nodes) ⇒ Object



122
123
124
125
# File 'lib/orbacle/graph.rb', line 122

def store_metod_nodes(metod_id, arguments_nodes)
  raise if !arguments_nodes.is_a?(Hash)
  metods[metod_id] ||= Metod.new(arguments_nodes, add_vertex(Node.new(:method_result, {})), [], [], nil)
end

#store_metod_subgraph(metod_id, arguments_nodes, caller_node, result_node, yields, all_nodes, all_edges) ⇒ Object



127
128
129
130
# File 'lib/orbacle/graph.rb', line 127

def store_metod_subgraph(metod_id, arguments_nodes, caller_node, result_node, yields, all_nodes, all_edges)
  raise if !arguments_nodes.is_a?(Hash)
  metods[metod_id] ||= MetodGraph.new(arguments_nodes, result_node, yields, caller_node, all_nodes, all_edges)
end

#verticesObject



54
55
56
# File 'lib/orbacle/graph.rb', line 54

def vertices
  @original.vertices
end