-
Notifications
You must be signed in to change notification settings - Fork 0
/
prov.py
executable file
·288 lines (239 loc) · 13 KB
/
prov.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import sys, os, decimal
import json
import networkx as nx
from pprint import pprint
import uuid
from time import strptime
import copy
from dateutil import parser
from pytz import utc
import calendar
#from datetime import datetime, timedelta
import datetime #, timedelta
#"2014-11-01T00:06:00" , "2015-02-02T12:39:51-08:00"
#DATETIME_FORMATS = ['%Y-%m-%dT%H:%M:%S' , '%Y-%m-%dT%H:%M:%S%:z' ]
# checks if input string contains a date in one of acceptable date-time formats
def is_valid_date(input_date_time):
result = None
#for format in DATETIME_FORMATS:
try:
result = parser.parse(input_date_time) #(input_date_time, format)
except:
print "Unexpected error:", sys.exc_info()[0]
pass
if result is None:
# print 'Malformed date.'
return False
else:
# print 'Date is fine.'
return True
return True
# returns a "date" format from a string containing a date in one of acceptable date-time formats
def get_date(input_date_time):
result = None
print input_date_time
try:
result = parser.parse(input_date_time)
except:
pass
if result.tzinfo is None:
return utc.localize(result)
else:
return result
# returns value of a field of an element from a list of elements
def getField(elementsList, element, filedId):
return elementsList[element][elementsList[element]['__namespace']+':'+filedId]
def validateJSONRequest(jsonobj):
# provg = nx.DiGraph()
#check that project field is in json request
#projectId = jsonobj.get('provdb:projectId') or ""
#if projectId == "":
# return False, "There is no projectId specified in JSON request"
#add nodes; currently entities and activities
entities = jsonobj.get('entity')
if entities is None:
return False, "There are no entities"
activities = jsonobj.get('activity') or []
unique_entities_dict = {} # (key:value) (uuid_creationTime:entity_name)
for k in entities:
# "foundry:en9"
namespace , entity_name = k.split(':')
#print "adding entity " +str (k) + " having value ="
#pprint(entities[k])
entity_uuid = entities[k].get(namespace+':UUID')
entity_creationTime = entities[k].get(namespace+':creationTime')
entity_version = entities[k].get(namespace+':version')
if entity_uuid is None:
return False, "There is no UUID in entity "+k
if entity_creationTime is None:
return False, "There is no creationTime in entity "+k
if entity_version is None:
return False, "There is no version in entity "+k
if not is_valid_date(entity_creationTime['$']):
return False, "Invalid creationTime format in entity "+k
entities_uniqueness_key = entity_uuid['$']+entity_creationTime['$'] + entity_version['$'] # maybe add version
existing_entity = unique_entities_dict.get(entities_uniqueness_key,None)
if existing_entity is not None:
return False, "Entities "+existing_entity+" and "+ k + " have the same UUID and creationTime"
else:
unique_entities_dict[entities_uniqueness_key] = k
entities[k]['__namespace'] = namespace
#print entities[k][namespace+':creationTime']['$']
# All good with entity
# provg.add_node(k, entities.get(k) )
for k in activities:
namespace , entity_name = k.split(':')
#print "adding activity " +str (k) + " having value ="
#pprint(activities[k])
if activities[k].get('prov:type') is None:
return False, "There is no type in activity "+k
if activities[k].get('prov:startTime') is None:
return False, "There is no startTime in activity "+k
if activities[k].get('prov:endTime') is None:
return False, "There is no endTime in activity "+k
if get_date(activities[k]['prov:endTime']) < \
get_date(activities[k]['prov:startTime']):
return False, "StartTime should be before endTime in activity "+k
if not is_valid_date(activities[k]['prov:startTime']):
return False, "Invalid startTime format in activity "+k
if not is_valid_date(activities[k]['prov:endTime']):
return False, "Invalid endTime format in activity "+k
activities[k]['__namespace'] = 'prov' #namespace
# provg.add_node(k, activities.get(k) )
#add relationships
wasGeneratedBy = jsonobj.get('wasGeneratedBy') or []
used = jsonobj.get('used') or []
wasDerivedFrom = jsonobj.get('wasDerivedFrom') or []
for k in wasGeneratedBy:
#The set of all entity nodes that were "wasGeneratedBy" an activity A have creation time between activity A's start_time and end_time
#print "adding wasGeneratedBy edge " +str (k) + " having value =" + str(wasGeneratedBy[k])
activity = wasGeneratedBy[k]['prov:activity']
entity = wasGeneratedBy[k]['prov:entity']
if get_date(getField(activities, activity, 'endTime')) < \
get_date(getField(entities, entity, 'creationTime')['$']):
return False, "wasGeneratedBy error for "+k+":creationTime of entity "+entity+\
" should be between startTime and endTime of activity "+activity
if get_date(getField(activities, activity, 'startTime')) > \
get_date(getField(entities, entity, 'creationTime')['$']):
return False, "wasGeneratedBy error for "+k+":creationTime of entity "+entity+\
" should be between startTime and endTime of activity "+activity
# provg.add_edge(entity,activity, dict(type=wasGeneratedBy, name=k) )
for k in used:
#The set of all entity nodes that were "used" by an activity A have creation time less than activity A's end_time.
#print "adding used edge " +str (k) + " having value =" + str(used[k])
activity = used[k]['prov:activity']
entity = used[k]['prov:entity']
if get_date(getField(activities,activity,'endTime')) < get_date(getField(entities, entity, 'creationTime')['$']):
return False, "used error for "+k+":creationTime of entity "+entity+\
" should be before endTime of activity "+activity
# provg.add_edge(activity,entity, dict(type=used, name=k) )
for k in wasDerivedFrom:
#In the "wasDerivedBy" edge the source entity has a version number and creation time that is
# lower than destination entity version number and creation time, but have the same UUID.
entity_source = wasDerivedFrom[k]['prov:usedEntity']
entity_destination =wasDerivedFrom[k]['prov:generatedEntity']
#print "source details: "
#pprint(entities[entity_source])
#print "destination details: "
#pprint(entities[entity_destination])
if getField(entities, entity_source, 'UUID') != getField(entities, entity_destination, 'UUID'):
return False, "wasDerivedFrom error for "+k+":UUID of entity "+entity_source+\
" should be the same as UUID of entity "+entity_destination
if get_date(getField(entities, entity_source, 'creationTime')['$']) > \
get_date(getField(entities, entity_destination, 'creationTime')['$']):
return False, "wasDerivedFrom error for "+k+":creationTime of entity "+entity_source+\
" should be lower than creationTime of entity "+entity_destination
#if getField(entities, entity_source, 'version') >= getField(entities, entity_destination, 'version'):
# return False, "wasDerivedFrom error for "+k+":version of entity "+entity_source+\
# " should be lower than version of entity "+entity_destination
#print "adding wasDerivedFrom edge " +str (k) + " having value =" + str(wasDerivedFrom[k])
# provg.add_edge(entity_source, entity_destination, dict(type=wasDerivedFrom, name=k) )
# remove additional
#print "-------------"
#print provg.nodes()
#print provg.edges()
return True,"validation successful"
def jsonid_rename(jsonobj):
#graph_uuid = projectId #"need to figure out what is a good id"
#str(uuid.uuid3(uuid.NAMESPACE_DNS, str("need to figure out what is a good id")))
name_mapping = {} # old_name:new_name
namespace = None
entities = jsonobj.get('entity')
activities = jsonobj.get('activity') or []
jsonobj2 = copy.deepcopy(jsonobj)
for k in entities:
namespace , entity_name = k.split(':')
namespace = namespace
#big_string = k[namespace+':UUID'] + k[namespace+':creationTime'] + k[namespace+':version']
big_string = str(namespace) + str(jsonobj['entity'][k][namespace+':UUID']) + \
str(jsonobj['entity'][k][namespace+':creationTime']) + \
str(jsonobj['entity'][k][namespace+':version'])
name_mapping[k] = str(uuid.uuid3(uuid.NAMESPACE_DNS, big_string))
#print "new value for entity "+str(k) +" is "+ name_mapping[k]
#add field to identify subgraph
jsonobj2['entity'][k]['provdb:projectId'] = namespace #graph_uuid
# we found the new name for object, let's change it
jsonobj2['entity'].setdefault(name_mapping[k], jsonobj2['entity'].pop(k))
for k in activities or []:
big_string = str(namespace) + str(jsonobj['activity'][k]['prov:type']) + \
str(jsonobj['activity'][k]['prov:startTime']) + \
str(jsonobj['activity'][k]['prov:endTime'])
#str(jsonobj['entity'][k][namespace+':version'])
name_mapping[k] = str(uuid.uuid3(uuid.NAMESPACE_DNS, big_string))
#name_mapping[k] = str(uuid.uuid3(uuid.NAMESPACE_DNS, str(jsonobj['activity'][k]['prov:type'])))
#print "new value for activity "+str(k) +" is "+ name_mapping[k]
#add field to identify subgraph
jsonobj2['activity'][k]['provdb:projectId'] = namespace #graph_uuid
# we found the new name for object, let's change it
jsonobj2['activity'].setdefault(name_mapping[k], jsonobj2['activity'].pop(k))
wasGeneratedBy = jsonobj.get('wasGeneratedBy') or []
used = jsonobj.get('used') or []
wasDerivedFrom = jsonobj.get('wasDerivedFrom') or []
for k in wasGeneratedBy:
activity = wasGeneratedBy[k]['prov:activity']
entity = wasGeneratedBy[k]['prov:entity']
#big_string = str(namespace) + str("activity") + str(jsonobj['wasGeneratedBy'][k]['prov:activity']) + \
# str(jsonobj['wasGeneratedBy'][k]['prov:entity'])
big_string = str(namespace) + str(k) + str("wasGeneratedBy") + name_mapping[activity] + name_mapping[entity]
name_mapping[k] = str(uuid.uuid3(uuid.NAMESPACE_DNS, big_string))
#add field to identify subgraph
jsonobj2['wasGeneratedBy'][k]['provdb:projectId'] = namespace#graph_uuid
# change values with new node names
jsonobj2['wasGeneratedBy'][k]['prov:activity'] = name_mapping[activity]
jsonobj2['wasGeneratedBy'][k]['prov:entity'] = name_mapping[entity]
jsonobj2['wasGeneratedBy'].setdefault(name_mapping[k], jsonobj2['wasGeneratedBy'].pop(k))
for k in used:
activity = used[k]['prov:activity']
entity = used[k]['prov:entity']
#big_string = str(namespace) + str("used") + str(jsonobj['used'][k]['prov:activity']) + \
# str(jsonobj['used'][k]['prov:entity'])
big_string = str(namespace) + str(k) + str("used") + name_mapping[activity] + name_mapping[entity]
name_mapping[k] = str(uuid.uuid3(uuid.NAMESPACE_DNS, big_string))
#add field to identify subgraph
jsonobj2['used'][k]['provdb:projectId'] = namespace #graph_uuid
# change values with new node names
jsonobj2['used'][k]['prov:activity'] = name_mapping[activity]
jsonobj2['used'][k]['prov:entity'] = name_mapping[entity]
jsonobj2['used'].setdefault(name_mapping[k], jsonobj2['used'].pop(k))
for k in wasDerivedFrom:
entity_source = wasDerivedFrom[k]['prov:usedEntity']
entity_destination =wasDerivedFrom[k]['prov:generatedEntity']
#big_string = str(namespace) + str("wasDerivedFrom") + str(jsonobj['wasDerivedFrom'][k]['prov:generatedEntity']) + \
# str(jsonobj['wasDerivedFrom'][k]['prov:usedEntity'])
big_string = str(namespace) + str(k) + str("wasDerivedFrom") + name_mapping[entity_source] + name_mapping[entity_destination]
name_mapping[k] = str(uuid.uuid3(uuid.NAMESPACE_DNS, big_string))
#add field to identify subgraph
jsonobj2['wasDerivedFrom'][k]['provdb:projectId'] = namespace #graph_uuid
# change values with new node names
jsonobj2['wasDerivedFrom'][k]['prov:usedEntity'] = name_mapping[entity_source]
jsonobj2['wasDerivedFrom'][k]['prov:generatedEntity'] = name_mapping[entity_destination]
jsonobj2['wasDerivedFrom'].setdefault(name_mapping[k], jsonobj2['wasDerivedFrom'].pop(k))
return namespace,jsonobj2
if __name__ == "__main__":
# For testing
json_data=open('C1-file1.json')
obj = json.load(json_data)
json_data.close()
#xxx=PROV(obj)
obj2 = jsonid_rename(obj)
pprint(obj2)