Class: Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/cecelia/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_name = "sqlite:/") ⇒ Graph

Returns a new instance of Graph.



5
6
7
8
9
10
11
12
# File 'lib/cecelia/graph.rb', line 5

def initialize(db_name = "sqlite:/")
  if defined? JRUBY_VERSION
    db_name = "jdbc:" + db_name.sub(":/",":")
  end
  @db = Sequel.connect(db_name)
  @beast = false
  require 'cecelia/graph_model.rb'
end

Instance Attribute Details

#beastObject

Returns the value of attribute beast.



13
14
15
# File 'lib/cecelia/graph.rb', line 13

def beast
  @beast
end

Instance Method Details

#add_edge(source, target, attributes = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cecelia/graph.rb', line 27

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cecelia/graph.rb', line 15

def add_vertex (label = "", attributes = {})
  unless @beast == true
    if Vertices.find(:label => label) == nil
      Vertices.create(:label=> label, :attributes => YAML.dump(attributes))
    end
  else
    if Vertices.find(:label => label) == nil
      @db[:vertices].insert(:label => label, :attributes => YAML.dump(attributes))
    end
  end
end

#dbObject



98
99
100
# File 'lib/cecelia/graph.rb', line 98

def db
  @db
end

#edgesObject



49
50
51
# File 'lib/cecelia/graph.rb', line 49

def edges
  Edges.all
end

#filter_edge(args = {}) ⇒ Object



76
77
78
# File 'lib/cecelia/graph.rb', line 76

def filter_edge(args = {})
  Edges.filter(args)
end

#filter_vertex(args = {}) ⇒ Object



72
73
74
# File 'lib/cecelia/graph.rb', line 72

def filter_vertex(args = {})
  Vertices.filter(args)
end

#find_edge(args = {}) ⇒ Object



68
69
70
# File 'lib/cecelia/graph.rb', line 68

def find_edge(args = {})
  Edges.find(args)
end

#find_vertex(args = {}) ⇒ Object



64
65
66
# File 'lib/cecelia/graph.rb', line 64

def find_vertex(args = {})
  Vertices.find(args)
end

#neighbors(id) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/cecelia/graph.rb', line 53

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



86
87
88
89
# File 'lib/cecelia/graph.rb', line 86

def remove_edge(id)
  id = id.to_i  
  Edges.find(:id => id).delete
end

#remove_vertex(label) ⇒ Object



80
81
82
83
84
# File 'lib/cecelia/graph.rb', line 80

def remove_vertex(label)
  if label.class == String 
    Vertices.find(:label => label).delete
  end
end

#transactionObject



91
92
93
94
95
96
# File 'lib/cecelia/graph.rb', line 91

def transaction
  Vertices.use_transactions = false
  @db.transaction do
    yield(self)
  end 
end

#verticesObject



45
46
47
# File 'lib/cecelia/graph.rb', line 45

def vertices
  Vertices.all
end