def create_context_from_rdf_class(rdf_class, entity_file: Dict[str, Any], onto, export_onto_url: str, PREFIX='pot', VERSION=1.1) -> Dict[str, Any]: """Return dict of generated properties to create Context 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: context_template (dict of str: Any): Dictionary of required parameters """ # Define main Context template context_template = { '@version': VERSION, '@vocab': f"{export_onto_url}Vocabulary/{entity_file.get('id')}", '@classDefinition': f"{export_onto_url}ClassDefinitions/{entity_file.get('id')}", rdf_class.entity.name: { "@id": rdf_class.entity.name }, '@schema': f"{export_onto_url}Schema/{entity_file.get('id')}", f'{PREFIX}': { '@id': f'{export_onto_url}Vocabulary/', '@prefix': True }, 'data': f'{PREFIX}:data', 'metadata': f'{PREFIX}:metadata', } if Identity in rdf_class.entity.ancestors(): context_template["@base"] = "https://api.oftrust.net/identities/v1/" # 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)}' nest_list = nest._get_indirect_values_for_class(rdf_attribute) if nest_list: attribute_properties['@nest'] = nest_list[0].name context_template[rdf_attribute.name] = attribute_properties context_wrapper = {'@context': context_template} return context_wrapper
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_schema_from_rdf_class(rdf_class, entity_file: Dict[str, Any], onto, export_onto_url: str) -> Dict[str, Any]: """Return dict of generated properties to create Schema 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. Returns: schema (dict of str: Any): Dictionary of required parameters """ properties = set() parents = set() labels = dict() comments = dict() examples = dict() required_attrs = list() # Define and fill propeties for each supported attribute total_attributes = build_attributes(rdf_class, onto) for attr in total_attributes: # Build required if build_required(attr): required_attrs.append(attr.name) # Build labels according to schema specifications attr_labels = build_labels(attr) if attr_labels.get('en-us'): labels[attr.name] = attr_labels['en-us'] else: attr_nested_labels = build_nested_labels(attr) if attr_nested_labels: labels[ attr.name] = attr_nested_labels[0]['rdfs:label']['en-us'] else: labels[attr.name] = '' # Build comments according to schema specifications attr_comments = build_comments(attr) if attr_comments.get('en-us'): comments[attr.name] = attr_comments['en-us'] else: attr_nested_comments = build_nested_comments(attr) if attr_nested_comments: for c in attr_nested_comments: for d in c.get('domain'): if rdf_class.entity.name == d.split(':')[-1]: attr_nested_comment = c['rdfs:comment'] if attr_nested_comment.get('en-us'): comments[ attr.name] = attr_nested_comment['en-us'] else: comments[attr.name] = '' else: comments[attr.name] = '' examples[attr.name] = attr.example if attr.example else '' # Build parent properties parent = nest._get_indirect_values_for_class( attr)[0] if nest._get_indirect_values_for_class(attr) else None if parent: properties.add((parent.name, attr.name)) else: parents.add(attr.name) properties_dict = {} for k, v in properties: if k in properties_dict: properties_dict[k].append(v) else: properties_dict[k] = [v] for p in parents: if p not in properties_dict: properties_dict[p] = [] result = dict() result["@context"] = { "type": "string", "minLength": 1, "const": f"{export_onto_url}Context/{entity_file.get('id')}/" } result["@type"] = { "type": "string", "minLength": 1, "enum": [rdf_class.entity.name], "const": rdf_class.entity.name } required_attrs.append("@context") required_attrs.append("@type") if 'id' in required_attrs: required_attrs.remove('id') required_attrs.append("@id") prop_parent = {v[0] for v in properties} for i in parents: if i in prop_parent: result[i] = { "title": "", "description": "", "type": "object", "properties": {} } else: result[i] = {"title": "", "description": "", "type": "string"} for key, values in properties_dict.items(): for v in values: result[key]["title"] = labels[key] result[key]["description"] = comments[key] result[key]["properties"][v] = { "title": labels[v] if v in labels else '', "description": comments[v] if v in comments else '', "type": "string" } if examples[key]: result[key]["examples"] = examples[key] if key in result and examples[v]: result[key]["properties"][v]["examples"] = examples[v] if 'id' in result: result['@id'] = result['id'] del result['id'] schema = { "$schema": "http://json-schema.org/draft-07/schema", "type": "object", "properties": result, "required": required_attrs } return schema
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
def create_context_from_data_product(rdf_class, entity_file: Dict[str, Any], onto, export_onto_url: str, PREFIX='pot', VERSION=1.1) -> Dict[str, Any]: """Return dict of generated properties to create Context 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: context_template (dict of str: Any): Dictionary of required parameters """ corresponds = { "DataProductContext": "DataProductContext", "DataProductOutput": "DataProductOutput", "DataProductParameters": "DataProductParameters", "DataProductParameters": "DataProductParameters", "WeatherForecastDataProductContext": "Weather", "WeatherForecastDataProductOutput": "Weather", "WeatherForecastDataProductParameters": "Weather", "ForecastDataProductContext": "Forecast", "ForecastDataProductOutput": "Forecast", "ForecastDataProductParameters": "Forecast", "LtifDataProductContext": "Ltif", "LtifDataProductOutput": "Ltif", "LtifDataProductParameters": "Ltif", "SensorDataProductContext": "Sensor", "SensorDataProductOutput": "Sensor", "SensorDataProductParameters": "Sensor", "AccuWeatherForecastDataProductContext": "AccuWeather", "AccuWeatherForecastDataProductOutput": "AccuWeather", "AccuWeatherForecastDataProductParameters": "AccuWeather", "DocumentDataProductContext": "Document", "DocumentDataProductOutput": "Document", "DocumentDataProductParameters": "Document", "DocumentSigningDataProductContext": "Signing", "DocumentSigningDataProductOutput": "Signing", "DocumentSigningDataProductParameters": "Signing", "SignSpaceDataProductContext": "SignSpace", "SignSpaceDataProductOutput": "SignSpace", "SignSpaceDataProductParameters": "SignSpace", "PriceForecastDataProductContext": "Price", "PriceForecastDataProductOutput": "Price", "PriceForecastDataProductParameters": "Price", "ElectricityPriceForecastDataProductContext": "Electricity", "ElectricityPriceForecastDataProductOutput": "Electricity", "ElectricityPriceForecastDataProductParameters": "Electricity" } if entity_file.get('id') not in ('DataProductContext', 'DataProductOutput', 'DataProductParameters'): entity_name = entity_file.get('id').split('/') new_path = [] for e in entity_name: new_path.append(corresponds[e]) entity_file['dir'], entity_file['filename'], entity_file['id'] = '/'.join(new_path[:-1]), f'{new_path[-1]}.jsonld', '/'.join(new_path) context_template = { '@version': VERSION, rdf_class.entity.name: {"@id": f'pot:{rdf_class.entity.name}'}, '@schema': f"{export_onto_url}Schema/{entity_file.get('id')}", f'{PREFIX}': { '@id': f'{export_onto_url}Vocabulary/', '@prefix': True } } # Hard Code for now context_template["productCode"] = {"@id": "pot:productCode"} context_template["timestamp"] = {"@id": "pot:timestamp"} context_template["parameters"] = {"@id": "pot:parameters"} # 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}:{str(rdf_attribute).split(".")[1]}' attribute_properties['@nest'] = "parameters" context_template[rdf_attribute.name] = attribute_properties for dependent in rdf_class.entity.subclasses(): context_template[dependent.name] = { 'rdfs:subClassOf': { '@id': f'{PREFIX}:{class_get_full_id(dependent).replace(f"/{dependent.name}", "")}' } } context_wrapper = {'@context': context_template} return context_wrapper