Version bump to 0.4.0
[graph.njae.git] / lib / graph.njae / edge.rb
index 4a1ba790d4f33570f53525cbd3f3c23660135240..7fd99b771bec5082e96f6fe2ebef7e11e283814c 100644 (file)
@@ -1,5 +1,3 @@
-require 'ostruct'
-
 # A simple graph library
 
 module GraphNjae
@@ -34,6 +32,37 @@ module GraphNjae
     def connection_at(vertex)
       self.connections.find {|c| c.end.equal?  vertex}
     end
+    
+    # Return the vertex at the other end of the one given.
+    # Self-loops should still return the vertex
+    def other_end(vertex)
+      if self.vertices[0] == vertex
+        self.vertices[1]
+      else
+        self.vertices[0]
+      end
+    end
+    
+    def to_s
+      '<E: ' + self.type.to_s + ' [' + self.vertices.map {|n| n.to_s}.join(', ') + '] >'
+    end
+    
+    def to_dot(opts = {})
+      if block_given?
+        yield self
+      else
+        dot = self.connections[0].end.object_id.to_s + " -- " + self.connections[1].end.object_id.to_s
+        if opts.size > 0
+          dot << ' {'
+          dot << opts.keys.map { |k|
+            (k.to_s + ' = "' + self.instance_eval(opts[k].to_s).to_s) + '"'
+                        }.join(', ')
+          dot << '}'
+        end
+        dot << ';'
+      end
+    end
+
   end
   
   # A connection between an Edge and a Vertex.The connection can have arbitrary attributes,