Class: Wewoo::Graph

Inherits:
Object
  • Object
show all
Includes:
Adapter
Defined in:
lib/wewoo/graph.rb

Defined Under Namespace

Classes: GraphElementNotFoundError, UnknownGraphError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Adapter

delete, get, handle_response, log, post, put

Constructor Details

#initialize(graph_name, host: nil, port: nil) ⇒ Graph

Returns a new instance of Graph.



20
21
22
23
24
25
26
27
# File 'lib/wewoo/graph.rb', line 20

def initialize( graph_name, host:nil, port:nil )
  Configuration.url( host, port )

  @name     = graph_name
  @base_url = "#{Configuration.url}/graphs"
  @url      = "#{@base_url}/#{@name}"
  validate
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.available_graphsObject



15
16
17
18
# File 'lib/wewoo/graph.rb', line 15

def self.available_graphs
  res = Adapter::get( "#{Configuration.url}/graphs" )
  res.graphs
end

Instance Method Details

#add_edge(from, to, label, props = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/wewoo/graph.rb', line 148

def add_edge( from, to, label, props={} )
  params = {
    '_outV'  => (from.is_a? Vertex) ? from.id : from,
    '_inV'   => (to.is_a? Vertex) ? to.id : to,
    '_label' => label
  }.merge( props )
  res = post( u( %W[edges] ),
        body: params.to_json,
        headers: { 'Content-Type'=> 'application/json'} )
  Edge.from_hash( self, res )
end

#add_vertex(props = {}) ⇒ Object



77
78
79
80
81
# File 'lib/wewoo/graph.rb', line 77

def add_vertex( props={} )
  vertex = Vertex.from_hash( self,
                             post( u( %W[vertices] ) ) )
  update_vertex( vertex.id, props )
end

#clearObject



43
44
45
# File 'lib/wewoo/graph.rb', line 43

def clear
  query( 'g.V.remove()')
end

#drop_index(index_name) ⇒ Object



55
56
57
# File 'lib/wewoo/graph.rb', line 55

