abstract_graph 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.rdoc +43 -0
- data/Rakefile +14 -0
- data/abstract_graph.gemspec +22 -0
- data/lib/abstract_graph.rb +10 -0
- data/lib/abstract_graph/composition.rb +11 -0
- data/lib/abstract_graph/composition/edge.rb +21 -0
- data/lib/abstract_graph/composition/edge/initialize.rb +18 -0
- data/lib/abstract_graph/composition/edge/is_coincident.rb +15 -0
- data/lib/abstract_graph/composition/uniquenamecollection.rb +21 -0
- data/lib/abstract_graph/composition/uniquenamecollection/add.rb +19 -0
- data/lib/abstract_graph/composition/uniquenamecollection/dup.rb +20 -0
- data/lib/abstract_graph/composition/uniquenamecollection/initialize.rb +14 -0
- data/lib/abstract_graph/composition/uniquenamecollection/link.rb +32 -0
- data/lib/abstract_graph/composition/uniquenamecollection/method_missing.rb +14 -0
- data/lib/abstract_graph/composition/vertex.rb +21 -0
- data/lib/abstract_graph/composition/vertex/delete.rb +12 -0
- data/lib/abstract_graph/composition/vertex/initialize.rb +14 -0
- data/lib/abstract_graph/graph.rb +25 -0
- data/lib/abstract_graph/graph/add_edge.rb +30 -0
- data/lib/abstract_graph/graph/add_vertex.rb +16 -0
- data/lib/abstract_graph/graph/delete_edge.rb +21 -0
- data/lib/abstract_graph/graph/delete_vertex.rb +21 -0
- data/lib/abstract_graph/graph/dup.rb +14 -0
- data/lib/abstract_graph/graph/has_edge.rb +13 -0
- data/lib/abstract_graph/graph/has_vertex.rb +13 -0
- data/lib/abstract_graph/graph/initialize.rb +16 -0
- data/lib/abstract_graph/version.rb +5 -0
- data/spec/abstract_graph/composition/edge/initialize_spec.rb +38 -0
- data/spec/abstract_graph/composition/edge/is_coincident_spec.rb +33 -0
- data/spec/abstract_graph/composition/edge/name_spec.rb +53 -0
- data/spec/abstract_graph/composition/edge/vertices_spec.rb +32 -0
- data/spec/abstract_graph/composition/edge_spec.rb +8 -0
- data/spec/abstract_graph/composition/uniquenamecollection/add_spec.rb +41 -0
- data/spec/abstract_graph/composition/uniquenamecollection/dup_spec.rb +29 -0
- data/spec/abstract_graph/composition/uniquenamecollection/initialize_spec.rb +21 -0
- data/spec/abstract_graph/composition/uniquenamecollection/link_spec.rb +67 -0
- data/spec/abstract_graph/composition/uniquenamecollection/method_missing_spec.rb +50 -0
- data/spec/abstract_graph/composition/uniquenamecollection_spec.rb +8 -0
- data/spec/abstract_graph/composition/vertex/delete_spec.rb +12 -0
- data/spec/abstract_graph/composition/vertex/initialize_spec.rb +45 -0
- data/spec/abstract_graph/composition/vertex/name_spec.rb +47 -0
- data/spec/abstract_graph/composition/vertex/value_spec.rb +46 -0
- data/spec/abstract_graph/composition/vertex_spec.rb +8 -0
- data/spec/abstract_graph/graph/add_edge_spec.rb +46 -0
- data/spec/abstract_graph/graph/add_vertex_spec.rb +29 -0
- data/spec/abstract_graph/graph/delete_edge_spec.rb +56 -0
- data/spec/abstract_graph/graph/delete_vertex_spec.rb +48 -0
- data/spec/abstract_graph/graph/dup_spec.rb +37 -0
- data/spec/abstract_graph/graph/has_edge_spec.rb +31 -0
- data/spec/abstract_graph/graph/has_vertex_spec.rb +27 -0
- data/spec/abstract_graph/graph/intialize_spec.rb +17 -0
- data/spec/abstract_graph/graph_spec.rb +6 -0
- data/spec/abstract_graph_spec.rb +7 -0
- data/spec/dummy_helper.rb +4 -0
- data/spec/spec_helper.rb +11 -0
- metadata +149 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# add a vertex named s to the graph
|
7
|
+
# p: String s represents the name of the wanted vertex
|
8
|
+
def add_vertex( s )
|
9
|
+
# create the vertex
|
10
|
+
vertex = Vertex.new s
|
11
|
+
@vertices.add vertex
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# delete an edge in a replicated graph
|
7
|
+
# p: String s represents the name of the edge
|
8
|
+
def delete_edge( s )
|
9
|
+
other = self.dup
|
10
|
+
other.delete_edge! s
|
11
|
+
end
|
12
|
+
|
13
|
+
# delete an edge in a current graph
|
14
|
+
# p: String s represents the name of the edge
|
15
|
+
def delete_edge!( s )
|
16
|
+
@edges.delete s
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# delete a vertex in a new graph
|
7
|
+
# p: String s represents the name of the vertex
|
8
|
+
def delete_vertex( s )
|
9
|
+
other = self.dup
|
10
|
+
other.delete_vertex! s
|
11
|
+
end
|
12
|
+
|
13
|
+
# delete a vertex in a current graph
|
14
|
+
# p: String s represents the name of the vertex
|
15
|
+
def delete_vertex!( s )
|
16
|
+
@vertices.delete s
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# required in "abstract_graph/graph"
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
class Graph
|
5
|
+
|
6
|
+
# returns whether there exists a vertex with name string
|
7
|
+
# p: String s represents name of query vertex
|
8
|
+
def has_vertex?( s )
|
9
|
+
@vertices.has_key? s || false
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Edge do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@v1 = Vertex.new "v1"
|
9
|
+
@v2 = Vertex.new "v2"
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#new(Vertex, Vertex)" do
|
13
|
+
|
14
|
+
before :each do
|
15
|
+
@edge = Edge.new( @v1, @v2 )
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns an object of class Edge" do
|
19
|
+
@edge.should be_an_instance_of(Edge)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#new(String, Vertex, Vertex)" do
|
25
|
+
|
26
|
+
before :each do
|
27
|
+
@edge = Edge.new("e1", @v1, @v2)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "allows vertices to be initiated with a string name" do
|
31
|
+
@edge.should_not be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Edge do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@v1 = Vertex.new "v1"
|
9
|
+
@v2 = Vertex.new "v2"
|
10
|
+
@v3 = Vertex.new "v3"
|
11
|
+
end
|
12
|
+
|
13
|
+
before :each do
|
14
|
+
@edge = Edge.new( "e1", @v1, @v2 )
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#is_coincident?(Edge)" do
|
18
|
+
|
19
|
+
it "returns true if it joins the same two vertices" do
|
20
|
+
edge2 = Edge.new( "e2", @v1, @v2 )
|
21
|
+
@edge.is_coincident?( edge2 ).should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns false if the two edges span >= 3 vertices" do
|
25
|
+
edge2 = Edge.new( "e2", @v1, @v3 )
|
26
|
+
@edge.is_coincident?( edge2 ).should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Edge do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@v1 = Vertex.new "v1"
|
9
|
+
@v2 = Vertex.new "v2"
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#name" do
|
13
|
+
|
14
|
+
before :each do
|
15
|
+
@arbitraryString = "arbitraryString"
|
16
|
+
@edge = Edge.new @arbitraryString, @v1, @v2
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns a String (Always)" do
|
20
|
+
@edge.name.should be_an_instance_of(String)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "equals to the String in #new(String)" do
|
24
|
+
@edge.name.should eql(@arbitraryString)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "equals to empty string if no string was passed in" do
|
28
|
+
edge1 = Edge.new @v1, @v2
|
29
|
+
edge1.name.should eql("")
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#name=" do
|
35
|
+
|
36
|
+
before :each do
|
37
|
+
@edge = Edge.new @v1, @v2
|
38
|
+
end
|
39
|
+
|
40
|
+
it "names the edge and retrieved by #name" do
|
41
|
+
@edge.name = "Hello World"
|
42
|
+
@edge.name.should eql("Hello World")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "doesn't allow a non-String input" do
|
46
|
+
expect { @edge.name = 100 }.to raise_error
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Edge do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
@v1 = Vertex.new "v1"
|
9
|
+
@v2 = Vertex.new "v2"
|
10
|
+
@edge = Edge.new( @v1, @v2 )
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#vertices" do
|
14
|
+
|
15
|
+
it "should be an enumerable" do
|
16
|
+
@edge.vertices.should be_an_instance_of(Array)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "must be two vertices" do
|
20
|
+
@edge.vertices.should have(2).vertices
|
21
|
+
end
|
22
|
+
|
23
|
+
it "is the vertices passed from constructor" do
|
24
|
+
@edge.vertices.should include( @v1 )
|
25
|
+
@edge.vertices.should include( @v2 )
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe UniqueNameCollection do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@collection = UniqueNameCollection.new
|
9
|
+
@dummy = Dummy.new
|
10
|
+
@dummy.name = "DummyName"
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#add(Object)" do
|
14
|
+
|
15
|
+
it "tracks an object that has implemented #name" do
|
16
|
+
@collection.add @dummy
|
17
|
+
end
|
18
|
+
|
19
|
+
it "doesn't allow us to add the same object twice" do
|
20
|
+
@collection.add @dummy
|
21
|
+
expect { @collection.add @dummy }.to raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
it "doesn't allow two objects to have the same name" do
|
25
|
+
dummy2 = Dummy.new
|
26
|
+
dummy2.name = "DummyName"
|
27
|
+
@collection.add @dummy
|
28
|
+
expect { @collection.add dummy2 }.to raise_error
|
29
|
+
end
|
30
|
+
|
31
|
+
it "allows a same named object after deleting it" do
|
32
|
+
@collection.add @dummy
|
33
|
+
@collection.delete @dummy.name
|
34
|
+
expect { @collection.add @dummy }.to_not raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe UniqueNameCollection do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@collection = UniqueNameCollection.new
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#dup" do
|
12
|
+
|
13
|
+
it "doesn't have the same id as the original" do
|
14
|
+
@collection.object_id.should_not == @collection.dup.object_id
|
15
|
+
end
|
16
|
+
|
17
|
+
it "creates a duplicate of every item in the collection" do
|
18
|
+
secondCollection = @collection.dup
|
19
|
+
dummy = Dummy.new
|
20
|
+
dummy.name = "Dummy"
|
21
|
+
@collection.add dummy
|
22
|
+
secondCollection.collection.size.should == 0
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe UniqueNameCollection do
|
6
|
+
|
7
|
+
before (:each) do
|
8
|
+
@collection = UniqueNameCollection.new
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#new" do
|
12
|
+
|
13
|
+
it "returns an object of class UniqueNameCollection" do
|
14
|
+
@collection.should be_an_instance_of(UniqueNameCollection)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe UniqueNameCollection do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@collection1 = UniqueNameCollection.new
|
9
|
+
@collection2 = UniqueNameCollection.new
|
10
|
+
@dummy = Dummy.new
|
11
|
+
@dummy.name = "DummyName"
|
12
|
+
@dummy2 = Dummy.new
|
13
|
+
@dummy2.name = "DummyName"
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#link(UniqueNameCollection)" do
|
17
|
+
|
18
|
+
it "doesn't allow first collection to add object of same name" do
|
19
|
+
@collection1.link @collection2
|
20
|
+
@collection2.add @dummy
|
21
|
+
expect { @collection1.add @dummy2 }.to raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
it "doesn't allow second collection to add object of same name" do
|
25
|
+
@collection1.link @collection2
|
26
|
+
@collection1.add @dummy
|
27
|
+
expect { @collection2.add @dummy2 }.to raise_error
|
28
|
+
end
|
29
|
+
|
30
|
+
it "doesn't allow second collection to re-add objects that already existed" do
|
31
|
+
@collection1.add @dummy
|
32
|
+
@collection1.link @collection2
|
33
|
+
expect { @collection2.add @dummy2 }.to raise_error
|
34
|
+
end
|
35
|
+
|
36
|
+
it "doesn't allow first collection to re-add objects that already existed" do
|
37
|
+
@collection2.add @dummy
|
38
|
+
@collection1.link @collection2
|
39
|
+
expect { @collection1.add @dummy2 }.to raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns nil if the intersection during link isn't nil" do
|
43
|
+
@collection1.add @dummy
|
44
|
+
@collection2.add @dummy2
|
45
|
+
@collection1.link(@collection2).should be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "allows a same object ofter deletion" do
|
49
|
+
@collection1.add @dummy
|
50
|
+
@collection1.link @collection2
|
51
|
+
@collection1.delete @dummy.name
|
52
|
+
expect { @collection2.add @dummy2 }.to_not raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it "can link more than 2 collections together" do
|
56
|
+
collection3 = UniqueNameCollection.new
|
57
|
+
@collection1.link @collection2
|
58
|
+
@collection2.link collection3
|
59
|
+
@collection1.add @dummy
|
60
|
+
expect { collection3.add @dummy2 }.to raise_error
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|