def by_function(): q = """ SELECT DISTINCT ?function ?name WHERE { ?function a cra:Function . ?function rdfs:label ?name } """ for function, name in cursor.execute(q, initNs=initNs, soft_limit=-1): print name q = """ SELECT DISTINCT ?year, ?amount WHERE { ?area cra:function %s . ?exp cra:area ?area . ?exp a cra:Expenditure . ?exp dc:date ?year . ?exp rdf:value ?amount } """ % function.n3() amounts = {} for year, amount in cursor.execute(q, initNs=initNs, soft_limit=-1): amounts[year] = amounts.setdefault(year, 0.0) + float(amount) g = Graph(identifier=str(function)) for year in amounts: exp = URIRef("%s#%s" % (function, year)) g.add((exp, RDF.type, CRA["Expenditure"])) g.add((exp, DC["date"], Literal(year))) g.add((exp, RDF.value, Literal(amount, datatype=XSD["float"]))) g.add( (exp, RDFS.label, Literal("%s Expenditure %s" % (name, year)))) g.add((exp, CRA["function"], function)) cursor.add_model(g)
def read_project(request, project_uri): """Returns a HttpResponse of the cached project metadata graph""" project_uri = URIRef(project_uri) if request.user.is_authenticated(): if permissions.has_permission_over(project_uri, user=request.user, permission=NS.perm.mayRead): identifier = uris.uri('semantic_store_projects', uri=project_uri) store_metadata_graph = get_project_metadata_graph(project_uri) ret_graph = Graph() ret_graph += store_metadata_graph add_is_described_bys(request, project_uri, ret_graph) for permission in ProjectPermission.objects.filter(identifier=project_uri): user = permission.user user_uri = uris.uri('semantic_store_users', username=user.username) perm_uri = permissions.PERMISSION_URIS_BY_MODEL_VALUE[permission.permission] ret_graph += user_metadata_graph(user=user) ret_graph.add((user_uri, NS.perm.hasPermissionOver, project_uri)) ret_graph.add((user_uri, perm_uri, project_uri)) if len(ret_graph) > 0: return NegotiatedGraphResponse(request, ret_graph) else: return HttpResponseNotFound() else: return HttpResponseForbidden('User "%s" does not have read permissions over project "%s"' % (request.user.username, project_uri)) else: return HttpResponse(status=401)
def testConjunction(self): self.addStuffInMultipleContexts() triple = (self.pizza, self.likes, self.pizza) # add to context 1 graph = Graph(self.graph.store, self.c1) graph.add(triple) self.assertEquals(len(self.graph), len(graph))
def __init__(self, graph_storage, cc_storage, file_storage, lcdURL, lcdPort): self.storage = graph_storage self.cc_storage = cc_storage self.file_storage = file_storage self.lcdURL = lcdURL self.lcdPort = lcdPort # TODO: get this from properties self.cc_inputFolder = 'data/corpus-composition/' inputFolder = self.cc_inputFolder # load corpus compositions for infile in os.listdir(inputFolder): main_logger.info('Adding ' + infile + ' to the corpus composition graph') self.cc_storage.add_file(inputFolder + infile, "turtle", None) # calculate gcc triples main_logger.info("Number of triples in GCC:" + str(self.cc_storage.count_triples())) # process corpus mapping - this should come from LCD with open('data/corpus-mapping.json') as data_file: data = json.load(data_file) g = Graph() for abbr in data: uri = data[abbr] corpusTypeURI = URIRef( 'http://h224.it.helsinki.fi:8080/varieng/types/Corpus') g.add((URIRef(uri), RDF.type, corpusTypeURI)) g.add((URIRef(uri), RDFS.label, Literal(abbr))) self.storage.add_graph(g, 'glcd') self.storage.export('glcd')
def reduce(mode='object'): # input comes from stdin previous_key = None graph = Graph() # load a triples with same _key into a model for line in sys.stdin: line = line.strip() current_key, triple = line.split('\t', 1) # all lines having same key have # been completely processed if current_key != previous_key and previous_key != None: if mode == 'object': #print('gggg', graph.serialize(format='nt')) replaceObjectUri(graph) elif mode == 'subject': replaceSubjectUri(graph) # clear graph graph = Graph() # load triple triple into temp graph # for easier processing tmp_graph = Graph() tmp_graph.parse(data=triple, format="nt") # get triple from temporary graph for sub, pred, obj in tmp_graph: # add to current graph graph.add((sub, pred, obj)) previous_key = current_key if mode == 'object': #print('gggg', graph) replaceObjectUri(graph) elif mode == 'subject': replaceSubjectUri(graph)
def harvest_collection(col_url, col_uri, store_host, manifest_file=None): store_host = clean_store_host(store_host) with transaction.commit_on_success(): col_g = Graph(store=rdfstore(), identifier=URIRef(col_uri)) collection.fetch_and_parse(col_url, col_g, manifest_file=manifest_file) localize_describes(store_host, col_uri, col_url, col_g) res_uris_urls = collection.aggregated_uris_urls(col_uri, col_g) for res_uri, res_url in res_uris_urls: res_g = Graph(store=rdfstore(), identifier=URIRef(res_uri)) collection.fetch_and_parse(res_url, res_g) for pred in col_res_attributes: for t in res_g.triples((res_uri, pred, None)): col_g.add(t) aggr_uris_urls = collection.aggregated_uris_urls(res_uri, res_g) for aggr_uri, aggr_url in aggr_uris_urls: if aggr_url: collection.fetch_and_parse(aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, col_g) seq_uris_urls = collection.aggregated_seq_uris_urls(res_uri, res_g) for seq_uri, seq_url in seq_uris_urls: page_uris_urls = collection.aggregated_uris_urls( seq_uri, res_g) for page_uri, page_url in page_uris_urls: localize_describes(store_host, page_uri, page_url, res_g) localize_describes(store_host, res_uri, res_url, res_g) localize_describes(store_host, res_uri, res_url, col_g)
def add_to_collection(self, name, uri): ''' Add a URI has a member of a collection @param name: the name of the collection @param uri: the uri of the resource to add as a member ''' # Get the URI for this collection collection_uri = self.get_uri(name) # Create and populate the description of the membership graph = Graph() graph.add((URIRef(uri), DCTERMS.isPartOf, URIRef(collection_uri))) data = graph.serialize(format="nt").decode() # Prepare the query query = """ INSERT DATA { __PAYLOAD__ } """.replace("__PAYLOAD__", data) sparql = SPARQLWrapper(self.sparul) sparql.setQuery(query) sparql.setMethod(POST) # Execute it sparql.query()
def harvest_collection(col_url, col_uri, store_host, manifest_file=None): store_host = clean_store_host(store_host) with transaction.commit_on_success(): col_g = Graph(store=rdfstore(), identifier=URIRef(col_uri)) collection.fetch_and_parse(col_url, col_g, manifest_file=manifest_file) localize_describes(store_host, col_uri, col_url, col_g) res_uris_urls = collection.aggregated_uris_urls(col_uri, col_g) for res_uri, res_url in res_uris_urls: res_g = Graph(store=rdfstore(), identifier=URIRef(res_uri)) collection.fetch_and_parse(res_url, res_g) for pred in col_res_attributes: for t in res_g.triples((res_uri, pred, None)): col_g.add(t) aggr_uris_urls = collection.aggregated_uris_urls(res_uri, res_g) for aggr_uri, aggr_url in aggr_uris_urls: if aggr_url: collection.fetch_and_parse(aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, col_g) seq_uris_urls = collection.aggregated_seq_uris_urls(res_uri, res_g) for seq_uri, seq_url in seq_uris_urls: page_uris_urls = collection.aggregated_uris_urls(seq_uri, res_g) for page_uri, page_url in page_uris_urls: localize_describes(store_host, page_uri, page_url, res_g) localize_describes(store_host, res_uri, res_url, res_g) localize_describes(store_host, res_uri, res_url, col_g)
def test_n3(self): g = Graph() g.add((URIRef("http://example.com/foo"), URIRef("http://example.com/bar"), URIRef("http://example.com/baz"))) n3 = g.serialize(format="n3") self.assertTrue("<http://example.com/foo> ns1:bar <http://example.com/baz> ." in n3)
def jsondict2graph(json_dict): g = Graph() [g.bind(*x) for x in ns_store.items()] for triple in json_dict['results']['bindings']: ts = triple['s'].get('type',None) vs = triple['s']['value'] if ts == 'uri': s = URIRef(vs) elif ts == 'literal': s = Literal(vs) elif ts == 'bnode': s = BNode(vs) #logging.debug(s) p = URIRef(triple['p']['value']) #logging.debug(p) to = triple['o'].get('type',None) vo = triple['o']['value'] dto = triple['o'].get('datatype',None) if to == 'uri': o = URIRef(triple['o']['value']) elif to == 'literal': o = Literal(triple['o']['value']) if dto: o.datatype = URIRef(dto) elif ts == 'bnode': o = BNode(vo) #logging.debug(o) g.add((s,p,o)) logging.debug(g.serialize(format='turtle')) return g
def delete_triples_from_project(request, uri): """Deletes the triples in a graph provided by a request object from the project graph. Returns an HttpResponse of all the triples which were successfully removed from the graph.""" if request.user.is_authenticated(): if permissions.has_permission_over(uri, user=request.user, permission=NS.perm.mayUpdate): removed = Graph() bind_namespaces(removed) try: g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) project_g = get_project_graph(uri) project_metadata_g = get_project_metadata_graph(uri) for t in g: if t in project_g: project_g.remove(t) removed.add(t) project_metadata_g.remove(t) return NegotiatedGraphResponse(request, removed) else: return HttpResponseForbidden('User "%s" does not have update permissions over project "%s"' % (request.user.username, uri)) else: return HttpResponse(status=401)
def transformResources(self, graphURI, sourceProperty, targetType): #Expressions query = """ SELECT ?ds ?prop ?obs ?label { ?obs qb:dataSet ?ds . ?obs %s ?prop . ?prop rdfs:label ?label }""" % (sourceProperty) #graph = g.get_context(graphURI) qres = self.dm.query(query, [graphURI, 'glcd']) self.dm.export(graphURI) ggen = Graph() for row in qres: obs = row.obs dsURI = row.ds prop = row.prop #main_logger.info(prop); label = row.label try: propURI = URIRef(prop) ggen.add( (obs, ns_lada[targetType], propURI )) ggen.add( (propURI, RDFS.label, label) ) except KeyError: main_logger.error("Could not add " + targetType + " URI for value " + prop) ggen.serialize('gen-cor.ttl', format='turtle') self.dm.add_graph(ggen, 'ggen')
def _sparql_construct(self, q, cursor): log.debug("_sparql_construct") g = Graph() results = cursor.execute(q) for result in results: g.add(resolve(cursor, x) for x in result) return g
def transformPeriods(self, graphURI, targetType): #Expressions query = """ SELECT ?ds ?obs ?startYear ?endYear { ?obs qb:dataSet ?ds . ?obs <http://h224.it.helsinki.fi:8080/varieng/data/startYear> ?startYear . ?obs <http://h224.it.helsinki.fi:8080/varieng/data/endYear> ?endYear . }""" qres = self.dm.query(query, graphURI) ggen = Graph() for row in qres: obs = row.obs dsURI = row.ds startYear = row.startYear endYear = row.endYear try: propURI = URIRef(dsURI + '/' + targetType + '/' + str(startYear) + '-' + str(endYear)) ggen.add( (obs, ns_lada[targetType], propURI) ) ggen.add( (propURI, RDFS.label, Literal(str(startYear) + '-' + str(endYear))) ) except KeyError: main_logger.error("Could not add " + targetType + " URI for value " + prop) #ggen.serialize("ggen-period.ttl", format="turtle") self.dm.add_graph(ggen, 'ggen')
def delete_triples_from_project(request, uri): """Deletes the triples in a graph provided by a request object from the project graph. Returns an HttpResponse of all the triples which were successfully removed from the graph.""" if request.user.is_authenticated(): if permissions.has_permission_over(uri, user=request.user, permission=NS.perm.mayUpdate): removed = Graph() bind_namespaces(removed) try: logger.debug('!!!!!!!!!!!!!!! project_texts.py - delete_triples_from_project') g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) project_g = get_project_graph(uri) project_metadata_g = get_project_metadata_graph(uri) for t in g: if t in project_g: project_g.remove(t) removed.add(t) project_metadata_g.remove(t) return NegotiatedGraphResponse(request, removed) else: return HttpResponseForbidden('User "%s" does not have update permissions over project "%s"' % (request.user.username, uri)) else: return HttpResponse(status=401)
def test_16_bnode(self): g = Graph(store, identifier=TEST_GRAPH) b = BNode() g.add((b, RDF.type, RDFS.Resource)) # get a new graph just to be sure g = Graph(store, identifier=TEST_GRAPH) assert (b, RDF.type, RDFS.Resource) in g
def _sparql_construct(self, q, cursor): log.debug("_sparql_construct") g = Graph() results = cursor.execute(q.encode("utf-8")) for result in results: g.add(resolve(cursor, x) for x in result) return g
def on_post(self, req, resp): main_logger.info("POST /infer") #self.dm.clear('ginf') queryStr = """ CONSTRUCT { ?s1 ?p1 ?o1 } WHERE { ?o1 <http://www.w3.org/2002/07/owl#sameAs> ?o2 . ?s1 ?p1 ?o2 . ?o2 ?p2 ?o3 . } """ self.dm.export('gmap') self.dm.export('ggroup') qres = self.dm.query(queryStr, ['gmap', 'ggen', 'ggroup']) ginf = Graph() for row in qres: #main_logger.info(row) pred = str(row[1]) #main_logger.info(pred) parts = pred.split('gen_') if "gen_" in pred: ginf.add((row[0], URIRef(parts[0] + parts[1]), row[2])) else: ginf.add(row) ginf.serialize("ginf.ttl", format="turtle") self.dm.add_graph(ginf, 'ginf') resp.status = falcon.HTTP_200 resp.content_type = 'application/json' resp.data = json.dumps({"status": "OK"})
def testGraphAdd(self): g1=Graph() g2=Graph() tarek = self.tarek michel = self.michel bob = self.bob likes = self.likes hates = self.hates pizza = self.pizza cheese = self.cheese g1.add((tarek, likes, pizza)) g2.add((bob, likes, cheese)) g3=g1+g2 self.assertEquals(len(g3), 2) self.assertEquals((tarek, likes, pizza) in g3, True) self.assertEquals((tarek, likes, cheese) in g3, False) self.assertEquals((bob, likes, cheese) in g3, True) g1+=g2 self.assertEquals(len(g1), 2) self.assertEquals((tarek, likes, pizza) in g1, True) self.assertEquals((tarek, likes, cheese) in g1, False) self.assertEquals((bob, likes, cheese) in g1, True)
class UniversalRestrictionTest(unittest.TestCase): def setUp(self): self.ontGraph = Graph() self.ontGraph.bind('ex', EX_NS) self.ontGraph.bind('owl', OWL_NS) Individual.factoryGraph = self.ontGraph def testNegatedDisjunctionTest(self): contains=Property(EX_NS.contains) omega = EX.Omega alpha = EX.Alpha innerDisjunct = omega | alpha foo = EX.foo testClass1 = foo & (contains|only|~innerDisjunct) testClass1.identifier = EX_NS.Bar self.assertEqual(repr(testClass1), "ex:foo that ( ex:contains only ( not ( ex:Omega or ex:Alpha ) ) )") NormalFormReduction(self.ontGraph) self.assertEqual(repr(testClass1), "ex:foo that ( not ( ex:contains some ( ex:Omega or ex:Alpha ) ) )") individual1 = BNode() individual2 = BNode() foo.extent = [individual1] contains.extent = [(individual1,individual2)] (EX.Baz).extent = [individual2] ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True) posRules,ignored=CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Bar]) self.failUnless(not posRules,"There should be no rules in the 0 strata!") self.assertEqual(len(ignored),2,"There should be 2 'negative' rules") testClass1.graph = network.inferredFacts self.failUnless(individual1 in testClass1.extent, "%s should be in ex:Bar's extent"%individual1) def testNominalPartition(self): partition = EnumeratedClass(EX_NS.part, members=[EX_NS.individual1, EX_NS.individual2, EX_NS.individual3]) subPartition = EnumeratedClass(members=[EX_NS.individual1]) partitionProp = Property(EX_NS.propFoo, range=partition.identifier) self.testClass = (EX.Bar) & (partitionProp|only|subPartition) self.testClass.identifier = EX_NS.Foo self.assertEqual(repr(self.testClass), "ex:Bar that ( ex:propFoo only { ex:individual1 } )") self.assertEqual(repr(self.testClass.identifier), "rdflib.term.URIRef('http://example.com/Foo')") NormalFormReduction(self.ontGraph) self.assertEqual(repr(self.testClass), "ex:Bar that ( not ( ex:propFoo value ex:individual2 ) ) and ( not ( ex:propFoo value ex:individual3 ) )") ruleStore,ruleGraph,network=SetupRuleStore(makeNetwork=True) ex = BNode() (EX.Bar).extent = [ex] self.ontGraph.add((ex,EX_NS.propFoo,EX_NS.individual1)) CalculateStratifiedModel(network,self.ontGraph,[EX_NS.Foo]) self.failUnless((ex,RDF.type,EX_NS.Foo) in network.inferredFacts, "Missing level 1 predicate (ex:Foo)")
def by_department(): q = """ SELECT DISTINCT ?dept ?name WHERE { ?dept a cra:Department . ?dept rdfs:label ?name } """ for dept, name in cursor.execute(q, initNs=initNs, soft_limit=-1): print name q = """ SELECT DISTINCT ?year, ?amount WHERE { ?area cra:department %s . ?exp cra:area ?area . ?exp a cra:Expenditure . ?exp dc:date ?year . ?exp rdf:value ?amount } """ % dept.n3() amounts = {} for year, amount in cursor.execute(q, initNs=initNs, soft_limit=-1): amounts[year] = amounts.setdefault(year, 0.0) + float(amount) g = Graph(identifier=str(dept)) for year in amounts: exp = URIRef("%s#%s" % (dept, year)) g.add((exp, RDF.type, CRA["Expenditure"])) g.add((exp, DC["date"], Literal(year))) g.add((exp, RDF.value, Literal(amounts[year], datatype=XSD["float"]))) g.add( (exp, RDFS.label, Literal("%s Expenditure %s" % (name, year)))) g.add((exp, CRA["department"], dept)) cursor.add_model(g)
def createTestOntGraph(): graph = Graph() graph.bind('ex',EX_NS,True) Individual.factoryGraph = graph kneeJoint = EX_CL.KneeJoint joint = EX_CL.Joint knee = EX_CL.Knee isPartOf = Property(EX_NS.isPartOf) graph.add((isPartOf.identifier,RDF.type,OWL_NS.TransitiveProperty)) structure = EX_CL.Structure leg = EX_CL.Leg hasLocation = Property(EX_NS.hasLocation,subPropertyOf=[isPartOf]) # graph.add((hasLocation.identifier,RDFS.subPropertyOf,isPartOf.identifier)) kneeJoint.equivalentClass = [joint & (isPartOf|some|knee)] legStructure = EX_CL.LegStructure legStructure.equivalentClass = [structure & (isPartOf|some|leg)] structure += leg structure += joint locatedInLeg = hasLocation|some|leg locatedInLeg += knee # print graph.serialize(format='n3') # newGraph = Graph() # newGraph.bind('ex',EX_NS,True) # newGraph,conceptMap = StructuralTransformation(graph,newGraph) # revDict = dict([(v,k) for k,v in conceptMap.items()]) # Individual.factoryGraph = newGraph # for oldConceptId ,newConceptId in conceptMap.items(): # if isinstance(oldConceptId,BNode): # oldConceptRepr = repr(Class(oldConceptId,graph=graph)) # if oldConceptRepr.strip() == 'Some Class': # oldConceptRepr = manchesterSyntax( # oldConceptId, # graph) # print "%s -> %s"%( # oldConceptRepr, # newConceptId # ) # # else: # print "%s -> %s"%( # oldConceptId, # newConceptId # ) # # for c in AllClasses(newGraph): # if isinstance(c.identifier,BNode) and c.identifier in conceptMap.values(): # print "## %s ##"%c.identifier # else: # print "##" * 10 # print c.__repr__(True) # print "################################" return graph
def _sparql_construct(self, q, cursor): g = Graph() with cursor: results = cursor.execute(q.encode("utf-8")) with cursor as resolver: for result in results: g.add(resolve(resolver, x) for x in result) return g
def test_10_add_graph(self): cursor = store.cursor() g = Graph(identifier=TEST_GRAPH) g.add((URIRef("http://irl.styx.org/"), RDFS.label, Literal("Idiosyntactix Research Laboratiries"))) g.add((URIRef("http://www.okfn.org/"), RDFS.label, Literal("Open Knowledge Foundation"))) cursor.add_graph(g) graphs = list(map(lambda x: x[0], store.query("SELECT DISTINCT ?g WHERE { graph ?g { ?s ?p ?o } } ORDER BY ?g"))) assert graphs == [URIRef(TEST_GRAPH)]
def packages(cursor): q = "SELECT DISTINCT ?pkg WHERE { ?pkg a scv:Dataset }" g = Graph(identifier="http://semantic.ckan.net/packages") for pkg, in cursor.execute(q, initNs=initNs, soft_limit=-1): print pkg g.add((pkg, RDF.type, SCV["Dataset"])) cursor.delete_model(g.identifier) cursor.add_model(g)
def test_conjunction(self): graph = self.open_graph() self.add_stuff_in_multiple_contexts(graph) triple = (pizza, likes, pizza) # add to context 1 g1 = Graph(graph.store, context1) g1.add(triple) self.assertEquals(len(graph), len(g1))
def make_function(function): func = CRA_DATA["functions/" + slugify(function)] g = Graph(identifier="%s" % func) g.add((func, RDF.type, CRA["Function"])) g.add((func, RDFS.label, Literal(function))) g.add((func, DC["title"], Literal(function))) g.add((func, DC["identifier"], Literal(slugify(function)))) g.add((func, SCV["dataset"], cra)) return g
def test_n32(self): # this test not generating prefixes for subjects/objects g = Graph() g.add((URIRef("http://example1.com/foo"), URIRef("http://example2.com/bar"), URIRef("http://example3.com/baz"))) n3 = g.serialize(format="n3") self.assertTrue(b("<http://example1.com/foo> ns1:bar <http://example3.com/baz> .") in n3)
def test_11_add_triple(self): g = Graph(store, identifier=TEST_GRAPH) s = ( URIRef("http://irl.styx.org/foo"), RDF.type, URIRef("http://irl.styx.org/Thing") ) g.add(s) assert store.exists(s, g)
def serializeWorkflowAnnotation(): g = Graph() workflow = URIRef(".workflow.conf") # g.add((workflow, RDF.type, URIRef("http://physiomeproject.org/workflow/1.0/rdf-schema#workflowproject"))) g.add((workflow, RDFS.subClassOf, URIRef("http://physiomeproject.org/workflow/1.0/rdf-schema#workflowproject"))) return g.serialize(format='xml')
def add(self, statement, context=None, **kw): """Add triples to the graph""" context = _get_context(context) if not self.exists(statement, context): cursor = self.cursor() g = Graph(identifier=context) g.add(skolemise(statement)) cursor.add_graph(g, replace=False)
def testBaseSerialize(self): g = Graph() g.add((URIRef('http://example.com/people/Bob'), URIRef('urn:knows'), URIRef('http://example.com/people/Linda'))) s = g.serialize(base='http://example.com/', format='n3') self.assertTrue(b('<people/Bob>') in s) g2 = ConjunctiveGraph() g2.parse(data=s, publicID='http://example.com/', format='n3') self.assertEqual(list(g), list(g2))
def testBaseSerialize(self): g = Graph() g.add((URIRef('http://example.com/people/Bob'), URIRef( 'urn:knows'), URIRef('http://example.com/people/Linda'))) s = g.serialize(base='http://example.com/', format='n3') self.assertTrue(b('<people/Bob>') in s) g2 = ConjunctiveGraph() g2.parse(data=s, publicID='http://example.com/', format='n3') self.assertEqual(list(g), list(g2))
def add_type(t, klass=None): g = Graph(identifier="http://semantic.ckan.net/data/ukgov-finances-cra/%s" % t) if klass is None: klass = t[0].upper() + t[1:-1] for x, in cursor.execute("SELECT DISTINCT ?x WHERE { ?x a %s }" % CRA[klass].n3(), soft_limit=-1): g.add((x, RDF.type, CRA[klass])) cursor.add_model(g) cursor.add((cra, RDFS.seeAlso, URIRef(g.identifier)), model_uri="http://semantic.ckan.net/data/ukgov-finances-cra") cursor.add((URIRef(g.identifier), RDFS.label, Literal("%ss" % klass))) print "Added", klass
def test_17_add_bnode(self): g = Graph(identifier=TEST_GRAPH) b = BNode() s = (b, RDFS.label, Literal("bnode with add_graph")) g.add(s) c = store.cursor() c.add_graph(g, replace=False) g = Graph(store, identifier=TEST_GRAPH) assert s in g
def make_department(department, deptcode): dept = CRA_DATA["departments/" + slugify(department)] g = Graph(identifier="%s" % dept) g.add((dept, RDF.type, CRA["Department"])) g.add((dept, RDFS.label, Literal(department))) g.add((dept, DC["title"], Literal(department))) g.add((dept, DC["identifier"], Literal(deptcode))) g.add((dept, SCV["dataset"], cra)) return g
def test_n3(self): g = Graph() g.add((URIRef("http://example.com/foo"), URIRef("http://example.com/bar"), URIRef("http://example.com/baz"))) n3 = g.serialize(format="n3") # Gunnar disagrees that this is right: # self.assertTrue("<http://example.com/foo> ns1:bar <http://example.com/baz> ." in n3) # as this is much prettier, and ns1 is already defined: self.assertTrue(b("ns1:foo ns1:bar ns1:baz .") in n3)
def add_stuff_in_multiple_contexts(self, graph): triple = (pizza, hates, tarek) # add to default context graph.add(triple) # add to context 1 g1 = Graph(graph.store, context1) g1.add(triple) # add to context 2 g2 = Graph(graph.store, context2) g2.add(triple)
def describeOverride(terms, bindings, graph): g = Graph() for term in terms: if isinstance(term, Variable) and term not in bindings: continue else: term = bindings.get(term, term) for s, p, o in graph.triples((term, FOAF.mbox, None)): g.add((s, p, o)) return g
def describeOverride(terms,bindings,graph): g=Graph() for term in terms: if isinstance(term,Variable) and term not in bindings: continue else: term=bindings.get(term,term) for s,p,o in graph.triples((term,FOAF.mbox,None)): g.add((s,p,o)) return g
def create_project(g): """Creates a project in the database (and the metadata cache) from an input graph""" print "Here 1" query = g.query("""SELECT ?uri ?user WHERE { ?user perm:hasPermissionOver ?uri . ?user rdf:type foaf:Agent . }""", initNs=ns) print "Here 2" for uri in g.subjects(NS.rdf.type, NS.dm.Project): print "Here 3" user = g.value(None, NS.perm.hasPermissionOver, uri) if user: print "Here 3.1" user_obj = User.objects.get(username=user.split('/')[-1]) print "Here 3.2" project_identifier = uris.uri('semantic_store_projects', uri=uri) print "Here 3.3" project_g = Graph(store=rdfstore(), identifier=project_identifier) print "Here 4" for text_uri in g.subjects(NS.rdf.type, NS.dcmitype.Text): text_graph = Graph() text_graph += g.triples((text_uri, None, None)) print "Here 4.1" if user: project_texts.update_project_text(text_graph, uri, text_uri, user_obj) print "Here 5" for t in g: project_g.add(t) for text_uri in g.subjects(NS.rdf.type, NS.dcmitype.Text): project_g.remove((text_uri, NS.cnt.chars, None)) url = uris.url('semantic_store_projects', uri=uri) project_g.set((uri, NS.dcterms['created'], Literal(datetime.utcnow()))) print "before user" if user: print "user is true" project_g.remove((user, None, None)) username = user.split("/")[-1] permissions.grant_full_project_permissions(username, uri) add_project_types(project_g, uri) build_project_metadata_graph(uri) print "Successfully created project with uri " + uri
def test_n32(self): # this test not generating prefixes for subjects/objects g = Graph() g.add((URIRef("http://example1.com/foo"), URIRef("http://example2.com/bar"), URIRef("http://example3.com/baz"))) n3 = g.serialize(format="n3") self.assertTrue( b("<http://example1.com/foo> ns1:bar <http://example3.com/baz> .") in n3)
def testBaseSerialize(self): g = Graph() g.add(( URIRef("http://example.com/people/Bob"), URIRef("urn:knows"), URIRef("http://example.com/people/Linda"), )) s = g.serialize(base="http://example.com/", format="n3") self.assertTrue("<people/Bob>".encode("latin-1") in s) g2 = ConjunctiveGraph() g2.parse(data=s, publicID="http://example.com/", format="n3") self.assertEqual(list(g), list(g2))
def get_orb_description(desc_dict,service_config): g = Graph() for attr in STANDARD_ATTRIBUTES: g.add((SCRY.orb, SCRY[attr], Literal(desc_dict[attr]))) for uri_string in service_config: proc = service_config[uri_string] desc = proc.get_description() g += desc return g
def mrs_to_rdf( m: delphin.mrs._mrs.MRS, MRSI: rdflib.term.URIRef, store: rdflib.plugins.memory.IOMemory = plugin.get("IOMemory", Store)(), defaultGraph: rdflib.graph.Graph = None ) -> rdflib.plugins.memory.IOMemory: """ Takes a PyDelphin MRS object "m" and serializes it into a named RDF graph inside a store. Args: m: a PyDelphin MRS instance to be converted into RDF format MRSI: URI of the MRS instance being converted store: RDFLib IOMemory store to add the graphs. defaultGraph : the default graph of the store. If not given, creates one from the 'store'. Inplace function that alters the store with the serialized MRS and return the store as well. """ # Making the arguments behave well: if defaultGraph is None: defaultGraph = Graph(store, identifier=BNode()) if defaultGraph.store != store: # Bad function input defaultGraph = Graph(store, identifier=BNode()) print( "'defaultGraph' argument not consistent with the 'store' argument. The argument was discarded" ) # MRS graph: mrsGraph = Graph(store, identifier=MRSI) # Creating the prefix of the MRS elements and relevant namespaces insprefix = Namespace(MRSI + '#') VARS = Namespace(insprefix + "variable-") RELS = Namespace(insprefix + "EP-") PREDS = Namespace(insprefix + "predicate-") SORTINFO = Namespace(insprefix + "sortinfo-") HCONS = Namespace(insprefix + "hcons-") ICONS = Namespace(insprefix + "icons-") # Adding top and index mrsGraph.add((MRSI, DELPH['hasTop'], VARS[m.top])) mrsGraph.add((MRSI, DELPH['hasIndex'], VARS[m.index])) # ALTERNATIVE: (BNode, DELPH['hasTop'], VARS[m.top]) # Populating the graphs _vars_to_rdf(m, mrsGraph, VARS, SORTINFO) _rels_to_rdf(m, mrsGraph, defaultGraph, MRSI, RELS, PREDS, VARS) _hcons_to_rdf(m, mrsGraph, defaultGraph, MRSI, HCONS, VARS) _icons_to_rdf(m, mrsGraph, defaultGraph, MRSI, ICONS, VARS) return store
def fix_relative_uriref(graph, fake_prefix): def torelative(node): if not isinstance(node, URIRef): return node if node.startswith(fake_prefix): return URIRef(node.replace(fake_prefix, '')) return node corrected_graph = Graph() for triple in graph: s, p, o = triple corrected_graph.add((torelative(s), torelative(p), torelative(o))) return corrected_graph
def addStuffInMultipleContexts(self): c1 = self.c1 c2 = self.c2 triple = (self.pizza, self.hates, self.tarek) # add to default context self.graph.add(triple) # add to context 1 graph = Graph(self.graph.store, c1) graph.add(triple) # add to context 2 graph = Graph(self.graph.store, c2) graph.add(triple)
def serializeWorkflowAnnotation(): g = Graph() workflow = URIRef(".workflow.conf") # g.add((workflow, RDF.type, URIRef("http://physiomeproject.org/workflow/1.0/rdf-schema#workflowproject"))) g.add(( workflow, RDFS.subClassOf, URIRef( "http://physiomeproject.org/workflow/1.0/rdf-schema#workflowproject" ))) return g.serialize(format='xml')
def add_areas(): for s, in cursor.execute("SELECT DISTINCT ?f WHERE { ?f a cra:SubFunction }", initNs=initNs, soft_limit=-1): g = Graph(identifier=str(s)) q = """ SELECT DISTINCT ?a WHERE { ?a a cra:Area . ?a cra:subfunction %s } """ % s.n3() for a, in cursor.execute(q, initNs=initNs, soft_limit=-1): g.add((s, CRA["hasArea"], a)) cursor.add_model(g) print "Filled in Areas"
def serializeWorkflowAnnotation(): g = Graph() workflow = URIRef(DEFAULT_WORKFLOW_PROJECT_FILENAME) # g.add((workflow, RDF.type, URIRef("http://physiomeproject.org/workflow/1.0/rdf-schema#workflowproject"))) g.add(( workflow, RDFS.subClassOf, URIRef( "http://physiomeproject.org/workflow/1.0/rdf-schema#workflowproject" ))) return g.serialize(format='xml')
def full_graph(): g = Graph() for t in c.test_thesauri: g.add((t, RDF.type, c.test_type_thesaurus)) for cncpt in c.test_concepts: g.add((cncpt, RDF.type, c.test_type_concept)) for narrow, broader in c.thsrs_broader: g.add((narrow, SKOS.broader, broader)) for narrow, broader in c.cncpt_broaders: g.add((narrow, SKOS.broader, broader)) for cncpt, label in c.test_labels: g.add((cncpt, SKOS.prefLabel, label)) return g
def load_mappings(self, filename, graph=None): tmp = Graph() if graph is None: graph = Graph() tmp.load(filename, format=self.extFromFilename(filename)) skosify.infer.skos_symmetric_mappings(tmp) for tr in tmp.triples_choices((None, [SKOS.exactMatch, SKOS.closeMatch, SKOS.broadMatch, SKOS.narrowMatch, SKOS.relatedMatch], None)): #if tr[0] in all_concepts: graph.add(tr) return graph