def agent_lod(request, agent_id): agents = EconomicAgent.objects.filter(id=agent_id) if not agents: return HttpResponse({}, mimetype='application/json') agent = agents[0] subject_assocs = agent.all_is_associates() object_assocs = agent.all_has_associates() 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, mimetype='application/json')
def agent_relationship_inv_lod(request, agent_assoc_id): aa = AgentAssociation.objects.filter(id=agent_assoc_id) if not aa: return HttpResponse({}, mimetype='application/json') else: agent_association = aa[0] 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() ref = URIRef(instance_abbrv + ":agent-relationship-inv-lod/" + str(agent_association.id) + "/") inv_ref = URIRef(instance_abbrv + ":agent-relationship-lod/" + str(agent_association.id) + "/") ref_object = URIRef(instance_abbrv + ":agent-lod/" + str(agent_association.is_associate.id) + "/") ref_subject = URIRef(instance_abbrv + ":agent-lod/" + str(agent_association.has_associate.id) + "/") property_name = camelcase_lower( agent_association.association_type.inverse_label) ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) store.add((ref, RDF.type, vf_ns["Relationship"])) store.add((ref, vf_ns["subject"], ref_subject)) store.add((ref, vf_ns["object"], ref_object)) store.add((ref, vf_ns["relationship"], ref_relationship)) store.add((ref, OWL.inverseOf, inv_ref)) ser = store.serialize(format='json-ld', context=context, indent=4) return HttpResponse(ser, mimetype='application/json')
def agent_relationship_inv_lod(request, agent_assoc_id): aa = AgentAssociation.objects.filter(id=agent_assoc_id) if not aa: return HttpResponse({}, mimetype='application/json') else: agent_association = aa[0] 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() ref = URIRef(instance_abbrv + ":agent-relationship-inv-lod/" + str(agent_association.id) + "/") inv_ref = URIRef(instance_abbrv + ":agent-relationship-lod/" + str(agent_association.id) + "/") ref_object = URIRef(instance_abbrv + ":agent-lod/" + str(agent_association.is_associate.id) + "/") ref_subject = URIRef(instance_abbrv + ":agent-lod/" + str(agent_association.has_associate.id) + "/") property_name = camelcase_lower(agent_association.association_type.inverse_label) ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) store.add((ref, RDF.type, vf_ns["Relationship"])) store.add((ref, vf_ns["subject"], ref_subject)) store.add((ref, vf_ns["object"], ref_object)) store.add((ref, vf_ns["relationship"], ref_relationship)) store.add((ref, OWL.inverseOf, inv_ref)) ser = store.serialize(format='json-ld', context=context, indent=4) return HttpResponse(ser, mimetype='application/json')
def agent_lod(request, agent_id): agents = EconomicAgent.objects.filter(id=agent_id) if not agents: return HttpResponse({}, mimetype='application/json') agent = agents[0] subject_assocs = agent.all_is_associates() object_assocs = agent.all_has_associates() 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, mimetype='application/json')
def agent_relationship_type_lod(request, agent_assoc_type_name): #import pdb; pdb.set_trace() aats = AgentAssociationType.objects.all() agent_assoc_type = None for aat in aats: if camelcase_lower(aat.label) == agent_assoc_type_name: agent_assoc_type = aat inverse = False elif camelcase_lower(aat.inverse_label) == agent_assoc_type_name: agent_assoc_type = aat inverse = True if not agent_assoc_type: return HttpResponse({}, mimetype='application/json') path, instance_abbrv, context, store, vf_ns = get_lod_setup_items() if inverse: property_name = camelcase_lower(agent_assoc_type.inverse_label) inverse_property_name = camelcase_lower(agent_assoc_type.label) label = agent_assoc_type.inverse_label else: property_name = camelcase_lower(agent_assoc_type.label) inverse_property_name = camelcase_lower(agent_assoc_type.inverse_label) label = agent_assoc_type.label ref = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) inv_ref = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + inverse_property_name) store.add((ref, RDF.type, RDF.Property)) store.add((ref, SKOS.prefLabel, Literal(label, lang="en"))) store.add((ref, OWL.inverseOf, inv_ref)) ser = store.serialize(format='json-ld', context=context, indent=4) return HttpResponse(ser, mimetype='application/json')
def agent_jsonld(request): #test = "{'@context': 'http://json-ld.org/contexts/person.jsonld', '@id': 'http://dbpedia.org/resource/John_Lennon', 'name': 'John Lennon', 'born': '1940-10-09', 'spouse': 'http://dbpedia.org/resource/Cynthia_Lennon' }" #test = '{ "@id": "http://nrp.webfactional.com/accounting/agent-lod/1", "@type": "Person", "vf:label": { "@language": "en", "@value": "Bob Haugen" } }' #return HttpResponse(test, mimetype='application/json') #mport pdb; pdb.set_trace() path, instance_abbrv, context, store, vf_ns = get_lod_setup_items() agent_types = AgentType.objects.all() #import pdb; pdb.set_trace() for at in agent_types: #if at.name != "Person" and at.name != "Organization" and at.name != "Group" and at.name != "Individual": if at.name != "Person" and at.name != "Group" and at.name != "Individual": class_name = camelcase(at.name) #ref = URIRef(at_ns[class_name]) ref = URIRef(instance_abbrv + ":agent-type-lod/" + class_name) store.add((ref, RDF.type, OWL.Class)) store.add((ref, SKOS.prefLabel, Literal(class_name, lang="en"))) if at.party_type == "individual": store.add((ref, RDFS.subClassOf, vf_ns.Person)) else: store.add((ref, RDFS.subClassOf, vf_ns.Group)) aa_types = AgentAssociationType.objects.all() #import pdb; pdb.set_trace() for aat in aa_types: property_name = camelcase_lower(aat.label) inverse_property_name = camelcase_lower(aat.inverse_label) ref = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) store.add((ref, RDF.type, RDF.Property)) store.add((ref, SKOS.prefLabel, Literal(aat.label, lang="en"))) #inverse = BNode() #store.add((ref, OWL.inverseOf, inverse)) #store.add((inverse, RDFS.label, Literal(aat.inverse_label, lang="en"))) if property_name != inverse_property_name: inv_ref = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + inverse_property_name) store.add((inv_ref, RDF.type, RDF.Property)) store.add( (inv_ref, SKOS.prefLabel, Literal(aat.inverse_label, lang="en"))) store.add((ref, OWL.inverseOf, inv_ref)) store.add((inv_ref, OWL.inverseOf, ref)) #import pdb; pdb.set_trace() associations = AgentAssociation.objects.filter(state="active") agents = [assn.is_associate for assn in associations] agents.extend([assn.has_associate for assn in associations]) agents = list(set(agents)) for agent in agents: 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.name != agent.nick: # store.add((ref, FOAF.nick, Literal(agent.nick, lang="en"))) #if agent.photo_url: # store.add((ref, vf_ns["image"], agent.photo_url)) for a in associations: ref = URIRef(instance_abbrv + ":agent-relationship-lod/" + str(a.id) + "/") inv_ref = URIRef(instance_abbrv + ":agent-relationship-inv-lod/" + str(a.id) + "/") ref_subject = URIRef(instance_abbrv + ":agent-lod/" + str(a.is_associate.id) + "/") ref_object = URIRef(instance_abbrv + ":agent-lod/" + str(a.has_associate.id) + "/") property_name = camelcase_lower(a.association_type.label) inv_property_name = camelcase_lower(a.association_type.inverse_label) ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) inv_ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + inv_property_name) store.add((ref, RDF.type, vf_ns["Relationship"])) store.add((ref, vf_ns["subject"], ref_subject)) store.add((ref, vf_ns["object"], ref_object)) store.add((ref, vf_ns["relationship"], ref_relationship)) store.add((inv_ref, RDF.type, vf_ns["Relationship"])) store.add((inv_ref, vf_ns["object"], ref_subject)) store.add((inv_ref, vf_ns["subject"], ref_object)) store.add((inv_ref, vf_ns["relationship"], inv_ref_relationship)) ser = store.serialize(format='json-ld', context=context, indent=4) #import pdb; pdb.set_trace() #import json #data = json.loads(ser) #simplyframe(data) #return HttpResponse(json.dumps(data, indent=4), mimetype='application/json') return HttpResponse(ser, mimetype='application/json')
def agent_jsonld(request): #test = "{'@context': 'http://json-ld.org/contexts/person.jsonld', '@id': 'http://dbpedia.org/resource/John_Lennon', 'name': 'John Lennon', 'born': '1940-10-09', 'spouse': 'http://dbpedia.org/resource/Cynthia_Lennon' }" #test = '{ "@id": "http://nrp.webfactional.com/accounting/agent-lod/1", "@type": "Person", "vf:label": { "@language": "en", "@value": "Bob Haugen" } }' #return HttpResponse(test, mimetype='application/json') #mport pdb; pdb.set_trace() path, instance_abbrv, context, store, vf_ns = get_lod_setup_items() agent_types = AgentType.objects.all() #import pdb; pdb.set_trace() for at in agent_types: #if at.name != "Person" and at.name != "Organization" and at.name != "Group" and at.name != "Individual": if at.name != "Person" and at.name != "Group" and at.name != "Individual": class_name = camelcase(at.name) #ref = URIRef(at_ns[class_name]) ref = URIRef(instance_abbrv + ":agent-type-lod/" +class_name) store.add((ref, RDF.type, OWL.Class)) store.add((ref, SKOS.prefLabel, Literal(class_name, lang="en"))) if at.party_type == "individual": store.add((ref, RDFS.subClassOf, vf_ns.Person)) else: store.add((ref, RDFS.subClassOf, vf_ns.Group)) aa_types = AgentAssociationType.objects.all() #import pdb; pdb.set_trace() for aat in aa_types: property_name = camelcase_lower(aat.label) inverse_property_name = camelcase_lower(aat.inverse_label) ref = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) store.add((ref, RDF.type, RDF.Property)) store.add((ref, SKOS.prefLabel, Literal(aat.label, lang="en"))) #inverse = BNode() #store.add((ref, OWL.inverseOf, inverse)) #store.add((inverse, RDFS.label, Literal(aat.inverse_label, lang="en"))) if property_name != inverse_property_name: inv_ref = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + inverse_property_name) store.add((inv_ref, RDF.type, RDF.Property)) store.add((inv_ref, SKOS.prefLabel, Literal(aat.inverse_label, lang="en"))) store.add((ref, OWL.inverseOf, inv_ref)) store.add((inv_ref, OWL.inverseOf, ref)) #import pdb; pdb.set_trace() associations = AgentAssociation.objects.filter(state="active") agents = [assn.is_associate for assn in associations] agents.extend([assn.has_associate for assn in associations]) agents = list(set(agents)) for agent in agents: 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.name != agent.nick: # store.add((ref, FOAF.nick, Literal(agent.nick, lang="en"))) #if agent.photo_url: # store.add((ref, vf_ns["image"], agent.photo_url)) for a in associations: ref = URIRef(instance_abbrv + ":agent-relationship-lod/" + str(a.id) + "/") inv_ref = URIRef(instance_abbrv + ":agent-relationship-inv-lod/" + str(a.id) + "/") ref_subject = URIRef(instance_abbrv + ":agent-lod/" + str(a.is_associate.id) + "/") ref_object = URIRef(instance_abbrv + ":agent-lod/" + str(a.has_associate.id) + "/") property_name = camelcase_lower(a.association_type.label) inv_property_name = camelcase_lower(a.association_type.inverse_label) ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + property_name) inv_ref_relationship = URIRef(instance_abbrv + ":agent-relationship-type-lod/" + inv_property_name) store.add((ref, RDF.type, vf_ns["Relationship"])) store.add((ref, vf_ns["subject"], ref_subject)) store.add((ref, vf_ns["object"], ref_object)) store.add((ref, vf_ns["relationship"], ref_relationship)) store.add((inv_ref, RDF.type, vf_ns["Relationship"])) store.add((inv_ref, vf_ns["object"], ref_subject)) store.add((inv_ref, vf_ns["subject"], ref_object)) store.add((inv_ref, vf_ns["relationship"], inv_ref_relationship)) ser = store.serialize(format='json-ld', context=context, indent=4) #import pdb; pdb.set_trace() #import json #data = json.loads(ser) #simplyframe(data) #return HttpResponse(json.dumps(data, indent=4), mimetype='application/json') return HttpResponse(ser, mimetype='application/json')