Class: Pacer::PacerGraph

Inherits:
Object
  • Object
show all
Includes:
Core::Graph::GraphIndexRoute, Core::Graph::GraphRoute, Core::Route, GraphTransactionsMixin, BulkJob, ElementType, Encoding, Indices, KeyIndices, Naming
Defined in:
lib/pacer/graph/pacer_graph.rb

Defined Under Namespace

Modules: BulkJob, ElementType, Encoding, Indices, KeyIndices, Naming

Instance Attribute Summary collapse

Attributes included from BulkJob

#in_bulk_job

Attributes included from Naming

#edge_name, #vertex_name

Attributes included from Core::Graph::GraphIndexRoute

#choose_best_index, #never_scan, #search_manual_indices

Attributes included from Core::Route

#hide_elements, #info, #lookahead_replacement, #pipe_args, #pipe_class, #remove_from_lookahead, #route_name

Attributes included from GraphTransactionsMixin

#disable_transactions, #implicit_transaction, #on_commit_block

Instance Method Summary collapse

Methods included from ElementType

#element_type, #element_type?

Methods included from KeyIndices

#build_key_index_parameters_from, #create_key_index, #drop_key_index, #edges_by_key, #key_indices, #vertices_by_key

Methods included from Indices

#drop_index, #drop_temp_index, #drop_temp_indices, #index, #index_class, #index_class?, #indices, #temp_index

Methods included from BulkJob

#bulk_job_size, #bulk_job_size=, #in_bulk_job?

Methods included from Encoding

#decode_property, #encode_property

Methods included from Core::Graph::GraphIndexRoute

#e, #v

Methods included from Core::Graph::GraphRoute

#==, #e, #filter, #hide_elements, #result, #root?, #v

Methods included from Core::Route

#-, #==, #add_extensions, #branch, #chain_route, #cond, #description, #detach, #each, #empty?, #except, #from_graph?, #gather, #help, #no_extensions, #only, #paths, #pipe, #random, #root?, #route, #scatter, #set_extensions, #set_pipe_source, #set_wrapper, #vars

Methods included from GraphTransactionsMixin

#close_implicit_transaction, #commit_implicit_transaction, #in_read_transaction?, #in_transaction?, #on_commit, #read_transaction, #reopen_read_transaction, #rollback_implicit_transaction, #transaction

Constructor Details

#initialize(encoder, open, shutdown = nil, graph_id = nil) ⇒ PacerGraph

Returns a new instance of PacerGraph.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pacer/graph/pacer_graph.rb', line 15

def initialize(encoder, open, shutdown = nil, graph_id = nil)
  self.base_vertex_wrapper = Pacer::Wrappers::VertexWrapper
  self.base_edge_wrapper = Pacer::Wrappers::EdgeWrapper
  if open.is_a? Proc
    @reopen = open
  else
    @reopen = proc { open }
  end
  @shutdown = shutdown
  reopen
  @encoder = encoder
  @graph_id = graph_id
end

Instance Attribute Details

#base_edge_wrapperObject

Returns the value of attribute base_edge_wrapper.



13
14
15
# File 'lib/pacer/graph/pacer_graph.rb', line 13

def base_edge_wrapper
  @base_edge_wrapper
end

#base_vertex_wrapperObject

Returns the value of attribute base_vertex_wrapper.



13
14
15
# File 'lib/pacer/graph/pacer_graph.rb', line 13

def base_vertex_wrapper
  @base_vertex_wrapper
end

#blueprints_graphObject (readonly)

Returns the value of attribute blueprints_graph.



11
12
13
# File 'lib/pacer/graph/pacer_graph.rb', line 11

def blueprints_graph
  @blueprints_graph
end

#encoderObject

Returns the value of attribute encoder.



13
14
15
# File 'lib/pacer/graph/pacer_graph.rb', line 13

def encoder
  @encoder
end

Instance Method Details

#create_edge(id, from_v, to_v, label, *args) ⇒ Object

TODO:

make id param optional

Create an edge in the graph.

