def get_rdf_template(item_uri, item_id): g = ConjunctiveGraph(identifier=item_uri) g.bind('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') g.bind('dcterms', 'http://purl.org/dc/terms/') g.add((URIRef(item_uri), URIRef('http://purl.org/dc/terms/identifier'), Literal(item_id))) data2 = g.serialize(format='xml', encoding="utf-8") + '\n' return data2
def dot(graph, simple=True, debug=False, landscape=False): """Format graph as a dot file.""" import sys from rdflib import ConjunctiveGraph from rdflib.plugin import register, Parser from rdflib.tools.rdf2dot import rdf2dot register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser') g = ConjunctiveGraph().parse( data=_jsonld(graph, 'expand'), format='json-ld', ) g.bind('prov', 'http://www.w3.org/ns/prov#') g.bind('foaf', 'http://xmlns.com/foaf/0.1/') g.bind('wfdesc', 'http://purl.org/wf4ever/wfdesc#') g.bind('wf', 'http://www.w3.org/2005/01/wf/flow#') g.bind('wfprov', 'http://purl.org/wf4ever/wfprov#') g.bind('schema', 'http://schema.org/') if debug: rdf2dot(g, sys.stdout) return sys.stdout.write('digraph { \n node [ fontname="DejaVu Sans" ] ; \n ') if landscape: sys.stdout.write('rankdir="LR" \n') if simple: _rdf2dot_simple(g, sys.stdout) return _rdf2dot_reduced(g, sys.stdout)
def change_status(vocabprefix, uri, predicate, message, action): if not action in ['add', 'remove']: return False vocab_uri = URIRef(uri) vocabdir = os.path.join(ag.vocabulariesdir, vocabprefix) vocabstatusfile = os.path.join(vocabdir, "status.rdf") if not os.path.isfile(vocabstatusfile): return False graph = Graph() graph.parse(vocabstatusfile) predicate = predicate.split(':') ns = predicate[0] term = predicate[1] if message and (message.startswith('http://') or message.startswith('file://')): message = URIRef(message) elif message: message = Literal(message) if action == 'add': for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) graph.add((vocab_uri, namespaces[ns][term], message)) elif action == 'remove': graph.remove((vocab_uri, namespaces[ns][term], message)) rdf_str = None rdf_str = graph.serialize() f = codecs.open(vocabstatusfile, 'w', 'utf-8') f.write(rdf_str) f.close() return True
def add_mediator(params): #Write user metadata and save the rdf file graph = Graph() for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) uri = URIRef("http://vocab.ox.ac.uk/owner/uuid:%s"%uuid.uuid4()) graph.add((uri, namespaces['foaf']['firstName'], Literal(params['firstname']))) graph.add((uri, namespaces['foaf']['lastName'], Literal(params['lastname']))) graph.add((uri, namespaces['foaf']['mbox'], Literal(params['email']))) graph.add((uri, namespaces['foaf']['account'], Literal(params['username']))) if 'title' in params and params['title']: graph.add((uri, namespaces['foaf']['title'], Literal(params['title']))) if 'department' in params and params['department']: department = params['department'].split(';') for d in department: graph.add((uri, namespaces['dcterms']['isPartOf'], Literal(d.strip()))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(os.path.join(ag.mediatorsdir, '%s.rdf'%params['username']), 'w', 'utf-8') f.write(rdf_str) f.close() graph2 = Graph() graph2.parse(ag.mediatorslist) for prefix, url in namespaces.iteritems(): graph2.bind(prefix, URIRef(url)) graph2.add((uri, namespaces['foaf']['account'], Literal(params['username']))) rdf_str = None rdf_str = graph2.serialize() f = codecs.open(ag.mediatorslist, 'w', 'utf-8') f.write(rdf_str) f.close() return True
def test_issue682_signing_named_graphs(): ns = Namespace("http://love.com#") mary = BNode() john = URIRef("http://love.com/lovers/john#") cmary=URIRef("http://love.com/lovers/mary#") cjohn=URIRef("http://love.com/lovers/john#") store = IOMemory() g = ConjunctiveGraph(store=store) g.bind("love",ns) gmary = Graph(store=store, identifier=cmary) gmary.add((mary, ns['hasName'], Literal("Mary"))) gmary.add((mary, ns['loves'], john)) gjohn = Graph(store=store, identifier=cjohn) gjohn.add((john, ns['hasName'], Literal("John"))) ig = to_isomorphic(g) igmary = to_isomorphic(gmary) assert len(igmary) == len(gmary) assert len(ig) == len(g) assert len(igmary) < len(ig) assert ig.graph_digest() != igmary.graph_digest()
def __init__(self, path=None): self.__dict__ = self.__shared_state if (self.data == None): if (path == None): raise ValueError("djubby's configuration MUST be initialized a first time, read http://code.google.com/p/djubby/wiki/GettingStarted") else: self.path = os.path.abspath(path) logging.debug("Reading djubby's configuration from %s..." % self.path) if (not os.path.exists(self.path)): raise ValueError("Not found a proper file at '%s' with a configuration for djubby. Please, provide a right path" % self.path) data = ConjunctiveGraph() data.bind("conf", ns.config) try: data.load(path, format='n3') except Exception, e: raise ValueError("Not found a proper N3 file at '%s' with a configuration for djubby. Please, provide a valid N3 file" % self.path) self.data = data try: self.graph = self.get_value("sparqlDefaultGraph") self.endpoint = self.get_value("sparqlEndpoint") except Exception, e: raise ValueError("Not found the graph not the endpoint that it's supposed djubby have to query. Please, provide a right donfiguration") logging.info("Using <%s> as default graph to query the endpoint <%s>" % (self.graph, self.endpoint)) self.__class__.__dict__['_Configuration__shared_state']["data"] = data #FIXME
class Config(object): def __init__(self, masterGraph, hubHost): self.etcd = etcd3.client(host=hubHost, port=9022) self.masterGraph = masterGraph self.hubHost = hubHost self.configGraph = ConjunctiveGraph() self.boards = [] self.etcPrefix = 'pi/' self.reread() deferToThread(self.watchEtcd) def watchEtcd(self): events, cancel = self.etcd.watch_prefix(self.etcPrefix) reactor.addSystemEventTrigger('before', 'shutdown', cancel) for ev in events: log.info('%s changed', ev.key) reactor.callFromThread(self.configChanged) def configChanged(self): self.cancelRead() self.rereadLater = reactor.callLater(.1, self.reread) def cancelRead(self): if getattr(self, 'rereadLater', None): self.rereadLater.cancel() self.rereadLater = None @STATS.configReread.time() def reread(self): self.rereadLater = None log.info('read config') self.configGraph = ConjunctiveGraph() for v, md in self.etcd.get_prefix(self.etcPrefix): log.info(' read file %r', md.key) self.configGraph.parse(StringInputSource(v), format='n3') self.configGraph.bind('', ROOM) self.configGraph.bind('rdf', RDF) # config graph is too noisy; maybe make it a separate resource #masterGraph.patch(Patch(addGraph=self.configGraph)) self.setupBoards() def setupBoards(self): thisHost = Literal(hostname) for row in self.configGraph.query( 'SELECT ?board WHERE { ?board a :PiBoard; :hostname ?h }', initBindings=dict(h=thisHost)): thisBoard = row.board break else: log.warn("config had no board for :hostname %s. Waiting for config update." % thisHost) self.boards = [] return log.info("found config for board %r" % thisBoard) self.boards = [Board(self.configGraph, self.masterGraph, thisBoard, self.hubHost)] self.boards[0].startPolling()
def parse(self, source, graph, encoding="utf-8"): if encoding not in [None, "utf-8"]: raise Exception( ("TriG files are always utf-8 encoded, ", "I was passed: %s") % encoding) # we're currently being handed a Graph, not a ConjunctiveGraph assert graph.store.context_aware, "TriG Parser needs a context-aware store!" conj_graph = ConjunctiveGraph(store=graph.store) conj_graph.default_context = graph # TODO: CG __init__ should have a # default_context arg # TODO: update N3Processor so that it can use conj_graph as the sink conj_graph.namespace_manager = graph.namespace_manager sink = RDFSink(conj_graph) baseURI = conj_graph.absolutize(source.getPublicId() or source.getSystemId() or "") p = TrigSinkParser(sink, baseURI=baseURI, turtle=True) p.loadStream(source.getByteStream()) for prefix, namespace in p._bindings.items(): conj_graph.bind(prefix, namespace)
def update_mediator(params): #Write user metadata and save the rdf file if not ('username' in params and params['username']): return False det = get_mediator_details(params['username']) graph = Graph() graph.parse(os.path.join(ag.mediatorsdir, '%s.rdf'%params['username'])) for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) uri = URIRef(det['uri']) if 'firstname' in params and params['firstname']: graph.remove((uri, namespaces['foaf']['firstName'], None)) graph.add((uri, namespaces['foaf']['firstName'], Literal(params['firstname']))) if 'lastname' in params and params['lastname']: graph.remove((uri, namespaces['foaf']['lastName'], None)) graph.add((uri, namespaces['foaf']['lastName'], Literal(params['lastname']))) if 'email' in params and params['email']: graph.remove((uri, namespaces['foaf']['mbox'], None)) graph.add((uri, namespaces['foaf']['mbox'], Literal(params['email']))) if 'title' in params and params['title']: graph.remove((uri, namespaces['foaf']['title'], None)) graph.add((uri, namespaces['foaf']['title'], Literal(params['title']))) if 'department' in params and params['department']: graph.remove((uri, namespaces['dcterms']['isPartOf'], None)) department = params['department'].split(';') for d in department: graph.add((uri, namespaces['dcterms']['isPartOf'], Literal(d.strip()))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(os.path.join(ag.mediatorsdir, '%s.rdf'%params['username']), 'w', 'utf-8') f.write(rdf_str) f.close() return True
def parse(self, source, graph, encoding="utf-8"): if encoding not in [None, "utf-8"]: raise Exception( ("TriG files are always utf-8 encoded, ", "I was passed: %s") % encoding) # we're currently being handed a Graph, not a ConjunctiveGraph assert graph.store.context_aware, "TriG Parser needs a context-aware store!" conj_graph = ConjunctiveGraph(store=graph.store, identifier=graph.identifier) conj_graph.default_context = graph # TODO: CG __init__ should have a # default_context arg # TODO: update N3Processor so that it can use conj_graph as the sink conj_graph.namespace_manager = graph.namespace_manager sink = RDFSink(conj_graph) baseURI = conj_graph.absolutize( source.getPublicId() or source.getSystemId() or "") p = TrigSinkParser(sink, baseURI=baseURI, turtle=True) p.loadStream(source.getByteStream()) for prefix, namespace in p._bindings.items(): conj_graph.bind(prefix, namespace)
def __init__(self, additional_bindings=list()): store = ConjunctiveGraph(identifier='default') nsBindings = [('quit', QUIT), ('foaf', FOAF), ('prov', PROV)] for prefix, namespace in nsBindings + additional_bindings: store.bind(prefix, namespace) super().__init__(store=store)
def generate_example(self, egtext, resource): # Yes really... exec(egtext) # Now in scope should be a top resource factory.pipe_scoped_contexts = False factory.toFile(top, compact=False) factory.pipe_scoped_contexts = True jsstr = factory.toString(top, compact=False, collapse=80) factory.pipe_scoped_contexts = False js = factory.toJSON(top) # Generate all our serializations nq = to_rdf(js, {"format": "application/nquads"}) g = ConjunctiveGraph() for ns in ['crm', 'dc', 'schema', 'dcterms', 'skos', 'la']: g.bind(ns, ctxt[ns]) g.parse(data=nq, format="nt") out = g.serialize(format="turtle") fp = js['id'][len(factory.base_url):] fp2 = fp + ".ttl" fh = open(os.path.join(factory.base_dir, fp2), 'w') fh.write(out) fh.close() # And build mermaid description mermaid = self.build_mermaid(js) # Build index references self.traverse(js, top.id, resource) # And return the JSON plus links, to be substed by the top level filter raw = top.id + ".json" self.example_list.append(raw) rawq = urllib.quote(raw).replace('/', "%2F") playground = "http://json-ld.org/playground-dev/#startTab=tab-expanded©Context=true&json-ld=%s" % rawq turtle = top.id + ".ttl" turtle_play = "http://cdn.rawgit.com/niklasl/ldtr/v0.2.2/demo/?edit=true&url=%s" % turtle egid = fp.replace('/', '_') resp = """ <a id="%s"></a> ```json %s ``` <div class="mermaid"> %s </div> Other Representations: [JSON-LD (Raw)](%s) | [JSON-LD (Playground)](%s) | [Turtle (Raw)](%s) | [Turtle (Styled)](%s) """ % (egid, jsstr, mermaid, raw, playground, turtle, turtle_play) return resp
def load_sentence(self, rdf_triples): """ Load the given triples into the triple store """ g = ConjunctiveGraph() g.bind("base", BASE) for triple in rdf_triples: g.add(triple) self.soh.add_triples(g, clear=True)
def example_1(): """Creates a ConjunctiveGraph and performs some BerkeleyDB tasks with it """ path = mktemp() # Declare we are using a BerkeleyDB Store graph = ConjunctiveGraph("BerkeleyDB") # Open previously created store, or create it if it doesn't exist yet # (always doesn't exist in this example as using temp file location) rt = graph.open(path, create=False) if rt == NO_STORE: # There is no underlying BerkeleyDB infrastructure, so create it print("Creating new DB") graph.open(path, create=True) else: print("Using existing DB") assert rt == VALID_STORE, "The underlying store is corrupt" print("Triples in graph before add:", len(graph)) print("(will always be 0 when using temp file for DB)") # Now we'll add some triples to the graph & commit the changes EG = Namespace("http://example.net/test/") graph.bind("eg", EG) graph.add((EG["pic:1"], EG.name, Literal("Jane & Bob"))) graph.add((EG["pic:2"], EG.name, Literal("Squirrel in Tree"))) graph.commit() print("Triples in graph after add:", len(graph)) print("(should be 2)") # display the graph in Turtle print(graph.serialize()) # close when done, otherwise BerkeleyDB will leak lock entries. graph.close() graph = None # reopen the graph graph = ConjunctiveGraph("BerkeleyDB") graph.open(path, create=False) print("Triples still in graph:", len(graph)) print("(should still be 2)") graph.close() # Clean up the temp folder to remove the BerkeleyDB database files... for f in os.listdir(path): os.unlink(path + "/" + f) os.rmdir(path)
class Config(object): def __init__(self, masterGraph): self.graph = ConjunctiveGraph() log.info('read config') for f in os.listdir('config'): if f.startswith('.'): continue self.graph.parse('config/%s' % f, format='n3') log.info(' parsed %s', f) self.graph.bind('', ROOM) self.graph.bind('rdf', RDF)
def createGlobalGraph(cls, gns): """ Creates a global graph representing a global context. @type gns: str @param gns: the global namespace """ globalGraph = ConjunctiveGraph() globalGraph.bind("owl", OWL) globalGraph.bind(gns[gns.rfind("/")+1:len(gns)-1], gns) return globalGraph
def render(self, data, accepted_media_type=None, renderer_context=None): jsonld = super(TurtleRenderer, self)\ .render(data, accepted_media_type, renderer_context) g = ConjunctiveGraph() for ns, uri in list(NAMESPACES.items()): g.bind(ns, Namespace(uri)) g.parse(data=jsonld, format='json-ld') return g.serialize(format='turtle')
def __init__(self, base_uri): graph = ConjunctiveGraph() self._graph = graph self._base_uri = self._validateURI(base_uri) self._uniq_ids = dict() # bind prefixes to namespaces graph.bind('dbp', DBPEDIA) graph.bind('dct', DCTERMS) graph.bind('dcat', DCAT) graph.bind('lang', LANG)
def create_temporal_graph(self, resources): graph = ConjunctiveGraph() if resources: for resource in resources: triples = self.graph.triples((URIRef(resource), None, None)) for triple in triples: graph.add(triple) #Bind namespaces to graph for ns in NSBINDINGS: graph.bind(ns, NSBINDINGS[ns]) return graph
def _process_data(self, document): ''' Creates the RDF graph describing the topic @param document: the DOM document of the topic ''' # Create the graph graph = ConjunctiveGraph() graph.bind('swc', SWC) graph.bind('cfp', CFP) graph.bind('ical', ICAL) graph.bind('foaf', FOAF) graph.bind('dct', DCT) graph.bind('lode', LODE) # Init the event topic_event = LDES[self.get_resource_name()] graph.add((topic_event, RDF.type, SIOCT['Tag'])) # Get the label try: label = document.find(id='inner_left').find('h1').text graph.add((topic_event, RDFS.label, Literal(label))) except: pass # Set the last modification date graph.add((self.get_named_graph(), DCT['modified'], Literal(datetime.now()))) # Save the data self.rdf_data = graph.serialize()
def rels2rdf(ns, verbose=False): """ Convert the reldicts derived from the IEER corpus in an RDF Graph. """ graph = ConjunctiveGraph() graph.bind("nltk", BASE) graph.bind("org", "http://nltk.org/terms/org#") graph.bind("loc", "http://nltk.org/terms/loc#") graph.bind("pred", "http://nltk.org/terms/pred#") graph.bind("class", "http://nltk.org/terms/class#") in_uri = sym2uri(ns, "pred", "in") loc_uri = sym2uri(ns, "class", "Location") org_uri = sym2uri(ns, "class", "Organization") graph.add((in_uri, RDFNS.type, RDFSNS.Property)) graph.add((loc_uri, RDFNS.type, RDFSNS.Class)) graph.add((org_uri, RDFNS.type, RDFSNS.Class)) graph.add((in_uri, RDFSNS.domain, org_uri)) graph.add((in_uri, RDFSNS.range, loc_uri)) from nltk.corpus import ieer IN = re.compile(r".*\bin\b(?!\b.+ing\b)") for item in ieer.fileids(): for doc in ieer.parsed_docs(item): for reldict in extract_rels("ORG", "LOC", doc, corpus="ieer", pattern=IN): graph.add(make_rdf(ns, reldict, relsym="in")) for triple in make_rdfs(ns, reldict): graph.add(triple) return graph
class Topic(object): def __init__(self, entity_name, entity_id): ''' Constructor ''' # Get the event page and compute its id self.entity_id = entity_id self.resource = LDES[self.entity_id] # Create the graph self.graph = ConjunctiveGraph() self.graph.bind('swc', SWC) self.graph.bind('cfp', CFP) self.graph.bind('ical', ICAL) self.graph.bind('foaf', FOAF) self.graph.bind('dct', DCT) self.graph.bind('lode', LODE) # Declare the type of the resource self.graph.add((self.resource, RDF.type, SIOCT['Tag'])) self.graph.add((self.named_graph(), DCT['modified'], Literal(datetime.now()))) def get_rdf_data(self): return self.graph.serialize() def named_graph(self): return URIRef(NAMED_GRAPHS_BASE + self.entity_id + '.rdf') def process(self, record, entity_url): # Get the document document = BeautifulSoup(urllib2.urlopen("http://eventseer.net" + entity_url).read()) del document
def rels2rdf(ns, verbose=False): """ Convert the reldicts derived from the IEER corpus in an RDF Graph. """ graph = ConjunctiveGraph() graph.bind('nltk',BASE) graph.bind('org', "http://nltk.org/terms/org#") graph.bind('loc', "http://nltk.org/terms/loc#") graph.bind('pred', "http://nltk.org/terms/pred#") graph.bind('class', "http://nltk.org/terms/class#") in_uri = sym2uri(ns, 'pred', 'in') loc_uri = sym2uri(ns, 'class', 'Location') org_uri = sym2uri(ns, 'class', 'Organization') graph.add((in_uri, RDFNS.type, RDFSNS.Property)) graph.add((loc_uri, RDFNS.type, RDFSNS.Class)) graph.add((org_uri, RDFNS.type, RDFSNS.Class)) graph.add((in_uri, RDFSNS.domain, org_uri)) graph.add((in_uri, RDFSNS.range, loc_uri)) from nltk.corpus import ieer IN = re.compile(r'.*\bin\b(?!\b.+ing\b)') for item in ieer.fileids(): for doc in ieer.parsed_docs(item): for reldict in extract_rels('ORG', 'LOC', doc, corpus='ieer', pattern=IN): graph.add(make_rdf(ns, reldict, relsym='in')) for triple in make_rdfs(ns, reldict): graph.add(triple) return graph
def add_ref_vocab(vocabprefix, source_uri): vocab_uri = URIRef("http://vocab.ox.ac.uk/%s"%vocabprefix) graph = Graph() if os.path.isfile(ag.vocabulariesref): graph.parse(ag.vocabulariesref) for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) graph.add((URIRef(vocab_uri), namespaces['dcterms']['isVersionOf'], URIRef(source_uri))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(ag.vocabulariesref, 'w', 'utf-8') f.write(rdf_str) f.close() return True
def rels2rdf(ns, verbose=False): """ Convert the reldicts derived from the IEER corpus in an RDF Graph. """ graph = ConjunctiveGraph() graph.bind('nltk',BASE) graph.bind('org', "http://nltk.org/terms/org#") graph.bind('loc', "http://nltk.org/terms/loc#") graph.bind('pred', "http://nltk.org/terms/pred#") graph.bind('class', "http://nltk.org/terms/class#") in_uri = sym2uri(ns, 'pred', 'in') loc_uri = sym2uri(ns, 'class', 'Location') org_uri = sym2uri(ns, 'class', 'Organization') graph.add((in_uri, RDFNS.type, RDFSNS.Property)) graph.add((loc_uri, RDFNS.type, RDFSNS.Class)) graph.add((org_uri, RDFNS.type, RDFSNS.Class)) graph.add((in_uri, RDFSNS.domain, org_uri)) graph.add((in_uri, RDFSNS.range, loc_uri)) from nltk.corpus import ieer IN = re.compile(r'.*\bin\b(?!\b.+ing\b)') for item in ieer.items: for doc in ieer.parsed_docs(item): for reldict in relextract('ORG', 'LOC', doc, pattern=IN): graph.add(make_rdf(ns, reldict, relsym='in')) for triple in make_rdfs(ns, reldict): graph.add(triple) return graph
def _process_data(self, document): ''' Creates the RDF graph describing the topic @param document: the DOM document of the topic ''' # Create the graph graph = ConjunctiveGraph() graph.bind('swc', SWC) graph.bind('cfp', CFP) graph.bind('ical', ICAL) graph.bind('foaf', FOAF) graph.bind('dct', DCT) graph.bind('lode', LODE) # Init the event topic_event = LDES[self.get_resource_name()] graph.add((topic_event, RDF.type, SIOCT['Tag'])) # Get the label try: label = document.find(id='inner_left').find('h1').text graph.add((topic_event, RDFS.label, Literal(label))) except: pass # Set the last modification date graph.add( (self.get_named_graph(), DCT['modified'], Literal(datetime.now()))) # Save the data self.rdf_data = graph.serialize()
class Update(RDFFile): """ TODO: Add documentation """ RDFBASE = "urn:mozilla:extension:%s" def __init__(self, package, xpiFile, urlBase): self.package = package self.id = package.install.get("id") self.basename = package.basename self.version = package.install.get("version") self.graph = ConjunctiveGraph() self.graph.bind("em", Namespace("http://www.mozilla.org/2004/em-rdf#")) self.urlBase = urlBase self.baseUri = URIRef(self.RDFBASE % self.id) self.versionUri = URIRef(self.RDFBASE % "%s:%s" % (self.id, self.version)) self.updateUri = URIRef(self._genURI()) self.updateList = URIRef(self._genURI()) self.graph.add((self.baseUri, self.em("updates"), self.updateList)) self.graph.add((self.updateList, self.rdf("type"), self.rdf("Seq"))) self.graph.add((self.updateList, self.rdf("li"), self.versionUri)) self.graph.add((self.versionUri, self.em("version"), Literal(self.version))) self.graph.add((self.versionUri, self.em("targetApplication"), self.updateUri)) for id, minVersion, maxVersion in self.package.install.getTargets(): self.graph.add((self.updateUri, self.em("id"), Literal(id))) self.graph.add((self.updateUri, self.em("minVersion"), Literal(minVersion))) self.graph.add((self.updateUri, self.em("maxVersion"), Literal(maxVersion))) self.graph.add((self.updateUri, self.em("updateLink"), Literal(URLBASE % xpiFile))) self.graph.add((self.updateUri, self.em("updateHash"), Literal("sha512:%s" % self._sha512sum(xpiFile)))) def save(self, fileName=None): if fileName is None: fileName = "%s-%s.rdf" % (self.basename, self.version) with open(fileName, "w") as f: f.write(self.graph.serialize()) return fileName def _sha512sum(self, fileName): sha512 = hashlib.sha512() with open(fileName) as f: for line in f.readlines(): sha512.update(line) return sha512.hexdigest()
def addMatchesToGraph(matches): g = ConjunctiveGraph() g.bind("aers", "http://aers.data2semantics.org/resource/") g.bind("dbpedia", "http://dbpedia.org/resource/") g.bind("owl", "http://www.w3.org/2002/07/owl#") g.bind("sider", "http://www4.wiwiss.fu-berlin.de/sider/resource/sider/") g.bind("skos", "http://www.w3.org/2004/02/skos/core#") print "Adding to graph..." for m in matches: for subj in m: for obj in m: if subj != obj: g.add((URIRef(subj), SKOS['exactMatch'], URIRef(obj))) print "... done" return g
def addMatchesToGraph(matches): g = ConjunctiveGraph() g.bind("aers", "http://aers.data2semantics.org/resource/") g.bind("dbpedia", "http://dbpedia.org/resource/") g.bind("owl", "http://www.w3.org/2002/07/owl#") g.bind("sider", "http://www4.wiwiss.fu-berlin.de/sider/resource/sider/") g.bind("skos","http://www.w3.org/2004/02/skos/core#") print "Adding to graph..." for m in matches : for subj in m : for obj in m : if subj != obj : g.add((URIRef(subj),SKOS['exactMatch'],URIRef(obj))) print "... done" return g
def _get_thing_graph(td): g = td.resource.to_graph() def_g = ConjunctiveGraph(identifier=td.resource.node) for ns, uri in R.agora.fountain.prefixes.items(): def_g.bind(ns, uri) for s, p, o in g: def_g.add((s, p, o)) td_node = td.node if not list(def_g.objects(td.resource.node, CORE.describedBy)): def_g.add((td.resource.node, CORE.describedBy, td_node)) return def_g
def get(self, id, format): # Content negotiation if format is None: accept = self.request.headers.get('Accept').lower() if "application/rdf+xml" in accept: self.set_status(303) self.set_header("Location", self.base_uri + "/signs/" + id + ".rdf") else: self.set_status(303) self.set_header("Location", self.base_uri + "/signs/" + id + ".json") self.finish() # invalid format elif format != ".json" and format != ".rdf": self.write_error(401, message="Format %s not supported" % format) self.finish() # call socrata else: crime = yield tornado.gen.Task(SocrataLookup.get_crime, id) # Connection still good after calling socrata if not self.request.connection.stream.closed(): if not crime: self.write_error(404, message="crime not found") self.finish() else: crime = models.Crime(crime, self.base_uri) # JSON if format == ".json": self.set_header("Content-Type", "application/json") self.write(json.dumps(crime)) # RDF elif format == ".rdf" or format == ".nt" or format == ".ttl": graph = ConjunctiveGraph() graph.bind('geo', GEO) graph.bind('dcterms', DCTERMS) graph.bind('dbpediaowl', DBPEDIAOWL) graph.bind('parksafecrime', PARKSAFECRIME) crimeURIRef = URIRef(crime['uri']) graph.add((crimeURIRef, RDF.type, DBPEDIAOWL['event'])) graph.add((crimeURIRef, GEO['lat'], Literal(crime['latitude']))) graph.add((crimeURIRef, GEO['lon'], Literal(crime['longitude']))) graph.add((crimeURIRef, PARKSAFECRIME['description'], Literal(crime['description']))) graph.add((crimeURIRef, PARKSAFECRIME['date'], Literal(crime['date']))) graph.add((crimeURIRef, PARKSAFECRIME['block'], Literal(crime['block']))) graph.add((crimeURIRef, PARKSAFECRIME['type'], Literal(crime['type']))) if format == ".rdf": self.set_header("Content-Type", "application/rdf+xml") self.write(graph.serialize()) elif format == ".nt": self.set_header("Content-Type", "text/plain") self.write(graph.serialize(format='nt')) else: self.set_header("Content-Type", "text/turtle") self.write(graph.serialize(format='turtle')) self.finish()
def skolemize(g): bn_map = {} skolem = ConjunctiveGraph() for prefix, ns in g.namespaces(): skolem.bind(prefix, ns) for s, p, o in g: if isinstance(s, BNode): if s not in bn_map: bn_map[s] = URIRef('/'.join([BNODE_SKOLEM_BASE, str(s)])) s = bn_map[s] if isinstance(o, BNode): if o not in bn_map: bn_map[o] = URIRef('/'.join([BNODE_SKOLEM_BASE, str(o)])) o = bn_map[o] skolem.add((s, p, o)) return skolem
class Store: def __init__(self): self.graph = ConjunctiveGraph() if os.path.exists(storefn): self.graph.load(storeuri, format='n3') self.graph.bind('dc', 'http://purl.org/dc/elements/1.1/') self.graph.bind('foaf', 'http://xmlns.com/foaf/0.1/') self.graph.bind('imdb', 'http://www.csd.abdn.ac.uk/~ggrimnes/dev/imdb/IMDB#') self.graph.bind('rev', 'http://purl.org/stuff/rev#') def save(self): self.graph.serialize(storeuri, format='n3') def who(self, who=None): if who is not None: name, email = (r_who.match(who).group(1), r_who.match(who).group(2)) self.graph.add( (URIRef(storeuri), DC['title'], Literal(title % name))) self.graph.add( (URIRef(storeuri + '#author'), RDF.type, FOAF['Person'])) self.graph.add( (URIRef(storeuri + '#author'), FOAF['name'], Literal(name))) self.graph.add( (URIRef(storeuri + '#author'), FOAF['mbox'], Literal(email))) self.save() else: return self.graph.objects(URIRef(storeuri + '#author'), FOAF['name']) def new_movie(self, movie): movieuri = URIRef('http://www.imdb.com/title/tt%s/' % movie.movieID) self.graph.add((movieuri, RDF.type, IMDB['Movie'])) self.graph.add((movieuri, DC['title'], Literal(movie['title']))) self.graph.add((movieuri, IMDB['year'], Literal(int(movie['year'])))) self.save() def new_review(self, movie, date, rating, comment=None): review = BNode( ) # @@ humanize the identifier (something like #rev-$date) movieuri = URIRef('http://www.imdb.com/title/tt%s/' % movie.movieID) self.graph.add( (movieuri, REV['hasReview'], URIRef('%s#%s' % (storeuri, review)))) self.graph.add((review, RDF.type, REV['Review'])) self.graph.add((review, DC['date'], Literal(date))) self.graph.add((review, REV['maxRating'], Literal(5))) self.graph.add((review, REV['minRating'], Literal(0))) self.graph.add((review, REV['reviewer'], URIRef(storeuri + '#author'))) self.graph.add((review, REV['rating'], Literal(rating))) if comment is not None: self.graph.add((review, REV['text'], Literal(comment))) self.save() def movie_is_in(self, uri): return (URIRef(uri), RDF.type, IMDB['Movie']) in self.graph
def deskolemize(g): bn_map = {} deskolem = ConjunctiveGraph() for prefix, ns in g.namespaces(): deskolem.bind(prefix, ns) for s, p, o in g: if isinstance(s, URIRef) and s.startswith(BNODE_SKOLEM_BASE): if s not in bn_map: bn_map[s] = BNode(s.replace(BNODE_SKOLEM_BASE + '/', '')) s = bn_map[s] if isinstance(o, URIRef) and o.startswith(BNODE_SKOLEM_BASE): if o not in bn_map: bn_map[o] = BNode(o.replace(BNODE_SKOLEM_BASE + '/', '')) o = bn_map[o] deskolem.add((s, p, o)) return deskolem
class Config(object): def __init__(self, masterGraph): self.graph = ConjunctiveGraph() log.info('read config') for f in os.listdir('config'): if f.startswith('.'): continue self.graph.parse('config/%s' % f, format='n3') self.graph.bind('', ROOM) # not working self.graph.bind('rdf', RDF) # config graph is too noisy; maybe make it a separate resource #masterGraph.patch(Patch(addGraph=self.graph)) def serialDevices(self): return dict([(row.dev, row.board) for row in self.graph.query( """SELECT ?board ?dev WHERE { ?board :device ?dev; a :ArduinoBoard . }""", initNs={'': ROOM})])
def generateCorpusGraph(ann_files_dir, serialization_format): g = ConjunctiveGraph() g.bind("chartex", "http://chartex.org/chartex-schema#") g.bind("crm", "http://www.cidoc-crm.org/rdfs/cidoc-crm-english-label#") for f in os.listdir(ann_files_dir): annotationFile = '' if f.endswith('.ann'): docid = os.path.splitext(f)[0] f_path = os.path.join(ann_files_dir,f) ctxt = URIRef("http://example.com/graph/" + docid) graph = Graph(g.store, ctxt) for t in generateDocumentGraph(f_path).triples((None,None,None)): graph.add(t) if serialization_format: return g.serialize(format=serialization_format) else: return g
def get_graph(self): if not self.uri: raise URINotSetException() g = ConjunctiveGraph(identifier=self.uri) for ns in self.namespaces: g.bind(ns, self.namespaces[ns]) #add global prefix bindings for ns in self.urihelper.namespaces: g.bind(ns, self.urihelper.namespaces[ns]) # Add type(s) for type in self.types: g.add((self.uri, self.namespaces['rdf']['type'], type)) for triple in self.triples: g.add((self.uri, triple[0], triple[1])) return g
def get_graph(self): if self.items and self.items_rdfobjects: g = ConjunctiveGraph() # add bindings from the first graph: ns_list = self.items_rdfobjects[self.items[0]].namespaces for prefix in ns_list: g.bind(prefix, ns_list[prefix]) #add global prefix bindings for ns in self.uh.namespaces: g.bind(ns, self.uh.namespaces[ns]) for item in self.items_rdfobjects: for s,p,o in self.items_rdfobjects[item].list_triples(): g.add((s,p,o)) self.items_rdfobjects[item].altered = False return g else: return ""
def graph(self): g = ConjunctiveGraph() for prefix in self.__prefixes: g.bind(prefix, self.__prefixes[prefix]) variables = {} def nodify(elm): if is_variable(elm): if not (elm in variables): elm_node = BNode(elm) variables[elm] = elm_node return variables[elm] else: if elm == 'a': return RDF.type elif elm.startswith('"'): return Literal(elm.lstrip('"').rstrip('"')) else: try: return float(elm) except ValueError: return URIRef(elm) nxg = nx.Graph() for (s, p, o) in self._tps: nxg.add_nodes_from([s, o]) nxg.add_edge(s, o) contexts = dict([(str(index), c) for (index, c) in enumerate(nx.connected_components(nxg))]) for (s, p, o) in self._tps: s_node = nodify(s) o_node = nodify(o) p_node = nodify(p) context = None for uid in contexts: if s in contexts[uid]: context = str(uid) g.get_context(context).add((s_node, p_node, o_node)) return g
def generateCorpusGraph(ann_files_dir, serialization_format, smush=True): """Remember, generateDocumentGraph does same_as smushing at the individual document level""" g = ConjunctiveGraph() g.bind("chartex", "http://chartex.org/chartex-schema#") g.bind("crm", "http://www.cidoc-crm.org/rdfs/cidoc-crm-english-label#") for f in os.listdir(ann_files_dir): annotationFile = '' if f.endswith('.ann'): docid = os.path.splitext(f)[0] f_path = os.path.join(ann_files_dir,f) ctxt = URIRef("http://chartex.org/document-graphid/" + docid) graph = Graph(g.store, ctxt) for t in generateDocumentGraph(f_path, smush=smush).triples((None,None,None)): graph.add(t) if serialization_format: return g.serialize(format=serialization_format) else: return g
def add_file_to_vocab_status(vocabprefix, properties, addHasFormat=True): vocabdir = os.path.join(ag.vocabulariesdir, vocabprefix) vocabstatusfile = os.path.join(vocabdir, "status.rdf") vocaburi = "http://vocab.ox.ac.uk/%s"%vocabprefix if not 'name' in properties or not properties['name'] or not 'path' in properties or not properties['path']: return False if not 'uri' in properties or not properties['uri']: properties['uri'] = URIRef("http://vocab.ox.ac.uk/%s/%s"%(vocabprefix, properties['name'])) if not 'format' in properties or not properties['format']: # get mimetype of file mt = None if os.path.isfile(vocabfile): if check_rdf(vocabfile): properties['format'] = 'application/rdf+xml' else: mt1 = mimetypes.guess_type(vocabfile) if mt1[0]: properties['format'] = mt1[0] else: properties['format'] = get_file_mimetype(vocabfile) graph = Graph() if os.path.isfile(vocabstatusfile): graph.parse(vocabstatusfile) else: return False for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) if addHasFormat: graph.add((URIRef(vocaburi), namespaces['dcterms']['hasFormat'], URIRef(properties['uri']))) if properties['format']: graph.add((URIRef(properties['uri']), namespaces['dcterms']['format'], Literal(properties['format']))) if os.path.isfile(properties['path']): graph.add((URIRef(properties['uri']), namespaces['nfo']['fileUrl'], Literal('file://%s'%properties['path']))) graph.add((URIRef(properties['uri']), namespaces['nfo']['fileName'], Literal(properties['name']))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(vocabstatusfile, 'w', 'utf-8') f.write(rdf_str) f.close() return True
def get_fragment(self, **kwargs): """ Return a complete fragment. :param gp: :return: """ gen, namespaces, plan = self.get_fragment_generator(**kwargs) graph = ConjunctiveGraph() [graph.bind(prefix, u) for (prefix, u) in namespaces] [graph.add((s, p, o)) for (_, s, p, o) in gen] return graph
def __init__(self, conn, name): user = irods.irodsUser(conn, name) self.id = user.getId() self.username = id self.email = "" self.address = "" self.tel = "" self.realName = "" self.description = "" self.flags = {} umd = user.getUserMetadata() for u in umd: if u[0] == 'rdf': # try to parse try: g = ConjunctiveGraph() except: continue for (key, val) in NS.iteritems(): g.bind(key, val) data = StringInputSource(umd[1]) try: if umd[2]: g.parse(data, umd[2]) else: g.parse(data) me = NS['demo']['users/%s'] % self.id for (p, o) in g.predicate_objects(me): if predicateMap.has_key(p): setattr(self, predicateMap[p], str(o)) except: # rdf exists, could parse, but is broken raise elif u[0] in self.simpleFields: setattr(self, u[0], u[1]) elif u[0] == 'flags': # should be a {} of flag : [obj, obj] # xxx pass
def properties_rdf_generator(): for prop in properties: observedprop, obsunit, typeofuri, xsdclass, uriprefix = prop.values( )[0] if observedprop: #.../station/ZORRO2/NO2/15022011/10 uri = RESOURCE_URI + 'prop/' + uriprefix #Initialization of graph ssn = Namespace("http://purl.oclc.org/NET/ssnx/ssn#") dc = Namespace("http://purl.org/dc/elements/1.1/") owl = Namespace("http://www.w3.org/2002/07/owl#") store = IOMemory() g = ConjunctiveGraph(store=store) g.bind("ssn", ssn) g.bind("dc", dc) g.bind("owl", owl) cpr = URIRef(uri) gpr = Graph(store=store, identifier=cpr) #Add data to the graph gpr.add((cpr, dc['description'], prop.keys()[0])) if typeofuri == 'Class': gpr.add((cpr, RDF.type, URIRef(observedprop))) else: gpr.add((cpr, owl["sameAs"], URIRef(observedprop))) gpr.add((cpr, RDF.type, ssn['Property'])) #print g.serialize(format="pretty-xml") insertGraph(g=gpr, sparql=VIRTUOSO_URL, resourceuri=RESOURCE_URI)
def updateObservationProps(stationcod, typeslist): for obstype in typeslist: uri = RESOURCE_URI + 'station/' + stationcod #Initialization of the graph ssn = Namespace("http://purl.oclc.org/NET/ssnx/ssn#") store = IOMemory() g = ConjunctiveGraph(store=store) g.bind("ssn", ssn) cpr = URIRef(uri) gpr = Graph(store=store, identifier=cpr) #Add data to the graph gpr.add((cpr, RDF.type, ssn['Sensor'])) gpr.add((cpr, ssn['observes'], RESOURCE_URI + 'prop/' + obstype)) #Update RDF print uri + ' | ' + obstype insertGraph(g=gpr, sparql=VIRTUOSO_URL, resourceuri=RESOURCE_URI)
def populate_ontology(): ont_path = path_kg + 'traffic_ontology.xml' metadata = pd.read_csv(path_src + 'trafficMetaData.csv', sep=',') g = ConjunctiveGraph() g.load(ont_path) g.add((URIRef(base_uri), RDF.type, OWL.Ontology)) g.bind("owl", OWL) g.bind("rdf", RDF) g.bind("rdfs", RDFS) # g.bind("city", base_uri) # populate from metadata: [Path, from[name], to[name], from[has[street]], to[has[street]]] populate_from_metadata(metadata, g) poi = parse_log() for entry in poi: point = entry[0][0].split('_')[0] + "_" + entry[0][0].split('_')[1] metadata_entry = metadata[metadata['REPORT_ID'] == int(entry[0][0].split('_')[2])] address_id = metadata_entry[point + '_NAME'].values[0] poi_list = entry[0][1] for tmp_poi in poi_list: # generate an id for the poi tmp_poi_id = str(abs(hash(point + '_' + str(address_id) + '_' + tmp_poi))) g.add((base_uri[tmp_poi_id], RDF.type, base_uri['Point_of_interest'])) g.add((base_uri[tmp_poi_id], RDF.type, base_uri[tmp_poi[0].upper() + tmp_poi[1:]])) g.add((base_uri[tmp_poi_id], base_uri['locatedAt'], base_uri[str(address_id)])) simple_sequence = [] events = pd.read_csv(path_processed + 'events.csv') mapping = pd.read_csv(path_processed + 'mapping.csv').T.to_dict() for k, v in mapping.iteritems(): g.add((base_uri[v['Unnamed: 0']], base_uri['occursAt'], base_uri[str(v['occursAt'])])) g.add((base_uri[v['Unnamed: 0']], RDF.type, base_uri[v['type']])) for e in events['Id']: simple_sequence.append(e) with open(path_processed + 'sequence.txt', "wb") as seq_file: seq_file.write(','.join(simple_sequence)) g.serialize(path_kg + 'traffic_individuals.xml', format='xml')
def serialize_graph(g, format=TURTLE, frame=None, skolem=True): if skolem: cg = skolemize(g) else: cg = ConjunctiveGraph() cg.__iadd__(g) context = build_graph_context(g) if format == TURTLE: for prefix, uri in g.namespaces(): if prefix in context: cg.bind(prefix, uri) return cg.serialize(format='turtle') ted_nquads = cg.serialize(format='nquads') ld = jsonld.from_rdf(ted_nquads) if frame is not None: ld = jsonld.frame(ld, {'context': context, '@type': str(frame)}) ld = jsonld.compact(ld, context) return json.dumps(ld, indent=3, sort_keys=True)
def dcdict2rdfpy(dc_dict): """Convert a DC dictionary into an RDF Python object.""" ark_prefix = 'ark: ark:' uri = URIRef('') # Create the RDF Python object. rdf_py = ConjunctiveGraph() # Set DC namespace definition. DC = Namespace('http://purl.org/dc/elements/1.1/') # Get the ark for the subject URI from the ark identifier. for element_value in dc_dict['identifier']: if element_value['content'].startswith(ark_prefix): uri = URIRef( element_value['content'].replace( ark_prefix, 'info:ark' ) ) # Bind the prefix/namespace pair. rdf_py.bind('dc', DC) # Get the values for each element in the ordered DC elements. for element_name in DC_ORDER: element_value_list = dc_dict.get(element_name, []) # Add the values to the RDF object. for element_value in element_value_list: # Handle URL values differently. if 'http' in element_value['content'] and ' ' not in element_value['content']: rdf_py.add(( uri, DC[element_name], URIRef(element_value['content']) )) else: rdf_py.add(( uri, DC[element_name], Literal(element_value['content']) )) return rdf_py
class Store: def __init__(self): self.graph = ConjunctiveGraph() if os.path.exists(storefn): self.graph.load(storeuri, format="n3") self.graph.bind("dc", DC) self.graph.bind("foaf", FOAF) self.graph.bind("imdb", IMDB) self.graph.bind("rev", "http://purl.org/stuff/rev#") def save(self): self.graph.serialize(storeuri, format="n3") def who(self, who=None): if who is not None: name, email = (r_who.match(who).group(1), r_who.match(who).group(2)) self.graph.add( (URIRef(storeuri), DC["title"], Literal(title % name))) self.graph.add( (URIRef(storeuri + "#author"), RDF.type, FOAF["Person"])) self.graph.add( (URIRef(storeuri + "#author"), FOAF["name"], Literal(name))) self.graph.add( (URIRef(storeuri + "#author"), FOAF["mbox"], Literal(email))) self.save() else: return self.graph.objects(URIRef(storeuri + "#author"), FOAF["name"]) def new_movie(self, movie): movieuri = URIRef("http://www.imdb.com/title/tt%s/" % movie.movieID) self.graph.add((movieuri, RDF.type, IMDB["Movie"])) self.graph.add((movieuri, DC["title"], Literal(movie["title"]))) self.graph.add((movieuri, IMDB["year"], Literal(int(movie["year"])))) self.save() def new_review(self, movie, date, rating, comment=None): review = BNode( ) # @@ humanize the identifier (something like #rev-$date) movieuri = URIRef("http://www.imdb.com/title/tt%s/" % movie.movieID) self.graph.add( (movieuri, REV["hasReview"], URIRef("%s#%s" % (storeuri, review)))) self.graph.add((review, RDF.type, REV["Review"])) self.graph.add((review, DC["date"], Literal(date))) self.graph.add((review, REV["maxRating"], Literal(5))) self.graph.add((review, REV["minRating"], Literal(0))) self.graph.add((review, REV["reviewer"], URIRef(storeuri + "#author"))) self.graph.add((review, REV["rating"], Literal(rating))) if comment is not None: self.graph.add((review, REV["text"], Literal(comment))) self.save() def movie_is_in(self, uri): return (URIRef(uri), RDF.type, IMDB["Movie"]) in self.graph