def build_restrictions(owl_property: Any) -> List[str]: """Return list of restrictions. Args: owl_property (Any): Property of entity. Can be: AnnotationPropertyClass, DataPropertyClass, ObjectPropertyClass. Returns: result_restrictions (list of str): List of property restrictions """ result_restrictions = [] for restriction_type in restriction._get_indirect_values_for_class( owl_property): # TODO try: result_restrictions.append( str( default_world._unabbreviate( base._universal_datatype_2_abbrev[restriction_type])). replace('http://www.w3.org/2001/XMLSchema#', 'xsd:')) except: result_restrictions.append( str(restriction_type).replace('XMLSchema.', 'xsd:')) return result_restrictions
def build_ranges(owl_property): """ Args: owl_property (AnnotationPropertyClass or DataPropertyClass): Property of entity. Returns: result_ranges (List[str]): Ranges of owl_property. """ result_ranges = list() for range_type in owl_range._get_indirect_values_for_class( owl_property): try: result_ranges.append( str( default_world._unabbreviate( base._universal_datatype_2_abbrev[range_type])). replace('http://www.w3.org/2001/XMLSchema#', 'xsd:')) except: result_ranges.append( str(range_type).replace('XMLSchema.', 'xsd:')) return result_ranges
def build_ranges(owl_property: Any) -> List[str]: """Return list of ranges. Args: owl_property (Any): Property of entity. Can be: AnnotationPropertyClass, DataPropertyClass. Returns: result_ranges (list of str): List of property ranges """ result_ranges = list() for range_type in owl_range._get_indirect_values_for_class(owl_property): # if isinstance(range_type, type): try: result_ranges.append( str( default_world._unabbreviate( base._universal_datatype_2_abbrev[range_type])). replace('http://www.w3.org/2001/XMLSchema#', 'xsd:')) except: result_ranges.append(str(range_type).replace('XMLSchema.', 'xsd:')) return result_ranges
def create_vocabulary_from_rdf_class(self): """Return dict of generated properties to create Vocabulary from rdf classes. Returns: vocabulary_template (dict of str: Any): Dictionary of required parameters """ # Define main Vocabulary template for class vocabulary_template = { '@context': { '@version': self.VERSION, f'{self.PREFIX}': { '@id': f'{self.export_onto_url}Vocabulary/', '@prefix': True }, 'rdf': { '@id': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', '@prefix': True }, 'rdfs': { '@id': 'http://www.w3.org/2000/01/rdf-schema#', '@prefix': True }, 'owl': { '@id': 'http://www.w3.org/2002/07/owl#', '@prefix': True }, 'vs': { '@id': 'http://www.w3.org/2003/06/sw-vocab-status/ns#', }, 'xsd': { '@id': 'http://www.w3.org/2001/XMLSchema#', '@prefix': True }, 'label': { '@id': 'rdfs:label', "@container": ['@language', '@set'] }, 'comment': { '@id': 'rdfs:comment', "@container": ['@language', '@set'] } } } # Define and fill entity propeties: subclasses, labels, comments entity_properties = dict() entity_properties[ '@id'] = f'{self.PREFIX}:{self.directories.get("id")}' entity_properties['@type'] = 'owl:Class' subclasses = self.build_subclass(self.entity) if subclasses and subclasses[0] != Thing: entity_properties[ 'subClassOf'] = f'{self.PREFIX}:{subclasses[0].name}' labels = self.build_labels(self.entity) if labels: entity_properties['rdfs:label'] = labels comments = self.build_comments(self.entity) if comments: entity_properties['rdfs:comment'] = comments vocabulary_template[self.entity.name] = entity_properties for dependent in self.entity.subclasses(): vocabulary_template[dependent.name] = { 'rdfs:subClassOf': { '@id': f'{self.PREFIX}:{self.class_get_full_id(dependent).replace(f"/{dependent.name}", "")}' } } # Define and fill propeties for each supported attribute total_attributes = self.build_attributes() for rdf_attribute in total_attributes: attribute_properties = dict() attribute_properties[ '@id'] = f'{self.PREFIX}:{self.prop_get_full_id(rdf_attribute)}' attribute_properties['@type'] = default_world._unabbreviate( rdf_attribute._owl_type).replace( 'http://www.w3.org/2002/07/owl#', 'owl:') subproperties = self.build_subproperty(rdf_attribute) if subproperties: attribute_properties[ 'subPropertyOf'] = f'{self.PREFIX}:{self.prop_get_full_id(subproperties[0])}' labels = self.build_labels(rdf_attribute) if labels.items(): attribute_properties["rdfs:label"] = labels comments = self.build_comments(rdf_attribute) if comments.items(): attribute_properties["rdfs:comment"] = comments nested_labes = self.build_nested_labels(rdf_attribute) if nested_labes: attribute_properties["label"] = nested_labes nested_comments = self.build_nested_comments(rdf_attribute) if nested_comments: attribute_properties["comment"] = nested_comments ranges = self.build_ranges(rdf_attribute) if ranges: attribute_properties[f'{self.PREFIX}:valueType'] = ranges restrictions = self.build_restrictions(rdf_attribute) if restrictions: attribute_properties['xsd:restriction'] = restrictions domains = self.build_domains(rdf_attribute) if domains: attribute_properties['domain'] = domains vocabulary_template[rdf_attribute.name] = attribute_properties return vocabulary_template
def create_definition_from_rdf_class(self): """Return dict of generated properties to create ClassDefinition from rdf classes. Returns: definition_template (dict of str: Any): Dictionary of required parameters """ # Define main ClassDefinition template definition_template = { '@context': { '@version': self.VERSION, '@vocab': f"{self.export_onto_url}Vocabulary/{self.directories.get('id')}", 'xsd': { '@id': 'http://www.w3.org/2001/XMLSchema#', '@prefix': True }, f'{self.PREFIX}': { '@id': f'{self.export_onto_url}Vocabulary/', '@prefix': True }, 'description': { '@id': 'rdfs:comment', '@container': ['@language', '@set'] }, 'label': { '@id': 'rdfs:label', '@container': ['@language', '@set'] }, 'comment': { '@id': 'rdfs:comment', '@container': ['@language', '@set'] } }, } # Define and fill entity propeties: subclasses, labels, comments entity_properties = dict() entity_properties[ '@id'] = f'{self.PREFIX}:{self.directories.get("id")}' entity_properties['@type'] = 'owl:Class' subclasses = self.build_subclass(self.entity) if subclasses and subclasses[0] != Thing: entity_properties[ 'subClassOf'] = f'{self.PREFIX}:{subclasses[0].name}' labels = self.build_labels(self.entity) if labels: entity_properties['rdfs:label'] = labels comments = self.build_comments(self.entity) if comments: entity_properties['rdfs:comment'] = comments # Define supported attributes template supported_attrs = { 'data': { '@id': f'{self.PREFIX}:data', '@type': f'{self.PREFIX}:SupportedAttribute', 'rdfs:label': 'data', 'rdfs:comment': { 'en-us': 'data' }, f'{self.PREFIX}:required': False, } } # Define and fill propeties for each supported attribute total_attributes = self.build_attributes() for rdf_attribute in total_attributes: attribute_properties = dict() attribute_properties[ '@id'] = f'{self.PREFIX}:{self.prop_get_full_id(rdf_attribute)}' attribute_properties['@type'] = default_world._unabbreviate( rdf_attribute._owl_type).replace( 'http://www.w3.org/2002/07/owl#', 'owl:') subproperties = self.build_subproperty(rdf_attribute) if subproperties: attribute_properties[ 'subPropertyOf'] = f'{self.PREFIX}:{self.prop_get_full_id(subproperties[0])}' labels = self.build_labels(rdf_attribute) if labels.items(): attribute_properties["rdfs:label"] = labels comments = self.build_comments(rdf_attribute) if comments.items(): attribute_properties["rdfs:comment"] = comments nested_labes = self.build_nested_labels(rdf_attribute) if nested_labes: attribute_properties["label"] = nested_labes nested_comments = self.build_nested_comments(rdf_attribute) if nested_comments: attribute_properties["comment"] = nested_comments ranges = self.build_ranges(rdf_attribute) if ranges: attribute_properties[f'{self.PREFIX}:valueType'] = ranges restrictions = self.build_restrictions(rdf_attribute) if restrictions: attribute_properties['xsd:restriction'] = restrictions is_required = self.build_required(rdf_attribute) attribute_properties[f'{self.PREFIX}:required'] = is_required is_readonly = readonly._get_indirect_values_for_class( rdf_attribute) if is_readonly: attribute_properties[f'{self.PREFIX}:readonly'] = is_readonly[ 0] supported_attrs[str(rdf_attribute.name)] = attribute_properties # Add attribute propeties to ClassDefinition template entity_properties[ f'{self.PREFIX}:supportedAttribute'] = supported_attrs definition_template[ f'{self.PREFIX}:supportedClass'] = entity_properties return definition_template
def create_vocabulary_from_rdf_class(rdf_class, entity_file: Dict[str, Any], onto, export_onto_url, PREFIX='pot', VERSION=1.1) -> Dict[str, Any]: """Return dict of generated properties to create Vocabulary from rdf classes. Args: rdf_class (models.RDFClass): RDFClass model object entity_file (dict of str: Any): Dictionary with directory, filename and id of entity onto (namespace.Ontology): An ontology loaded with owlready2. export_onto_url (str): Link to base ontologies. PREFIX (str, optional): Id prefix. VERSION (number, optional): Version number Returns: vocabulary_template (dict of str: Any): Dictionary of required parameters """ # Define main Vocabulary template for class vocabulary_template = { '@context': { '@version': VERSION, f'{PREFIX}': { '@id': f'{export_onto_url}Vocabulary/', '@prefix': True }, 'rdf': { '@id': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', '@prefix': True }, 'rdfs': { '@id': 'http://www.w3.org/2000/01/rdf-schema#', '@prefix': True }, 'owl': { '@id': 'http://www.w3.org/2002/07/owl#', '@prefix': True }, 'vs': { '@id': 'http://www.w3.org/2003/06/sw-vocab-status/ns#', }, 'xsd': { '@id': 'http://www.w3.org/2001/XMLSchema#', '@prefix': True }, 'label': { '@id': 'rdfs:label', "@container": ['@language', '@set'] }, 'comment': { '@id': 'rdfs:comment', "@container": ['@language', '@set'] } } } # Define and fill entity propeties: subclasses, labels, comments entity_properties = dict() entity_properties['@id'] = f'{PREFIX}:{entity_file.get("id")}' entity_properties['@type'] = 'owl:Class' subclasses = build_subclass(rdf_class.entity) if subclasses and subclasses[0] != Thing: entity_properties['subClassOf'] = f'{PREFIX}:{subclasses[0].name}' labels = build_labels(rdf_class.entity) if labels: entity_properties['rdfs:label'] = labels comments = build_comments(rdf_class.entity) if comments: entity_properties['rdfs:comment'] = comments vocabulary_template[rdf_class.entity.name] = entity_properties for dependent in rdf_class.entity.subclasses(): vocabulary_template[dependent.name] = { 'rdfs:subClassOf': { '@id': f'{PREFIX}:{class_get_full_id(dependent).replace(f"/{dependent.name}", "")}' } } # Define and fill propeties for each supported attribute total_attributes = build_attributes(rdf_class, onto) for rdf_attribute in total_attributes: attribute_properties = dict() attribute_properties['@id'] = f'{PREFIX}:{prop_get_full_id(rdf_attribute)}' attribute_properties['@type'] = default_world._unabbreviate(rdf_attribute._owl_type).replace( 'http://www.w3.org/2002/07/owl#', 'owl:') subproperties = build_subproperty(rdf_attribute) if subproperties: attribute_properties[ 'subPropertyOf'] = f'{PREFIX}:{prop_get_full_id(subproperties[0])}' labels = build_labels(rdf_attribute) if labels.items(): attribute_properties["rdfs:label"] = labels comments = build_comments(rdf_attribute) if comments.items(): attribute_properties["rdfs:comment"] = comments nested_labes = build_nested_labels(rdf_attribute) if nested_labes: attribute_properties["label"] = nested_labes nested_comments = build_nested_comments(rdf_attribute) if nested_comments: attribute_properties["comment"] = nested_comments ranges = build_ranges(rdf_attribute) if ranges: attribute_properties[f'{PREFIX}:valueType'] = ranges restrictions = build_restrictions(rdf_attribute) if restrictions: attribute_properties['xsd:restriction'] = restrictions domains = build_domains(rdf_attribute) if domains: attribute_properties['domain'] = domains vocabulary_template[rdf_attribute.name] = attribute_properties return vocabulary_template
def create_vocabulary_from_rdf_property(rdf_property: Any, export_onto_url: str, PREFIX='pot', VERSION=1.1): """Return dict of generated properties to create Vocabulary from rdf property. Args: rdf_property (models.RDFProperty): RDFProperty model object export_onto_url (str): Link to base ontologies. PREFIX (str, optional): Id prefix. VERSION (number, optional): Version number Returns: vocabulary_template (dict of str: Any): Dictionary of required parameters """ # Define main Vocabulary template for property vocabulary_template = { '@context': { '@version': VERSION, f'{PREFIX}': { '@id': f'{export_onto_url}Vocabulary/', '@prefix': True }, 'rdf': { '@id': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', '@prefix': True }, 'rdfs': { '@id': 'http://www.w3.org/2000/01/rdf-schema#', '@prefix': True }, 'owl': { '@id': 'http://www.w3.org/2002/07/owl#', '@prefix': True }, 'vs': { '@id': 'http://www.w3.org/2003/06/sw-vocab-status/ns#', }, 'xsd': { '@id': 'http://www.w3.org/2001/XMLSchema#', '@prefix': True }, 'label': { '@id': 'rdfs:label', "@container": ['@language', '@set'] }, 'comment': { '@id': 'rdfs:comment', "@container": ['@language', '@set'] } } } # Define and fill entity propeties: subclasses, labels, comments entity_properties = dict() entity_properties['@id'] = f'{PREFIX}:{prop_get_full_id(rdf_property.entity)}' entity_properties['@type'] = default_world._unabbreviate(rdf_property.entity._owl_type).replace( 'http://www.w3.org/2002/07/owl#', 'owl:') subproperties = build_subproperty(rdf_property.entity) if subproperties: entity_properties['subPropertyOf'] = f'{PREFIX}:{prop_get_full_id(subproperties[0])}' labels = build_labels(rdf_property.entity) if labels.items(): entity_properties["rdfs:label"] = labels comments = build_comments(rdf_property.entity) if comments.items(): entity_properties["rdfs:comment"] = comments nested_labes = build_nested_labels(rdf_property.entity) if nested_labes: entity_properties["label"] = nested_labes nested_comments = build_nested_comments(rdf_property.entity) if nested_comments: entity_properties["comment"] = nested_comments ranges = build_ranges(rdf_property.entity) if ranges: entity_properties[f'{PREFIX}:valueType'] = ranges restrictions = build_restrictions(rdf_property.entity) if restrictions: entity_properties['xsd:restriction'] = restrictions domains = build_domains(rdf_property.entity) if domains: entity_properties['domain'] = domains vocabulary_template[rdf_property.entity.name] = entity_properties return vocabulary_template
def create_definition_from_rdf_class(rdf_class, entity_file: Dict[str, str], onto, export_onto_url: str, PREFIX='pot', VERSION=1.1) -> Dict[str, Any]: """Return dict of generated properties to create ClassDefinition from rdf classes. Args: rdf_class (models.RDFClass): RDFClass model object entity_file (dict of str: Any): Dictionary with directory, filename and id of entity onto (namespace.Ontology): An ontology loaded with owlready2. export_onto_url (str): Link to base ontologies. PREFIX (str, optional): Id prefix. VERSION (number, optional): Version number Returns: definition_template (dict of str: Any): Dictionary of required parameters """ # Define main ClassDefinition template definition_template = { '@context': { '@version': VERSION, '@vocab': f"{export_onto_url}Vocabulary/{entity_file.get('id')}", 'xsd': { '@id': 'http://www.w3.org/2001/XMLSchema#', '@prefix': True }, f'{PREFIX}': { '@id': f'{export_onto_url}Vocabulary/', '@prefix': True }, 'description': { '@id': 'rdfs:comment', '@container': ['@language', '@set'] }, 'label': { '@id': 'rdfs:label', '@container': ['@language', '@set'] }, 'comment': { '@id': 'rdfs:comment', '@container': ['@language', '@set'] } }, } # Define and fill entity propeties: subclasses, labels, comments entity_properties = dict() entity_properties['@id'] = f'{PREFIX}:{entity_file.get("id")}' entity_properties['@type'] = 'owl:Class' subclasses = build_subclass(rdf_class.entity) if subclasses and subclasses[0] != Thing: entity_properties['subClassOf'] = f'{PREFIX}:{subclasses[0].name}' labels = build_labels(rdf_class.entity) if labels: entity_properties['rdfs:label'] = labels comments = build_comments(rdf_class.entity) if comments: entity_properties['rdfs:comment'] = comments # Define supported attributes template supported_attrs = { 'data': { '@id': f'{PREFIX}:data', '@type': f'{PREFIX}:SupportedAttribute', 'rdfs:label': 'data', 'rdfs:comment': { 'en-us': 'data' }, f'{PREFIX}:required': False, } } # Define and fill propeties for each supported attribute total_attributes = build_attributes(rdf_class, onto) for rdf_attribute in total_attributes: attribute_properties = dict() attribute_properties[ '@id'] = f'{PREFIX}:{prop_get_full_id(rdf_attribute)}' attribute_properties['@type'] = default_world._unabbreviate( rdf_attribute._owl_type).replace('http://www.w3.org/2002/07/owl#', 'owl:') subproperties = build_subproperty(rdf_attribute) if subproperties: attribute_properties[ 'subPropertyOf'] = f'{PREFIX}:{prop_get_full_id(subproperties[0])}' labels = build_labels(rdf_attribute) if labels.items(): attribute_properties["rdfs:label"] = labels comments = build_comments(rdf_attribute) if comments.items(): attribute_properties["rdfs:comment"] = comments nested_labes = build_nested_labels(rdf_attribute) if nested_labes: attribute_properties["label"] = nested_labes nested_comments = build_nested_comments(rdf_attribute) if nested_comments: attribute_properties["comment"] = nested_comments ranges = build_ranges(rdf_attribute) if ranges: attribute_properties[f'{PREFIX}:valueType'] = ranges restrictions = build_restrictions(rdf_attribute) if restrictions: attribute_properties['xsd:restriction'] = restrictions is_required = build_required(rdf_attribute) attribute_properties[f'{PREFIX}:required'] = is_required is_readonly = readonly._get_indirect_values_for_class(rdf_attribute) if is_readonly: attribute_properties[f'{PREFIX}:readonly'] = is_readonly[0] supported_attrs[str(rdf_attribute.name)] = attribute_properties # Add attribute propeties to ClassDefinition template entity_properties[f'{PREFIX}:supportedAttribute'] = supported_attrs definition_template[f'{PREFIX}:supportedClass'] = entity_properties return definition_template