Class: Pacer::Wrappers::VertexWrapper

Inherits:
ElementWrapper show all
Includes:
Core::Graph::VerticesRoute, Vertex
Defined in:
lib/pacer/transform/branch.rb,
lib/pacer/wrappers/vertex_wrapper.rb

Instance Attribute Summary collapse

Attributes inherited from ElementWrapper

#graph

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Graph::VerticesRoute

#add_edges_to, #both, #both_e, #element_type, #in, #inE, #in_e, #out, #outE, #out_e, #route_labels, #v

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_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/vertex_wrapper.rb', line 40

def element
  @element
end

Class Method Details

.clear_cacheObject



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

def clear_cache
  @wrappers = {}
end

.wrapper_for(exts) ⇒ Object



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

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

.wrappersObject



15
16
17
# File 'lib/pacer/wrappers/vertex_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 type and have the same id and the same graph, regardless of extensions.

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 vertex, this will always return false because otherwise the == method would not be symetrical.

Parameters:

  • other


209
210
211
212
213
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 209

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

#add_extensions(exts) ⇒ Pacer::EdgeWrapper

Add extensions to this vertex.

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

Parameters:

Returns:

  • (Pacer::EdgeWrapper)

    this vertex wrapped up and including the extensions



56
57
58
59
60
61
62
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 56

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

#as(*exts) {|v| ... } ⇒ Object

Checks that the given extensions can be applied to the vertex. If they can then yield the vertex correctly extended or return the extended vertex. If not then do not yield and return nil.

Parameters:

  • exts (extensions)

    extensions to add if possible

Yields:

  • (v)

    Optional block yields the vertex with the extensions added.

Returns:

  • nil or the result of the block or the extended vertex



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 77

def as(*exts)
  if exts.length == 1 and not exts.first.is_a?(Module) and not exts.first.is_a?(Class) and exts.first
    # NB: oops, I defined route#as to be the same as route#section. If the signature is
    #     matching the section method, then defer to it.
    section exts.first
  elsif as?(*exts)
    exts_to_add = extensions_missing(exts)
    extended = exts_to_add.empty? ? self : add_extensions(exts_to_add)
    if block_given?
      yield extended
    else
      extended
    end
  end
end

#as?(*exts) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 104

def as?(*exts)
  extensions_missing(exts).all? do |ext|
    rc = if ext.respond_to? :route_conditions
           ext.route_conditions(graph)
         elsif ext.respond_to? :lookup
           ext.lookup(graph)
         end
    if rc
      rc.all? do |k, v|
        if v.is_a? Set
          v.include? self[k]
        else
          self[k] == v
        end
      end
    else
      true
    end
  end
end

#both_edges(*labels_and_extensions) ⇒ Object



181
182
183
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 181

def both_edges(*labels_and_extensions)
  get_edges_helper Pacer::Pipes::BOTH, *labels_and_extensions
end

#both_vertices(*labels_and_extensions) ⇒ Object



193
194
195
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 193

def both_vertices(*labels_and_extensions)
  get_vertices_helper Pacer::Pipes::BOTH, *labels_and_extensions
end

#branch(&block) ⇒ Object



17
18
19
# File 'lib/pacer/transform/branch.rb', line 17

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

#clone_into(target_graph, opts = nil) {|v| ... } ⇒ Pacer::Wrappers::VertexWrapper

Copies including the vertex id unless a vertex with that id already exists.

Parameters:

Yields:

  • (v)

    Optional block yields the vertex after it has been created.

Returns:



151
152
153
154
155
156
157
158
159
160
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 151

def clone_into(target_graph, opts = nil)
  v_idx = target_graph.temp_index("tmp-v-#{graph.graph_id}", :vertex, :create => true)
  v = v_idx.first('id', element_id)
  unless v
    v = target_graph.create_vertex element_id, properties
    v_idx.put('id', element_id, v.element)
    yield v if block_given?
  end
  v
end

#copy_into(target_graph) {|v| ... } ⇒ Pacer::Wrappers::VertexWrapper

Make a new copy of the element with the next available vertex id.

Parameters:

Yields:

  • (v)

    Optional block yields the vertex after it has been created.

Returns:



167
168
169
170
171
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 167

def copy_into(target_graph)
  v = target_graph.create_vertex properties
  yield v if block_given?
  v
end

#delete!Object

Deletes the vertex from its graph along with all related edges.



141
142
143
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 141

def delete!
  graph.remove_vertex element
end

#display_nameString

Returns the display name of the vertex.

Returns:

  • (String)


136
137
138
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 136

def display_name
  graph.vertex_name.call self if graph and graph.vertex_name
end

#element_payload=(data) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 223

def element_payload=(data)
  if element.is_a? Pacer::Payload::Vertex
    element.payload = data
  else
    @element = Pacer::Payload::Vertex.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



44
45
46
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 44

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.



219
220
221
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 219

def hash
  element.hash
end

#in_edges(*labels_and_extensions) ⇒ Object



177
178
179
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 177

def in_edges(*labels_and_extensions)
  get_edges_helper Pacer::Pipes::IN, *labels_and_extensions
end

#in_vertices(*labels_and_extensions) ⇒ Object



189
190
191
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 189

def in_vertices(*labels_and_extensions)
  get_vertices_helper Pacer::Pipes::IN, *labels_and_extensions
end

#inspectString

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

Returns:

  • (String)


128
129
130
131
132
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 128

def inspect
  graph.read_transaction do
    "#<#{ ["V[#{element_id}]", display_name].compact.join(' ') }>"
  end
end

#no_extensionsVertexWrapper

Returns the element with a new simple wrapper.

Returns:



66
67
68
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 66

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

#only_as(*exts) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 93

def only_as(*exts)
  if as?(*exts)
    extended = exts.empty? ? no_extensions : no_extensions.add_extensions(exts)
    if block_given?
      yield extended
    else
      extended
    end
  end
end

#out_edges(*labels_and_extensions) ⇒ Object



173
174
175
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 173

def out_edges(*labels_and_extensions)
  get_edges_helper Pacer::Pipes::OUT, *labels_and_extensions
end

#out_vertices(*labels_and_extensions) ⇒ Object



185
186
187
# File 'lib/pacer/wrappers/vertex_wrapper.rb', line 185

def out_vertices(*labels_and_extensions)
  get_vertices_helper Pacer::Pipes::OUT, *labels_and_extensions
end