def test_merging_records_json(self): test_json = """ { "entity": { "e1": [ {"prov:label": "First instance of e1"}, {"prov:label": "Second instance of e1"} ] }, "activity": { "a1": [ {"prov:label": "An activity with no time (yet)"}, {"prov:startTime": "2011-11-16T16:05:00"}, {"prov:endTime": "2011-11-16T16:06:00"} ] } }""" g = ProvBundle.from_provjson(test_json) e1 = g.get_record("e1") self.assertEqual( len(e1.get_attribute("prov:label")), 2, "e1 was not merged correctly, expecting two prov:label attributes" ) a1 = g.get_record("a1") self.assertIsNotNone(a1.get_startTime(), "a1 was not merged correctly, expecting startTime set.") self.assertIsNotNone(a1.get_endTime(), "a1 was not merged correctly, expecting startTime set.") self.assertEqual( len(a1.get_attribute("prov:label")), 1, "a1 was not merged correctly, expecting one prov:label attribute" )
def create(request): if request.method == 'POST': if 'convert' in request.POST or 'randomize' in request.POST: graphml_str = request.POST["graphml"] filename = request.POST["filename"] provn_str, output = convert_graphml_string(graphml_str) output_list = output.splitlines() output_list.sort() valid, validation_url = validate(provn_str) if valid: json_str = provn_str.get_provjson() else: json_str={} if 'randomize' in request.POST: json_str = json.dumps(get_random_graph(json.loads(json_str))) return render_to_response('provmanager/create.html', {"output_list":output_list, "filename":filename, "graphml_str": graphml_str, "json_str": json_str, "valid":valid, "validation_url":validation_url}, context_instance=RequestContext(request)) elif 'save' in request.POST: graphml_str = request.POST["graphml"] provn_str, output = convert_graphml_string(graphml_str) valid, validation_url = validate(provn_str) if valid: name = request.POST["filename"] if name=="": name = "PLEASE ENTER A NAME" inconsistencies_list, clean_graph = get_inconsistencies(json.loads(provn_str.get_provjson())) if inconsistencies_list: attribute1 = json.dumps(inconsistencies_list[0]) attribute2 = json.dumps(inconsistencies_list[1]) type = Provenance.TYPE_CRON else: attribute1 = None attribute2 = None #TODO: is there a better way to check the type? #if there are no inconsistencies, then we assume it is a MOP_TEMPLATE provenance document type = Provenance.TYPE_MOP_TEMPLATE bundle = ProvBundle.from_provjson(json.dumps(clean_graph)) store_id = API.submit_document(bundle, name, public=False) provenance = Provenance(name=name, store_id=store_id, attribute1=attribute1, attribute2=attribute2, type=type) provenance.save() return HttpResponseRedirect(reverse('provmanager_index')) return render_to_response('provmanager/create.html', context_instance=RequestContext(request))
def testAllExamples(self): num_graphs = len(examples.tests) logger.info('Testing %d provenance graphs' % num_graphs) counter = 0 for name, graph in examples.tests: counter += 1 logger.info('%d. Testing the %s example' % (counter, name)) g1 = graph() logger.debug('Original graph in PROV-N\n%s' % g1.get_provn()) json_str = g1.get_provjson(indent=4) logger.debug('Original graph in PROV-JSON\n%s' % json_str) g2 = ProvBundle.from_provjson(json_str) logger.debug('Graph decoded from PROV-JSON\n%s' % g2.get_provn()) self.assertEqual(g1, g2, 'Round-trip JSON encoding/decoding failed: %s.' % name)
def testLoadAllJSON(self): json_path = os.path.dirname(os.path.abspath(__file__)) + '/json/' filenames = os.listdir(json_path) fails = [] for filename in filenames: if filename.endswith('.json'): with open(json_path + filename) as json_file: try: g1 = json.load(json_file, cls=ProvBundle.JSONDecoder) json_str = g1.get_provjson(indent=4) g2 = ProvBundle.from_provjson(json_str) self.assertEqual(g1, g2, 'Round-trip JSON encoding/decoding failed: %s.' % filename) except: fails.append(filename) self.assertFalse(fails, 'Failed to load %d JSON files (%s)' % (len(fails), ', '.join(fails)))
def test_datetime_with_tz(self): """ test that timezone is taken in to account while parsing json""" test_json = """ { "activity": { "a1": [ {"prov:label": "An activity with timezone"}, {"prov:startTime": "2011-11-16T16:05:00.123456+03:00"}, {"prov:endTime": "2011-11-16T16:06:00.654321"} ] } }""" g = ProvBundle.from_provjson(test_json) a1 = g.get_record("a1") self.assertEqual( a1.get_startTime().isoformat(), "2011-11-16T16:05:00.123456+03:00", "timezone is not set correctly" ) self.assertEqual(a1.get_endTime().isoformat(), "2011-11-16T16:06:00.654321", "timezone is not set correctly")
def randomize_document(mopDocument): #bundle = API.get_document(task.provenance.store_id) #TODO randomize provn/json json_graph = getProvJson(mopDocument.provenance) random_graph = get_random_graph(json_graph) inconsistencies_list, clean_graph = get_inconsistencies(random_graph) attribute1 = json.dumps(inconsistencies_list[0]) attribute2 = json.dumps(inconsistencies_list[1]) bundle = ProvBundle.from_provjson(json.dumps(clean_graph)) name = "%s (randomized)" % (mopDocument.provenance.name) store_id = API.submit_document(bundle, name, public=False) provenance = Provenance(name=name, store_id=store_id, attribute1=attribute1, attribute2=attribute2, type=Provenance.TYPE_MOP_INSTANCE) provenance.save() randomizedDocument = RandomizedDocument.objects.create(mopDocument=mopDocument, provenance=provenance) return randomizedDocument
def create(request): if request.method == 'POST': if 'convertMop' in request.POST or 'convertCron' in request.POST or 'randomize' in request.POST: if 'convertCron' in request.POST: isCron = True isMop = False else: isCron = False isMop = True #graphml_str = request.POST["graphml"] filename, graphml_str = getStuff(request) provn_str, output = convert_graphml_string(graphml_str) output_list = output.splitlines() output_list.sort() valid = True validation_url = "bla" #valid, validation_url = validate(provn_str) if valid: json_str = provn_str.get_provjson() else: json_str={} #print json_str inconsistencies_list = None spoilers = False if 'randomize' in request.POST: print request.POST random_graph = get_random_graph(json.loads(json_str)) json_str = json.dumps(random_graph) inconsistencies_list, clean_graph = get_inconsistencies(random_graph) if 'spoilers' in request.POST: spoilers = True else: spoilers = False json_str = json.dumps(clean_graph) print spoilers return render(request, 'provmanager/create.html', {"inconsistencies_list":inconsistencies_list, "spoilers":spoilers, "output_list":output_list, "filename":filename, "json_str": json_str, "isMop":isMop, "isCron":isCron, "valid":valid, "validation_url":validation_url}) elif 'saveMop' in request.POST or 'saveCron' in request.POST: if 'saveCron' in request.POST: type = Provenance.TYPE_CRON else: type = Provenance.TYPE_MOP_TEMPLATE filename, graphml_str = getStuff(request) provn_str, output = convert_graphml_string(graphml_str) valid = True validation_url = "bla" #valid, validation_url = validate(provn_str) if valid: name = request.POST["filename"] if name=="": name = "PLEASE ENTER A NAME" inconsistencies_list, clean_graph = get_inconsistencies(json.loads(provn_str.get_provjson())) if inconsistencies_list: attribute1 = json.dumps(inconsistencies_list[0]) attribute2 = json.dumps(inconsistencies_list[1]) else: attribute1 = None attribute2 = None bundle = ProvBundle.from_provjson(json.dumps(clean_graph)) store_id = API.submit_document(bundle, name, public=False) provenance = Provenance(name=name, store_id=store_id, attribute1=attribute1, attribute2=attribute2, type=type) provenance.save() return HttpResponseRedirect(reverse('provmanager_index')) try: filename = request.session['filename'] except: filename = "" return render(request, 'provmanager/create.html', {"filename":filename})