Skip to content

Commit

Permalink
showing 1st level relations
Browse files Browse the repository at this point in the history
  • Loading branch information
adarsh-why committed Jun 10, 2016
1 parent 371e02a commit d8f7664
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 12 deletions.
47 changes: 37 additions & 10 deletions graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def process input
words = input.split(' ')
return create_node words[1] if words[0] == 'node'
return create_edge words[1], words[2].to_i, words[3].to_i if words[0] == 'edge'
return create_edge words[1], words[2], words[3] if words[0] == 'edge'
end

def create_node name
Expand All @@ -21,42 +21,69 @@ def create_node name
puts "Node '#{name}' is created"
end

def create_edge label, ins, out
def create_edge label, from, to
@edge_id << @edge_id.last+1
edge = {
id: @edge_id.last,
name: label,
ins: [].push(ins),
out: [].push(out)
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[:id] == edge[:ins].last or vertex[:id] == edge[:out].last
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|
@graph.each do |line|
file.puts "#{line.to_json}\n"
end
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"
from, relation = input.split('.')
puts find_relation from, relation
end
end

while true
print "Graph>>> "
input = gets.chomp
if input.downcase == "check graph"
if input.downcase == "g.show"
puts @graph.inspect
elsif input.downcase == "graph to json"
elsif input.downcase == "g.json"
graph_to_json
elsif input.downcase == "g.query"
query_mode
else
process(input)
end
Expand Down
185 changes: 183 additions & 2 deletions result.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,183 @@
{"id":1,"name":"Adarsh","edge":[{"id":1,"name":"friends","ins":[1],"out":[2]}]}
{"id":2,"name":"Pankaj","edge":[{"id":1,"name":"friends","ins":[1],"out":[2]}]}
[
{
"id": 1,
"name": "saturn",
"edge": [

]
},
{
"id": 2,
"name": "sky",
"edge": [

]
},
{
"id": 3,
"name": "sea",
"edge": [

]
},
{
"id": 4,
"name": "jupiter",
"edge": [
{
"id": 1,
"name": "father",
"from": "jupiter",
"to": "saturn"
},
{
"id": 2,
"name": "lives",
"from": "jupiter",
"to": "sky"
},
{
"id": 3,
"name": "brother",
"from": "jupiter",
"to": "neptune"
},
{
"id": 11,
"name": "brother",
"from": "jupiter",
"to": "pluto"
}
]
},
{
"id": 5,
"name": "neptune",
"edge": [
{
"id": 4,
"name": "brother",
"from": "neptune",
"to": "jupiter"
},
{
"id": 5,
"name": "lives",
"from": "neptune",
"to": "sea"
},
{
"id": 14,
"name": "brother",
"from": "neptune",
"to": "pluto"
}
]
},
{
"id": 6,
"name": "hercules",
"edge": [
{
"id": 6,
"name": "father",
"from": "hercules",
"to": "jupiter"
},
{
"id": 7,
"name": "mother",
"from": "hercules",
"to": "alcmene"
},
{
"id": 8,
"name": "battled",
"from": "hercules",
"to": "nemean"
},
{
"id": 9,
"name": "battled",
"from": "hercules",
"to": "hydra"
},
{
"id": 10,
"name": "battled",
"from": "hercules",
"to": "cerberus"
}
]
},
{
"id": 7,
"name": "alcmene",
"edge": [

]
},
{
"id": 8,
"name": "pluto",
"edge": [
{
"id": 12,
"name": "brother",
"from": "pluto",
"to": "jupiter"
},
{
"id": 13,
"name": "brother",
"from": "pluto",
"to": "neptune"
},
{
"id": 15,
"name": "pet",
"from": "pluto",
"to": "cerberus"
},
{
"id": 16,
"name": "lives",
"from": "pluto",
"to": "tartarus"
}
]
},
{
"id": 9,
"name": "nemean",
"edge": [

]
},
{
"id": 10,
"name": "hydra",
"edge": [

]
},
{
"id": 11,
"name": "cerberus",
"edge": [
{
"id": 17,
"name": "lives",
"from": "cerberus",
"to": "tartarus"
}
]
},
{
"id": 12,
"name": "tartarus",
"edge": [

]
}
]

0 comments on commit d8f7664

Please sign in to comment.