Class: Graph
- Inherits:
-
Object
- Object
- Graph
- Defined in:
- lib/cecelia/graph.rb
Instance Method Summary collapse
- #add_edge(source, target, attributes = {}) ⇒ Object
- #add_vertex(label = "", attributes = {}) ⇒ Object
- #edges ⇒ Object
- #filter_edge(args = {}) ⇒ Object
- #filter_vertex(args = {}) ⇒ Object
- #find_edge(args = {}) ⇒ Object
- #find_vertex(args = {}) ⇒ Object
-
#initialize(db_name = "sqlite:/") ⇒ Graph
constructor
A new instance of Graph.
- #neighbors(id) ⇒ Object
- #remove_edge(id) ⇒ Object
- #remove_vertex(label) ⇒ Object
- #vertices ⇒ Object
Constructor Details
#initialize(db_name = "sqlite:/") ⇒ Graph
Returns a new instance of Graph.
5 6 7 8 |
# File 'lib/cecelia/graph.rb', line 5 def initialize(db_name = "sqlite:/") @db = Sequel.connect(db_name) require 'cecelia/graph_model.rb' end |
Instance Method Details
#add_edge(source, target, attributes = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cecelia/graph.rb', line 16 def add_edge(source, target, attributes = {}) if source.class == String && target.class == String begin source_id = Vertices.find(:label => source)[:id] target_id = Vertices.find(:label => target)[:id] rescue add_vertex(source) add_vertex(target) source_id = Vertices.find(:label => source)[:id] target_id = Vertices.find(:label => target)[:id] ensure add_edge_id(source_id, target_id, attributes = {}) end else add_edge_id(source, target, attributes = {}) end end |
#add_vertex(label = "", attributes = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/cecelia/graph.rb', line 10 def add_vertex (label = "", attributes = {}) if Vertices.find(:label => label) == nil Vertices.create(:label=> label, :attributes => YAML.dump(attributes)) end end |
#edges ⇒ Object
38 39 40 |
# File 'lib/cecelia/graph.rb', line 38 def edges Edges.all end |
#filter_edge(args = {}) ⇒ Object
65 66 67 |
# File 'lib/cecelia/graph.rb', line 65 def filter_edge(args = {}) Edges.filter(args) end |
#filter_vertex(args = {}) ⇒ Object
61 62 63 |
# File 'lib/cecelia/graph.rb', line 61 def filter_vertex(args = {}) Vertices.filter(args) end |
#find_edge(args = {}) ⇒ Object
57 58 59 |
# File 'lib/cecelia/graph.rb', line 57 def find_edge(args = {}) Edges.find(args) end |
#find_vertex(args = {}) ⇒ Object
53 54 55 |
# File 'lib/cecelia/graph.rb', line 53 def find_vertex(args = {}) Vertices.find(args) end |
#neighbors(id) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cecelia/graph.rb', line 42 def neighbors(id) dataset = nil if id.class == Integer dataset = Edges.filter(:source => id).all elsif id.class == String label = Vertices.find(:label => id)[:id] dataset = Edges.filter(:source => label).all end dataset end |
#remove_edge(id) ⇒ Object
75 76 77 78 |
# File 'lib/cecelia/graph.rb', line 75 def remove_edge(id) id = id.to_i Edges.find(:id => id).delete end |
#remove_vertex(label) ⇒ Object
69 70 71 72 73 |
# File 'lib/cecelia/graph.rb', line 69 def remove_vertex(label) if label.class == String Vertices.find(:label => label).delete end end |
#vertices ⇒ Object
34 35 36 |
# File 'lib/cecelia/graph.rb', line 34 def vertices Vertices.all end |