module Pacer import com.tinkerpop.blueprints.Parameter class PacerGraph include GraphTransactionsMixin include Pacer::Core::Route include Pacer::Core::Graph::GraphRoute include Pacer::Core::Graph::GraphIndexRoute attr_reader :blueprints_graph attr_accessor :encoder, :base_vertex_wrapper, :base_edge_wrapper def initialize(encoder, open, shutdown = nil, graph_id = nil) self.base_vertex_wrapper = Pacer::Wrappers::VertexWrapper self.base_edge_wrapper = Pacer::Wrappers::EdgeWrapper if open.is_a? Proc @reopen = open else @reopen = proc { open } end @shutdown = shutdown reopen @encoder = encoder @graph_id = graph_id end # The current graph # # @return [PacerGraph] returns self def graph self end def vendor(full = false) g = blueprints_graph g = g.raw_graph if g.respond_to? :raw_graph if full g.java_class.name else g.java_class.name.split('.')[1] end end def reopen @blueprints_graph = unwrap_graph @reopen.call self end def shutdown @shutdown.call self if @shutdown @blueprints_graph = nil self end def use_wrapper(klass) reopen = proc do klass.new unwrap_graph(@reopen.call) end PacerGraph.new encoder, reopen, @shutdown end def use_encoder(encoder) PacerGraph.new encoder, @reopen, @shutdown end def graph_id @graph_id ||= blueprints_graph.object_id end def equals(other) other.class == self.class and graph_id == other.graph_id end # Get a vertex by id. # # @overload vertex(id) # @param [element id] id # @overload vertex(id, *modules) # @param [element id] id # @param [Module, Class] *modules extensions to add to the returned # vertex. def vertex(id, *modules) begin v = blueprints_graph.getVertex(id) rescue java.lang.RuntimeException end wrap_element v, base_vertex_wrapper, Pacer::Wrappers::VertexWrapper, modules end # Get an edge by id. # # @overload edge(id) # @param [element id] id # @overload edge(id, *modules) # @param [element id] id # @param [Module, Class] *modules extensions to add to the returned # edge. def edge(id, *modules) begin v = blueprints_graph.getEdge(id) rescue Java::JavaLang::RuntimeException end wrap_element v, base_edge_wrapper, Pacer::Wrappers::EdgeWrapper, modules end def wrap_element(v, base, klass, modules) if v if modules wrapper = modules.detect { |obj| obj.ancestors.include? klass } if wrapper v = wrapper.new graph, v modules.delete wrapper else v = base.new graph, v end v.add_extensions modules else base.new graph, v end end end # Create a vertex in the graph. # # @overload create_vertex(*args) # @param [extension, Hash] *args extension (Module/Class) arguments will be # added to the current vertex. A Hash will be # treated as element properties. # @overload create_vertex(id, *args) # @param [element id] id the desired element id. Some graphs # ignore this. # @param [extension, Hash] *args extension (Module/Class) arguments will be # added to the current vertex. A Hash will be # treated as element properties. def create_vertex(*args) id, wrapper, modules, props = id_modules_properties(args) raw_vertex = creating_elements { blueprints_graph.addVertex(id) } if wrapper vertex = wrapper.new graph, raw_vertex else vertex = base_vertex_wrapper.new graph, raw_vertex end if modules.any? vertex = vertex.add_extensions modules end props.each { |k, v| vertex[k.to_s] = v } if props vertex end # Create an edge in the graph. # # @param [element id] id some graphs allow you to specify your own edge id. # @param [Pacer::Wrappers::VertexWrapper] from_v the new edge's out_vertex # @param [Pacer::Wrappers::VertexWrapper] to_v the new edge's in_vertex # @param [#to_s] label the edge label # @param [extension, Hash] *args extension (Module/Class) arguments will be # added to the returned edge. A Hash will be # treated as element properties. # # @todo make id param optional def create_edge(id, from_v, to_v, label, *args) _, wrapper, modules, props = id_modules_properties(args) raw_edge = creating_elements { blueprints_graph.addEdge(id, from_v.element, to_v.element, label.to_s) } if wrapper edge = wrapper.new graph, raw_edge else edge = base_edge_wrapper.new graph, raw_edge end if modules.any? edge = edge.add_extensions modules end props.each { |k, v| edge[k.to_s] = v } if props edge end def remove_vertex(vertex) blueprints_graph.removeVertex vertex end def remove_edge(edge) blueprints_graph.removeEdge edge end # Directly loads an array of vertices by id. # # @param [[vertex ids]] ids # @return [[Pacer::Wrappers::VertexWrapper]] def load_vertices(ids) ids.map do |id| vertex id end.compact end # Directly loads an array of edges by id. # # @param [[edge ids]] ids # @return [[Pacer::Wrappers::EdgeWrapper]] def load_edges(ids) ids.map do |id| edge id end.compact end def features blueprints_graph.getFeatures end def inspect "# e if e.message =~ /already exists/ raise ElementExists, e.message else raise end end end def source_iterator [blueprints_graph] end end end