6
6
7
7
# Fastest way I can think to convert the data into a dict
8
8
# TODO : Probably should be made more efficient...definitely should be made more efficient
9
- # return ::Dict{String, Any}
10
9
function _convertNodeToDict (abstractNode:: N ) where N <: AbstractCGNode
11
10
cp = deepcopy (abstractNode)
12
11
data = length (cp. data) != 0 ? JSON2. write (cp. data) : " {}"
@@ -16,8 +15,6 @@ function _convertNodeToDict(abstractNode::N) where N <: AbstractCGNode
16
15
return ser
17
16
end
18
17
19
- # TODO : Refactor, #HACK :D (but it works!)
20
- # return ::Session
21
18
function _convertDictToSession (dict:: Dict{String, Any} )
22
19
data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
23
20
session = Session (
@@ -31,8 +28,7 @@ function _convertDictToSession(dict::Dict{String, Any})
31
28
dict[" lastUpdatedTimestamp" ])
32
29
return session
33
30
end
34
- # TODO : Refactor, #HACK :D (but it works!)
35
- # returns ::Robot
31
+
36
32
function _convertDictToRobot (dict:: Dict{String, Any} )
37
33
data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
38
34
robot = Robot (
@@ -45,8 +41,7 @@ function _convertDictToRobot(dict::Dict{String, Any})
45
41
dict[" lastUpdatedTimestamp" ])
46
42
return robot
47
43
end
48
- # TODO : Refactor, #HACK :D (but it works!)
49
- # returns ::User
44
+
50
45
function _convertDictToUser (dict:: Dict{String, Any} )
51
46
data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
52
47
user = User (
@@ -59,81 +54,33 @@ function _convertDictToUser(dict::Dict{String, Any})
59
54
return user
60
55
end
61
56
62
- # returns ::User
63
- function createUser (dfg:: Neo4jDFG , user:: User )
64
- Symbol (dfg. userId) != user. id && error (" DFG user ID must match user's ID" )
65
- ! isValidLabel (user) && error (" Node cannot have an ID '$(user. id) '." )
66
-
67
- props = _convertNodeToDict (user)
68
- # TODO : Switch to _queryNeo4j
69
- retNode = _createNode (dfg. neo4jInstance, [" USER" , String (user. id)], props, nothing )
70
- return user
71
- end
72
-
73
- # returns ::Robot
74
- function createRobot (dfg:: Neo4jDFG , robot:: Robot )
75
- Symbol (dfg. robotId) != robot. id && error (" DFG robot ID must match robot's ID" )
76
- Symbol (dfg. userId) != robot. userId && error (" DFG user ID must match robot's user ID" )
77
- ! isValidLabel (robot) && error (" Node cannot have an ID '$(robot. id) '." )
78
-
79
- # Find the parent
80
- parents = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:USER:`$(dfg. userId) `)" )
81
- length (parents) == 0 && error (" Cannot find user '$(dfg. userId) '" )
82
- length (parents) > 1 && error (" Found multiple users '$(dfg. userId) '" )
83
-
84
- # Already exists?
85
- length (_getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:`$(dfg. userId) `:`$(robot. id) `)" )) != 0 &&
86
- error (" Robot '$(robot. id) ' already exists for user '$(robot. userId) '" )
87
-
88
- props = _convertNodeToDict (robot)
89
- # TODO : Switch to _queryNeo4j
90
- retNode = _createNode (dfg. neo4jInstance, [" ROBOT" , String (robot. userId), String (robot. id)], props, parents[1 ], :ROBOT )
91
- return robot
92
- end
93
-
94
- # returns ::Session
95
- function createSession (dfg:: Neo4jDFG , session:: Session )
96
- Symbol (dfg. robotId) != session. robotId && error (" DFG robot ID must match session's robot ID" )
97
- Symbol (dfg. userId) != session. userId && error (" DFG user ID must match session's->robot's->user ID" )
98
- ! isValidLabel (session) && error (" Node cannot have an ID '$(session. id) '." )
99
-
100
- # Find the parent
101
- parents = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:`$(dfg. robotId) `:`$(dfg. userId) `)" )
102
- length (parents) == 0 && error (" Cannot find robot '$(dfg. robotId) ' for user '$(dfg. userId) '" )
103
- length (parents) > 1 && error (" Found multiple robots '$(dfg. robotId) ' for user '$(dfg. userId) '" )
104
-
105
- # Already exists?
106
- length (_getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:`$(session. userId) `:`$(session. robotId) `:`$(session. id) `)" )) != 0 &&
107
- error (" Session '$(session. id) ' already exists for robot '$(session. robotId) ' and user '$(session. userId) '" )
108
-
109
- props = _convertNodeToDict (session)
110
- # TODO : Switch to _queryNeo4j
111
- retNode = _createNode (dfg. neo4jInstance, [" SESSION" , String (session. userId), String (session. robotId), String (session. id)], props, parents[1 ], :SESSION )
112
- return session
113
- end
114
-
115
57
"""
116
58
$(SIGNATURES)
117
- Shortcut method to create the user, robot, and session if it doesn't already exist.
118
-
119
- Notes
120
- - return `::Session`
59
+ Efficient shortcut method to create the user, robot, and session if it doesn't already exist.
121
60
"""
122
61
function createDfgSessionIfNotExist (dfg:: Neo4jDFG )
123
62
strip (dfg. userId) == " " && error (" User ID is not populated in DFG." )
124
63
strip (dfg. robotId) == " " && error (" Robot ID is not populated in DFG." )
125
64
strip (dfg. sessionId) == " " && error (" Session ID is not populated in DFG." )
65
+ ! isValidLabel (dfg. userId) && error (" Node cannot have an ID '$(dfg. userId) '." )
66
+ ! isValidLabel (dfg. robotId) && error (" Node cannot have an ID '$(dfg. robotId) '." )
67
+ ! isValidLabel (dfg. sessionId) && error (" Node cannot have an ID '$(dfg. sessionId) '." )
68
+
126
69
user = User (Symbol (dfg. userId), dfg. userId, " Description for $(dfg. userId) " , Dict {Symbol, String} ())
127
70
robot = Robot (Symbol (dfg. robotId), Symbol (dfg. userId), dfg. robotId, " Description for $(dfg. userId) :$(dfg. robotId) " , Dict {Symbol, String} ())
128
71
session = Session (Symbol (dfg. sessionId), Symbol (dfg. robotId), Symbol (dfg. userId), dfg. sessionId, dfg. description, Dict {Symbol, String} ())
129
72
130
- _getNodeCount (dfg. neo4jInstance, [dfg. userId, " USER" ]) == 0 && createUser (dfg, user)
131
- _getNodeCount (dfg. neo4jInstance, [dfg. userId, dfg. robotId, " ROBOT" ]) == 0 && createRobot (dfg, robot)
132
- if _getNodeCount (dfg. neo4jInstance, [dfg. userId, dfg. robotId, dfg. sessionId, " SESSION" ]) == 0
133
- return createSession (dfg, session)
134
- else
135
- return getSession (dfg)
136
- end
73
+ # NOTE that this doesn't get updated then, you need to use the set* (e.g. setSessionData) functions yourself.
74
+ query = """
75
+ MERGE (u:USER:`$(String (user. id)) `) ON CREATE SET $(join ([" u.$k = '$(v) '" for (k,v) in _convertNodeToDict (user)], " , " )) \r\n
76
+ MERGE (r:ROBOT:`$(String (user. id)) `:`$(String (robot. id)) `) ON CREATE SET $(join ([" r.$k = '$(v) '" for (k,v) in _convertNodeToDict (robot)], " , " )) \r\n
77
+ MERGE (s:SESSION:`$(String (user. id)) `:`$(String (robot. id)) `:`$(String (session. id)) `) ON CREATE SET $(join ([" s.$k = '$(v) '" for (k,v) in _convertNodeToDict (session)], " , " ))
78
+ MERGE (u)-[:ROBOT]->(r)
79
+ MERGE (r)-[:SESSION]->(s)
80
+ """
81
+
82
+ _queryNeo4j (dfg. neo4jInstance, query)
83
+ return nothing
137
84
end
138
85
139
86
"""
@@ -320,7 +267,7 @@ Notes
320
267
- Returns `::Neo4jDFG `
321
268
"""
322
269
function copySession! (sourceDFG:: Neo4jDFG , destDFG:: Union{Nothing, <:Neo4jDFG} )
323
- if destDFG == nothing
270
+ if destDFG === nothing
324
271
destDFG = _getDuplicatedEmptyDFG (sourceDFG)
325
272
end
326
273
_copyIntoGraph! (sourceDFG, destDFG, union (listVariables (sourceDFG), listFactors (sourceDFG)), true )
0 commit comments