Permalink
Browse files

Add ability to rename vertices and edges. Closes #8

  • Loading branch information...
1 parent 3c5f4d4 commit dd58b25dd528107b309248ce7007cb658817d8fb @D4L committed Jan 11, 2013
@@ -19,3 +19,4 @@ class UniqueNameCollection
require "abstract_graph/composition/uniquenamecollection/dup"
require "abstract_graph/composition/uniquenamecollection/method_missing"
require "abstract_graph/composition/uniquenamecollection/link"
+require "abstract_graph/composition/uniquenamecollection/rename"
@@ -0,0 +1,28 @@
+# required in "abstract_graph/composition/uniquenamecollection"
+
+module AbstractGraph
+ module Composition
+ class UniqueNameCollection
+
+ # Change the name of a vertex in our graph
+ # p: String oldname represents the current vertex's name
+ # String newname represents the new name of our vertex
+ # note: the object needs to implement #name=
+ def rename( oldname, newname )
+ return nil if @collection[oldname].nil?
+
+ @otherUnique.each do |unc|
+ throw Exception if unc[newname]
+ end
+
+ # rename the object itself
+ @collection[oldname].name = newname
+ # remap the name
+ @collection[newname] = @collection[oldname]
+ # clear the old name
+ @collection.delete oldname
+ end
+
+ end
+ end
+end
@@ -23,3 +23,5 @@ class Graph
require "abstract_graph/graph/add_edge"
require "abstract_graph/graph/has_edge"
require "abstract_graph/graph/delete_edge"
+require "abstract_graph/graph/change_vertex_name"
+require "abstract_graph/graph/change_edge_name"
@@ -0,0 +1,15 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # changes an edge name to another valid name
+ # p: String s represents the current edge we want to rename
+ # String snew is what we want to rename the edge to
+ def change_edge_name( s, snew )
+ return nil if @edges.rename(s, snew).nil?
+ self
+ end
+
+ end
+end
@@ -0,0 +1,15 @@
+# required in "abstract_graph/graph"
+
+module AbstractGraph
+ class Graph
+
+ # changes a vertex name to another valid name
+ # p: String s represents the current vertex we want to rename
+ # String snew is what we want to rename the vertex to
+ def change_vertex_name( s, snew )
+ return nil if @vertices.rename(s, snew).nil?
+ self
+ end
+
+ end
+end
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe UniqueNameCollection do
+
+ before :all do
+ @name = "name"
+ @newname = "newname"
+ end
+
+ before :each do
+ @collection = UniqueNameCollection.new
+ @dummy = Dummy.new
+ @dummy.name = @name
+ @collection.add @dummy
+ end
+
+ describe "#rename(String,String)" do
+
+ it "removes the original name from the hash" do
+ @collection.rename @name, @newname
+ @collection[@name].should be_nil
+ end
+
+ it "renames the dummy object" do
+ @collection.rename @name, @newname
+ @dummy.name.should == @newname
+ end
+
+ it "throws an exception if the new name exists" do
+ @dummy = Dummy.new
+ @dummy.name = @newname
+ @collection.add @dummy
+ expect { @collection.rename @name, @newname }.to raise_error
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+module AbstractGraph
+ describe Graph do
+
+ before :all do
+ @vertex1 = "Vertex1"
+ @vertex2 = "Vertex2"
+ @vertex3 = "Vertex3"
+ @edge = "MyEdge"
+ @edgechanged = "MyChangedName"
+ end
+
+ before :each do
+ @graph = Graph.new
+ @graph.add_vertex @vertex1
+ @graph.add_vertex @vertex2
+ @graph.add_vertex @vertex3
+ @graph.add_edge @edge, @vertex1, @vertex2
+ end
+
+ describe "#change_edge_name(String,String)" do
+
+ it "returns an object of class Graph" do
+ @graph.change_edge_name( @edge, @edgechanged ).should be_an_instance_of(Graph)
+ end
+
+ it "changes the name of the vertex" do
+ @graph.change_edge_name @edge, @edgechanged
+ @graph.has_edge?( @edge ).should be_false
+ @graph.has_edge?( @edgechanged ).should be_true
+ end
+
+ it "throws an exception if the name has already been taken" do
+ @graph.add_edge @edgechanged, @vertex2, @vertex3
+ expect { @graph.change_edge_name( @edge, @edgechanged ) }.to raise_error
+ end
+
+ end
+
+ end
+end
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+module AbstractGraph
+ describe Graph do
+
+ before :all do
+ @vertex = "MyVertex"
+ @vertexchanged = "MyChangedName"
+ end
+
+ before :each do
+ @graph = Graph.new
+ @graph.add_vertex @vertex
+ end
+
+ describe "#change_vertex_name(String,String)" do
+
+ it "returns an object of class Graph" do
+ @graph.change_vertex_name( @vertex, @vertexchanged ).should be_an_instance_of(Graph)
+ end
+
+ it "changes the name of the vertex" do
+ @graph.change_vertex_name( @vertex, @vertexchanged )
+ @graph.has_vertex?( @vertex ).should be_false
+ @graph.has_vertex?( @vertexchanged ).should be_true
+ end
+
+ it "returns nil if the vertex does not exist" do
+ @graph.change_vertex_name( @vertexchanged, @vertex ).should be_nil
+ end
+
+ it "throws an exception if the name has already been taken" do
+ @graph.add_vertex @vertexchanged
+ expect { @graph.change_vertex_name( @vertex, @vertexchanged ) }.to raise_error
+ end
+
+ end
+
+ end
+end

0 comments on commit dd58b25

Please sign in to comment.