-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.rb
102 lines (91 loc) · 2.38 KB
/
graph.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'json'
@graph = []
@node_id = [0]
@edge_id = [0]
def process input
words = input.split(' ')
return create_node words[1] if words[0] == 'node'
return create_edge words[1], words[2], words[3] if words[0] == 'edge'
end
def create_node name
@node_id << @node_id.last+1
node = {
id: @node_id.last,
name: name,
edge: []
}
@graph.push(node)
puts "Node '#{name}' is created"
end
def create_edge label, from, to
@edge_id << @edge_id.last+1
edge = {
id: @edge_id.last,
name: label,
from: from,
to: to
}
mix_node_and edge
puts "Edge '#{label}' is created"
end
def mix_node_and edge
@graph.each do |vertex|
if vertex[:name] == edge[:from] #or vertex[:name] == edge[:to]
vertex[:edge] << edge
end
end
end
def graph_to_json
File.open('result.json', 'w') do |file|
file.puts JSON.pretty_generate(@graph)
end
puts 'data replaced in "result.json" inside root folder'
end
def query_mode
def find_relation from, relation
result = []
@graph.each do |hash|
hash[:edge].each do |h|
if relation == 'in'
result << h[:from] if from == h[:to]
elsif relation == 'out'
result << h[:to] if from == h[:from]
else
result << h[:to] if (h[:name] == relation and h[:from] == from)
end
end
end
return result
end
puts "enter 'exit' to exit query mode"
while true
print "query>>> "
input = gets.chomp
break if input.downcase == "exit"
query_array = Array.new(input.split('.'))
from = query_array.shift
relation = query_array.shift
final_result = find_relation from, relation
if query_array.empty?
puts final_result
else
query_array.each do |relation|
final_result = find_relation final_result.last, relation
end
puts final_result
end
end
end
while true
print "Graph>>> "
input = gets.chomp
if input.downcase == "g.show"
puts @graph.inspect
elsif input.downcase == "g.json"
graph_to_json
elsif input.downcase == "g.query"
query_mode
else
process(input)
end
end