示例#1
0
    def toPython(self, noId=False, parent_domain=None):
        result = {'@id': self.get_new_type_id()}

        if noId:
            del result['@id']

        # Determine type
        result['@type'] = self.get_type()

        if self.get_real_parents():
            result['subPropertyOf'] = str(self.get_real_parents()[0])

        #Labels
        labels = self.get_labels(label_domain_selected=parent_domain)
        if len(labels):
            result['rdfs:label'] = labels

        #Comments
        comments = self.get_comments(comment_domain_selected=parent_domain)
        if len(comments):
            result['rdfs:comment'] = comments

        #Doamin
        domains = []
        for domain in self.graph.triples((self.uriref, RDFS.domain, None)):
            domains.append(uri2niceString(domain[2], self.graph.namespaces()))
        if len(domains):
            result['domain'] = domains

        #Ranges
        ranges = []
        for r in self.graph.triples((self.uriref, RDFS.range, None)):
            ranges.append(uri2niceString(r[2], self.graph.namespaces()))
        if len(ranges):
            result['range'] = ranges

        # OWL Version Info
        try:
            result['owl:versionInfo'] = next(
                self.graph.triples((self.uriref, OWL.versionInfo, None)))[2]
        except Exception as e:
            pass

        # VS Status
        try:
            result['vs:term_status'] = next(
                self.graph.triples((self.uriref, SW.term_status, None)))[2]
        except Exception as e:
            pass

        return result
示例#2
0
def create_identity_from_rdf_class(rdf_class, flat_definition,
                                   current_context):
    identity_dict = deepcopy(BASE_IDENTITY_POT)
    if current_context == 'pot':
        vocabulary = '{}ClassDefinitions/{}'.format(
            POT_EXPORT,
            rdf_class.get_new_type_id()[4:])
        identity_dict['@vocab'] = '{}Vocabulary/{}'.format(
            POT_EXPORT,
            rdf_class.get_new_type_id()[4:])
    else:
        vocabulary = '{}ClassDefinitions/{}'.format(
            DLI_EXPORT,
            rdf_class.get_new_type_id()[4:])
        identity_dict['@vocab'] = '{}Vocabulary/{}'.format(
            DLI_EXPORT,
            rdf_class.get_new_type_id()[4:])
    identity_dict['@classDefinition'] = vocabulary
    total_attributes = set(rdf_class.get_properties())
    for domain in total_attributes:
        key = domain.get_context_name(domain_selected=rdf_class)
        if uri2niceString(rdf_class.uriref,
                          rdf_class.namespaces()) not in flat_definition:
            identity_dict[key] = {
                '@id': domain.get_new_type_id(),
            }
            if domain.get_nested_at():
                identity_dict[key]['@nest'] = domain.get_nested_at()
        else:
            identity_dict[key] = domain.get_new_type_id()
    return {'@context': identity_dict}
示例#3
0
 def get_comments(self, comment_domain_selected=None):
     comments = {}
     domains = [
         uri2niceString(x[2], self.graph.namespaces())
         for x in self.graph.triples((self.uriref, RDFS.domain, None))
     ]
     for comment in self.graph.triples((self.uriref, DLI.comment, None)):
         if not isinstance(comment[2], BNode):
             continue
         try:
             comment_domain = list(
                 self.graph.triples((comment[2], DLI.domain, None)))[0]
         except IndexError:
             continue
         comment_text = list(
             self.graph.triples((comment[2], RDFS.comment, None)))[0]
         if isinstance(comment_domain[2], Literal):
             comment_domain = str(comment_domain[2])
         if comment_domain not in domains:
             continue
         if comment_domain_selected and str(
                 comment_domain_selected) != comment_domain:
             not_found = True
             parents = [
                 RDFClass(x[2], self.graph) for x in self.graph.triples((
                     comment_domain_selected.uriref, RDFS.subClassOf, None))
             ]
             while len(parents):
                 tParents = []
                 for parent in parents:
                     if parent.uriref == self.uriref:
                         continue
                     if str(parent) == comment_domain:
                         not_found = False
                         break
                     tParents += [
                         RDFClass(x[2], self.graph)
                         for x in self.graph.triples((parent.uriref,
                                                      RDFS.subClassOf,
                                                      None))
                     ]
                 parents = tParents.copy()
             if not_found:
                 continue
         comments[comment_text[2].language] = str(comment_text[2])
     if not comments:
         for comment in self.graph.triples(
             (self.uriref, DLI.comment, None)):
             if not isinstance(comment[2], BNode):
                 continue
             try:
                 comment_domain = list(
                     self.graph.triples((comment[2], DLI.domain, None)))[0]
                 continue
             except IndexError:
                 comment_text = list(
                     self.graph.triples(
                         (comment[2], RDFS.comment, None)))[0]
                 comments[comment_text[2].language] = str(comment_text[2])
     return comments
示例#4
0
 def get_restrictions(self):
     restriction = {}
     for item in self.graph.triples((self.uriref, XSD.restriction, None)):
         for bnode in self.graph.triples((item[2], None, None)):
             restriction[uri2niceString(bnode[1],
                                        self.graph.namespaces())] = bnode[2]
     return restriction
示例#5
0
 def get_type(self):
     try:
         rdf_type = uri2niceString(
             next(self.graph.triples((self.uriref, RDF.type, None)))[2],
             self.graph.namespaces())
     except Exception as e:
         return None
     return rdf_type
示例#6
0
 def get_new_type_id(self):
     parents_path = ''
     if self.get_real_parents():
         real_parent = self.get_real_parents()[0]
         while real_parent:
             parents_path = real_parent.title() + '/' + parents_path
             if real_parent.get_real_parents():
                 real_parent = real_parent.get_real_parents()[0]
             else:
                 real_parent = None
     name = uri2niceString(self.uriref, self.namespaces())
     uri, name = name.split(':')
     return uri + ':' + parents_path + self.title()
示例#7
0
    def toPython(self):
        result = {'@id': self.get_new_type_id(), '@type': self.get_type()}

        #Parents
        if self.get_real_parents():
            result['subClassOf'] = self.get_real_parents()[0].get_new_type_id()
        else:
            parents = list(
                self.graph.triples((self.uriref, RDFS.subClassOf, None)))
            not_emplty_parents = []
            for p in parents:
                if uri2niceString(
                        p[2]
                ) != 'https://standards.oftrust.net/ontologies/pot.jsonld#':
                    not_emplty_parents.append(p)
            parents = not_emplty_parents
            if len(parents) > 1:
                result['subClassOf'] = [x.get_new_type_id() for x in parents]
            elif len(parents) == 1:
                result['subClassOf'] = uri2niceString(parents[0][2],
                                                      self.graph.namespaces())

        #Labels
        labels = self.get_labels()
        if len(labels):
            result['rdfs:label'] = labels

        #Comments
        comments = {}
        for comment in self.graph.triples((self.uriref, RDFS.comment, None)):
            if type(comment[2]) != Literal:
                continue
            comments[comment[2].language] = str(comment[2])

        if len(comments):
            result['rdfs:comment'] = comments

        return result
示例#8
0
 def __str__(self):
     return uri2niceString(self.uriref, self.namespaces())
示例#9
0
 def context(self):
     name = uri2niceString(self.uriref, self.namespaces())
     uri, name = name.split(':')
     return uri
示例#10
0
 def title(self):
     name = uri2niceString(self.uriref, self.namespaces())
     uri, name = name.split(':')
     return name