Exemplo n.º 1
0
 def language_exists(self, rdf_tag, allowed_languages):
     if hasattr(rdf_tag, 'language') and rdf_tag.language not in allowed_languages and rdf_tag.language is not None and rdf_tag.language.strip() != '':
         newlang = models.DLanguage()
         newlang.pk = rdf_tag.language
         newlang.languagename = rdf_tag.language
         newlang.isdefault = False
         newlang.save()
         return False
     return True
Exemplo n.º 2
0
    def save_concepts_from_skos(self, graph, overwrite_options=None):
        """
        given an RDF graph, tries to save the concpets to the system

        Keyword arguments: 
        overwrite_options -- 'overwrite', 'ignore', 'stage'

        """

        baseuuid = uuid.uuid4()
        allowed_languages = models.DLanguage.objects.values_list('pk',
                                                                 flat=True)

        value_types = models.DValueType.objects.all()
        skos_value_types = value_types.filter(namespace='skos')
        skos_value_types_list = skos_value_types.values_list('valuetype',
                                                             flat=True)
        dcterms_value_types = value_types.filter(namespace='dcterms')

        relation_types = models.DRelationType.objects.all()
        skos_relation_types = relation_types.filter(namespace='skos')

        # if the graph is of the type rdflib.graph.Graph
        if isinstance(graph, Graph):

            # Search for ConceptSchemes first
            for scheme, v, o in graph.triples(
                (None, RDF.type, SKOS.ConceptScheme)):
                scheme_id = self.generate_uuid_from_subject(baseuuid, scheme)
                concept_scheme = Concept({
                    'id': scheme_id,
                    'legacyoid': str(scheme),
                    'nodetype': 'ConceptScheme'
                })

                for predicate, object in graph.predicate_objects(
                        subject=scheme):
                    if str(DCTERMS) in predicate and predicate.replace(
                            DCTERMS, '') in dcterms_value_types.values_list(
                                'valuetype', flat=True):
                        if hasattr(
                                object, 'language'
                        ) and object.language not in allowed_languages:
                            newlang = models.DLanguage()
                            newlang.pk = object.language
                            newlang.languagename = object.language
                            newlang.isdefault = False
                            newlang.save()
                            allowed_languages = models.DLanguage.objects.values_list(
                                'pk', flat=True)

                        try:
                            # first try and get any values associated with the concept_scheme
                            value_type = dcterms_value_types.get(
                                valuetype=predicate.replace(DCTERMS, '')
                            )  # predicate.replace(SKOS, '') should yield something like 'prefLabel' or 'scopeNote', etc..
                            val = self.unwrapJsonLiteral(object)
                            if predicate == DCTERMS.title:
                                concept_scheme.addvalue({
                                    'id':
                                    val['value_id'],
                                    'value':
                                    val['value'],
                                    'language':
                                    object.language,
                                    'type':
                                    'prefLabel',
                                    'category':
                                    value_type.category
                                })
                                print 'Casting dcterms:title to skos:prefLabel'
                            elif predicate == DCTERMS.description:
                                concept_scheme.addvalue({
                                    'id':
                                    val['value_id'],
                                    'value':
                                    val['value'],
                                    'language':
                                    object.language,
                                    'type':
                                    'scopeNote',
                                    'category':
                                    value_type.category
                                })
                                print 'Casting dcterms:description to skos:scopeNote'
                        except:
                            pass

                    if str(SKOS) in predicate:
                        if predicate == SKOS.hasTopConcept:
                            self.relations.append({
                                'source':
                                scheme_id,
                                'type':
                                'hasTopConcept',
                                'target':
                                self.generate_uuid_from_subject(
                                    baseuuid, object)
                            })

                self.nodes.append(concept_scheme)

                if len(self.nodes) == 0:
                    raise Exception('No ConceptScheme found in file.')

                # Search for Concepts
                for s, v, o in graph.triples((None, SKOS.inScheme, scheme)):
                    concept = Concept({
                        'id':
                        self.generate_uuid_from_subject(baseuuid, s),
                        'legacyoid':
                        str(s),
                        'nodetype':
                        'Concept'
                    })

                    # loop through all the elements within a <skos:Concept> element
                    for predicate, object in graph.predicate_objects(
                            subject=s):
                        if str(SKOS) in predicate:
                            if hasattr(
                                    object, 'language'
                            ) and object.language not in allowed_languages:
                                newlang = models.DLanguage()
                                newlang.pk = object.language
                                newlang.languagename = object.language
                                newlang.isdefault = False
                                newlang.save()
                                allowed_languages = models.DLanguage.objects.values_list(
                                    'pk', flat=True)

                            relation_or_value_type = predicate.replace(
                                SKOS, ''
                            )  # this is essentially the skos element type within a <skos:Concept> element (eg: prefLabel, broader, etc...)

                            if relation_or_value_type in skos_value_types_list:
                                value_type = skos_value_types.get(
                                    valuetype=relation_or_value_type)
                                val = self.unwrapJsonLiteral(object)
                                concept.addvalue({
                                    'id':
                                    val['value_id'],
                                    'value':
                                    val['value'],
                                    'language':
                                    object.language,
                                    'type':
                                    value_type.valuetype,
                                    'category':
                                    value_type.category
                                })
                            elif predicate == SKOS.broader:
                                self.relations.append({
                                    'source':
                                    self.generate_uuid_from_subject(
                                        baseuuid, object),
                                    'type':
                                    'narrower',
                                    'target':
                                    self.generate_uuid_from_subject(
                                        baseuuid, s)
                                })
                            elif predicate == SKOS.narrower:
                                self.relations.append({
                                    'source':
                                    self.generate_uuid_from_subject(
                                        baseuuid, s),
                                    'type':
                                    relation_or_value_type,
                                    'target':
                                    self.generate_uuid_from_subject(
                                        baseuuid, object)
                                })
                            elif predicate == SKOS.related:
                                self.relations.append({
                                    'source':
                                    self.generate_uuid_from_subject(
                                        baseuuid, s),
                                    'type':
                                    relation_or_value_type,
                                    'target':
                                    self.generate_uuid_from_subject(
                                        baseuuid, object)
                                })

                    self.nodes.append(concept)

            # insert and index the concpets
            with transaction.atomic():
                for node in self.nodes:
                    if overwrite_options == 'overwrite':
                        node.save()
                    elif overwrite_options == 'ignore':
                        try:
                            models.Concept.objects.get(pk=node.id)
                        except:
                            node.save()
                    else:  # 'stage'
                        pass

                # insert the concept relations
                for relation in self.relations:
                    newrelation = models.Relation.objects.get_or_create(
                        conceptfrom_id=relation['source'],
                        conceptto_id=relation['target'],
                        relationtype_id=relation['type'])

                # need to index after the concepts and relations have been entered into the db
                # so that the proper context gets indexed with the concept
                for node in self.nodes:
                    node.index()

            return self
        else:
            raise Exception(
                'graph argument should be of type rdflib.graph.Graph')