Class: Mementus::GraphBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mementus/graph_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(is_directed) ⇒ GraphBuilder

Returns a new instance of GraphBuilder.



3
4
5
6
7
# File 'lib/mementus/graph_builder.rb', line 3

def initialize(is_directed)
  @structure = Structure::IncidenceList.new(is_directed)
  @node_ids = IntegerId.new
  @edge_ids = IntegerId.new
end

Instance Method Details

#add_edge(id: nil, from: nil, to: nil, label: nil, props: {}) ⇒ Object



22
23
24
25
# File 'lib/mementus/graph_builder.rb', line 22

def add_edge(id: nil, from: nil, to: nil, label: nil, props: {})
  id = @edge_ids.next_id unless id
  @structure.set_edge(Edge.new(id: id, from: from, to: to, label: label, props: props))
end

#add_node(id: nil, label: nil, props: {}) ⇒ Object



17
18
19
20
# File 'lib/mementus/graph_builder.rb', line 17

def add_node(id: nil, label: nil, props: {})
  id = @node_ids.next_id unless id
  @structure.set_node(Node.new(id: id, label: label, props: props))
end

#create_edge {|builder| ... } ⇒ Object

Yields:

  • (builder)


34
35
36
37
38
39
# File 'lib/mementus/graph_builder.rb', line 34

def create_edge(&block)
  builder = EdgeBuilder.new
  yield builder
  builder.id = @edge_ids.next_id unless builder.id
  set_edge(builder.to_edge)
end

#create_node {|builder| ... } ⇒ Object

Yields:

  • (builder)


27
28
29
30
31
32
# File 'lib/mementus/graph_builder.rb', line 27

def create_node(&block)
  builder = NodeBuilder.new
  yield builder
  builder.id = @node_ids.next_id unless builder.id
  set_node(builder.to_node)
end

#graphObject



41
42
43
# File 'lib/mementus/graph_builder.rb', line 41

def graph
  @structure
end

#set_edge(edge) ⇒ Object



13
14
15
# File 'lib/mementus/graph_builder.rb', line 13

def set_edge(edge)
  @structure.set_edge(edge)
end

#set_node(node) ⇒ Object



9
10
11
# File 'lib/mementus/graph_builder.rb', line 9

def set_node(node)
  @structure.set_node(node)
end