Class: V

Inherits:
ActiveOrient::Model show all
Defined in:
lib/model/vertex.rb

Constant Summary

Constants included from OrientDB

OrientDB::DocumentDatabase, OrientDB::DocumentDatabasePool, OrientDB::DocumentDatabasePooled, OrientDB::GraphDatabase, OrientDB::IndexType, OrientDB::OClassImpl, OrientDB::OTraverse, OrientDB::PropertyImpl, OrientDB::RemoteStorage, OrientDB::SQLCommand, OrientDB::SQLSynchQuery, OrientDB::Schema, OrientDB::SchemaProxy, OrientDB::SchemaType, OrientDB::ServerAdmin, OrientDB::User, OrientDB::UsingJava

Instance Attribute Summary

Attributes inherited from ActiveOrient::Model

#metadata

Attributes inherited from ActiveOrient::Base

#metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveOrient::Model

_to_partial_path, autoload_object, delete_class, #document, #persisted?, #to_ary, to_or, use_or_allocate

Methods included from ModelClass

#add_edge_link, #all, #alter_property, #classname, #count, #create, #create_class, #create_index, #create_properties, #create_property, #custom_where, #delete_property, #delete_record, #delete_records, #first, #get, #get_records, #indexes, #last, #link_list, #match, #namespace_prefix, #naming_convention, #orientdb_class, #print_properties, #properties, #query_database, #require_model_file, #update_all, #upsert, #where

Methods included from OrientSupport::Support

#compose_where, #generate_sql_list

Methods included from CustomClass

#like

Methods included from ActiveOrient::BaseProperties

#==, #content_attributes, #default_attributes, #embedded, #set_attribute_defaults, #update_missing

Methods inherited from ActiveOrient::Base

#[], #[]=, attr_accessible, attr_protected, #attributes, #attributes=, belongs_to, display_rid, exclude_the_following_properties, get_rid, has_many, has_one, #included_links, #initialize, #my_metadata, remove_rid, reset_rid_store, serialize, store_rid, #to_model, #update_attribute

Constructor Details

This class inherits a constructor from ActiveOrient::Base

Class Method Details

.create(set: {}, **attributes) ⇒ Object

specialized creation of vertices, overloads model#create


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/model/vertex.rb', line 7

def self.create set: {},  **attributes

new_vert = db.execute( transaction: false, tolerated_error_code: /found duplicated key/) do
 "CREATE VERTEX #{ref_name} CONTENT #{set.merge(attributes).to_orient.to_json}"
 end
	 
new_vert =  new_vert.pop if new_vert.is_a?( Array) && new_vert.size == 1
if new_vert.nil?
	logger.error('Vertex'){ "Table #{ref_name} ->>  create failed:  #{set.merge(attributes).inspect}" } 
elsif block_given?
	yield new_vert
else
	new_vert # returns the created vertex (or an array of created vertices)
end
end

.delete(where: {}, **args) ⇒ Object

Vertex#delete fires a “delete vertex” command to the database. The where statement can be empty ( “” or {}“), then all vertices are removed

The rid-cache is reset, too


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/model/vertex.rb', line 28

def self.delete where: {} , **args
if args[:all] == true 
	where = {}
else
	where.merge!(args) if where.is_a?(Hash)
	return 0 if where.empty?
end
# query returns [{count => n }]
count= db.execute { "delete vertex #{ref_name} #{db.compose_where(where)}" }.first[:count] rescue 0
  reset_rid_store
count #  return count of affected records
end

Instance Method Details

#assign(vertex:, via: E, attributes: {}) ⇒ Object

Assigns another Vertex via an EdgeClass. If specified, puts attributes on the edge.

Wrapper for

Edge.create in: self, out: a_vertex, attributes: { some_attributes on the edge }

returns the assigned vertex, thus enableing to chain vertices through

Vertex.assign() via: E , vertex: VertexClass.create()).assign( via: E, ... )

or (1..100).each{|n| vertex = vertex.assign(via: E2, vertext: V2.create(item: n))}


125
126
127
128
129
130
# File 'lib/model/vertex.rb', line 125

def assign vertex: , via: E , attributes: {}

  via.create from: self, to: vertex, set: attributes
  
vertex
end

#edges(*args) ⇒ Object

List edges

  1. call without any parameter: list all edges present

  2. call with :in or :out : list any incoming or outgoing edges

  3. call with /regexp/, Class, symbol or string: restrict to this edges, including inheritence If a pattern, symbol string or class is provided, the default is to list outgoing edges

:call-seq:
edges in_or_out, pattern

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/model/vertex.rb', line 53

def edges *args
	if args.empty?
	detect_edges  :both
	else
		kind =   [:in, :out, :both, :all].detect{|x|  args.include? x }
		if kind.present?
			args =  args -[ kind ]
		else
			kind = :both
		end
	detect_edges  kind, args.first

	end

end

#in_e(edge_name = nil) ⇒ Object Also known as: in

»in« and »out« provide the main access to edges. »in» is a reserved keyword. Therfore its only an alias to `in_e`.

If called without a parameter, all connected edges are retrieved.

If called with a string, symbol or class, the edge-class is resolved and even inherented edges are retrieved.


147
148
149
# File 'lib/model/vertex.rb', line 147

def in_e edge_name= nil
  detect_edges :in, edge_name
end

#in_edgesObject

Retrieves connected edges

The basic usage is to fetch all/ incomming/ outgoing edges

Model-Instance.edges :in  :out | :both, :all

One can filter specific edges by providing parts of the edge-name

Model-Instance.edges /sector/, :in
Model-Instance.edges :out, /sector/
Model-Instance.edges  /sector/
Model-Instance.edges  :in

The method returns an array of expandes edges.

»in_edges« and »out_edges« are shortcuts to »edges :in« and »edges :out«

Its easy to expand the result:

tg.out( :ohlc).out.out_edges
 => [["#102:11032", "#121:0"]] 
 tg.out( :ohlc).out.out_edges.from_orient
 => [[#<TG::GRID_OF:0x00000002620e38

this displays the out-edges correctly

whereas tg.out( :ohlc).out.edges( :out)

=> [["#101:11032", "#102:11032", "#94:10653", "#121:0"]]

returns all edges. The parameter (:out) is not recognized, because out is already a nested array.

this

tg.out( :ohlc).first.out.edges( :out)

is a walkaround, but using in_- and out_edges is more elegant.


194
195
196
# File 'lib/model/vertex.rb', line 194

def in_edges
  edges :in
end

#nodes(in_or_out = :out, via: nil, where: nil, expand: false) ⇒ Object

Lists all connected Vertices

The Edge-classes can be specified via Classname or a regular expression.

If a regular expression is used, the database-names are searched and inheritance is supported.


75
76
77
78
79
80
81
82
# File 'lib/model/vertex.rb', line 75

def nodes in_or_out = :out, via:  nil, where: nil, expand:  false
		edges =  detect_edges( in_or_out, via, expand: false )
		return [] if edges.empty?
		q = OrientSupport::OrientQuery.new 
		edges = nil if via.nil?
		q.nodes in_or_out, via:  edges , where: where, expand: expand
		detected_nodes=	query( q ){| record | record.is_a?(Hash)?  record.values.first : record  }
end

#out(edge_name = nil) ⇒ Object


153
154
155
# File 'lib/model/vertex.rb', line 153

def out edge_name =  nil
  detect_edges :out, edge_name
end

#out_edgesObject


197
198
199
# File 'lib/model/vertex.rb', line 197

def out_edges
  edges :out
end

#to_humanObject

Human readable represantation of Vertices

Format: < Classname : Edges, Attributes >


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/model/vertex.rb', line 209

def to_human
	count_and_display_classes = ->(array){array.map(&:class)&.group_by(&:itself)&.transform_values(&:count)} 

	the_ins =    count_and_display_classes[ in_e] 
	the_outs =  count_and_display_classes[ out]

	in_and_out = in_edges.empty? ? "" : "in: #{the_ins}, " 
	in_and_out += out_edges.empty? ? "" : "out: #{the_outs}, " 


	#Default presentation of ActiveOrient::Model-Objects

	"<#{self.class.to_s.demodulize}[#{rid}]: " + in_and_out  + content_attributes.map do |attr, value|
		v= case value
			 when ActiveOrient::Model
				 "< #{self.class.to_s.demodulize} : #{value.rid} >"
			 when OrientSupport::Array
				 value.to_s
#					 value.rrid #.to_human #.map(&:to_human).join("::")
			 else
				 value.from_orient
			 end
		"%s : %s" % [ attr, v]  unless v.nil?
	end.compact.sort.join(', ') + ">".gsub('"' , ' ')
end

#traverse(in_or_out = :out, via: nil, depth: 1, execute: true, start_at: 1, where: nil) ⇒ Object

Returns a collection of all vertices passed during the traversal

fires a query

select  from  ( traverse  outE('}#{via}').in  from #{vertex}  while $depth <= #{depth}   ) 
        where $depth >= #{start_at}

If » excecute: false « is specified, the traverse-statement is returned (as Orient-Query object)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/model/vertex.rb', line 93

def traverse in_or_out = :out, via: nil,  depth: 1, execute: true, start_at: 1, where: nil

	edges = detect_edges( in_or_out, via, expand: false)
	the_query = OrientSupport::OrientQuery.new kind: 'traverse' 
	the_query.where where if where.present?
	the_query.while "$depth <= #{depth} " unless depth <=0
	the_query.from   self
	edges.each{ |ec| the_query.nodes in_or_out, via: ec, expand: false }
	outer_query = OrientSupport::OrientQuery.new from: the_query, where: "$depth >= #{start_at}"
	if execute 
		query( outer_query ) 
		else
#			the_query.from self  #  complete the query by assigning self 
			the_query            #  returns the OrientQuery  -traverse object
		end
end