Skip to content

Instantly share code, notes, and snippets.

@kunitoo
Last active December 26, 2015 08:07
Show Gist options
  • Save kunitoo/0170099d3eacb3364a28 to your computer and use it in GitHub Desktop.
Save kunitoo/0170099d3eacb3364a28 to your computer and use it in GitHub Desktop.
第2回 ESMオフラインどう書く 例題 https://gist.github.com/mattsan/38a752dca30423902b0a
require 'rubygems'
require 'neography'
@neo = Neography::Rest.new
def clean_nodes
@neo.execute_query('match n detach delete n')
end
def setup_nodes
@nodes = ('A'..'J').to_a.map {|name|
node = Neography::Node.create(name: name)
@neo.add_label(node, 'Vertex')
[name.to_sym, node]
}.to_h
create_reloationship(@nodes[:A], @nodes[:H], @nodes[:I])
create_reloationship(@nodes[:B], @nodes[:D], @nodes[:G])
create_reloationship(@nodes[:C], @nodes[:J], @nodes[:A])
create_reloationship(@nodes[:D], @nodes[:F], @nodes[:I])
create_reloationship(@nodes[:E], @nodes[:B], @nodes[:C])
create_reloationship(@nodes[:F], @nodes[:H], @nodes[:A])
create_reloationship(@nodes[:G], @nodes[:D], @nodes[:E])
create_reloationship(@nodes[:H], @nodes[:J], @nodes[:C])
create_reloationship(@nodes[:I], @nodes[:F], @nodes[:G])
create_reloationship(@nodes[:J], @nodes[:B], @nodes[:E])
end
def create_reloationship(from_node, r_node, w_node)
Neography::Relationship.create(:R, from_node, r_node)
Neography::Relationship.create(:W, from_node, w_node)
end
def resolve(start_name, directions)
directions.inject([start_name]) {|r, d|
query = "match ({name: {start_name}})-[:#{d}]->(e) return e.name"
response = @neo.execute_query(query, start_name: r.last)
r << response.dig('data').join
}.join
end
def test(str, expect)
start_name = str.chars.first
directions = str.chars.drop(1)
res = resolve(start_name, directions)
puts %Q[test(#{str}, #{expect}) #=> #{res == expect}]
end
clean_nodes
setup_nodes
test("AW", "AI")
test("GR", "GD")
test("GW", "GE")
test("IR", "IF")
test("HR", "HJ")
test("BWW", "BGE")
test("ARW", "AHC")
test("GRR", "GDF")
test("BWR", "BGD")
test("JWWW", "JECA")
test("DRRR", "DFHJ")
test("CWWR", "CAIF")
test("HWWW", "HCAI")
test("GWRWR", "GEBGD")
test("FRRRW", "FHJBG")
test("JRRWW", "JBDIG")
test("JWWRRW", "JECJBG")
test("GRRRWW", "GDFHCA")
test("BRWRWR", "BDIFAH")
test("IRWRRWR", "IFAHJEB")
test("IWWWRRW", "IGECJBG")
test("GWWRWWR", "GECJECJ")
test("HRRWRWRW", "HJBGDIFA")
test("FRWWWRRW", "FHCAIFHC")
test("HRWWWRWRW", "HJECAHCJE")
test("CWWWRRWWW", "CAIGDFAIG")
test("BRRRWRRRRW", "BDFHCJBDFA")
test("FRWRRWRRWW", "FHCJBGDFAI")
test("GWRRRRWRWRW", "GEBDFHCJEBG")
test("DRWWWWWWRRW", "DFAIGECAHJE")
test("ARRRRWRRRRWW", "AHJBDIFHJBGE")
test("AWWWWWWRRWRR", "AIGECAIFHCJB")
test("JWWWRRWRWRWWR", "JECAHJEBGDIGD")
test("CRWRWRRWWWRWW", "CJEBGDFAIGDIG")
test("DWRWRWRWRWWRWW", "DIFAHCJEBGEBGE")
test("GRWWWRRRRWRWRR", "GDIGEBDFHCJEBD")
test("ARWWWRWWRWWWWWW", "AHCAIFAIFAIGECA")
test("DWRWRRWRWWRWWRW", "DIFAHJEBGEBGEBG")
test("JRWRRRRRWRRRRRWR", "JBGDFHJBGDFHJBGD")
test("IRWWRRWWWWWRRWWR", "IFAIFHCAIGEBDIGD")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment