def get_concept(self): concept_uri = self.request.values.get("uri") q = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dct: <http://purl.org/dc/terms/> SELECT ?predicate # 0 ?object # 1 ?predicateLabel # 2 ?objectLabel # 3 ?prefLabel # 4 ?lang # 5 WHERE {{ <{concept_uri}> ?predicate ?object . OPTIONAL {{ ?predicate rdfs:label ?predicateLabel . FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "") BIND(lang(?predicateLabel) AS ?lang) }} OPTIONAL {{ ?object skos:prefLabel|rdfs:label ?objectLabel . # Don't filter prefLabel language FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") BIND(lang(?objectLabel) AS ?lang) }} }} """.format(concept_uri=concept_uri, language=self.language) result = self.gr.query(q) assert result, "FILE source is unable to query concepts for {}".format( self.request.values.get("uri")) prefLabel = None related_objects = {} for r in result: predicateUri = str(r[0]) # Special case for prefLabels if predicateUri == "http://www.w3.org/2004/02/skos/core#prefLabel": predicateLabel = "Multilingual Labels" preflabel_lang = r[1].language if hasattr(r[1], "language") else "en" # Use default language or no language prefLabel as primary if (not prefLabel and not preflabel_lang) or (preflabel_lang == self.language): prefLabel = str(r[1]) # Set current language prefLabel # # Omit current language string from list (remove this if we want to show all) # if preflabel_lang in ['', self.language]: # continue # Apend language code to prefLabel literal related_object = "{} ({})".format(str(r[1]), preflabel_lang) related_objectLabel = None else: predicateLabel = (str(r[2]) if r[2] is not None else vocprez.app.make_title(str(r[0]))) if not str(r[1]).startswith("http"): related_object = str(r[1]) related_objectLabel = None else: related_object = str(r[1]) related_objectLabel = (str(r[3]) if r[3] is not None else vocprez.app.make_title(str(r[1]))) relationship_dict = related_objects.get(predicateUri) if relationship_dict is None: relationship_dict = {"label": predicateLabel, "objects": {}} related_objects[predicateUri] = relationship_dict relationship_dict["objects"][related_object] = related_objectLabel related_objects = OrderedDict([( predicate, { "label": related_objects[predicate]["label"], "objects": OrderedDict([(key, related_objects[predicate]["objects"][key]) for key in sorted(related_objects[predicate] ["objects"].keys())]), }, ) for predicate in sorted(related_objects.keys())]) return Concept( vocab_id=self.vocab_id, uri=concept_uri, prefLabel=prefLabel, related_objects=related_objects, semantic_properties=None, source=self, )
def get_concept(self, vocab_uri, uri): vocab = g.VOCABS[vocab_uri] q = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT * WHERE { <xxxx> a skos:Concept ; ?p ?o . FILTER(!isLiteral(?o) || lang(?o) = "en" || lang(?o) = "") OPTIONAL { ?p skos:prefLabel|rdfs:label ?ppl . FILTER(!isLiteral(?ppl) || lang(?ppl) = "en" || lang(?ppl) = "") } OPTIONAL { ?o skos:prefLabel|rdfs:label ?opl . FILTER(!isLiteral(?opl) || lang(?opl) = "en" || lang(?opl) = "") } } """.replace("xxxx", uri) pl = None d = None c = None s = { "provenance": None, "source": None, "wasDerivedFrom": None, } annotation_types = { "http://www.opengis.net/def/metamodel/ogc-na/status": "Status", "http://www.w3.org/2004/02/skos/core#note": "Note", "http://www.w3.org/2004/02/skos/core#changeNote": "Change Note", "http://www.w3.org/2004/02/skos/core#historyNote": "History Note", "http://www.w3.org/2004/02/skos/core#editorialNote": "Editorial Note", "http://www.w3.org/2004/02/skos/core#scopeNote": "Scope Note", "http://www.w3.org/2004/02/skos/core#example": "Example", "http://www.w3.org/2004/02/skos/core#altLabel": "Alternative Label", "http://www.w3.org/2004/02/skos/core#hiddenLabel": "Hidden Label", "http://www.w3.org/2004/02/skos/core#notation": "Notation", # DCTERMS "http://purl.org/dc/terms/conformsTo": "Conforms To", "http://purl.org/dc/terms/abstract": "Abstract", "http://purl.org/dc/terms/identifier": "Identifier", "http://purl.org/dc/terms/audience": "Audience", "http://purl.org/dc/terms/publisher": "Publisher", "http://purl.org/dc/terms/isRequiredBy": "Is Required By", "http://purl.org/dc/terms/replaces": "Replaces", "http://purl.org/dc/terms/provenance": "Provenance", "http://purl.org/dc/terms/requires": "Requires", "http://purl.org/dc/terms/language": "Language", "http://purl.org/dc/terms/description": "Description", "http://purl.org/dc/terms/title": "Title", # DC "http://purl.org/dc/elements/1.1/contributor": "Contributor", "http://purl.org/dc/elements/1.1/coverage": "Coverage", "http://purl.org/dc/elements/1.1/creator": "Creator", "http://purl.org/dc/elements/1.1/date": "Date", "http://purl.org/dc/elements/1.1/description": "Description", "http://purl.org/dc/elements/1.1/format": "Format", "http://purl.org/dc/elements/1.1/identifier": "Identifier", "http://purl.org/dc/elements/1.1/language": "Language", "http://purl.org/dc/elements/1.1/publisher": "Publisher", "http://purl.org/dc/elements/1.1/relation": "Relation", "http://purl.org/dc/elements/1.1/rights": "Rights", "http://purl.org/dc/elements/1.1/source": "Source", "http://purl.org/dc/elements/1.1/subject": "Subject", "http://purl.org/dc/elements/1.1/title": "Title", "http://purl.org/dc/elements/1.1/type": "Type", # RDFS "http://www.w3.org/2000/01/rdf-schema#label": "Label", "http://www.w3.org/2000/01/rdf-schema#comment": "Comment", "http://www.w3.org/2000/01/rdf-schema#seeAlso": "See Also", } annotations = {} agent_types = { 'http://purl.org/dc/terms/contributor': "Contributor", 'http://purl.org/dc/terms/creator': "Creator", 'http://purl.org/dc/terms/publisher': "Publisher", } agent = {} related_instance_types = { 'http://www.w3.org/2004/02/skos/core#exactMatch': "Exact Match", 'http://www.w3.org/2004/02/skos/core#closeMatch': "Close Match", 'http://www.w3.org/2004/02/skos/core#broadMatch': "Broad Match", 'http://www.w3.org/2004/02/skos/core#narrowMatch': "Narrow Match", 'http://www.w3.org/2004/02/skos/core#broader': "Broader", 'http://www.w3.org/2004/02/skos/core#narrower': "Narrower", "http://www.w3.org/2004/02/skos/core#related": "Related", "http://purl.org/dc/terms/relation": "Relation", } related_instances = {} other_property_types = { "http://purl.org/dc/terms/date": "Date", "http://purl.org/dc/terms/source": "Source", "http://purl.org/dc/terms/references": "References", "http://purl.org/dc/terms/isVersionOf": "Is Version Of", "http://purl.org/dc/terms/isReplacedBy": "Is Replaced By", "http://purl.org/dc/terms/modified": "Date Modified", "http://purl.org/dc/terms/issued": "Date Issued", "http://purl.org/dc/terms/format": "Format", "http://purl.org/dc/terms/isReferencedBy": "Is Referenced By", "http://purl.org/dc/terms/license": "License", "http://purl.org/dc/terms/rights": "Rights", "http://purl.org/dc/terms/isPartOf": "Is Part Of", "http://purl.org/dc/terms/coverage": "Coverage", "http://purl.org/dc/terms/medium": "Medium", "http://purl.org/dc/terms/available": "Date Available", "http://purl.org/dc/terms/spatial": "Spatial Coverage", "http://purl.org/dc/terms/valid": "Date Valid", "http://www.w3.org/2000/01/rdf-schema#subClassOf": "Sub Class Of", "http://www.w3.org/2000/01/rdf-schema#isDefinedBy": "Is Defined By", } other_properties = {} found = False for r in sparql_query(q, vocab.sparql_endpoint, vocab.sparql_username, vocab.sparql_password): prop = r["p"]["value"] val = r["o"]["value"] property_label = None if r.get("ppl") is not None: property_label = r["ppl"]["value"] object_label = None if r.get("opl") is not None: object_label = r["opl"]["value"] found = True if val == "http://www.w3.org/2004/02/skos/core#Concept": pass elif prop == "http://www.w3.org/2004/02/skos/core#prefLabel": pl = val elif prop == "http://www.w3.org/2004/02/skos/core#definition": d = val elif prop == "http://www.w3.org/2000/01/rdf-schema#comment": c = val elif prop == "http://purl.org/dc/terms/provenance": s["provenance"] = val elif prop == "http://purl.org/dc/terms/source": s["source"] = val elif prop == "http://www.w3.org/ns/prov#wasDerivedFrom": s["wasDerivedFrom"] = val elif prop in annotation_types.keys(): if property_label is None: property_label = annotation_types.get(prop) if property_label is not None: annotations[prop] = (Property(prop, property_label, val, object_label)) elif prop in related_instance_types.keys(): if property_label is None: property_label = related_instance_types.get(prop) if property_label is not None: related_instances[prop] =(Property(prop, property_label, val, object_label)) else: # other properties if val != "http://www.w3.org/2004/02/skos/core#Concept" and prop not in suppressed_properties(): if property_label is None: property_label = other_property_types.get(prop) if property_label is not None: if not prop in other_properties : other_properties[prop] = [] other_properties[prop].append((Property(prop, property_label, val, object_label))) if not found: return None from vocprez.model.concept import Concept if not d: d = c return Concept( vocab_uri, uri, pl, d, related_instances.values(), annotations.values(), other_properties=other_properties.values() )
def get_concept(self, uri): vocab = g.VOCABS[self.vocab_uri] # q = """ # PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> # PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # # SELECT * # WHERE {{ # <{concept_uri}> a skos:Concept ; # ?p ?o . # # OPTIONAL {{ # GRAPH ?predicateGraph {{?p rdfs:label ?predicateLabel .}} # FILTER(lang(?predicateLabel) = "{language}" || lang(?predicateLabel) = "") # }} # OPTIONAL {{ # ?o skos:prefLabel ?objectLabel . # FILTER(?prefLabel = skos:prefLabel || lang(?objectLabel) = "{language}" || lang(?objectLabel) = "") # # Don't filter prefLabel language # }} # }} # """.format( # concept_uri=uri, language=self.language # ) q = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT * WHERE {{ <{concept_uri}> a skos:Concept ; ?p ?o . OPTIONAL {{ ?o skos:prefLabel ?ropl . }} }} """.format(concept_uri=uri, language=self.language) pl = None d = None s = { "provenance": None, "source": None, "wasDerivedFrom": None, } annotation_types = { "http://www.opengis.net/def/metamodel/ogc-na/status": "Status" } annotations = {} agent_types = { 'http://purl.org/dc/terms/contributor': "Contributor", 'http://purl.org/dc/terms/creator': "Creator", 'http://purl.org/dc/terms/publisher': "Publisher", } agent = {} related_instance_types = { 'http://www.w3.org/2004/02/skos/core#exactMatch': "Exact Match", 'http://www.w3.org/2004/02/skos/core#closeMatch': "Close Match", 'http://www.w3.org/2004/02/skos/core#broadMatch': "Broad Match", 'http://www.w3.org/2004/02/skos/core#narrowMatch': "Narrow Match", 'http://www.w3.org/2004/02/skos/core#broader': "Broader", 'http://www.w3.org/2004/02/skos/core#narrower': "Narrower" } related_instances = {} found = False for r in sparql_query(q, vocab.sparql_endpoint, vocab.sparql_username, vocab.sparql_password): found = True if r["p"][ "value"] == "http://www.w3.org/2004/02/skos/core#prefLabel": pl = r["o"]["value"] elif r["p"][ "value"] == "http://www.w3.org/2004/02/skos/core#definition": d = r["o"]["value"] elif r["p"]["value"] == "http://purl.org/dc/terms/provenance": s["provenance"] = r["o"]["value"] elif r["p"]["value"] == "http://purl.org/dc/terms/source": s["source"] = r["o"]["value"] elif r["p"]["value"] == "http://www.w3.org/ns/prov#wasDerivedFrom": s["wasDerivedFrom"] = r["o"]["value"] elif r["p"]["value"] in annotation_types.keys(): if r.get("ropl") is not None: # annotation value has a labe too annotations[r["p"]["value"]] = ( annotation_types[r["p"]["value"]], r["o"]["value"], r["ropl"]["value"]) else: # no label annotations[r["p"]["value"]] = ( annotation_types[r["p"]["value"]], r["o"]["value"]) elif r["p"]["value"] in related_instance_types.keys(): if related_instances.get(r["p"]["value"]) is None: related_instances[r["p"]["value"]] = {} related_instances[r["p"]["value"]] = { "instances": [], "label": related_instance_types[r["p"]["value"]] } related_instances[r["p"]["value"]]["instances"].append( (r["o"]["value"], r["ropl"]["value"] if r["ropl"] is not None else None)) if not found: return None # TODO: Agents # TODO: more Annotations from vocprez.model.concept import Concept return Concept(self.vocab_uri, uri, pl, d, related_instances, annotations)
def get_concept(self, uri): vocab = g.VOCABS[self.vocab_uri] q = """ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT DISTINCT * WHERE {{ <{concept_uri}> a skos:Concept ; ?p ?o . OPTIONAL {{ ?o skos:prefLabel ?ropl . }} }} """.format(concept_uri=uri) pl = None d = None s = { "provenance": None, "source": None, "wasDerivedFrom": None, } annotation_types = { "http://www.opengis.net/def/metamodel/ogc-na/status": "Status", 'http://www.w3.org/2004/02/skos/core#altLabel': "Alternative Label", 'http://www.w3.org/2004/02/skos/core#note': "Note", 'http://www.w3.org/2004/02/skos/core#scopeNote': "Scope Note", 'http://www.w3.org/2004/02/skos/core#hostryNote': "History Note", } annotations = [] agent_types = { 'http://purl.org/dc/terms/contributor': "Contributor", 'http://purl.org/dc/terms/creator': "Creator", 'http://purl.org/dc/terms/publisher': "Publisher", } agent = {} related_instance_types = { 'http://www.w3.org/2004/02/skos/core#exactMatch': "Exact Match", 'http://www.w3.org/2004/02/skos/core#closeMatch': "Close Match", 'http://www.w3.org/2004/02/skos/core#broadMatch': "Broad Match", 'http://www.w3.org/2004/02/skos/core#narrowMatch': "Narrow Match", 'http://www.w3.org/2004/02/skos/core#broader': "Broader", 'http://www.w3.org/2004/02/skos/core#narrower': "Narrower" } provenance_property_types = { "http://purl.org/pav/hasCurrentVersion": "Has Current Version", "http://purl.org/pav/version": "Version", "http://www.w3.org/2002/07/owl#deprecated": "Deprecated", "http://purl.org/pav/previousVersion": "Previous Version", "http://purl.org/dc/terms/isVersionOf": "Is Version Of", "http://purl.org/pav/authoredOn": "Authored On" } related_instances = {} other_properties = [] for r in u.sparql_query(q, vocab.sparql_endpoint, vocab.sparql_username, vocab.sparql_password): if r["p"][ "value"] == "http://www.w3.org/2004/02/skos/core#prefLabel": pl = r["o"]["value"] elif r["p"][ "value"] == "http://www.w3.org/2004/02/skos/core#definition": d = r["o"]["value"] elif r["p"]["value"] == "http://purl.org/dc/terms/provenance": s["provenance"] = r["o"]["value"] elif r["p"]["value"] == "http://purl.org/dc/terms/source": s["source"] = r["o"]["value"] elif r["p"]["value"] == "http://www.w3.org/ns/prov#wasDerivedFrom": s["wasDerivedFrom"] = r["o"]["value"] elif r["p"]["value"] in annotation_types.keys(): annotations.append( Property(r["p"]["value"], annotation_types[r["p"]["value"]], r["o"]["value"])) elif r["p"]["value"] in related_instance_types.keys(): if related_instances.get(r["p"]["value"]) is None: related_instances[r["p"]["value"]] = {} related_instances[r["p"]["value"]] = { "instances": [], "label": related_instance_types[r["p"]["value"]] } related_instances[r["p"]["value"]]["instances"].append( (r["o"]["value"], r["ropl"]["value"] if r.get("ropl") is not None else None)) elif r["p"]["value"] in provenance_property_types.keys(): other_properties.append( Property(r["p"]["value"], provenance_property_types[r["p"]["value"]], r["o"]["value"].rstrip(".0"))) elif r["p"][ "value"] == "http://www.w3.org/2004/02/skos/core#altLabel": other_properties.append( Property(r["p"]["value"], "Alternative Label", r["o"]["value"])) # TODO: Agents # TODO: more Annotations if pl is None: return None from vocprez.model.concept import Concept return Concept(self.vocab_uri, uri, pl, d, related_instances, annotations=annotations, other_properties=other_properties)