Ejemplo n.º 1
0
    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