Class: Twb::Util::CypherPython

Inherits:
Object
  • Object
show all
Defined in:
lib/twb/util/cypherpython.rb

Constant Summary collapse

@@docfileName =

@@hasher = Digest::SHA256.new

'./ttdoc/cypherpython.log'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCypherPython

Returns a new instance of CypherPython.


34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#cleanupObject

Returns the value of attribute cleanup


32
33
34
# File 'lib/twb/util/cypherpython.rb', line 32

def cleanup
  @cleanup
end

#edgesObject


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

def edges
  @edges
end

#fileNameObject

Returns the value of attribute fileName


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

def fileName
  @fileName
end

#nodesObject


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

def nodes
  @nodes
end

#passwordObject

Returns the value of attribute password


31
32
33
# File 'lib/twb/util/cypherpython.rb', line 31

def password
  @password
end

#userObject

Returns the value of attribute user


31
32
33
# File 'lib/twb/util/cypherpython.rb', line 31

def user
  @user
end

Instance Method Details

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


116
117
118
# 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 = '') ⇒ Object


110
111
112
113
114
# 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

#renderObject


45
46
47
48
49
50
51
52
53
54
55
56
# 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

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


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#renderNodesObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_sObject


120
121
122
# File 'lib/twb/util/cypherpython.rb', line 120

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