Class: Pacer::Wrappers::EdgeWrapper

Inherits:
ElementWrapper show all
Includes:
Core::Graph::EdgesRoute, Edge
Defined in:
lib/pacer/transform/branch.rb,
lib/pacer/wrappers/edge_wrapper.rb

Instance Attribute Summary collapse

Attributes inherited from ElementWrapper

#graph

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Graph::EdgesRoute

#both_v, #e, #element_type, #inV, #in_v, #labels, #outV, #out_v, #to_h

Methods inherited from ElementWrapper

#<=>, #[], #[]=, add_extensions, base_edge_wrapper, base_vertex_wrapper, #chain_route, #element_id, #element_payload, extensions, #from_graph?, #initialize, lookup, #properties, #properties=, #property_keys, #reload, #result, route_conditions, wrap

Methods included from Core::Graph::ElementRoute

#[], #build_index, #e, #element_ids, #filter, #payload, #properties, #property?, #raw_property_maps, #result, #subgraph, #typed_property, #v

Methods included from Routes::RouteOperations

#aggregate, #all, #as, #as_var, #at, #breadth_first, #compact, #count, #count_section, #counted, #custom_sort_section, #deepest, #difference_sections, #edges_route?, #fast_group_count, #flat_map, #frequency_counts, #frequency_groups, #gather_section, #group_count, #has?, #has_count?, #has_count_route, #identity, #inspect_class_name, #intersect_sections, #is, #is_not, #is_unique, #java_loop, #join, #left_difference_sections, #limit, #limit_section, #lookahead, #lookup_ids, #loop, #make_pairs, #map, #mixed_route?, #most_frequent, #neg_lookahead, #offset, #pages, #parallel, #process, #range, #reducer, #reject, #repeat, #right_difference_sections, #section, #select, #sort_section, #stream_sort, #stream_uniq, #uniq, #uniq_in_section, #unique?, #unique_path, #unjoin, #unless, #vertices_route?, #visitor, #where

Methods included from Routes::BulkOperations

#bulk_job, #bulk_map

Constructor Details

This class inherits a constructor from Pacer::Wrappers::ElementWrapper

Instance Attribute Details

#elementObject (readonly)

This method must be defined here rather than in the superclass in order to correctly override the method in an included module



40
41
42
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 40

def element
  @element
end

Class Method Details

.clear_cacheObject



27
28
29
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 27

def clear_cache
  @wrappers = {}
end

.wrapper_for(exts) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 19

def wrapper_for(exts)
  if exts
    base_edge_wrapper.wrappers[exts] ||= build_edge_wrapper(exts)
  else
    fail Pacer::LogicError, "Extensions should not be nil"
  end
end

.wrappersObject



15
16
17
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 15

def wrappers
  @wrappers ||= {}
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Test equality to another object.

Elements are equal if they are the same element type and have the same id and the same graph, regardless of extensions/wrappers.

If the graphdb instantiates multiple copies of the same element this method will return true when comparing them.

If the other instance is an unwrapped edge, this will always return false because otherwise the == method would not be symetrical.

Parameters:

  • other


191
192
193
194
195
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 191

def ==(other)
  other.is_a? EdgeWrapper and
    element_id == other.element_id and
    graph == other.graph
end

#add_extensions(exts) ⇒ Pacer::Wrappers::EdgeWrapper

Add extensions to this edge.

If any extension has a Edge module within it, this edge will be extended with the extension’s Edge module.

Parameters:

Returns:



72
73
74
75
76
77
78
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 72

def add_extensions(exts)
  if exts.any?
    self.class.wrap(self, extensions + exts.to_a)
  else
    self
  end
end

#branch(&block) ⇒ Object



23
24
25
# File 'lib/pacer/transform/branch.rb', line 23

def branch(&block)
  e.branch &block
end

#clone_into(target_graph, opts = {}) {|e| ... } ⇒ Pacer::Wrappers::EdgeWrapper

Clones this edge into the target graph.

This differs from the #copy_into in that it tries to set the new element_id the same as the original element_id.

Parameters:

Options Hash (opts):

  • :create_vertices (true)

    Create the vertices associated to this edge if they don’t already exist.

Yields:

  • (e)

    Optional block yields the edge after it has been created.

Returns:

Raises:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 136

