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
# File 'lib/twb/util/cypher.rb', line 32

def initialize
  emit "Cypher.initialize"
  @fileName  = 'Neo4jCommands'
  @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

#nodesObject



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

def nodes
  @nodes
end

Instance Method Details

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



96
97
98
# File 'lib/twb/util/cypher.rb', line 96

def encode command, varName, node, terminator=''
  "%-8s (%s:%s { name:'%s', uuid: '%s' } ) %s" % [command, varName, node.type, node.name, node.uuid, terminator ]
end

#encodeEdge(command, varName, node, terminator = '') ⇒ Object



100
101
102
# File 'lib/twb/util/cypher.rb', line 100

def encodeEdge command, varName, node, terminator=''
  "%-8s (%s:%s { uuid: '%s' } ) %s" % [command, varName, node.type, node.uuid, terminator ]
end

#renderObject



40
41
42
43
44
45
46
# File 'lib/twb/util/cypher.rb', line 40

def 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);



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/twb/util/cypher.rb', line 81

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



48
49
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
# File 'lib/twb/util/cypher.rb', line 48

def renderNodes
  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('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_sObject



104
105
106
# File 'lib/twb/util/cypher.rb', line 104

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