class Twb::Util::CypherPython

Attributes

cleanup[RW]
edges[RW]
fileName[RW]
nodes[RW]
password[RW]
user[RW]

Public Class Methods

new() click to toggle source
# File lib/twb/util/cypherpython.rb, line 34
def initialize
  @docfile   = File.open(@@docfileName,'a+')
  @docfile.puts "Starting up the Cypher in Python process"
  @fileName  = 'Neo4jCypherPython'
  @user      = 'neo4j'
  @password  = 'imthepwd2oo'
  @nodes     = []
  @edges     = []
  @cleanup   = false
end

Public Instance Methods

encodeEdge(command, varName, node, terminator='') click to toggle source
# File lib/twb/util/cypherpython.rb, line 116
def encodeEdge command, varName, node, terminator=''
  "%-8s (%s:%s { uuid: '%s' } ) %s" % [command, varName, node.type, node.uuid, terminator ]
end
encodeNode(command, varName, node, terminator='') click to toggle source
# File lib/twb/util/cypherpython.rb, line 110
def encodeNode command, varName, node, terminator=''
# "g.run('MERGE    (node:CalculatedField { name:\"YTD Cost Amount\", uuid: \"ccd4f66d0c8ee09eca10ab3a1adabe35\" } ) ');"
# "g.run"
  "graph.run( \"%-8s (%s:%s { name:'%s', uuid: '%s' } ) %s\")\nprint '.'," % [command, varName, node.type, node.name.gsub('"','\"'), node.uuid, terminator ]
end
render() click to toggle source
# File lib/twb/util/cypherpython.rb, line 45
def render
  @file = File.open("./ttdoc/#{@fileName}.py",'w')
  @file.puts '# -*- coding: latin-1 -*-'
  @file.puts ' '
  @file.puts 'from py2neo import Graph'
  @file.puts "graph = Graph('http://localhost:7474/db/data', user='#{@user}', password='#{@password}')"
  @file.puts ' '
  renderNodes
  renderEdges
  @file.close
  return @file
end
renderEdges() click to toggle source

USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM “file:://C:/tech/Tableau/Tableau Tools/Ruby/experiments/GraphElements.nodes.csv” AS row CREATE (:row.Type {name: row.Name, uuid: row.UUID});

# File lib/twb/util/cypherpython.rb, line 91
def renderEdges
  csv = CSV.open("./ttdoc/#{@fileName}.edges.csv",'w')
  csv << ['Source Type' , 'Source Name'  , 'Source UUID'  , 'Relationship', 'Target Type', 'Target Name', 'Target UUID' ]
  @edges.each do |edge|
    relationship = edge.relationship.upcase.gsub(/[ ]+/,'_')
    csv << [edge.from.type,  edge.from.name,  edge.from.uuid,  relationship ,  edge.to.type , edge.to.name,  edge.to.uuid ]
    @file.puts ' '
    @file.puts 'query = """'
    @file.puts encodeEdge('MATCH', 'source', edge.from)
    @file.puts encodeEdge('MATCH', 'target', edge.to)
    @file.puts "%-8s (source)-[r:%s]->(target) " % ['MERGE', edge.relationship.upcase.gsub(/[ ]+/,'_')]
    @file.puts "RETURN source.name, type(r), target.name ;"
    @file.puts '"""'
    @file.puts 'graph.run(query)'
    @file.puts "print '-',"
  end
  csv.close
end
renderNodes() click to toggle source
# File lib/twb/util/cypherpython.rb, line 58
def renderNodes
  csv = CSV.open("./ttdoc/#{@fileName}.py.nodes.csv",'w')
  csv << ['Type','Name','UUID']
  nodesCSV = Set.new
  nodeCmds = SortedSet.new
  nodesByType = Hash.new { |type,nodes| type[nodes] = [] }
  @nodes.each do |node|
    nodesCSV << [node.type, node.name, node.uuid]
    nodeCmds << encodeNode('MERGE','node',node,';')
    nodesByType[node.type] << node
  end
  if @cleanup
    nodesByType.keys.each do |type|
      @file.puts "DROP   CONSTRAINT ON (node:#{type}) ASSERT node.uuid IS UNIQUE ;"
    end
    @file.puts "MATCH (n) DETACH DELETE n ;"
    nodesByType.keys.each do |type|
      @file.puts "CREATE CONSTRAINT ON (node:#{type}) ASSERT node.uuid IS UNIQUE ;"
    end
    @file.puts "//--"
  end
  nodesCSV.each do |rec|
    csv << rec
  end
  nodeCmds.each do |cmd|
    @file.puts cmd
  end
  csv.close
end
to_s() click to toggle source
# File lib/twb/util/cypherpython.rb, line 120
def to_s
  "file:#{@fileName}; #nodes:#{@nodes.length}; #edges:#{@edges.length}"
end