def clone_into(target_graph, opts = {})
  e_idx = target_graph.temp_index("tmp-e-#{graph.graph_id}", :edge, :create => true)
  e = e_idx.first('id', element_id)
  unless e
    v_idx = target_graph.temp_index("tmp-v-#{graph.graph_id}", :vertex, :create => true)
    iv = v_idx.first('id', in_vertex.element_id)
    ov = v_idx.first('id', out_vertex.element_id)
    if opts[:create_vertices]
      iv ||= in_vertex.clone_into target_graph
      ov ||= out_vertex.clone_into target_graph
    end
    if not iv or not ov
      message = "Vertex not found for #{ self.inspect }: #{ iv.inspect } -> #{ ov.inspect }"
      puts message if opts[:show_missing_vertices]
      fail Pacer::ElementNotFound, message unless opts[:ignore_missing_vertices]
      return nil
    end
    e = target_graph.create_edge(element_id, ov, iv, label, properties)
    e_idx.put('id', element_id, e)
    yield e if block_given?
  end
  e
end

#copy_into(target_graph) {|e| ... } ⇒ Pacer::Wrappers::EdgeWrapper

Copies this edge into the target graph with the next available edge id.

Parameters:

Yields:

  • (e)

    Optional block yields the edge after it has been created.

Returns:

Raises:



168
169
170
171
172
173
174
175
176
177
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 168

def copy_into(target_graph)
  v_idx = target_graph.temp_index("tmp-v-#{graph.graph_id}", :vertex, :create => true)
  iv = v_idx.first('id', in_vertex.element_id) || target_graph.vertex(in_vertex.element_id)
  ov = v_idx.first('id', out_vertex.element_id) || target_graph.vertex(out_vertex.element_id)

  fail Pacer::ElementNotFound 'vertices not found' if not iv or not ov
  e = target_graph.create_edge nil, ov, iv, label, properties
  yield e if block_given?
  e
end

#delete!Object

Deletes the edge from its graph.



106
107
108
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 106

def delete!
  graph.remove_edge element
end

#display_nameString

Returns the display name of the edge.

Returns:

  • (String)


97
98
99
100
101
102
103
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 97

def display_name
  if graph and graph.edge_name
    graph.edge_name.call self
  else
    "#{ out_vertex.element_id }-#{ getLabel }-#{ in_vertex.element_id }"
  end
end

#element_payload=(data) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 205

def element_payload=(data)
  if element.is_a? Pacer::Payload::Edge
    element.payload = data
  else
    @element = Pacer::Payload::Edge.new element, data
  end
end

#extensionsObject

This method must be defined here rather than in the superclass in order to correctly override the method in an included module



60
61
62
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 60

def extensions
  self.class.extensions
end

#hashObject

Neo4j and Orient both have hash collisions between vertices and edges which causes problems when making a set out of a path for instance. Simple fix: negate edge hashes.



201
202
203
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 201

def hash
  -element.hash
end

#in_vertex(extensions = nil) ⇒ Pacer::Wrappers::VertexWrapper

The incoming vertex for this edge.



48
49
50
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 48

def in_vertex(extensions = nil)
  wrap_vertex element.getVertex(Pacer::Pipes::IN), extensions
end

#inspectString

Returns a human-readable representation of the edge using the standard ruby console representation of an instantiated object.

Returns:

  • (String)


89
90
91
92
93
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 89

def inspect
  graph.read_transaction do
    "#<E[#{element_id}]:#{display_name}>"
  end
end

#labelObject



42
43
44
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 42

def label
  getLabel
end

#no_extensionsEdgeWrapper

Returns the element with a new simple wrapper.

Returns:



82
83
84
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 82

def no_extensions
  self.class.base_edge_wrapper.new graph, element
end

#out_vertex(extensions = nil) ⇒ Pacer::Wrappers::VertexWrapper

The outgoing vertex for this edge.



54
55
56
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 54

def out_vertex(extensions = nil)
  wrap_vertex element.getVertex(Pacer::Pipes::OUT), extensions
end

#reverse!(opts = {}) ⇒ Object

Creates a new edge between the same elements with the same properties, but in the opposite direction and optionally with a new label. Attempts to reuse the edge id.



113
114
115
116
117
118
119
120
121
# File 'lib/pacer/wrappers/edge_wrapper.rb', line 113

def reverse!(opts = {})
  iv = out_vertex
  ov = in_vertex
  id = element_id if opts[:reuse_id]
  new_label = opts.fetch(:label, label)
  props = properties
  delete!
  graph.create_edge id, ov, iv, new_label, props
end