def _neighbours_rdf(self, mimetype): query = ''' SELECT * WHERE { <%(uri)s> ?p ?o . } ''' % { 'uri': self.uri } g = Graph() g.bind('prov', Namespace('http://www.w3.org/ns/prov#')) for r in _database.query(query)['results']['bindings']: if r['o']['type'] == 'literal': g.add((URIRef(self.uri), URIRef(r['p']['value']), Literal(r['o']['value']))) else: g.add((URIRef(self.uri), URIRef(r['p']['value']), URIRef(r['o']['value']))) query2 = ''' SELECT * WHERE { ?s ?p <%(uri)s> . } ''' % { 'uri': self.uri } for r in _database.query(query2)['results']['bindings']: g.add((URIRef(r['s']['value']), URIRef(r['p']['value']), URIRef(self.uri))) return Response( g.serialize(format=LDAPI.get_rdf_parser_for_mimetype(mimetype)), status=200, mimetype=mimetype)
def _prov_rdf(self): query = ''' SELECT * WHERE { <%(uri)s> ?p ?o . } ''' % { 'uri': self.uri } g = Graph() g.bind('prov', Namespace('http://www.w3.org/ns/prov#')) for r in _database.query(query)['results']['bindings']: if r['o']['type'] == 'literal': g.add((URIRef(self.uri), URIRef(r['p']['value']), Literal(r['o']['value']))) else: g.add((URIRef(self.uri), URIRef(r['p']['value']), URIRef(r['o']['value']))) query2 = ''' SELECT * WHERE { ?s ?p <%(uri)s> . } ''' % { 'uri': self.uri } for r in _database.query(query2)['results']['bindings']: g.add((URIRef(r['s']['value']), URIRef(r['p']['value']), URIRef(self.uri))) return g
def _get_details(self): """ Get the details for an ReportingSystem from an RDF triplestore""" query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX proms: <http://promsns.org/def/proms#> PREFIX vcard: <http://www.w3.org/2006/vcard/ns#> SELECT * WHERE { <%(uri)s> a proms:ReportingSystem ; rdfs:label ?label . OPTIONAL { <%(uri)s> prov:actedOnBehalfOf ?aobo . ?aobo rdfs:label ?aobo_label } OPTIONAL { <%(uri)s> proms:validation ?v . } } ''' % { 'uri': self.uri } reportingsystem = _database.query(query) if reportingsystem and 'results' in reportingsystem: if len(reportingsystem['results']['bindings']) > 0: ret = reportingsystem['results']['bindings'][0] self.label = ret['label']['value'] if 'aobo' in ret: self.aobo = ret['aobo']['value'] self.aobo_label = ret['aobo_label']['value'] self.v = ret['v']['value'] if 'v' in ret else None
def _get_used_svg(self): """ Construct SVG code for the prov:used Activities of an Entity """ script = '' query = ''' PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { GRAPH ?g { ?a prov:used <%(uri)s> . ?a rdfs:label ?label . } } ''' % { 'uri': self.uri } entity_result = _database.query(query) if entity_result and 'results' in entity_result: used = entity_result['results']['bindings'] if len(used) == 1: if used[0].get('label'): label = used[0]['label']['value'] else: label = 'uri' uri_encoded = uriparse.quote(used[0]['a']['value']) script = ''' var aLabel = "%(label)s"; var aUri = "%(instance_endpoint)s?_uri=%(uri_encoded)s"; var activityUsed = addActivity(555, 205, aLabel, aUri); addLink(activityUsed, entity, "prov:used", TOP); ''' % { 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } # TODO: Test, no current Entities have multiple prov:used Activities elif len(used) > 1: # TODO: Check query query_encoded = uriparse.quote(query) script += ''' activityUsed1 = addActivity(555, 215, "", ""); activityUsed2 = addActivity(550, 210, "", ""); activityUsedN = addActivity(545, 205, "Multiple Activities, click to search", "%(sparql_endpoint)s?query=%(query_encoded)s"); addLink(activityUsedN, entity, "prov:used", TOP); ''' % { 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } else: # do nothing as no Activities found pass else: # we have a fault script += ''' var activityFault = addActivity(550, 205, "There is a fault with retrieving Activities that may have used this Entity", ""); ''' self.script += script
def get_agent_was_attributed_to_svg(self): """ Construct the SVG code for the prov:wasAttributedTo Entities of an Person """ script = '' query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT DISTINCT ?e ?label WHERE { GRAPH ?g { { ?e a prov:Entity .} UNION { ?e a prov:Plan .} ?e prov:wasAttributedTo <%(agent_uri)s> ; OPTIONAL { ?e rdfs:label ?label . } } } ''' % { 'agent_uri': self.uri } entity_results = _database.query(query) if entity_results and 'results' in entity_results: wat = entity_results['results'] if len(wat['bindings']) > 0: if wat['bindings'][0].get('label'): label = wat['bindings'][0]['label']['value'] else: label = 'uri' uri_encoded = uriparse.quote(wat['bindings'][0]['e']['value']) script += ''' entityLabel = "%(label)s"; entityUri = "%(instance_endpoint)s?_uri=%(uri_encoded)s"; var entityWAT = addEntity(385, 430, entityLabel, entityUri); addLink(entity, entityWAT, "prov:wasAttributedTo", RIGHT); ''' % { 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } elif len(wat['bindings']) > 1: query_encoded = uriparse.quote(query) script += ''' var entityWAT1 = addEntity(395, 440, "", ""); var entityWAT2 = addEntity(390, 435, "", ""); var entityWATN = addEntity(385, 430, "Multiple Entities, click here to search", "%(sparql_endpoint)s?query=%(query_encoded)s"); addLink(agent, entityWATN, "prov:wasAttributedTo", RIGHT); ''' % { 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } else: # do nothing as no Activities found pass else: # we have a fault script += ''' var addEntity(550, 200, "There is a fault with retrieving Activities that may have used this Entity", ""); ''' return script
def _get_details(self): """ Get the details for an Agent from an RDF triplestore""" # formulate the query query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT * WHERE { GRAPH ?g { <%(uri)s> a prov:Agent ; rdfs:label ?label . OPTIONAL { <%(uri)s> prov:actedOnBehalfOf ?aobo . ?aobo rdfs:label ?aobo_label } } } ''' % { 'uri': self.uri } # run the query agent_details = _database.query(query) # extract results into instance vars if 'results' in agent_details: if len(agent_details['results']['bindings']) > 0: ret = agent_details['results']['bindings'][0] self.label = ret['label']['value'] if 'aobo' in ret: self.aobo = ret['aobo']['value'] self.aobo_label = ret['aobo_label']['value']
def _prov_rdf(self): self.g = Graph() # get the URIs of Named Graphs that contain references to this Entity q = ''' SELECT DISTINCT ?g WHERE { GRAPH ?g { {<%(uri)s> ?p ?o .} UNION {?s ?p2 <%(uri)s> .} } } ''' % { 'uri': self.uri } # retrieve all the triple of each Named Graph containing this entity # add them to in-memory graph for row in _database.query(q)['results']['bindings']: q2 = ''' CONSTRUCT { ?s ?p ?o . } WHERE { GRAPH <%(g)s> { ?s ?p ?o . } } ''' % { 'g': str(row['g']['value']) } ng = _database.query_turtle(q2) self.g.parse(data=ng, format='turtle')
def _get_wif_svg(self): """ Construct the SVG code for the prov:wasInformedBy Activities of an Activity """ script = '' query = ''' PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { GRAPH ?g { <%(uri)s> a prov:Activity . <%(uri)s> prov:wasInformedBy ?wif . OPTIONAL { ?wif rdfs:label ?wif_label . } } } ''' % { 'uri': self.uri } activity_results = _database.query(query) if activity_results and 'results' in activity_results: wif = activity_results['results'] if len(wif['bindings']) == 1: if wif['bindings'][0].get('wif_label'): label = wif['bindings'][0]['wif_label']['value'] else: label = 'uri' uri_encoded = uriparse.quote( wif['bindings'][0]['wif']['value']) script += ''' var activityWIB = addActivity(275, 399, "%(label)s", "%(instance_endpoint)s?_uri=%(uri_encoded)s"); addLink(activity, activityWIB, "prov:wasInformedBy", RIGHT); ''' % { 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } # TODO: Check query elif len(wif['bindings']) > 1: query_encoded = uriparse.quote(query) script += ''' var activityWIB1 = addActivity(275, 399, "", ""); var activityWIB2 = addActivity(270, 394, "", "") var activityWIBN = addActivity(2650, 389, "Multiple Activities, click here to search", "%(sparql_endpoint)s?query=%(query_encoded)s"); addLink(activity, activityWIBN, "prov:wasInformedBy", RIGHT); ''' % { 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } else: # do nothing as no Activities found pass else: # we have a fault script += ''' var activityUsedFaultText = addActivity(550, 200, "There is a fault with retrieving Activities that may have used this Entity", ""); ''' self.script += script
def get_agent_was_associated_with_svg(self): """ Construct the SVG code for the prov:wasAssociatedWith Activities of an Agent""" script = '' query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT DISTINCT ?a ?label WHERE { GRAPH ?g { { ?a a prov:Activity . } ?waw prov:wasAssociatedWith <%(uri)s> ; OPTIONAL { ?waw rdfs:label ?waw_label . } } } ''' % { 'uri': self.uri } activity_results = _database.query(query) if activity_results and 'results' in activity_results: waw = activity_results['results'] if len(waw['bindings']) > 0: if waw['bindings'][0].get('waw_label'): waw_label = waw['bindings'][0]['waw_label']['value'] else: waw_label = 'uri' waw_uri_encoded = uriparse.quote( waw['bindings'][0]['waw']['value']) script += ''' activityLabel = "%(waw_label)s"; activityUri = "%(instance_endpoint)s?_uri=%(waw_uri_encoded)s"; var activityWAW = addActivity(5, 200, activityLabel, activityUri); addLink(agent, activityWAW, "prov:wasAssociatedWith", TOP); ''' % { 'waw_label': waw_label, 'instance_endpoint': self.endpoints['instance'], 'waw_uri_encoded': waw_uri_encoded } elif len(waw['bindings']) > 1: query_encoded = uriparse.quote(query) script += ''' var activityWAW1 = addActivity(15, 210, "", ""); var activityWAW2 = addActivity(10, 205, "", ""); var activityWAWN = addActivity(5, 200, "Multiple Activities, click here to search", "%(sparql_endpoint)s?query=%(query_encoded)s'"); addLink(agent, activityWAWN, "prov:wasAssociatedWith", TOP); ''' % { 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } else: # do nothing as no Activities found pass else: # we have a fault script += ''' var activityUsedFaultText = addActivity(5, 200, "There is a fault with retrieving Activities that may be associated with this Person", ""); ''' self.script += script
def _get_wdf_svg(self): """ Get the Entity/Entities this Entity prov:wasDerivedFrom""" script = '' query = ''' PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?e ?label WHERE { GRAPH ?g { <%(uri)s> prov:wasDerivedFrom ?e . ?e rdfs:label ?label . } } ''' % { 'uri': self.uri } entity_results = _database.query(query) if entity_results and 'results' in entity_results: wdf = entity_results['results']['bindings'] if len(wdf) == 1: if wdf[0].get('label'): label = wdf[0]['label']['value'] else: label = 'uri' uri_encoded = uriparse.quote(wdf[0]['e']['value']) script += ''' var entityWDF = addEntity(355, 440, "%(label)s", "%(instance_endpoint)s?_uri=%(uri_encoded)s"); drawLink(entityWDF, entity, "prov:wasDerivedFrom", TOP); ''' % { 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } elif len(wdf) > 1: query_encoded = uriparse.quote(query) script += ''' var entityWDF1 = addEntity(355, 440, "", ""); var entityWDF2 = addEntity(350, 435, "", ""); var entityWDFN = addEntity(345, 430, "Multiple Entities, click here to search", "%(sparql_endpoint)s?query=%(query_encoded)s"); drawLink(entityWDFN, entity, "prov:wasDerivedFrom", TOP); ''' % { 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } else: # do nothing as no Entities found pass else: # we have a fault script += ''' var entityFaultText = addEntity(350, 440, "There is a fault with retrieving Activities that may have used this Entity", ""); ''' self.script += script
def _get_details(self): """ Get the details for an Entity from an RDF triplestore""" # formulate the query query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX proms: <http://promsns.org/def/proms#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT * WHERE { GRAPH ?g { <%(uri)s> rdfs:label ?label . OPTIONAL { ?a_u prov:used <%(uri)s> . } OPTIONAL { <%(uri)s> prov:value ?v . } OPTIONAL { ?a_g prov:generated <%(uri)s> . } OPTIONAL { <%(uri)s> prov:generatedAtTime ?gat . } OPTIONAL { <%(uri)s> prov:wasAttributedTo ?wat . } OPTIONAL { ?wat prov:wasAttributedTo ?wat_label . } } } ''' % { 'uri': self.uri } # run the query entity_details = _database.query(query) # extract results into instance vars if entity_details and 'results' in entity_details: if len(entity_details['results']['bindings']) > 0: ret = entity_details['results']['bindings'][0] self.label = ret['label']['value'] self.gat = ret['gat']['value'] if 'gat' in ret else None self.value = ret['v']['value'] if 'v' in ret else None self.wat = ret['wat']['value'] if 'wat' in ret else None self.wat_label = ret['wat_label'][ 'value'] if 'wat_label' in ret else None self.a_u = ret['a_u']['value'] if 'a_u' in ret else None self.a_g = ret['a_g']['value'] if 'a_g' in ret else None
def _get_wgb_svg(self): """ Get all prov:wasGeneratedBy Activities for an Entity """ script = '' query = ''' PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { GRAPH ?g { {?a prov:generated <%(uri)s> .} UNION {<%(uri)s> prov:wasGeneratedBy ?a.} ?a rdfs:label ?label . } } ''' % { 'uri': self.uri } entity_results = _database.query(query) if entity_results and 'results' in entity_results: wgb = entity_results['results']['bindings'] if len(wgb) == 1: if wgb[0].get('label'): label = wgb[0]['label']['value'] else: label = 'uri' uri_encoded = uriparse.quote(wgb[0]['a']['value']) script += ''' var aLabel = "%(label)s"; var aUri = "%(instance_endpoint)s?_uri=%(uri_encoded)s"; var activityWGB = addActivity(5, 205, aLabel, aUri); addLink(entity, activityWGB, "prov:wasGeneratedBy", TOP); ''' % { 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } else: pass self.script += script
def get_class_register(class_uri): q = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?uri ?label WHERE { GRAPH ?g { ?uri a <%(class_uri)s> ; rdfs:label ?label . } } ORDER BY ?label ''' % {'class_uri': class_uri} instances = [] for r in _database.query(q)['results']['bindings']: instances.append({ 'uri': r['uri']['value'], 'label': r['label']['value'] }) return instances
def get_contents_classes(): query = ''' SELECT DISTINCT ?c WHERE { GRAPH ?g { ?s a ?c . } FILTER(!REGEX(STR(?g), "/pingback/", "i")) . } ORDER BY ?c ''' classes = [] try: for c in _database.query(query)['results']['bindings']: classes.append({ 'uri': c['c']['value'], 'uri_encoded': uriparse.quote_plus(c['c']['value']) }) except ValueError: pass # i.e. no result return classes
def _get_details(self): """ Get the details of an Activity from an RDF triplestore""" # formulate the query query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX proms: <http://promsns.org/def/proms#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT * WHERE { GRAPH ?g { <%(uri)s> rdfs:label ?label ; prov:startedAtTime ?sat ; prov:endedAtTime ?eat . OPTIONAL { <%(uri)s> prov:wasAssociatedWith ?waw . ?waw a prov:Agent . ?waw rdfs:label ?waw_label . } } } ''' % { 'uri': self.uri } # run the query activity_details = _database.query(query) # extract results into instance vars if activity_details and 'results' in activity_details: if len(activity_details['results']['bindings']) > 0: ret = activity_details['results']['bindings'][0] self.label = ret['label']['value'] self.sat = ret['sat']['value'] if 'sat' in ret else None self.eat = ret['eat']['value'] if 'eat' in ret else None if 'waw' in ret: self.waw = ret['waw']['value'] self.waw_encoded = uriparse.quote_plus(self.waw) self.waw_label = ret['waw_label']['value']
def _get_details(self): """ Get the details (label only) for an Class from an RDF triplestore""" # formulate the query query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT * WHERE { GRAPH ?g { <%(uri)s> rdfs:label ?label . } } ''' % { 'uri': self.uri } # run the query class_details = _database.query(query) # extract results into instance vars if 'results' in class_details: if len(class_details['results']['bindings']) > 0: ret = class_details['results']['bindings'][0] self.label = ret['label']['value']
def _get_generated_svg(self): """ Construct the SVG code for the prov:generated Entities of an Activity""" script = '' query = ''' PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { GRAPH ?g { { <%(uri)s> prov:generated ?u . } UNION { ?u prov:wasGeneratedBy <%(uri)s> .} OPTIONAL {?u rdfs:label ?wgb_label .} } } ''' % { 'uri': self.uri } activity_results = _database.query(query) if activity_results and 'results' in activity_results: gen = activity_results['results'] if len(gen['bindings']) > 0: if gen['bindings'][0].get('wgb_label'): wgb_label = gen['bindings'][0]['wgb_label']['value'] else: wgb_label = 'uri' uri_encoded = uriparse.quote(gen['bindings'][0]['u']['value']) script += ''' var entityGen1 = addEntity(605, 250, "%(wgb_label)s", "%(instance_endpoint)s?_uri=%(uri_encoded)s"); addLink(activity, entityGen1, "prov:generated", TOP); ''' % { 'wgb_label': wgb_label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } if len(gen['bindings']) > 1: if gen['bindings'][1].get('wgb_label'): wgb_label = gen['bindings'][1]['wgb_label']['value'] else: wgb_label = 'uri' uri_encoded = uriparse.quote( gen['bindings'][1]['u']['value']) script += ''' var entityGen2 = addEntity(605, 100, "%(wgb_label)s", "%(inst_endpoint)s?_uri=%(uri_encoded)s"); addLink(activity, entityGen2, "prov:generated", TOP); ''' % { 'wgb_label': wgb_label, 'inst_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } if len(gen['bindings']) == 3: if gen['bindings'][2].get('wgb_label'): wgb_label = gen['bindings'][2]['wgb_label'][ 'value'] else: wgb_label = 'uri' uri_encoded = uriparse.quote( gen['bindings'][2]['u']['value']) script += ''' var entityGen3 = addEntity(605, 400, "%(wgb_label)s", "%(inst_end)s?_uri=%(uri_encoded)s"); addLink(activity, entityGen3, "prov:generated", TOP); ''' % { 'wgb_label': wgb_label, 'inst_end': self.endpoints['instance'], 'uri_encoded': uri_encoded } elif len(gen['bindings']) > 3: # TODO: Check query query_encoded = uriparse.quote(query) script = '' # reset script script += ''' var entityGen1 = addEntity(615, 260, "", ""); var entityGen2 = addEntity(610, 255, "", ""); var entityGenN = addEntity(605, 250, "Multiple Entities, click here to search", ''' \ '''%(sparql_endpoint)s?query=%(query_encoded)s"); addLink(activity, entityGenN, "prov:generated", TOP); ''' % {'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded} else: # zero pass else: # we have a fault script += ''' var entityGenFaultText = addEntity(349, 200, '''\ '''"There is a fault with retrieving Entities that may have been used by this Activity", ""); ''' self.script += script
def _get_details(self): """ Get the details for a Report from an RDF triplestore""" # formulate the query query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX proms: <http://promsns.org/def/proms#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT * WHERE { GRAPH ?g { <%(uri)s> a ?rt ; rdfs:label ?label ; proms:nativeId ?nid ; prov:generatedAtTime ?gat ; proms:wasReportedBy ?rs . OPTIONAL { ?rs rdfs:label ?rs_label . } OPTIONAL { <%(uri)s> proms:startingActivity ?sa . ?sa rdfs:label ?sa_label . } OPTIONAL { <%(uri)s> proms:endingActivity ?ea . ?ea rdfs:label ?ea_label . } . } } ''' % { 'uri': self.uri } # run the query report_details = _database.query(query) # extract results into instance vars if report_details and 'results' in report_details: if len(report_details['results']['bindings']) > 0: ret = report_details['results']['bindings'][0] self.rt = ret['rt']['value'] if 'Basic' in self.rt: self.rt_label = 'Basic' elif 'Internal' in self.rt: self.rt_label = 'Internal' elif 'External' in self.rt: self.rt_label = 'External' self.label = ret['label']['value'] self.nid = ret['nid']['value'] self.gat = ret['gat']['value'] self.rs = ret['rs']['value'] self.rs_encoded = uriparse.quote_plus(self.rs) self.rs_label = ret['rs_label'][ 'value'] if 'rs_label' in ret else self.rs if 'sa' in ret: self.sa = ret['sa']['value'] self.sa_label = ret['sa_label']['value'] if 'ea' in ret: self.ea = ret['ea']['value'] self.ea_label = ret['ea_label']['value']
def _get_reports_svg(self): """ Construct SVG code for all Reports contained in a ReportingSystem""" query = ''' PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX proms: <http://promsns.org/def/proms#> SELECT * WHERE { GRAPH ?g { { ?r a proms:BasicReport } UNION { ?r a proms:ExternalReport } UNION { ?r a proms:InternalReport } ?r proms:wasReportedBy <%(uri)s> . ?r prov:generatedAtTime ?gat . ?r rdfs:label ?label . ?r proms:nativeId ?nid . } } ORDER BY DESC(?gat) ''' % { 'uri': self.uri } reports_results = _database.query(query) if reports_results and 'results' in reports_results: reports = reports_results['results'] if len(reports['bindings']) > 0: label = reports['bindings'][0]['label']['value'] uri_encoded = uriparse.quote( reports['bindings'][0]['r']['value']) nid = reports['bindings'][0]['nid']['value'] y_top = 5 x_pos = 350 script = ''' var reports = []; var report0 = addReport(%(x_pos)s, %(y_top)s, "%(label)s", "%(instance_endpoint)s?_uri=%(uri_encoded)s", "%(nid)s"); reports.push(report0); ''' % { 'x_pos': str(x_pos), 'y_top': str(y_top), 'nid': nid, 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded, } if len(reports['bindings']) > 1: reports = reports['bindings'][1:] y_gap = 15 report_height = 100 i = 1 for report in reports: y_offset = y_top + (i * report_height) + (i * y_gap) if i == 3: query_encoded = uriparse.quote(query) script += ''' var report = addReport(%(x_pos)s, %(y_offset)s, "Multiple Reports, click to search", "%(sparql_endpoint)s?query=%(query_encoded)s"); reports.push(report); ''' % { 'x_pos': str(x_pos), 'y_offset': str(y_offset), 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } break uri = report['r']['value'] uri_encoded = uriparse.quote(uri) label = report['label']['value'] nid = report['nid']['value'] script += ''' var report = addReport(%(x_pos)s, %(y_offset)s, "%(label)s", "%(instance_endpoint)s?_uri=%(uri_encoded)s", "%(nid)s"); reports.push(report); ''' % { 'x_pos': str(x_pos), 'y_offset': str(y_offset), 'nid': nid, 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded, } i += 1 script += ''' addConnectedLinks(reportingSystem, reports, "proms:reportingSystem"); ''' else: # no reports script = '' else: # we have a fault script = ''' //var reportUsedFaultText = addReport(550, 200, "There is a fault with retrieving Reports that may have used this ReportingSystem", ""); var reportUsedFaultText = addReport(550, 0, "No Reports for this RS", ""); ''' self.script += script
def _get_used_svg(self): """ Construct the SVG code for the prov:used Entities of an Activity""" script = '' query = ''' PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { GRAPH ?g { <%(uri)s> prov:used ?u . OPTIONAL {?u rdfs:label ?label .} } } ''' % { 'uri': self.uri } activity_results = _database.query(query) if activity_results and 'results' in activity_results: used = activity_results['results'] if len(used['bindings']) > 0: if used['bindings'][0].get('label'): label = used['bindings'][0]['label']['value'] else: label = 'uri' uri_encoded = uriparse.quote(used['bindings'][0]['u']['value']) script += ''' var entityUsed1 = addEntity(105, 250, "%(label)s", "%(instance_endpoint)s?_uri=%(uri_encoded)s"); addLink(activity, entityUsed1, "prov:used", TOP); ''' % { 'label': label, 'instance_endpoint': self.endpoints['instance'], 'uri_encoded': uri_encoded } if len(used['bindings']) > 1: if used['bindings'][1].get('u_label'): u_label = used['bindings'][1]['u_label']['value'] else: u_label = 'uri' uri_encoded = uriparse.quote( used['bindings'][1]['u']['value']) script += ''' var entityUsed2 = addEntity(105, 100, "%(u_label)s", "%(inst_end)s?_uri=%(uri_encoded)s"); addLink(activity, entityUsed2, "prov:used", TOP); ''' % { 'u_label': u_label, 'inst_end': self.endpoints['instance'], 'uri_encoded': uri_encoded } if len(used['bindings']) == 3: if used['bindings'][2].get('u_label'): u_label = used['bindings'][2]['u_label']['value'] else: u_label = 'uri' uri_encoded = uriparse.quote( used['bindings'][2]['u']['value']) script += ''' var entityUsed3 = addEntity(105, 400, "%(u_label)s", "%(inst_end)s?_uri=%(uri_encoded)s"); addLink(activity, entityUsed3, "prov:used", RIGHT); ''' % { 'u_label': u_label, 'inst_end': self.endpoints['instance'], 'uri_encoded': uri_encoded } elif len(used['bindings']) > 3: query_encoded = uriparse.quote(query) script = '' # reset script script += ''' var entityUsed1 = addEntity(105, 260, "", ""); var entityUsed2 = addEntity(100, 255, "", ""); var entityUsedN = addEntity(95, 250, "Multiple Entities, click to search", ''' \ '''"%(sparql_endpoint)s?query=%(query_encoded)s"); addLink(activity, entityUsedN, "prov:used", TOP); ''' % { 'sparql_endpoint': self.endpoints['sparql'], 'query_encoded': query_encoded } else: # do nothing as no Agents found pass else: # we have a fault script += 'var entityUsedFaultText = addEntity(1, 200, ' \ '"There is a fault with retrieving Entities that may have been used by this Activity", "");' self.script += script