def testHandlesStandardFieldNameNoLiterals(self): standard_field_name = 'run_command_1' list_concepts = rdf_helper.DecomposeStandardFieldName( standard_field_name) self.assertIn('Run', list_concepts) self.assertIn('Command', list_concepts) self.assertIsNot('1', list_concepts)
def testAbstractGraphGeneration(self): fake_abstract = 'fake_resources/Abstract.yaml' file_contents = rdf_helper.ReadFile(fake_abstract) yaml_abstract = yaml_handler.ImportYamlFiles(file_contents) # Create global graph graph = rdflib.Graph() # Generate the graph by the function handler graph = rdflib_function_handler.GenerateGraph(yaml_abstract, graph) # Build a second graph manually in order to compare it with the one above: # DD: # description: "Dual duct flow control (hot deck, cold deck)." # is_abstract: true # opt_uses: # - discharge_air_temperature_sensor # - run_command # uses: # - heating_air_flowrate_sensor # - heating_air_flowrate_setpoint # implements: # - CONTROL expected_graph = rdflib.Graph() # Main Types functionality = rdflib.URIRef(constants.HVAC_NS['Functionality']) entity_type = rdflib.URIRef( constants.DIGITAL_BUILDINGS_NS['EntityType']) dd_description = 'Dual duct flow control (hot deck, cold deck).' # Main Classes dd_expected = infixowl.Class(identifier=constants.HVAC_NS['Dd'], graph=expected_graph, subClassOf=[functionality]) expected_graph, _ = rdf_helper.CreateClassInGraph( graph=expected_graph, class_name='Dd', class_description=dd_description, parent_clazz=functionality, entity_namespace=constants.HVAC_NS) infixowl.Class(identifier=constants.SUBFIELDS_NS['Point_type'], graph=expected_graph) uses_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['uses'], baseType=infixowl.OWL_NS.ObjectProperty, graph=expected_graph) is_composed_of_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['isComposedOf'], baseType=infixowl.OWL_NS.ObjectProperty, graph=expected_graph) uses_property_optionally = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['usesOptional'], baseType=infixowl.OWL_NS.ObjectProperty, graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Air'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Flowrate'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Heating'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Run'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Command'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Discharge'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Temperature'], graph=expected_graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Sensor'], graph=expected_graph) infixowl.Class(identifier=constants.FIELDS_NS['Field'], graph=expected_graph) heating_air_flowrate_sensor = infixowl.Class( identifier=constants.FIELDS_NS['Heating_air_flowrate_sensor'], graph=expected_graph, subClassOf=[constants.FIELDS_NS['Field']]) heating_air_flowrate_setpoint = infixowl.Class( identifier=constants.FIELDS_NS['Heating_air_flowrate_setpoint'], graph=expected_graph, subClassOf=[constants.FIELDS_NS['Field']]) concat = heating_air_flowrate_sensor & heating_air_flowrate_setpoint dd_expected.subClassOf = [uses_property | infixowl.only | concat] discharge_air_temperature_sensor = infixowl.Class( identifier=constants.FIELDS_NS['Discharge_air_temperature_sensor'], graph=expected_graph, subClassOf=[constants.FIELDS_NS['Field']]) run_command = infixowl.Class( identifier=constants.FIELDS_NS['Run_command'], graph=expected_graph, subClassOf=[constants.FIELDS_NS['Field']]) concat2 = discharge_air_temperature_sensor | run_command dd_expected.subClassOf = [ uses_property_optionally | infixowl.some | concat2 ] list_composition = rdf_helper.DecomposeStandardFieldName( 'Discharge_air_temperature_sensor') expected_graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=expected_graph, list_composition=list_composition, standard_field_name='Discharge_air_temperature_sensor', is_composed_of_property=is_composed_of_property) list_composition = rdf_helper.DecomposeStandardFieldName('Run_command') expected_graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=expected_graph, list_composition=list_composition, standard_field_name='Run_command', is_composed_of_property=is_composed_of_property) list_composition = rdf_helper.DecomposeStandardFieldName( 'Heating_air_flowrate_sensor') expected_graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=expected_graph, list_composition=list_composition, standard_field_name='Heating_air_flowrate_sensor', is_composed_of_property=is_composed_of_property) list_composition = rdf_helper.DecomposeStandardFieldName( 'Heating_air_flowrate_setpoint') expected_graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=expected_graph, list_composition=list_composition, standard_field_name='Heating_air_flowrate_setpoint', is_composed_of_property=is_composed_of_property) expected_graph, functionality_class = rdf_helper.CreateClassInGraph( graph=expected_graph, class_name='Functionality', class_description=None, parent_clazz=entity_type, entity_namespace=constants.HVAC_NS) expected_graph, application = rdf_helper.CreateClassInGraph( graph=expected_graph, class_name='Application', class_description=None, parent_clazz=entity_type) expected_graph, control = rdf_helper.CreateClassInGraph( graph=expected_graph, class_name='Control', class_description=None, parent_clazz=application[0]) expected_graph, _ = rdf_helper.CreateClassInGraph( graph=expected_graph, class_name='Dd', class_description=dd_description, parent_clazz=functionality_class[0], entity_namespace=constants.HVAC_NS) expected_graph, _ = rdf_helper.CreateClassInGraph( graph=expected_graph, class_name='Dd', class_description=dd_description, parent_clazz=control[0], entity_namespace=constants.HVAC_NS) # Check if they are similar self.assertTrue(compare.similar(graph, expected_graph))
def GenerateGraph(yaml_object, graph, entity_namespace): """Utility function updates an RDF graph with the yaml content. Updates an RDF graph with the yaml content Args: yaml_object: the yaml content. graph: the global graph where the ontology is being built in RDF. entity_namespace: the namespace to be attributed to the given types Returns: a graph with the contents of the yaml file merged """ # Ignore entities implementing the following keywords in their definition ignore_generation_set = { "DEPRECATED", "INCOMPLETE", "IGNORE", "REMAP_REQUIRED" } field = rdflib.URIRef(constants.FIELDS_NS["Field"]) # Prepare properties to be added to the graph and not in the yaml uses_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["uses"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) uses_property_optionally = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["usesOptional"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) is_composed_of_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["isComposedOf"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) infixowl.Class(identifier=constants.SUBFIELDS_NS["Point_type"], graph=graph) # Traverse the yaml content for clazz, clazz_content in yaml_object.items(): class_name = clazz.capitalize() implements = clazz_content.get("implements") # implements content will decide if the class is added to the graph or not ignore_class = False if implements is not None: for implements_item in implements: if implements_item in ignore_generation_set: ignore_class = True # Generate the class if it does not have implements field # or no (DEPRECATED or INCOMPLETE) if ignore_class: continue # Create the class parent = (entity_namespace[implements[0].capitalize()] if implements is not None else namespace.OWL.Thing) graph, _ = rdf_helper.CreateClassInGraph( graph=graph, class_name=class_name, class_description=clazz_content.get("description"), parent_clazz=parent, entity_namespace=entity_namespace) # update parents only if implements is not None if implements is not None: for implements_item in implements[1:]: graph.add((entity_namespace[class_name.capitalize()], rdflib.RDFS.subClassOf, entity_namespace[implements_item.capitalize()])) # check the mandatory fields uses = clazz_content.get("uses") if uses is not None and isinstance(uses, list): for each_item in uses: list_composition = rdf_helper.DecomposeStandardFieldName( each_item) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=graph, list_composition=list_composition, standard_field_name=each_item, is_composed_of_property=is_composed_of_property) class_owl = infixowl.Class( identifier=entity_namespace[class_name], graph=graph, subClassOf=[parent]) graph = rdf_helper.CreateCompositionInGraph( list_standard_field_names=uses, composition_operator="&", composition_property=uses_property, restriction=infixowl.only, class_owl=class_owl, graph=graph, entity_namespace=constants.FIELDS_NS, sub_class_of=[field]) # check the optional fields opt_uses = clazz_content.get("opt_uses") if opt_uses is not None and isinstance(opt_uses, list): for each_item in opt_uses: list_composition = rdf_helper.DecomposeStandardFieldName( each_item) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=graph, list_composition=list_composition, standard_field_name=each_item, is_composed_of_property=is_composed_of_property) class_owl = infixowl.Class( identifier=entity_namespace[class_name], graph=graph, subClassOf=[parent]) graph = rdf_helper.CreateCompositionInGraph( list_standard_field_names=opt_uses, composition_operator="|", composition_property=uses_property_optionally, restriction=infixowl.some, class_owl=class_owl, graph=graph, entity_namespace=constants.FIELDS_NS, sub_class_of=[field]) return graph
def GenerateGraph(yaml_object, graph): """Utility function updates an RDF graph with the yaml content of the GeneralTypes.yaml. The content of each object is similar to the following: CHL: id: "9950288448474578944" description: "Tag for chillers." is_abstract: true implements: - EQUIPMENT opt_uses: - cooling_thermal_power_capacity - power_capacity - efficiency_percentage_specification - flowrate_requirement Updates an RDF graph with the yaml content. Args: yaml_object: the yaml content. graph: the global graph where the ontology is being built in RDF. Returns: a graph with the contents of the yaml file merged """ # Applications Set used to avoid recreating applications again in the graph applications_set = {"Equipment"} # Prepare classes to be added to the graph and not in the yaml equipment = rdflib.URIRef(constants.DIGITAL_BUILDINGS_NS["Equipment"]) entity_type = rdflib.URIRef(constants.DIGITAL_BUILDINGS_NS["EntityType"]) field = rdflib.URIRef(constants.FIELDS_NS["Field"]) infixowl.Class(identifier=constants.FIELDS_NS["Field"], graph=graph) infixowl.Class(identifier=constants.SUBFIELDS_NS["Point_type"], graph=graph) graph, application_class = rdf_helper.CreateClassInGraph( graph=graph, class_name="Application", class_description=None, parent_clazz=entity_type) graph, _ = rdf_helper.CreateClassInGraph(graph=graph, class_name="Equipment", class_description=None, parent_clazz=entity_type) # Prepare properties to be added to the graph and not in the yaml uses_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["uses"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) uses_property_optionally = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["usesOptional"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) is_composed_of_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["isComposedOf"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) # Traverse the yaml content for clazz in yaml_object.keys(): clazz_content = yaml_object.get(clazz) class_name = clazz.capitalize() implements = clazz_content.get("implements") # implements will decide if the entity gets generated in the graph, # for now the mother class is equipment, this might change in the near # future due to changes in the ontology, keeping the implementation as it is if implements is None: continue else: mother_class = equipment # Create the class graph, clazz_object = rdf_helper.CreateClassInGraph( graph=graph, class_name=class_name, class_description=clazz_content.get("description"), parent_clazz=mother_class, entity_namespace=constants.HVAC_NS) if implements is not None: graph, applications_set = rdf_helper.CreatesImplementsInGraph( graph=graph, implements_list=implements, applications_set=applications_set, application_class=application_class, class_object=clazz_object) uses = clazz_content.get("uses") if uses is not None: if isinstance(uses, list): for each_item in uses: list_composition = rdf_helper.DecomposeStandardFieldName( each_item) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=graph, list_composition=list_composition, standard_field_name=each_item, is_composed_of_property=is_composed_of_property) class_owl = infixowl.Class( identifier=constants.HVAC_NS[class_name], graph=graph, sub_class_of=[equipment]) graph = rdf_helper.CreateCompositionInGraph( list_standard_field_names=uses, composition_operator="&", composition_property=uses_property, restriction=infixowl.only, class_owl=class_owl, graph=graph, entity_namespace=constants.FIELDS_NS, sub_class_of=[field]) opt_uses = clazz_content.get("opt_uses") if opt_uses is not None: if isinstance(opt_uses, list): for each_item in opt_uses: list_composition = rdf_helper.DecomposeStandardFieldName( each_item) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=graph, list_composition=list_composition, standard_field_name=each_item, is_composed_of_property=is_composed_of_property) class_owl = infixowl.Class( identifier=constants.HVAC_NS[class_name], graph=graph, subClassOf=[equipment]) graph = rdf_helper.CreateCompositionInGraph( list_standard_field_names=opt_uses, composition_operator="|", composition_property=uses_property_optionally, restriction=infixowl.some, class_owl=class_owl, graph=graph, entity_namespace=constants.FIELDS_NS, sub_class_of=[field]) return graph
def testHandlesStandardFieldNameNoSplit(self): standard_field_name = 'run' list_concepts = rdf_helper.DecomposeStandardFieldName( standard_field_name) self.assertIn('Run', list_concepts)
def GenerateGraph(yaml_object, graph): """Utility function updates an RDF graph with the yaml content of the Abstract.yaml. The content of each object is similar to the following: DXRC: description: "Compressor run control on return air side (RC)." opt_uses: discharge_air_temperature_sensor leaving_cooling_coil_temperature_sensor cooling_thermal_power_capacity uses: return_air_temperature_setpoint return_air_temperature_sensor compressor_run_command compressor_run_status implements: CONTROL Updates an RDF graph with the yaml content. Args: yaml_object: the yaml content. graph: the global graph where the ontology is being built in RDF. Returns: a graph with the contents of the yaml file merged """ # Applications Set used to avoid recreating applications again in the graph applications_set = set() # Prepare classes to be added to the graph and not in the yaml functionality = rdflib.URIRef(constants.HVAC_NS["Functionality"]) entity_type = rdflib.URIRef(constants.DIGITAL_BUILDINGS_NS["EntityType"]) field = rdflib.URIRef(constants.FIELDS_NS["Field"]) infixowl.Class(identifier=constants.FIELDS_NS["Field"], graph=graph) infixowl.Class(identifier=constants.SUBFIELDS_NS["Point_type"], graph=graph) graph, _ = rdf_helper.CreateClassInGraph( graph=graph, class_name="Functionality", class_description=None, parent_clazz=entity_type, entity_namespace=constants.HVAC_NS) graph, application_class = rdf_helper.CreateClassInGraph( graph=graph, class_name="Application", class_description=None, parent_clazz=entity_type) # Prepare properties to be added to the graph and not in the yaml uses_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["uses"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) uses_property_optionally = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["usesOptional"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) is_composed_of_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS["isComposedOf"], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) # Traverse the yaml content for clazz, clazz_content in yaml_object.items(): class_name = clazz.capitalize() graph, clazz_object = rdf_helper.CreateClassInGraph( graph=graph, class_name=class_name, class_description=clazz_content.get("description"), parent_clazz=functionality, entity_namespace=constants.HVAC_NS) implements = clazz_content.get("implements") if implements is not None: graph, applications_set = rdf_helper.CreatesImplementsInGraph( graph=graph, implements_list=implements, applications_set=applications_set, application_class=application_class, class_object=clazz_object) uses = clazz_content.get("uses") if uses is not None: if isinstance(uses, list): for each_item in uses: list_composition = rdf_helper.DecomposeStandardFieldName( each_item) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=graph, list_composition=list_composition, standard_field_name=each_item, is_composed_of_property=is_composed_of_property) class_owl = infixowl.Class( identifier=constants.HVAC_NS[class_name], graph=graph, subClassOf=[functionality]) graph = rdf_helper.CreateCompositionInGraph( list_standard_field_names=uses, composition_operator="&", composition_property=uses_property, restriction=infixowl.only, class_owl=class_owl, graph=graph, entity_namespace=constants.FIELDS_NS, sub_class_of=[field]) opt_uses = clazz_content.get("opt_uses") if opt_uses is not None: if isinstance(opt_uses, list): for each_item in opt_uses: list_composition = rdf_helper.DecomposeStandardFieldName( each_item) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( graph=graph, list_composition=list_composition, standard_field_name=each_item, is_composed_of_property=is_composed_of_property) class_owl = infixowl.Class( identifier=constants.HVAC_NS[class_name], graph=graph, subClassOf=[functionality]) graph = rdf_helper.CreateCompositionInGraph( list_standard_field_names=opt_uses, composition_operator="|", composition_property=uses_property_optionally, restriction=infixowl.some, class_owl=class_owl, graph=graph, entity_namespace=constants.FIELDS_NS, sub_class_of=[field]) return graph