Class: Wewoo::Graph
Defined Under Namespace
Classes: GraphElementNotFoundError, UnknownGraphError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_edge(from, to, label, props = {}) ⇒ Object
-
#add_vertex(props = {}) ⇒ Object
-
#clear ⇒ Object
-
#drop_index(index_name) ⇒ Object
-
#edge_exists?(id) ⇒ Boolean
-
#edges(page: nil, per_page: nil) ⇒ Object
(also: #E)
-
#ensure_index(key, type = :vertex) ⇒ Object
-
#find_edge(id) ⇒ Object
(also: #e)
-
#find_edges(key, value, page: nil, per_page: nil) ⇒ Object
-
#find_first_edge(key, value) ⇒ Object
-
#find_first_vertex(key, value) ⇒ Object
-
#find_vertex(id) ⇒ Object
(also: #v)
-
#find_vertex_by_gid(gid) ⇒ Object
-
#find_vertices(key, value, page: nil, per_page: nil) ⇒ Object
-
#index(index_name, clazz: 'vertex', options: {}) ⇒ Object
-
#initialize(graph_name, host: nil, port: nil) ⇒ Graph
constructor
-
#key_indices ⇒ Object
-
#load(filename, append: false) ⇒ Object
-
#query(command, page: nil, per_page: nil) ⇒ Object
(also: #q)
-
#remove_edge(id) ⇒ Object
-
#remove_vertex(id) ⇒ Object
-
#save(filename) ⇒ Object
-
#set_key_index(type, key) ⇒ Object
-
#to_s ⇒ Object
(also: #inspect)
-
#update_edge(id, props) ⇒ Object
-
#update_vertex(id, props) ⇒ Object
BOZO!! - not really an update as all props will be replaced!.
-
#vertex_exists?(id) ⇒ Boolean
-
#vertices(page: nil, per_page: nil) ⇒ Object
(also: #V)
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
#name ⇒ Object
Returns the value of attribute name.
13
14
15
|
# File 'lib/wewoo/graph.rb', line 13
def name
@name
end
|
#url ⇒ Object
Returns the value of attribute url.
13
14
15
|
# File 'lib/wewoo/graph.rb', line 13
def url
@url
end
|
Class Method Details
.available_graphs ⇒ Object
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
|
#clear ⇒ Object
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
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
#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
#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_indices ⇒ Object
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 )
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_s ⇒ Object
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
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
|