Class: Twb::Util::Cypher

Inherits:
Object
  • Object
show all
Includes:
TabTool
Defined in:
lib/twb/util/cypher.rb

Constant Summary collapse

@@hasher =
Digest::SHA256.new

Instance Attribute Summary collapse

Attributes included from TabTool

#alerts, #docDir, #docfiles, #funcdoc, #id, #licensed, #logfilename, #logger, #loglevel, #metrics, #properties, #ttdocdir, #type, #uuid

Instance Method Summary collapse

Methods included from TabTool

#addDocFile, #alert, #closeDocFiles, #config, #docFile, #docFileMaxNameLen, #docfilesdoc, #docfilesdocto_s, #emit, #emitCSV, #finis, #hasConfig, #init, #initCSV, #initDocDir, #initLogger, #license=, #licensed?, #loadConfig

Constructor Details

#initializeCypher

Returns a new instance of Cypher.



32
33
34
35
36
37
38
39
# File 'lib/twb/util/cypher.rb', line 32

def initialize
  emit "Cypher.initialize"
  @fileName  = 'Neo4jCommands'
  @mode      = :create
  @nodes     = []
  @edges     = []
  @cleanup   = false
end

Instance Attribute Details

#cleanupObject

Returns the value of attribute cleanup.



30
31
32
# File 'lib/twb/util/cypher.rb', line 30

def cleanup
  @cleanup
end

#edgesObject



29
30
31
# File 'lib/twb/util/cypher.rb', line 29

def edges
  @edges
end

#fileNameObject



29
30
31
# File 'lib/twb/util/cypher.rb', line 29

def fileName
  @fileName
end

#modeObject



29
30
31
# File 'lib/twb/util/cypher.rb', line 29

def mode
  @mode
end

#nodesObject



29
30
31
# File 'lib/twb/util/cypher.rb', line 29

def nodes
  @nodes
end

Instance Method Details

#encode(node, terminator = '') ⇒ Object



99
100
101
102
103
104
105
# 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 = '') ⇒ Object



107
108
109
# 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

#renderObject



41
42
43
44
45
46
47
48
# 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

#renderEdgesObject

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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

#renderNodesObject



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
# 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_sObject



111
112
113
# File 'lib/twb/util/cypher.rb', line 111

def to_s
  "file:#{@fileName}; #nodes:#{@nodes.length}; #edges:#{@edges.length}"
end