def descInfo(self, m): predicates = [ self.dc.description, self.dct.description, self.rdfs.comment, self.dct.abstract ] for p in predicates: try: v = m.find_statements( RDF.Statement(RDF.Uri(self.spec_url), p, None)) if v.current(): return v.current().object.literal_value['string'] except: pass for p in predicates: try: v = m.find_statements( RDF.Statement(RDF.Uri(self.ns_url), p, None)) if v.current(): return v.current().object.literal_value['string'] except: pass for p in predicates: v = m.find_statements(RDF.Statement(None, p, None)) if v.current(): return v.current().object.literal_value['string'] return ""
def posts(self, *args): alltags = Set() for arg in args: if isinstance(arg, Tag): alltags.add(arg) url = "http://del.icio.us/rss/" + self.user if len(alltags) > 0: url += "/" + "+".join([str(tag) for tag in alltags]) model = RDF.Model() parser = RDF.Parser() try: parser.parse_string_into_model(model, get_url_contents(url), RDF.Uri("http://foo")) posts = [ RSSTagPost(model, p.subject) for p in model.find_statements( RDF.Statement( None, RDF.Uri( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), RDF.Uri("http://purl.org/rss/1.0/item"))) ] for post in posts: post.user = self return posts except: return []
def __init__(self, graph): """Initialize a graph with the given name. Parameters: ----------- graph : str The name of the graph. """ self.graph = graph # Load the formats map self.formats = util.loadFormatsMap() # Set up the temporary model which accumulates triples when addDataset # is called self.model = None # Synchronize the newly added namespaces to the Graph object # for faster referencing self.graph.ns = NAMESPACES # Add fixed statements # # Note: These are inserted regardless of whether or not they already # exist prov = self.graph.ns['prov'] owl = self.graph.ns['owl'] self.graph.insert(RDF.Uri(prov + 'wasRevisionOf'), RDF.Uri(owl + 'inverseOf'), RDF.Uri(prov + 'hadRevision'))
def action_uris(self): return [ node.uri for node in self.model.get_sources( # class should be type. fix midicodes.n3 too. RDF.Uri("http://www.w3.org/2000/01/rdf-schema#Class"), RDF.Uri("http://projects.bigasterisk.com/room/action")) ]
def addStatement(model, s, p, o): # Assume subject is a URI string if it is not an RDF.Node if type(s) is not RDF.Node: s_node = RDF.Uri(s) else: s_node = s # Assume predicate is a URI string if it is not an RDF.Node if type(p) is not RDF.Node: p_node = RDF.Uri(p) else: p_node = p # Assume object is a literal if it is not an RDF.Node if type(o) is not RDF.Node: o_node = RDF.Node(o) else: o_node = o statement = RDF.Statement(s_node, p_node, o_node) if statement is None: raise Exception("new RDF.Statement failed") model.add_statement(statement)
def get_option(self, model, subject): option = Option(subject.uri) statement = RDF.Statement(subject, RDF.Uri("http://okfnpad.org/flow/0.1/node"), None) uri = None for s in model.find_statements(statement): if s.object.is_resource(): uri = s.object.uri option.node = uri statement = RDF.Statement(subject, RDF.Uri("http://okfnpad.org/flow/0.1/text"), None) for s in model.find_statements(statement): if s.object.is_literal(): option.text = s.object.literal_value['string'] statement = RDF.Statement(subject, RDF.Uri("http://okfnpad.org/flow/0.1/query"), None) for s in model.find_statements(statement): if s.object.is_literal(): option.query = s.object.literal_value['string'] return option
def _generateStatement(subjectNode, attributeNode, objectValue): """ Internal function to generate a statement from its own attributes! @param RDFNode subjectNode : The subject node for this statement. @param RDFNode attributeNode : The attribute node for this statement. @param undef object : The object where the type should be determined! @return Statement : @author """ #Object node might be another resource or a literal if(isinstance(objectValue, Resource)): if(objectValue.isBlank): #It is a blank resource objectNode = RDF.Node(blank = objectValue.blankIdentifier) else: #It is a normal resource with uri objectNode = RDF.Uri(objectValue.uri) else: #It is a literal - Get info about it literalValues = Resource.castTypeToLiteral(objectValue) #Check what the datatype is (might be None for plain literals) if(literalValues[1] is not None): dt = RDF.Uri(literalValues[1]) objectNode = RDF.Node(literal = literalValues[0], datatype = dt) else: objectNode = RDF.Node(literal = literalValues[0]) #Now return the newly created statement return RDF.Statement(subjectNode, attributeNode, objectNode)
def get_node(self, model, subject, is_question): node = Node(subject.uri, is_question) statement = RDF.Statement(subject, RDF.Uri("http://okfnpad.org/flow/0.1/text"), None) for s in model.find_statements(statement): if s.object.is_literal(): node.text = s.object.literal_value['string'] statement = RDF.Statement(subject, RDF.Uri("http://okfnpad.org/flow/0.1/query"), None) for s in model.find_statements(statement): if s.object.is_literal(): node.query = s.object.literal_value['string'] statement = RDF.Statement( subject, RDF.Uri("http://okfnpad.org/flow/0.1/option"), None) for s in model.find_statements(statement): self.get_options(node, model, s.object) return node
def docTerms(self, category, list, m): """ A wrapper class for listing all the terms in a specific class (either Properties, or Classes. Category is 'Property' or 'Class', list is a list of term names (strings), return value is a chunk of HTML. """ doc = "" nspre = self.spec_pre for t in list: if (t.startswith(self.spec_url)) and (len( t[len(self.spec_url):].split("/")) < 2): term = t #t = t.split(self.spec_url[-1])[1] t = t.replace(self.spec_url, "") curie = "%s:%s" % (nspre, t) term_uri = RDF.Uri("%s%s" % (self.spec_url, t)) elif (t.startswith(self.ns_url)) and (len( t[len(self.ns_url):].split("/")) < 2): #t = t.split(self.ns_url[-1])[1] t = t.replace(self.ns_url, "") term = RDF.Uri("%s%s" % (self.ns_url, t)) curie = "%s:%s" % (nspre, t) term_uri = term else: if t.startswith("http://"): term = t curie = self.getShortName(t) t = self.getAnchor(t) else: term = self.spec_ns[t] curie = "%s:%s" % (nspre, t) try: term_uri = term.uri except: term_uri = term doc += """<div class="specterm" id="term_%s">\n<h3>%s: %s</h3>\n""" % ( t, category, curie) doc += """<p style="font-family:monospace; font-size:0.em;">URI: <a href="%s">%s</a></p>""" % ( term_uri, term_uri) label, comment = self.get_rdfs(m, term) status = self.get_status(m, term) doc += "<p><em>%s</em> - %s </p>" % (label, comment) terminfo = "" if category == 'Property': terminfo += self.owlInfo(term, m) terminfo += self.rdfsPropertyInfo(term, m) if category == 'Class': terminfo += self.rdfsClassInfo(term, m) if category == 'Instance': terminfo += self.rdfsInstanceInfo(term, m) if (len(terminfo) > 0): #to prevent empty list (bug #882) doc += "\n<dl>%s</dl>\n" % terminfo doc += self.htmlDocInfo(t) doc += "<p style=\"float: right; font-size: small;\">[<a href=\"#sec-glance\">back to top</a>]</p>\n\n" doc += "\n\n</div>\n\n" return doc
def test_can_insert_a_triple(graph): graph.clear() assert graph.size() == 0 graph.insert(s=RDF.Uri('http://example.org/#Foo'), p=RDF.Uri('http://example.org/#isA'), o=RDF.Uri('http://name.org/Foo')) assert graph.size() == 1
def r_to_l(element): if isinstance(element, rdflib.URIRef): RDF.Uri(element) elif isinstance(element, rdflib.Literal): if element.datatype: kwargs = dict(datatype=RDF.Uri(element.datatype)) else: kwargs = dict(language=element.language) RDF.Node(literal=str(element), **kwargs) elif isinstance(element, rdflib.BNode): RDF.Node(blank=str(element))
def get_alt_options(self, node, model, subject): statement = RDF.Statement(subject, None, None) for s in model.find_statements(statement): if str(s.predicate.uri).startswith( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_'): statement2 = RDF.Statement( s.object, RDF.Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), RDF.Uri("http://okfnpad.org/flow/0.1/Option")) for ss in model.find_statements(statement2): node.add_options(self.get_option(model, ss.subject))
def test_graph_can_list_its_graphs(graph): # clear_graphs(graph) graph.name = "canadd" graph.create_graph() graph.insert(s=RDF.Uri('http://example.org/#Foo'), p=RDF.Uri('http://example.org/#isA'), o=RDF.Uri('http://name.org/Foo')) assert 'canadd' in graph.graphs()
def export_model(self, uri, model): for attr in self.attribute_db.list_keys(): if attr.startswith("__"): continue if attr.startswith(VOLATILE_NS): continue values = self.resolve_list(uri, attr, follow_inheritence=False) for v in values: statement=RDF.Statement(RDF.Uri(uri), RDF.Uri(attr), RDF.Node(v)) model.add_statement(statement)
def get_stock_sources(self, user_uri): sources = [] query_statement = RDF.Statement(RDF.Uri(user_uri), RDF.Uri(NS_CATALOG + "source"), None) for statement in self._model.find_statements(query_statement): source_uri = str(statement.object) source = self.get_source(user_uri=user_uri, source_uri=source_uri) sources.append(source) return sources
def get_options(self, node, model, option): statement = RDF.Statement( option, RDF.Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), None) for s in model.find_statements(statement): if s.object.is_resource() and s.object.uri == RDF.Uri( "http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt"): self.get_alt_options(node, model, s.subject) elif s.object.is_resource() and s.object.uri == RDF.Uri( "http://okfnpad.org/flow/0.1/Option"): node.add_options(self.get_option(model, s.subject))
def state_audio_metadata(ts, file_uri, metadata): # it doesn't matter if we add the same statement multiple times, so don't # bother checking if it exists (this could cause problems if we have eg. # different artist names with the same musicbrainz_artistid # artist artist_uri = 'http://zitgist.com/music/artist/' + metadata[ 'musicbrainz_artistid'][0] artist_uri = RDF.Node(RDF.Uri(artist_uri)) ts.state(artist_uri, ns['rdf'].type, ns['mo'].MusicArtist) ts.state(artist_uri, ns['foaf'].name, RDF.Node(metadata['artist'][0])) # album album_uri = 'http://zitgist.com/music/record/' + metadata[ 'musicbrainz_albumid'][0] album_uri = RDF.Node(RDF.Uri(album_uri)) ts.state(album_uri, ns['rdf'].type, ns['mo'].Record) ts.state(album_uri, ns['dc'].title, RDF.Node(metadata['album'][0])) if 'musicbrainz_albumartistid' in metadata: album_artist_uri = 'http://zitgist.com/music/artist/' + metadata[ 'musicbrainz_albumartistid'][0] album_artist_uri = RDF.Node(RDF.Uri(album_artist_uri)) else: album_artist_uri = artist_uri # XXX does album artist have an MO term? ts.state(album_uri, ns['foaf'].maker, album_artist_uri) # track track_uri = 'http://zitgist.com/music/track/' + metadata[ 'musicbrainz_trackid'][0] track_uri = RDF.Node(RDF.Uri(track_uri)) ts.state(track_uri, ns['rdf'].type, ns['mo'].Track) ts.state(album_uri, ns['mo'].track, track_uri) ts.state(track_uri, ns['foaf'].maker, artist_uri) ts.state(track_uri, ns['dc'].title, RDF.Node(metadata['title'][0])) # some artists get clever and have craaaaazzy track numbers if 'tracknumber' in metadata: tn = metadata['tracknumber'][0] else: tn = '0' tn = RDF.Node(literal=tn, datatype=ns['xs'].int.uri) ts.state(track_uri, ns['mo'].track_number, tn) # the particular file ts.state(file_uri, ns['mo'].encodes, track_uri)
def prepareTerm(self, term): """Prepare an RDF term to be added to an RDF Model. A term is either: - An RDF.Node - An RDF.Uri - A string, which is either: - A binding string (e.g., '?s') - A URI reference (e.g., 'rdf:type') - A URI (e.g., http://...) - A literal If the term is a str with a namespace prefix that the Interface knows about then that namespace will be interpolated prior to making the term into an RDF.Uri. Arguments: term : str | RDF.Node | RDF.Uri The RDF term (subject, predicate, or object) to be prepared. Returns: str | RDF.Node | RDF.Uri """ if isinstance(term, RDF.Uri) or isinstance(term, RDF.Node): return term elif isinstance(term, str) or isinstance(term, unicode): # Binding?: Do nothing if term.startswith('?'): return term # Conver 'http...' strings to RDF.Uri if term.startswith('http'): return RDF.Uri(term) parts = term.split(':') # URI if len(parts) > 1 and parts[0] in self.graph.ns: prefix = self.graph.ns[parts[0]] other_parts = parts[1:] term = RDF.Uri(prefix + ':'.join(other_parts)) else: # Literal term = RDF.Node(term) else: raise Exception( "Invalid term sent can't be prepared: (type is %s) Term is `%s`." % (type(term), term)) return term
def serialize(self): """ Serializes the given resource to triple statements from the Redland RDF library. This is done by using the model manager and its attribute wrapper. @return : @author """ #First set yourself the class type if it is not Resource or already set if(type(self).__name__ != "Resource"): if(not hasattr(self, "type")): self.type = ListenerList([], self, "type") ownClassUri = self.modelManager.classMapper.getClassResource(type(self).__name__) #Do this typing stuff and check if the type is not a resource found = False for r in self.type: try: if(r.uri == ownClassUri): found = True break except: #Do nothing pass if(not found): #Add your own class type self.type.append(Resource(self.modelManager, uri = ownClassUri)) #Initialize an array to store the results in results = [] #Iterate over all variables and store the values for (key, value) in list(self.__dict__.items()): #Only store the values of "public" variables if(not key.startswith("_")): #Subject and attribute are always the of normal type resource if(not self.isBlank): subjectNode = RDF.Uri(self.uri) else: subjectNode = RDF.Node(blank=self.blankIdentifier) attributeNode = RDF.Uri(self.modelManager.attrMapper.getAttributeUri(key)) #Object node might be a list or a skalar if(value is None): #Do nothing pass elif(isinstance(value, list)): for subValue in value: results.append(self._generateStatement(subjectNode, attributeNode, subValue)) else: results.append(Resource._generateStatement(subjectNode, attributeNode, value)) #Now return the results return results
def _get_linked_work(self, predicate, object): """ Return linked work for a source or post (Entry type defined by predicate) """ query_statement = RDF.Statement(None, RDF.Uri(predicate), RDF.Uri(object)) for statement, context in self._model.find_statements_context(query_statement): entry_type = self._model.get_targets(statement.subject, RDF.Uri(NS_RDF + 'type')).current() # we don't have User entries yet, so type is None occassionally if entry_type is not None and entry_type.uri == RDF.Uri(Work.rdf_type): return Work.from_model(self._model, str(statement.subject)) return None
def get_nodes(self, model): statement = RDF.Statement( None, RDF.Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), None) for s in model.find_statements(statement): if s.subject.is_resource() and s.object.is_resource(): #if s.object.uri == RDF.Uri('http://okfnpad.org/flow/0.1/Answer'): # self.answers[str(s.subject.uri)] = self.get_node(model, s.subject, False) if s.object.uri == RDF.Uri( 'http://okfnpad.org/flow/0.1/Question'): self.questions[str(s.subject.uri)] = self.get_node( model, s.subject, True)
def posts(): url = "http://del.icio.us/rss/" model = RDF.Model() parser = RDF.Parser() parser.parse_string_into_model(model, get_url_contents(url), RDF.Uri("http://foo")) posts = [ RSSTagPost(model, p.subject) for p in model.find_statements( RDF.Statement( None, RDF.Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ), RDF.Uri("http://purl.org/rss/1.0/item"))) ] return posts
def test_can_delete_triples(graph): graph.clear() assert graph.size() == 0 graph.insert(s=RDF.Uri('http://example.org/#Foo'), p=RDF.Uri('http://example.org/#isA'), o=RDF.Uri('http://name.org/Foo')) assert graph.size() == 1 payload_data = u"<%s> %s %s" % (RDF.Uri('http://example.org/#Foo'), "?p", "?o") graph.delete_data(payload=payload_data) assert graph.size() == 0
def get_root(self, model): statement = RDF.Statement(None, RDF.Uri("http://okfnpad.org/flow/0.1/root"), None) for s in model.find_statements(statement): if s.object.is_resource(): self.root = str(s.object.uri)
def __init__(self, results, model_path): super(ParsedVocabulary, self).__init__(results) if model_path is not None: model = realpath(model_path) else: model = realpath(dirname(__file__) + "/../rdf/rdf-schema.rdf") parser_model = get_parser(model) model_stream = parser_model.parse_as_stream("file://%s" % model) self.results['s'] = {} self.results['p'] = {} self.results['o'] = {} self.results['sum'] = {} self.schema = None for statement in model_stream: if str(statement.predicate) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"\ and str(statement.object) == "http://www.w3.org/2002/07/owl#Ontology": self.schema = statement.subject #FIXME isDefinedBy nur aufnehmen, wenn es aus selbem namespace wie resource kommt if str(statement.predicate ) == "http://www.w3.org/2000/01/rdf-schema#isDefinedBy": self.results['s'][str(statement.subject)] = 0 self.results['p'][str(statement.subject)] = 0 self.results['o'][str(statement.subject)] = 0 self.results['sum'][str(statement.subject)] = 0 if self.schema is None: self.schema = RDF.Uri("file://%s" % model)
def parse_handle_to_model(self, rooted=False, storage=None, parse_format='turtle', context=None, **kwargs): '''Parse self.handle into RDF model self.model.''' if storage is None: # store RDF model in memory for now storage = new_storage() if self.model is None: self.model = RDF.Model(storage) if self.model is None: raise CDAOError("new RDF.model failed") model = self.model self.rooted = rooted parser = RDF.Parser(name=parse_format) if parser is None: raise Exception('Failed to create RDF.Parser for MIME type %s' % mime_type) if 'base_uri' in kwargs: base_uri = kwargs['base_uri'] else: base_uri = RDF.Uri(string="file://"+os.path.abspath(self.handle.name)) statements = parser.parse_string_as_stream(self.handle.read(), base_uri) for s in statements: model.append(s) return self.parse_model(model, context=context)
def posts(self,*args): alltags = Set() extratags = "" user = None for arg in args: if isinstance(arg,Tag): alltags.add(arg) if isinstance(arg,User): user = arg if len(alltags)>0: extratags = "+"+"+".join([str(tag) for tag in alltags]) if user is not None: url = "http://del.icio.us/rss/"+str(user)+"/"+self.name+extratags else: url = "http://del.icio.us/rss/tag/"+self.name model = RDF.Model() parser = RDF.Parser() try: parser.parse_string_into_model(model,get_url_contents(url),RDF.Uri("http://foo")) posts = [RSSTagPost(model,p.subject,self) for p in model.find_statements(RDF.Statement(None,RDF.Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),RDF.Uri("http://purl.org/rss/1.0/item")))] if user is not None: for post in posts: post.user = user return posts except: return []
def voidify(self): """present stats in VoID (http://www.w3.org/TR/void/)""" serializer = self.get_serializer() ########################### # VoID dataset definition # ########################### void_model = RDF.Model() void_dataset_uri = self.namespaces.get_namespace( 'ls_void') + "?source=" + self.rdf_stats.uri #TODO: URI encode ? void_dataset_entity = RDF.Uri(void_dataset_uri) void_model.append( RDF.Statement(void_dataset_entity, self.namespaces.get_rdf_namespace("rdf").type, self.namespaces.get_rdf_namespace("void").Dataset)) #self.generate_general_void_metadata(void_model, void_dataset_entity) #Number of triples number_of_triples_node = RDF.Node( literal=str(self.rdf_stats.get_no_of_triples()), datatype=self.namespaces.get_rdf_namespace("xsd").integer.uri) void_model.append( RDF.Statement(void_dataset_entity, self.namespaces.get_rdf_namespace("void").triples, number_of_triples_node)) # voidify results from custom stats for stat in lodstats.stats.stats_to_do: stat.voidify(void_model, void_dataset_entity) return serializer.serialize_model_to_string(void_model)
def addFiles(model, baseUri, inputPath): for root, subFolders, files in os.walk(inputPath): rootUri = root[len(inputPath) + 1:] sys.stderr.write("rootUri: " + rootUri + "\n") files = [f for f in files if not f[0] == '.'] subFolders[:] = [d for d in subFolders if not d[0] == '.'] for fileName in files: f = open(os.path.join(root, fileName), 'r') if (len(rootUri) > 0): fileUri = RDF.Uri(baseUri + rootUri + "/" + fileName) else: fileUri = RDF.Uri(baseUri + fileName) sys.stderr.write("Add Resource: " + fileUri.__str__() + "\n") markdownString = f.read() model.append( RDF.Statement( fileUri, RDF.Uri("http://ns.ontowiki.net/SysOnt/Site/content"), RDF.Node(markdownString, datatype=RDF.Uri( "http://ns.ontowiki.net/SysOnt/Markdown")))) model.append( RDF.Statement( fileUri, RDF.Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), RDF.Uri("http://xmlns.com/foaf/0.1/Document"))) model.append( RDF.Statement( fileUri, RDF.Uri("http://www.w3.org/2000/01/rdf-schema#label"), fileName))
def do_lightlevel(self, light_uri, level): prefix = "http://projects.bigasterisk.com/room/lights/" lightname = str(light_uri)[len(prefix):] assert (level.literal_value['datatype'] == RDF.Uri( "http://www.w3.org/2001/XMLSchema#float")) level = float(level.literal_value['string']) self.set_light(lightname, level)