Parameters:

  • id (element id)

    some graphs allow you to specify your own edge id.

  • from_v (Pacer::Wrappers::VertexWrapper)

    the new edge’s out_vertex

  • to_v (Pacer::Wrappers::VertexWrapper)

    the new edge’s in_vertex

  • label (#to_s)

    the edge label

  • *args (extension, Hash)

    extension (Module/Class) arguments will be added to the returned edge. A Hash will be treated as element properties.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/pacer/graph/pacer_graph.rb', line 163

def create_edge(id, from_v, to_v, label, *args)
  _, wrapper, modules, props = id_modules_properties(args)
  raw_edge = creating_elements { blueprints_graph.addEdge(id, from_v.element, to_v.element, label.to_s) }
  if wrapper
    edge = wrapper.new graph, raw_edge
  else
    edge = base_edge_wrapper.new graph, raw_edge
  end
  if modules.any?
    edge = edge.add_extensions modules
  end
  props.each { |k, v| edge[k.to_s] = v } if props
  edge
end

#create_vertex(*args) ⇒ Object #create_vertex(id, *args) ⇒ Object

Create a vertex in the graph.

Overloads:

  • #create_vertex(*args) ⇒ Object

    Parameters:

    • *args (extension, Hash)

      extension (Module/Class) arguments will be added to the current vertex. A Hash will be treated as element properties.

  • #create_vertex(id, *args) ⇒ Object

    Parameters:

    • id (element id)

      the desired element id. Some graphs ignore this.

    • *args (extension, Hash)

      extension (Module/Class) arguments will be added to the current vertex. A Hash will be treated as element properties.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pacer/graph/pacer_graph.rb', line 137

def create_vertex(*args)
  id, wrapper, modules, props = id_modules_properties(args)
  raw_vertex = creating_elements { blueprints_graph.addVertex(id) }
  if wrapper
    vertex = wrapper.new graph, raw_vertex
  else
    vertex = base_vertex_wrapper.new graph, raw_vertex
  end
  if modules.any?
    vertex = vertex.add_extensions modules
  end
  props.each { |k, v| vertex[k.to_s] = v } if props
  vertex
end

#edge(id) ⇒ Object #edge(id, *modules) ⇒ Object

Get an edge by id.

Overloads:

  • #edge(id) ⇒ Object

    Parameters:

    • id (element id)
  • #edge(id, *modules) ⇒ Object

    Parameters:

    • id (element id)
    • *modules (Module, Class)

      extensions to add to the returned edge.



100
101
102
103
104
105
106
# File 'lib/pacer/graph/pacer_graph.rb', line 100

def edge(id, *modules)
  begin
    v = blueprints_graph.getEdge(id)
  rescue Java::JavaLang::RuntimeException
  end
  wrap_element v, base_edge_wrapper, Pacer::Wrappers::EdgeWrapper, modules
end

#equals(other) ⇒ Object



72
73
74
# File 'lib/pacer/graph/pacer_graph.rb', line 72

def equals(other)
  other.class == self.class and graph_id == other.graph_id
end

#featuresObject



208
209
210
# File 'lib/pacer/graph/pacer_graph.rb', line 208

def features
  blueprints_graph.getFeatures
end

#graphPacerGraph

The current graph

Returns:



32
33
34
# File 'lib/pacer/graph/pacer_graph.rb', line 32

def graph
  self
end

#graph_idObject



68
69
70
# File 'lib/pacer/graph/pacer_graph.rb', line 68

def graph_id
  @graph_id ||= blueprints_graph.object_id
end

#inspectObject



212
213
214
# File 'lib/pacer/graph/pacer_graph.rb', line 212

def inspect
  "#<PacerGraph #{ blueprints_graph.to_s }"
end

#load_edges(ids) ⇒ [Pacer::Wrappers::EdgeWrapper]

Directly loads an array of edges by id.

Parameters:

Returns:



202
203
204
205
206
# File 'lib/pacer/graph/pacer_graph.rb', line 202

def load_edges(ids)
  ids.map do |id|
    edge id
  end.compact
end

#load_vertices(ids) ⇒ [Pacer::Wrappers::VertexWrapper]

Directly loads an array of vertices by id.

Parameters:

Returns:



192
193
194
195
196
# File 'lib/pacer/graph/pacer_graph.rb', line 192

def load_vertices(ids)
  ids.map do |id|
    vertex id
  end.compact
end

#remove_edge(edge) ⇒ Object

This method accepts only an unwrapped Blueprints Edge



184
185
186
# File 'lib/pacer/graph/pacer_graph.rb', line 184

def remove_edge(edge)
  blueprints_graph.removeEdge edge
end

#remove_vertex(vertex) ⇒ Object

This method accepts only an unwrapped Blueprints Vertex



179
180
181
# File 'lib/pacer/graph/pacer_graph.rb', line 179

def remove_vertex(vertex)
  blueprints_graph.removeVertex vertex
end

#reopenObject



46
47
48
49
# File 'lib/pacer/graph/pacer_graph.rb', line 46

def reopen
  @blueprints_graph = unwrap_graph @reopen.call
  self
end

#shutdownObject



51
52
53
54
55
# File 'lib/pacer/graph/pacer_graph.rb', line 51

def shutdown
  @shutdown.call self if @shutdown
  @blueprints_graph = nil
  self
end

#use_encoder(encoder) ⇒ Object



64
65
66
# File 'lib/pacer/graph/pacer_graph.rb', line 64

def use_encoder(encoder)
  PacerGraph.new encoder, @reopen, @shutdown
end

#use_wrapper(klass) ⇒ Object



57
58
59
60
61
62
# File 'lib/pacer/graph/pacer_graph.rb', line 57

def use_wrapper(klass)
  reopen = proc do
    klass.new unwrap_graph(@reopen.call)
  end
  PacerGraph.new encoder, reopen, @shutdown
end

#vendor(full = false) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/pacer/graph/pacer_graph.rb', line 36

def vendor(full = false)
  g = blueprints_graph
  g = g.raw_graph if g.respond_to? :raw_graph
  if full
    g.java_class.name
  else
    g.java_class.name.split('.')[1]
  end
end

#vertex(id) ⇒ Object #vertex(id, *modules) ⇒ Object

Get a vertex by id.

Overloads:

  • #vertex(id) ⇒ Object

    Parameters:

    • id (element id)
  • #vertex(id, *modules) ⇒ Object

    Parameters:

    • id (element id)
    • *modules (Module, Class)

      extensions to add to the returned vertex.



84
85
86
87
88
89
90
# File 'lib/pacer/graph/pacer_graph.rb', line 84

def vertex(id, *modules)
  begin
    v = blueprints_graph.getVertex(id)
  rescue java.lang.RuntimeException
  end
  wrap_element v, base_vertex_wrapper, Pacer::Wrappers::VertexWrapper, modules
end

#wrap_element(v, base, klass, modules) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pacer/graph/pacer_graph.rb', line 108

def wrap_element(v, base, klass, modules)
  if v
    if modules
      wrapper = modules.detect { |obj| obj.ancestors.include? klass }
      if wrapper
        v = wrapper.new graph, v
        modules.delete wrapper
      else
        v = base.new graph, v
      end
      v.add_extensions modules
    else
      base.new graph, v
    end
  end
end