def get_vocabulary(self): from model.vocabulary import Vocabulary q = '''PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dct: <http://purl.org/dc/terms/> PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT * WHERE { ?s a skos:ConceptScheme ; dct:title ?t ; dct:description ?d . OPTIONAL {?s dct:creator ?c } OPTIONAL {?s dct:created ?cr } OPTIONAL {?s dct:modified ?m } OPTIONAL {?s owl:versionInfo ?v } }''' for r in self.g.query(q): v = Vocabulary(self.vocab_id, r['s'], r['t'], r['d'], r['c'], r['cr'], r['m'], r['v'], [], None, None) q2 = '''PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT * WHERE { ?s skos:hasTopConcept ?tc . ?tc skos:prefLabel ?pl . }''' # add the top concepts to the Vocabulary class instance v.hasTopConcepts = [(x['tc'], x['pl']) for x in self.g.query(q2)] # sort the top concepts by prefLabel v.hasTopConcepts.sort(key=lambda tup: tup[1]) return v
def get_vocabulary(self): from model.vocabulary import Vocabulary result = self.g.query('''PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dct: <http://purl.org/dc/terms/> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?s ?title ?description ?creator ?created ?modified ?version ?hasTopConcept ?topConceptLabel WHERE {{ {{ ?s a skos:ConceptScheme . ?s skos:prefLabel ?title . }} UNION {{ ?s a skos:ConceptScheme . ?s dct:title ?title . MINUS {{ ?s skos:prefLabel ?prefLabel }} }} UNION {{ ?s a skos:ConceptScheme . ?s rdfs:label ?title . MINUS {{ ?s skos:prefLabel ?prefLabel }} MINUS {{ ?s dct:title ?prefLabel }} }} OPTIONAL {{ ?s dct:description ?description }} OPTIONAL {{ ?s dct:creator ?creator }} OPTIONAL {{ ?s dct:created ?created }} OPTIONAL {{ ?s dct:modified ?modified }} OPTIONAL {{ ?s owl:versionInfo ?version }} OPTIONAL {{ ?s skos:hasTopConcept ?hasTopConcept . ?hasTopConcept skos:prefLabel ?topConceptLabel . }} }}''') title = None description = None creator = None created = None modified = None version = None topConcepts = [] for r in result: self.uri = str(r['s']) if title is None: title = r['title'] if description is None: description = r['description'] if creator is None: creator = r['creator'] if created is None: created = r['created'] if modified is None: modified = r['modified'] if version is None: version = r['version'] if r['hasTopConcept'] and r['topConceptLabel'] is not None: topConcepts.append((r['hasTopConcept'], r['topConceptLabel'])) v = Vocabulary( self.vocab_id, self.uri, title, description, creator, created, modified, version, topConcepts ) # sort the top concepts by prefLabel v.hasTopConcepts = topConcepts if v.hasTopConcepts: v.hasTopConcepts.sort(key=lambda tup: tup[1]) v.conceptHierarchy = self.get_concept_hierarchy() return v