-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
101 lines (84 loc) · 4.29 KB
/
example.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
import urllib.request
import json
import dml
import prov.model
import datetime
import uuid
class example(dml.Algorithm):
contributor = 'alice_bob'
reads = []
writes = ['alice_bob.lost', 'alice_bob.found']
@staticmethod
def execute(trial = False):
'''Retrieve some data sets (not using the API here for the sake of simplicity).'''
startTime = datetime.datetime.now()
# Set up the database connection.
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('alice_bob', 'alice_bob')
url = 'http://cs-people.bu.edu/lapets/591/examples/lost.json'
response = urllib.request.urlopen(url).read().decode("utf-8")
r = json.loads(response)
s = json.dumps(r, sort_keys=True, indent=2)
repo.dropCollection("lost")
repo.createCollection("lost")
repo['alice_bob.lost'].insert_many(r)
repo['alice_bob.lost'].metadata({'complete':True})
print(repo['alice_bob.lost'].metadata())
url = 'http://cs-people.bu.edu/lapets/591/examples/found.json'
response = urllib.request.urlopen(url).read().decode("utf-8")
r = json.loads(response)
s = json.dumps(r, sort_keys=True, indent=2)
repo.dropCollection("found")
repo.createCollection("found")
repo['alice_bob.found'].insert_many(r)
repo.logout()
endTime = datetime.datetime.now()
return {"start":startTime, "end":endTime}
@staticmethod
def provenance(doc = prov.model.ProvDocument(), startTime = None, endTime = None):
'''
Create the provenance document describing everything happening
in this script. Each run of the script will generate a new
document describing that invocation event.
'''
# Set up the database connection.
client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('alice_bob', 'alice_bob')
doc.add_namespace('alg', 'http://datamechanics.io/algorithm/') # The scripts are in <folder>#<filename> format.
doc.add_namespace('dat', 'http://datamechanics.io/data/') # The data sets are in <user>#<collection> format.
doc.add_namespace('ont', 'http://datamechanics.io/ontology#') # 'Extension', 'DataResource', 'DataSet', 'Retrieval', 'Query', or 'Computation'.
doc.add_namespace('log', 'http://datamechanics.io/log/') # The event log.
doc.add_namespace('bdp', 'https://data.cityofboston.gov/resource/')
this_script = doc.agent('alg:alice_bob#example', {prov.model.PROV_TYPE:prov.model.PROV['SoftwareAgent'], 'ont:Extension':'py'})
resource = doc.entity('bdp:wc8w-nujj', {'prov:label':'311, Service Requests', prov.model.PROV_TYPE:'ont:DataResource', 'ont:Extension':'json'})
get_found = doc.activity('log:uuid'+str(uuid.uuid4()), startTime, endTime)
get_lost = doc.activity('log:uuid'+str(uuid.uuid4()), startTime, endTime)
doc.wasAssociatedWith(get_found, this_script)
doc.wasAssociatedWith(get_lost, this_script)
doc.usage(get_found, resource, startTime, None,
{prov.model.PROV_TYPE:'ont:Retrieval',
'ont:Query':'?type=Animal+Found&$select=type,latitude,longitude,OPEN_DT'
}
)
doc.usage(get_lost, resource, startTime, None,
{prov.model.PROV_TYPE:'ont:Retrieval',
'ont:Query':'?type=Animal+Lost&$select=type,latitude,longitude,OPEN_DT'
}
)
lost = doc.entity('dat:alice_bob#lost', {prov.model.PROV_LABEL:'Animals Lost', prov.model.PROV_TYPE:'ont:DataSet'})
doc.wasAttributedTo(lost, this_script)
doc.wasGeneratedBy(lost, get_lost, endTime)
doc.wasDerivedFrom(lost, resource, get_lost, get_lost, get_lost)
found = doc.entity('dat:alice_bob#found', {prov.model.PROV_LABEL:'Animals Found', prov.model.PROV_TYPE:'ont:DataSet'})
doc.wasAttributedTo(found, this_script)
doc.wasGeneratedBy(found, get_found, endTime)
doc.wasDerivedFrom(found, resource, get_found, get_found, get_found)
repo.logout()
return doc
# example.execute()
# doc = example.provenance()
# print(doc.get_provn())
# print(json.dumps(json.loads(doc.serialize()), indent=4))
## eof