def drop_index( index_name )
  delete( u( %W[indices #{index_name}] ) )
end

#edge_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
# File 'lib/wewoo/graph.rb', line 127

def edge_exists?( id )
  e(id) && true
  true
rescue
  false
end

#edges(page: nil, per_page: nil) ⇒ Object Also known as: E



182
183
184
185
186
# File 'lib/wewoo/graph.rb', line 182

def edges( page:nil, per_page:nil )
  get( u(:edges), params: page_params(page, per_page) ).map do |res|
    Edge.from_hash( self, res )
  end
end

#ensure_index(key, type = :vertex) ⇒ Object



59
60
61
62
63
# File 'lib/wewoo/graph.rb', line 59

def ensure_index( key, type=:vertex )
  unless key_indices[type.to_s].include? key.to_s
    post( u( %W[keyindices #{type} #{key}] ) )
  end
end

#find_edge(id) ⇒ Object Also known as: e



164
165
166
167
168
# File 'lib/wewoo/graph.rb', line 164

def find_edge( id )
  Edge.from_hash( self, get( u %W[edges #{id}] ) )
rescue InvalidRequestError => ex
  raise GraphElementNotFoundError, ex.message
end

#find_edges(key, value, page: nil, per_page: nil) ⇒ Object



171
172
173
174
175
176
# File 'lib/wewoo/graph.rb', line 171

def find_edges( key, value, page:nil, per_page:nil )
  params = { key: key,
             value: map_value( value )
  }.merge page_params( page, per_page )
  get( u(:edges), params: params ).map { |e| Edge.from_hash( self, e ) }
end

#find_first_edge(key, value) ⇒ Object



178
179
180
# File 'lib/wewoo/graph.rb', line 178

def find_first_edge( key, value )
  find_edges( key, value, page:1, per_page:1 ).first
end

#find_first_vertex(key, value) ⇒ Object



108
109
110
# File 'lib/wewoo/graph.rb', line 108

def find_first_vertex( key, value )
  find_vertices( key, value ).first
end

#find_vertex(id) ⇒ Object Also known as: v



114
115
116
117
118
# File 'lib/wewoo/graph.rb', line 114

def find_vertex( id )
  Vertex.from_hash( self, get( u %W[vertices #{id}] ) )
rescue InvalidRequestError => ex
  raise GraphElementNotFoundError, ex.message
end

#find_vertex_by_gid(gid) ⇒ Object



111
112
113
# File 'lib/wewoo/graph.rb', line 111

def find_vertex_by_gid( gid )
  find_first_vertex( :gid, gid )
end

#find_vertices(key, value, page: nil, per_page: nil) ⇒ Object



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

def find_vertices( key, value, page:nil, per_page:nil )
  params = { key: key, value: map_value(value) }.merge page_params( page, per_page )
  res    = get( u(:vertices), params:  params )

  res.map do |res|
    Vertex.from_hash( self, res )
  end
end

#index(index_name, clazz: 'vertex', options: {}) ⇒ Object



65
66
67
# File 'lib/wewoo/graph.rb', line 65

def index( index_name, clazz:'vertex', options:{} )
  post( u(%W[indices #{index_name}]), params: {class: clazz}.merge(options))
end

#key_indicesObject



47
48
49
# File 'lib/wewoo/graph.rb', line 47

def key_indices
  Map[ *get( u(:keyindices ) ).flatten]
end

#load(filename, append: false) ⇒ Object



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

def load( filename, append:false )
  raise "Graph file does not exist. #{filename}" unless File.exists?(filename)

  clear unless append
  q( "g.loadGraphML( '#{filename}' )" )
end

#query(command, page: nil, per_page: nil) ⇒ Object Also known as: q



69
70
71
72
73
74
# File 'lib/wewoo/graph.rb', line 69

def query( command, page:nil, per_page:nil )
  # command = "g." + command unless command[/\Ag\./]
  ResultSet.new( self, get( u(%w[tp gremlin]),
       params:{script: command}.merge(page_params(page, per_page)),
       headers: { 'Content-Type'=> 'application/json'} ) ).hydrate
end

#remove_edge(id) ⇒ Object



160
161
162
# File 'lib/wewoo/graph.rb', line 160

def remove_edge( id )
  delete( u %W[edges #{id}] )
end

#remove_vertex(id) ⇒ Object



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

def remove_vertex( id )
  delete( u(%W[vertices #{id}] ) )
end

#save(filename) ⇒ Object



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

def save( filename )
  dir = File.dirname( filename )
  FileUtils.mkdir_p( dir ) unless File.exists?( dir )

  q( "g.saveGraphML( '#{filename}' )" )
end

#set_key_index(type, key) ⇒ Object



51
52
53
# File 'lib/wewoo/graph.rb', line 51

def set_key_index( type, key )
  post( u( %W[keyindices #{type} #{key}] ) )
end

#to_sObject Also known as: inspect



189
190
191
192
193
194
195
# File 'lib/wewoo/graph.rb', line 189

def to_s
  res = get( url, headers: { 'Content-Type' =>
                             'application/vnd.rexster-typed-v1+json' } )
  stats = res.graph.match( /.*\[vertices:(\d+) edges:(\d+)\]/ ).captures

  "#{name} [Vertices:#{stats.first}, Edges:#{stats.last}]"
end

#update_edge(id, props) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/wewoo/graph.rb', line 138

def update_edge( id, props )
  e     = find_edge( id )
  props = e.props.merge( props )

  res = put( u( %W[edges #{id}] ),
        body: props.to_json,
        headers: { 'Content-Type'=> 'application/json'} )
  Edge.from_hash( self, res )
end

#update_vertex(id, props) ⇒ Object

BOZO!! - not really an update as all props will be replaced!



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

def update_vertex( id, props )
  v = find_vertex( id )
  props = v.props.merge( props )
  res = put( u( %W[vertices #{id}] ),
        body:    props.to_json,
        headers: { 'Content-Type'=> 'application/json'} )
  Vertex.from_hash( self, res )
end

#vertex_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

def vertex_exists?( id )
  v(id) && true
rescue
  false
end

#vertices(page: nil, per_page: nil) ⇒ Object Also known as: V



93
94
95
96
97
# File 'lib/wewoo/graph.rb', line 93

def vertices( page:nil, per_page:nil )
  get( u(:vertices), {params: page_params(page, per_page)} ).map do |res|
    Vertex.from_hash( self, res )
  end
end