Class: Arango::Traversal

Inherits:
Object
  • Object
show all
Includes:
Database_Return, Helper_Error, Helper_Return
Defined in:
lib/Traversal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Database_Return

#database=

Methods included from Helper_Return

#return_delete, #return_directly?, #return_element

Methods included from Helper_Error

#satisfy_category?, #satisfy_class?, #warning_deprecated

Constructor Details

#initialize(body: {}, edgeCollection: nil, sort: nil, direction: nil, minDepth: nil, vertex:, visitor: nil, itemOrder: nil, strategy: nil, filter: nil, init: nil, maxIterations: nil, maxDepth: nil, uniqueness: nil, order: nil, expander: nil) ⇒ Traversal

Returns a new instance of Traversal.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/Traversal.rb', line 9

def initialize(body: {}, edgeCollection: nil,
  sort: nil, direction: nil, minDepth: nil,
  vertex:, visitor: nil, itemOrder: nil, strategy: nil,
  filter: nil, init: nil, maxIterations: nil, maxDepth: nil,
  uniqueness: nil, order: nil, expander: nil)
  satisfy_category?(direction, ["outbound", "inbound", "any", nil])
  satisfy_category?(itemOrder, ["forward", "backward", nil])
  satisfy_category?(strategy, ["depthfirst", "breadthfirst", nil])
  satisfy_category?(order, ["preorder", "postorder", "preorder-expander", nil])
  body[:sort]           ||= sort
  body[:direction]      ||= direction
  body[:maxDepth]       ||= maxDepth
  body[:minDepth]       ||= minDepth
  body[:startVertex]    ||= vertex
  body[:visitor]        ||= visitor
  body[:itemOrder]      ||= itemOrder
  body[:strategy]       ||= strategy
  body[:filter]         ||= filter
  body[:init]           ||= init
  body[:maxiterations]  ||= maxIterations
  body[:uniqueness]     ||= uniqueness
  body[:order]          ||= order
  body[:expander]       ||= expander
  body[:edgeCollection] ||= edgeCollection
  assign_body(body)
  @vertices = nil
  @paths = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



41
42
43
# File 'lib/Traversal.rb', line 41

def body
  @body
end

#collectionObject (readonly)

Returns the value of attribute collection.



41
42
43
# File 'lib/Traversal.rb', line 41

def collection
  @collection
end

#databaseObject (readonly)

Returns the value of attribute database.



41
42
43
# File 'lib/Traversal.rb', line 41

def database
  @database
end

#directionObject

Returns the value of attribute direction.



41
42
43
# File 'lib/Traversal.rb', line 41

def direction
  @direction
end

#edgeCollectionObject

Returns the value of attribute edgeCollection.



41
42
43
# File 'lib/Traversal.rb', line 41

def edgeCollection
  @edgeCollection
end

#expanderObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def expander
  @expander
end

#filterObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def filter
  @filter
end

#graphObject (readonly)

Returns the value of attribute graph.



41
42
43
# File 'lib/Traversal.rb', line 41

def graph
  @graph
end

#initObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def init
  @init
end

#itemOrderObject

Returns the value of attribute itemOrder.



41
42
43
# File 'lib/Traversal.rb', line 41

def itemOrder
  @itemOrder
end

#maxDepthObject Also known as: max

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def maxDepth
  @maxDepth
end

#maxiterationsObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def maxiterations
  @maxiterations
end

#minDepthObject Also known as: min

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def minDepth
  @minDepth
end

#orderObject

Returns the value of attribute order.



41
42
43
# File 'lib/Traversal.rb', line 41

def order
  @order
end

#pathsObject (readonly)

Returns the value of attribute paths.



41
42
43
# File 'lib/Traversal.rb', line 41

def paths
  @paths
end

#serverObject (readonly)

Returns the value of attribute server.



41
42
43
# File 'lib/Traversal.rb', line 41

def server
  @server
end

#sortObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def sort
  @sort
end

#strategyObject

Returns the value of attribute strategy.



41
42
43
# File 'lib/Traversal.rb', line 41

def strategy
  @strategy
end

#uniquenessObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def uniqueness
  @uniqueness
end

#verticesObject (readonly)

Returns the value of attribute vertices.



41
42
43
# File 'lib/Traversal.rb', line 41

def vertices
  @vertices
end

#visitorObject

DEFINE ===



40
41
42
# File 'lib/Traversal.rb', line 40

def visitor
  @visitor
end

Instance Method Details

#anyObject



142
143
144
# File 'lib/Traversal.rb', line 142

def any
  @direction = "any"
end

#executeObject

EXECUTE ===



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/Traversal.rb', line 180

def execute
  body = {
    "sort": @sort,
    "direction": @direction,
    "maxDepth": @maxDepth,
    "minDepth": @minDepth,
    "startVertex": @vertex&.id,
    "visitor": @visitor,
    "itemOrder": @itemOrder,
    "strategy": @strategy,
    "filter": @filter,
    "init": @init,
    "maxiterations": @maxiterations,
    "uniqueness": @uniqueness,
    "order": @order,
    "graphName": @graph&.name,
    "expander": @expander,
    "edgeCollection": @edgeCollection&.name
  }
  result = @database.request("POST", "_api/traversal", body: body)
  return result if @server.async != false
  @vertices = result[:result][:visited][:vertices].map do |x|
    collection = Arango::Collection.new(name: x[:_id].split("/")[0],
      database:  @database)
    Arango::Document.new(name: x[:_key], collection: collection, body: x)
  end
  @paths = result[:result][:visited][:paths].map do |x|
    {
      "edges": x[:edges].map do |e|
        collection_edge = Arango::Collection.new(name: e[:_id].split("/")[0],
          database:  @database, type: :edge)
        Arango::Document.new(name: e[:_key], collection: collection_edge,
          body: e, from: e[:_from], to: e[:_to])
      end,
      "vertices": x[:vertices].map do |v|
        collection_vertex = Arango::Collection.new(name: v[:_id].split("/")[0],
          database:  @database)
        Arango::Document.new(name: v[:_key], collection: collection_vertex, body: v)
      end
    }
  end
  return return_directly?(result) ? result : self
end

#inObject



134
135
136
# File 'lib/Traversal.rb', line 134

def in
  @direction = "inbound"
end

#outObject



138
139
140
# File 'lib/Traversal.rb', line 138

def out
  @direction = "outbound"
end

#startVertex=(vertex) ⇒ Object Also known as: vertex=, return_vertex



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/Traversal.rb', line 85

def startVertex=(vertex)
  case vertex
  when Arango::Edge
  when Arango::Document, Arango::Vertex
    @vertex = vertex
    @collection = @vertex.collection
    @database = @collection.database
    @graph  = @collection.graph
    @server = @database.server
    return
  when String
    if @database.nil?
      raise Arango::Error.new err: :database_undefined_for_traversal
    elsif vertex.include? "/"
      val = vertex.split("/")
      @collection = Arango::Collection.new(database: @database, name: val[0])
      @vertex = Arango::Document.new(collection: @collection, name: val[1])
      return
    end
  end
  raise Arango::Error.new err: :wrong_start_vertex_type
end

#to_hObject

TO HASH ===



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/Traversal.rb', line 148

def to_h
  {
    "sort": @sort,
    "direction": @direction,
    "maxDepth": @maxDepth,
    "minDepth": @minDepth,
    "visitor": @visitor,
    "itemOrder": @itemOrder,
    "strategy": @strategy,
    "filter": @filter,
    "init": @init,
    "maxiterations": @maxiterations,
    "uniqueness": @uniqueness,
    "order": @order,
    "expander": @expander,
    "vertices": @vertices&.map{|x| x.id},
    "paths": @paths&.map do |x|
      {
        "edges": x[:edges]&.map{|e| e.id},
        "vertices": x[:vertices]&.map{|v| v.id}
      }
    end,
    "idCache": @idCache,
    "startVertex": @vertex&.id,
    "graph": @graph&.name,
    "edgeCollection": @edgeCollection&.name,
    "database": @database.name
  }.delete_if{|k,v| v.nil?}
end