本文整理汇总了Python中rdflib.URIRef类的典型用法代码示例。如果您正苦于以下问题:Python URIRef类的具体用法?Python URIRef怎么用?Python URIRef使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了URIRef类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_synthesis_contributors
def get_synthesis_contributors(self, id_only=True):
# author of important extracts
from .idea_content_link import Extract
from .auth import AgentProfile
from .post import Post
from sqlalchemy.sql.functions import count
local_uri = AssemblQuadStorageManager.local_uri()
discussion_storage = \
AssemblQuadStorageManager.discussion_storage_name()
idea_uri = URIRef(self.uri(local_uri))
clause = '''select distinct ?annotation where {
%s idea:includes* ?ideaP .
?annotation assembl:resourceExpressesIdea ?ideaP }'''
extract_ids = [x for (x,) in self.db.execute(
SparqlClause(clause % (
idea_uri.n3(),),
quad_storage=discussion_storage.n3()))]
r = list(self.db.query(AgentProfile.id, count(Extract.id)).join(
Post, Post.creator_id==AgentProfile.id).join(Extract).filter(
Extract.important == True, Extract.id.in_(extract_ids)))
r.sort(key=lambda x: x[1], reverse=True)
if id_only:
return [AgentProfile.uri_generic(a) for (a, ce) in r]
else:
ids = [a for (a, ce) in r]
order = {id: order for (order, id) in enumerate(ids)}
agents = self.db.query(AgentProfile).filter(AgentProfile.id.in_(ids)).all()
agents.sort(key=lambda a: order[a.id])
return agents
示例2: process_journal
def process_journal(records, writer, mappings):
record, fields = majority_vote(records, ('Journal',), mappings)
if record.get('issn'):
uri = URIRef('urn:issn:%s' % record['issn'])
graph_uri = URIRef('/graph/issn/%s' % record['issn'])
elif record.get('x-nlm-ta'):
uri = URIRef('/id/journal/%s' % sluggify(record['x-nlm-ta']))
graph_uri = URIRef('/graph/journal/%s' % sluggify(record['x-nlm-ta']))
elif record.get('name'):
uri = URIRef('/id/journal/%s' % sluggify(record['name']))
graph_uri = URIRef('/graph/journal/%s' % sluggify(record['name']))
else:
sys.stderr.write("Unidentifiable: %s" % record)
return
for id, _ in fields['id']:
mappings['id'][id] = uri
mappings['journal'][uri] = graph_uri.split('/', 3)[-1]
writer.send((uri, RDF.type, FABIO.Journal, graph_uri))
for key, predicate in JOURNAL_DATA_PROPERTIES:
if key in record:
writer.send((uri, predicate, Literal(record[key]), graph_uri))
if isinstance(record.get('publisher'), URIRef):
writer.send((uri, DCTERMS.publisher, record['publisher'], graph_uri))
示例3: view
def view(rtype, rid):
if '.' in rid:
rid, suffix = rid.rsplit('.', 1)
else:
suffix = None
path = rtype + '/' + rid
uri = URIRef(app.config['RESOURCE_BASE'] + path)
#if template:
services = app.config['SERVICES']
rq = render_template("queries/%s.rq" % rtype,
prefixes=RQ_PREFIXES, this=uri.n3(), services=services)
fmt = _conneg_format(suffix)
if fmt == 'rq':
return rq, 200, {'Content-Type': 'text/plain'}
res = run_query(app.config['ENDPOINT'], rq)
#else:
# url = data_base + path + '.n3'
# res = requests.get(url)
graph = to_graph(res.content)
this = graph.resource(uri)
if fmt in ('html', 'xhtml'):
return render_template(rtype + '.html',
path=path, this=this, curies=graph.qname)
else:
headers = {'Content-Type': MIMETYPES.get(fmt) or 'text/plain'}
fmt = {'rdf': 'xml', 'ttl': 'turtle'}.get(fmt) or fmt
return graph.serialize(format=fmt), 200, headers
示例4: discussion_as_graph
def discussion_as_graph(self, discussion_id):
self.ensure_discussion_storage(None)
from assembl.models import Discussion
d_storage_name = self.discussion_storage_name()
d_graph_iri = URIRef(self.discussion_graph_iri())
v = get_virtuoso(self.session, d_storage_name)
discussion_uri = URIRef(
Discussion.uri_generic(discussion_id, self.local_uri()))
subjects = list(v.query(
"""SELECT DISTINCT ?s WHERE {
?s assembl:in_conversation %s }""" % (discussion_uri.n3())))
subjects.append([discussion_uri])
# print len(subjects)
cg = ConjunctiveGraph(identifier=d_graph_iri)
for (s,) in subjects:
# Absurdly slow. DISTINCT speeds up a lot, but I get numbers.
for p, o in v.query(
'SELECT ?p ?o WHERE { graph %s { %s ?p ?o }}' % (
d_graph_iri.n3(), s.n3())):
cg.add((s, p, o))
for (s, o, g) in v.query(
'''SELECT ?s ?o ?g WHERE {
GRAPH ?g {?s catalyst:expressesIdea ?o } .
?o assembl:in_conversation %s }''' % (discussion_uri.n3())):
cg.add((s, CATALYST.expressesIdea, o, g))
# TODO: Add roles
return cg
示例5: discussion_as_graph
def discussion_as_graph(self, discussion_id):
from assembl.models import Discussion, AgentProfile
local_uri = self.local_uri()
discussion = Discussion.get(discussion_id)
d_storage_name = self.discussion_storage_name()
d_graph_iri = URIRef(self.discussion_graph_iri())
v = get_virtuoso(self.session, d_storage_name)
discussion_uri = URIRef(
Discussion.uri_generic(discussion_id, local_uri))
subjects = [s for (s,) in v.query(
"""SELECT DISTINCT ?s WHERE {
?s assembl:in_conversation %s }""" % (discussion_uri.n3()))]
subjects.append(discussion_uri)
participant_ids = list(discussion.get_participants(True))
profiles = {URIRef(AgentProfile.uri_generic(id, local_uri))
for id in participant_ids}
subjects.extend(profiles)
# add pseudo-accounts
subjects.extend((URIRef("%sAgentAccount/%d" % (local_uri, id))
for id in participant_ids))
# print len(subjects)
cg = ConjunctiveGraph(identifier=d_graph_iri)
self.add_subject_data(v, cg, subjects)
# add relationships of non-pseudo accounts
for ((account, p, profile), g) in v.triples((None, SIOC.account_of, None)):
if profile in profiles:
cg.add((account, SIOC.account_of, profile, g))
# Tempting: simplify with this.
# cg.add((profile, FOAF.account, account, g))
for (s, o, g) in v.query(
'''SELECT ?s ?o ?g WHERE {
GRAPH ?g {?s catalyst:expressesIdea ?o } .
?o assembl:in_conversation %s }''' % (discussion_uri.n3())):
cg.add((s, CATALYST.expressesIdea, o, g))
return cg
示例6: detect_namespace
def detect_namespace(rdf):
"""Try to automatically detect the URI namespace of the vocabulary.
Return namespace as URIRef.
"""
# pick a concept
conc = rdf.value(None, RDF.type, SKOS.Concept, any=True)
if conc is None:
logging.critical(
"Namespace auto-detection failed. "
"Set namespace using the --namespace option.")
sys.exit(1)
ln = localname(conc)
ns = URIRef(conc.replace(ln, ''))
if ns.strip() == '':
logging.critical(
"Namespace auto-detection failed. "
"Set namespace using the --namespace option.")
sys.exit(1)
logging.info(
"Namespace auto-detected to '%s' "
"- you can override this with the --namespace option.", ns)
return ns
示例7: instance_view_jsonld
def instance_view_jsonld(request):
from assembl.semantic.virtuoso_mapping import AssemblQuadStorageManager
from rdflib import URIRef, ConjunctiveGraph
ctx = request.context
user_id = authenticated_userid(request) or Everyone
permissions = get_permissions(
user_id, ctx.get_discussion_id())
instance = ctx._instance
if not instance.user_can(user_id, CrudPermissions.READ, permissions):
return HTTPUnauthorized()
discussion = ctx.get_instance_of_class(Discussion)
if not discussion:
raise HTTPNotFound()
aqsm = AssemblQuadStorageManager()
uri = URIRef(aqsm.local_uri() + instance.uri()[6:])
d_storage_name = aqsm.discussion_storage_name(discussion.id)
v = get_virtuoso(instance.db, d_storage_name)
cg = ConjunctiveGraph(v, d_storage_name)
result = cg.triples((uri, None, None))
#result = v.query('select ?p ?o ?g where {graph ?g {<%s> ?p ?o}}' % uri)
# Something is wrong here.
triples = '\n'.join([
'%s %s %s.' % (uri.n3(), p.n3(), o.n3())
for (s, p, o) in result
if '_with_no_name_entry' not in o])
return aqsm.quads_to_jsonld(triples)
示例8: post
def post(self):
chan = URIRef(self.get_argument('chan'))
sub = self.settings.currentSub()
chanKey = Literal(chan.rsplit('/', 1)[1])
old = sub.get_levels().get(chanKey, 0)
sub.editLevel(chan, 0 if old else 1)
示例9: test_creation_with_unknown_ns
def test_creation_with_unknown_ns(self):
uri = 'http://localhost:8000/resource/aggregation/ton-smits-huis/454'
predicate = RDFPredicate(uri)
graph = Graph()
graph.add((URIRef(uri), FOAF.name, Literal("sjoerd")))
subject = list(graph.subjects())[0]
uri_ref = URIRef(uri)
assert uri_ref.n3() == "ns1:454"
assert predicate is not None
assert predicate.label is not None
示例10: get_service
def get_service():
g = new_graph()
me = URIRef(url_for('get_service', _external=True))
g.add((me, RDF.type, SERVICE_TYPE))
for db_resource in service_graph.subjects(RDF.type, PARTITION.Root):
db_resource = URIRef(
url_for('get_resource', rid=db_resource.replace(URI_PREFIX, ""), _external=True))
g.add((me, CONTAINMENT_LINK, db_resource))
response = make_response(g.serialize(format='turtle'))
response.headers['Content-Type'] = 'text/turtle'
return response
示例11: enterNewAnnotationMenu
def enterNewAnnotationMenu(self):
'''
Interactive input for a new annotation
'''
self.printNewAnnotationMenu()
i = 1
for year in self.yearsAnnotations:
print '{}) {}'.format(i,year["year"])
i += 1
print
year = raw_input('Table to annotate: ')
cell = raw_input('Cell to annotate: ')
author = raw_input('Author: ')
corrected = raw_input('Corrected value (leave blank if none): ')
flag = raw_input('Flag: ')
graphURI = URIRef(self.yearsAnnotations[int(year)-1]["uri"])
d2sGraphURI = graphURI.replace("cedar-project.nl", "www.data2semantics.org")
annoURI = URIRef(d2sGraphURI + '/NOORDBRABANT/' + cell)
cellURI = annoURI.replace("annotations", "data")
# Create the new annotation
query = """
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT INTO GRAPH <""" + graphURI + """>
{
<""" + annoURI + """> a oa:Annotation;
oa:annotated \"""" + str(datetime.datetime.now().strftime("%Y-%m-%d")) + """\"^^xsd:date;
oa:annotator \"""" + author + """\";
oa:generated \"""" + str(datetime.datetime.now().strftime("%Y-%m-%d")) + """\"^^xsd:date;
oa:generator <https://cedar-project.nl/tools/cedar-demo.py>;
oa:hasBody [ rdf:value \"""" + corrected + ' ' + flag + """\" ];
oa:hasTarget <""" + cellURI + """>;
oa:modelVersion <http://www.openannotation.org/spec/core/20120509.html> .
}
"""
# query = "INSERT INTO GRAPH <http://cedar-project.nl/annotations/VT_1859_01_H1> {<http://a> rdf:type <http:b>}"
print query
self.sparql.setQuery(query)
self.sparql.setReturnFormat(JSON)
self.results = self.sparql.query().convert()
示例12: check_valid_uri
def check_valid_uri(self, uri):
""" checks to see if a uri is valid """
valid = False
if isinstance(uri, str):
uri_out = False
try:
uri_test = URIRef(uri)
uri_out = uri_test.n3()
except:
# some sort of error thrown, so not valid
valid = False
if isinstance(uri_out, str):
valid = True
return valid
示例13: tableView_objectValueForTableColumn_row_
def tableView_objectValueForTableColumn_row_(self, tableView, tableColumn, row):
id = tableColumn.identifier()
uri = self.resources[row]
if id=="uri":
base =self.context
base = base.split("#", 1)[0]
uri = URIRef(uri.replace(base, "", 1)) # relativize
return uri
elif id=="label":
return self.redfoot.label(uri, "")
elif id=="comment":
return self.redfoot.comment(uri, "")
else:
return ""
示例14: term
def term(self, term, use_prefix=True):
if isinstance(term, Namespace):
term = URIRef(term)
if term is None:
return RDF.nil
elif not hasattr(term, 'n3'):
return self.term(Literal(term))
elif use_prefix and isinstance(term, URIRef):
return self.uri(term)
elif isinstance(term, Literal):
if term.datatype in (XSD.double, XSD.integer, XSD.float, XSD.boolean):
return unicode(term).lower()
elif isinstance(term, Namespace):
return unicode(term)
return term.n3()
示例15: __init__
def __init__(self, store, identifier=None, graph=None):
if graph is not None:
assert identifier is None
np = store.node_pickler
identifier = md5()
s = list(graph.triples((None, None, None)))
s.sort()
for t in s:
identifier.update("^".join((np.dumps(i) for i in t)))
identifier = URIRef("data:%s" % identifier.hexdigest())
super(GraphValue, self).__init__(store, identifier)
for t in graph:
store.add(t, context=self)
else:
super(GraphValue, self).__init__(store, identifier)