def agent_lod(request, agent_id):
agents = EconomicAgent.objects.filter(id=agent_id)
if not agents:
return HttpResponse({}, content_type='application/json')
agent = agents[0]
subject_assocs = agent.all_is_associates()
object_assocs = agent.all_has_associates()
from rdflib import Graph, Literal, BNode
from rdflib.namespace import FOAF, RDF, RDFS, OWL, SKOS
from rdflib.serializer import Serializer
from rdflib import Namespace, URIRef
path, instance_abbrv, context, store, vf_ns = get_lod_setup_items()
#Lynn: I made a change here for consistency. Please check and fix if needed.
ref = URIRef(instance_abbrv + ":agent-lod/" + str(agent.id) + "/")
if agent.agent_type.name == "Individual" or agent.agent_type.name == "Person":
store.add((ref, RDF.type, vf_ns.Person))
#elif agent.agent_type.name == "Organization":
# store.add((ref, RDF.type, vf_ns.Organization))
else:
at_class_name = camelcase(agent.agent_type.name)
ref_class = URIRef(instance_abbrv + ":agent-type-lod/" + at_class_name)
store.add((ref, RDF.type, ref_class))
store.add((ref, vf_ns["label"], Literal(agent.name, lang="en")))
#if agent.photo_url:
# store.add((ref, vf_ns["image"], agent.photo_url))
#if subject_assocs or object_assocs:
# store.add(( ))
if subject_assocs:
for a in subject_assocs:
obj_ref = URIRef(instance_abbrv + ":agent-relationship-lod/" + str(a.id) + "/")
property_name = camelcase_lower(a.association_type.label)
ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name)
store.add((ref, ref_relationship, obj_ref))
if object_assocs:
for a in object_assocs:
subj_ref = URIRef(instance_abbrv + ":agent-relationship-inv-lod/" + str(a.id) + "/")
inv_property_name = camelcase_lower(a.association_type.inverse_label)
inv_ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + inv_property_name)
store.add((ref, inv_ref_relationship, subj_ref))
ser = store.serialize(format='json-ld', context=context, indent=4)
return HttpResponse(ser, content_type='application/json')
#following method supplied by Niklas at rdflib-jsonld support to get the desired output for nested rdf inputs for rdflib
评论列表
文章目录