class Twb::Util::Cypher

Attributes

cleanup[RW]
edges[RW]
fileName[RW]
mode[RW]
nodes[RW]

Public Class Methods

new() click to toggle source
# File lib/twb/util/cypher.rb, line 32
def initialize
  emit "Cypher.initialize"
  @fileName  = 'Neo4jCommands'
  @mode      = :create
  @nodes     = []
  @edges     = []
  @cleanup   = false
end

Public Instance Methods

encode(node, terminator='') click to toggle source
# File lib/twb/util/cypher.rb, line 99
def encode node, terminator=''
  puts "def encode node: #{node}  "
  case @mode
  when :merge  then "MERGE (%s:%s { name:'%s', uuid: '%s' } ) %s" % [node.uuid, varName, node.type, node.name, terminator ]
  when :create then "CREATE (#{node.uuid}:#{node.type} {} )"
  end
end
encodeEdge(command, varName, node, terminator='') click to toggle source
# File lib/twb/util/cypher.rb, line 107
def encodeEdge command, varName, node, terminator=''
  "%-8s (%s:%s { uuid: '%s' } ) %s" % [command, varName, node.type, node.uuid, terminator ]
end
render() click to toggle source
# File lib/twb/util/cypher.rb, line 41
def render
  puts "Cypher.render"
  @file = File.open(docFile("#{@fileName}.cypher"),'w')
  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/cypher.rb, line 84
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 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 ;"
  end
  csv.close
end
renderNodes() click to toggle source
# File lib/twb/util/cypher.rb, line 50
def renderNodes
  puts "Cypher def renderNodes @nodes:#{@nodes.to_s}"
  csv = CSV.open(docFile("#{@fileName}.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 << encode(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/cypher.rb, line 111
def to_s
  "file:#{@fileName}; #nodes:#{@nodes.length}; #edges:#{@edges.length}"
end