Module: OQGraph::InstanceMethods

Defined in:
lib/acts_as_oqgraph.rb

Instance Method Summary collapse

Instance Method Details

#create_edge_to(other, weight = 1.0) ⇒ Object

Creates a one way edge from this node to another with a weight.



162
163
164
# File 'lib/acts_as_oqgraph.rb', line 162

def create_edge_to(other, weight = 1.0)
  edge_class.create!(:from_id => id, :to_id => other.id, :weight => weight)
end

#create_edge_to_and_from(other, weight = 1.0) ⇒ Object

other graph node to edge to weight positive float denoting edge weight Creates a two way edge between this node and another.



169
170
171
172
# File 'lib/acts_as_oqgraph.rb', line 169

def create_edge_to_and_from(other, weight = 1.0)
  edge_class.create!(:from_id => id, :to_id => other.id, :weight => weight)
  edge_class.create!(:from_id => other.id, :to_id => id, :weight => weight)
end

#edge_classObject

The class used for the edges between nodes



157
158
159
# File 'lib/acts_as_oqgraph.rb', line 157

def edge_class
  self.class.edge_class
end

#originatingObject

Returns an array of all nodes which can trace to this node



185
186
187
# File 'lib/acts_as_oqgraph.rb', line 185

def originating
  edge_class.originating_nodes(self)
end

#originating?(other) ⇒ Boolean

true if the other node can reach this node.

Returns:

  • (Boolean)


190
191
192
# File 'lib/acts_as_oqgraph.rb', line 190

def originating?(other)
  originating.include?(other)
end

#path_weight_to(other) ⇒ Object

other The target node to find a route to Gives the path weight as a float of the shortest path to the other



206
207
208
# File 'lib/acts_as_oqgraph.rb', line 206

def path_weight_to(other)
  edge_class.shortest_path(self,other,:method => :djikstra).map{|edge| edge.weight.to_f}.sum
end

#reachableObject

Returns all nodes reachable from this node.



195
196
197
# File 'lib/acts_as_oqgraph.rb', line 195

def reachable
  edge_class.reachable_nodes(self)
end

#reachable?(other) ⇒ Boolean

true if the other node is reachable from this one

Returns:

  • (Boolean)


200
201
202
# File 'lib/acts_as_oqgraph.rb', line 200

def reachable?(other)
 reachable.include?(other)
end

#shortest_path_to(other, options = {:method => :djikstra}) ⇒ Object

other The target node to find a route to options A hash of options: Currently the only option is

:method => :djiskstra or :breadth_first

Returns an array of nodes in order starting with this node and ending in the target This will be the shortest path from this node to the other. The :djikstra method takes edge weights into account, the :breadth_first does not.



180
181
182
# File 'lib/acts_as_oqgraph.rb', line 180

def shortest_path_to(other, options = {:method => :djikstra})
  edge_class.shortest_path(self,other, options)
end