def testCreatesCompositionInGraphSomeRestriction(self): # Prepare data # Build First Graph with the method graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(rdflib.Graph()) namespace_manager.bind('owl', rdflib.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager list_standard_field_name = [ 'cooling_air_flowrate_sensor_1', 'run_command' ] uses_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['uses'], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) class_owl = infixowl.Class( identifier=constants.DIGITAL_BUILDINGS_NS['DFSS'], graph=graph) graph = rdf_helper.CreateCompositionInGraph( composition_operator='|', list_standard_field_names=list_standard_field_name, restriction=infixowl.some, composition_property=uses_property, class_owl=class_owl, graph=graph) # Build a second graph that is expected expected_graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager_expected = namespace.NamespaceManager(rdflib.Graph()) namespace_manager_expected.bind('owl', rdflib.OWL) namespace_manager_expected.bind('db', constants.DIGITAL_BUILDINGS_NS) expected_graph.namespace_manager = namespace_manager_expected class_owl_expected = infixowl.Class( identifier=constants.DIGITAL_BUILDINGS_NS['DFSS'], graph=expected_graph) sfn1_expected = infixowl.Class( identifier=constants. DIGITAL_BUILDINGS_NS['Cooling_air_flowrate_sensor_1'], graph=expected_graph) sfn2_expected = infixowl.Class( identifier=constants.DIGITAL_BUILDINGS_NS['Run_command'], graph=expected_graph) uses_property_expected = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['uses'], baseType=infixowl.OWL_NS.ObjectProperty, graph=expected_graph) concat = sfn1_expected | sfn2_expected class_owl_expected.subClassOf = [ uses_property_expected | infixowl.some | concat ] # Check if they are similar self.assertTrue(compare.similar(graph, expected_graph))
def testStatesGraphGeneration(self): fake_states = 'fake_resources/states.yaml' file_contents = rdf_helper.ReadFile(fake_states) yaml_states = yaml_handler.ImportYamlFiles(file_contents) # Create global graph graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind('owl', rdflib.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager g_states = rdflib_states_handler.GenerateGraph(yaml_states, graph) # Main Types state = rdflib.URIRef(constants.STATES_NS['State']) on = rdflib.URIRef(constants.STATES_NS['On']) off = rdflib.URIRef(constants.STATES_NS['Off']) on_desc = 'Powered on.' # Main Classes state_class = (state, rdflib.RDF.type, rdflib.OWL.Class) on_class = (on, rdflib.RDF.type, rdflib.OWL.Class) off_class = (off, rdflib.RDF.type, rdflib.OWL.Class) # Assertions # Class assertions self.assertIn(state_class, g_states) self.assertIn(on_class, g_states) self.assertIn(off_class, g_states) # SubClasses assertions self.assertIn((state, rdflib.RDFS.subClassOf, rdflib.OWL.Thing), g_states) self.assertIn((on, rdflib.RDFS.subClassOf, state), g_states) self.assertIn((off, rdflib.RDFS.subClassOf, state), g_states) # Relations assertions self.assertIn((on, rdflib.RDFS.comment, rdflib.Literal(on_desc)), g_states)
def testFacilitiesGraphGeneration(self): fake_fans = 'fake_resources/FAN.yaml' file_contents = rdf_helper.ReadFile(fake_fans) yaml_fans = yaml_handler.ImportYamlFiles(file_contents) # Create global graph graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind('owl', rdflib.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager g_fan = rdflib_carson_types_handler.GenerateGraph( yaml_fans, graph, constants.HVAC_NS) # Main Types fan_ss_refm_csp = rdflib.URIRef(constants.HVAC_NS['Fan_ss_refm_csp']) fan_s_vscf = rdflib.URIRef(constants.HVAC_NS['Fan_s_vscf']) fan_ss_ffb = rdflib.URIRef(constants.HVAC_NS['Fan_ss_ffb']) # Main Classes fan_ss_refm_csp_class = (fan_ss_refm_csp, rdflib.RDF.type, rdflib.OWL.Class) fan_s_vscf_class = (fan_s_vscf, rdflib.RDF.type, rdflib.OWL.Class) fan_ss_ffb_class = (fan_ss_ffb, rdflib.RDF.type, rdflib.OWL.Class) # Assertions # Class assertions self.assertIn(fan_ss_refm_csp_class, g_fan) # Should not be created self.assertIsNot(fan_s_vscf_class, g_fan) self.assertIsNot(fan_ss_ffb_class, g_fan)
def testRdfOntInitGraphGeneration(self): # Create global graph graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind("owl", rdflib.OWL) namespace_manager.bind("db", constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager g_ont_init = rdf_ont_init.GenerateGraph(graph) # Main Types entity_type = rdflib.URIRef( constants.DIGITAL_BUILDINGS_NS["EntityType"]) physical_location = rdflib.URIRef( constants.FACILITIES_NS["PhysicalLocation"]) state = rdflib.URIRef(constants.STATES_NS["State"]) field = rdflib.URIRef(constants.FIELDS_NS["Field"]) application = rdflib.URIRef( constants.DIGITAL_BUILDINGS_NS["Application"]) equipment = rdflib.URIRef(constants.DIGITAL_BUILDINGS_NS["Equipment"]) functionality = rdflib.URIRef(constants.HVAC_NS["Functionality"]) # Main Classes entity_type_class = (entity_type, rdflib.RDF.type, rdflib.OWL.Class) multi_state_class = (state, rdflib.RDF.type, rdflib.OWL.Class) physical_location_class = (physical_location, rdflib.RDF.type, rdflib.OWL.Class) field_class = (field, rdflib.RDF.type, rdflib.OWL.Class) application_class = (application, rdflib.RDF.type, rdflib.OWL.Class) equipment_class = (equipment, rdflib.RDF.type, rdflib.OWL.Class) functionality_class = (functionality, rdflib.RDF.type, rdflib.OWL.Class) # Assertions # Class assertions self.assertIn(entity_type_class, g_ont_init) self.assertIn(physical_location_class, g_ont_init) self.assertIn(multi_state_class, g_ont_init) self.assertIn(field_class, g_ont_init) self.assertIn(application_class, g_ont_init) self.assertIn(equipment_class, g_ont_init) self.assertIn(functionality_class, g_ont_init) # SubClasses assertions self.assertIn((entity_type, rdflib.RDFS.subClassOf, rdflib.OWL.Thing), g_ont_init) self.assertIn((physical_location, rdflib.RDFS.subClassOf, entity_type), g_ont_init) self.assertIn((state, rdflib.RDFS.subClassOf, rdflib.OWL.Thing), g_ont_init) self.assertIn((field, rdflib.RDFS.subClassOf, rdflib.OWL.Thing), g_ont_init) self.assertIn((application, rdflib.RDFS.subClassOf, entity_type), g_ont_init) self.assertIn((equipment, rdflib.RDFS.subClassOf, entity_type), g_ont_init) self.assertIn((functionality, rdflib.RDFS.subClassOf, entity_type), g_ont_init) # Disjoint assertions self.assertIn((application, rdflib.OWL.disjointWith, equipment), g_ont_init) self.assertIn((application, rdflib.OWL.disjointWith, functionality), g_ont_init) self.assertIn( (application, rdflib.OWL.disjointWith, physical_location), g_ont_init) self.assertIn((equipment, rdflib.OWL.disjointWith, application), g_ont_init) self.assertIn((equipment, rdflib.OWL.disjointWith, functionality), g_ont_init) self.assertIn((equipment, rdflib.OWL.disjointWith, physical_location), g_ont_init) self.assertIn((functionality, rdflib.OWL.disjointWith, application), g_ont_init) self.assertIn((functionality, rdflib.OWL.disjointWith, equipment), g_ont_init) self.assertIn( (functionality, rdflib.OWL.disjointWith, physical_location), g_ont_init) self.assertIn( (physical_location, rdflib.OWL.disjointWith, application), g_ont_init) self.assertIn( (physical_location, rdflib.OWL.disjointWith, functionality), g_ont_init) self.assertIn((physical_location, rdflib.OWL.disjointWith, equipment), g_ont_init)
def main(): global gFiles global dFiles updateFileLists() index = 1 numTriples = 0 print(gPath) noWork = [] missing = [] gurjapCounter = 0 megaGraph = Graph() megaGraph.parse("organizations.ttl", format="turtle") namespace_manager = namespace.NamespaceManager(megaGraph) bind_ns(namespace_manager, NS_DICT) # f = open("namesAndTriples6.txt", "w") for name in gFiles: # alGraph = Graph() dGraph = Graph() gGraph = Graph() fileName = name[0:-4] # alFileName = aPath + fileName + "-cf.txt" gFileName = gPath + name dFileName = dPath + fileName + "-cod.txt" codExists = False # graph.parse(dFileName,format="turtle") print(name[0:-4], isFileD(fileName + "-cod.txt")) # continue # if isFileA(fileName+ "-cf.txt") == False: # continue # if(os.path.isfile(dFileName)): # graph.parse(dFileName,format="turtle") # print("got one from deb") # debNum += 1 # alGraph.parse(alFileName,format="turtle") # if os.path.isfile(dFileName) == True: # continue if isFileD(fileName + "-cod.txt"): dGraph.parse(dFileName, format="turtle") codExists = True # else: # missing.append(fileName + "-cod.txt") gGraph.parse(gFileName, format="turtle") gurjapCounter += len(gGraph) graph = dGraph + gGraph namespace_manager = namespace.NamespaceManager(graph) bind_ns(namespace_manager, NS_DICT) index += 1 numTriples += len(graph) megaGraph += graph graph.serialize(destination=dtnPath_turtle + fileName + '.ttl', format='turtle') graph.serialize(destination=dtnPath_rdf + fileName + '.rdf', format='pretty-xml') # gFiles.remove(name) # print(len(aFiles)) # print(len(aFiles)) if codExists: dFiles.remove(fileName + "-cod.txt") # print("number of triples",numTriples) # f.close() megaGraph.serialize("BIOGRAPHY" + '.ttl', format='turtle') megaGraph.serialize("BIOGRAPHY" + '.rdf', format='pretty-xml') print("total Triples: ", numTriples) print("total Gurjap Triples: ", gurjapCounter) print("didnt work ==============") for no in noWork: print(no) print("wasn't there ============") for jk in missing: print(jk)
def testSubfieldsGraphGeneration(self): fake_units = 'fake_resources/subfields.yaml' file_contents = rdf_helper.ReadFile(fake_units) yaml_subfields = yaml_handler.ImportYamlFiles(file_contents) # Create global graph graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind('owl', namespace.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager g_subfields = rdflib_subfields_handler.GenerateGraph( yaml_subfields, graph) # Main Types subfield_type = rdflib.URIRef(constants.SUBFIELDS_NS['SubField']) point_type = rdflib.URIRef(constants.SUBFIELDS_NS['Point_type']) capacity = rdflib.URIRef(constants.SUBFIELDS_NS['Capacity']) capacity_description = ( 'A design parameter quantity. Ex: design motor ' 'power capacity. Is always a maximum limit.') point_type_class = (point_type, rdflib.RDF.type, rdflib.OWL.Class) capacity_class = (capacity, rdflib.RDF.type, rdflib.OWL.Class) measurement_descriptor = rdflib.URIRef( constants.SUBFIELDS_NS['Measurement_descriptor']) absolute = rdflib.URIRef(constants.SUBFIELDS_NS['Absolute']) absolute_description = ( 'Quality of media with respect to non-relativistic ' 'boudaries (e.g. absolute temperature).') measurement_descriptor_class = (measurement_descriptor, rdflib.RDF.type, rdflib.OWL.Class) absolute_class = (absolute, rdflib.RDF.type, rdflib.OWL.Class) measurement = rdflib.URIRef(constants.SUBFIELDS_NS['Measurement']) concentration = rdflib.URIRef(constants.SUBFIELDS_NS['Concentration']) concentration_description = ( 'Concentration of chemical (usually in parts ' 'per million or parts per billion).') measurement_class = (measurement, rdflib.RDF.type, rdflib.OWL.Class) concentration_class = (concentration, rdflib.RDF.type, rdflib.OWL.Class) descriptor = rdflib.URIRef(constants.SUBFIELDS_NS['Descriptor']) air = rdflib.URIRef(constants.SUBFIELDS_NS['Air']) air_description = 'Atmospheric air, either conditioned or unconditioned' descriptor_class = (descriptor, rdflib.RDF.type, rdflib.OWL.Class) air_class = (air, rdflib.RDF.type, rdflib.OWL.Class) component = rdflib.URIRef(constants.SUBFIELDS_NS['Component']) coil = rdflib.URIRef(constants.SUBFIELDS_NS['Coil']) coil_description = ('Component that exchanges heat between two media ' 'streams.') component_class = (component, rdflib.RDF.type, rdflib.OWL.Class) coil_class = (coil, rdflib.RDF.type, rdflib.OWL.Class) aggregation = rdflib.URIRef(constants.SUBFIELDS_NS['Aggregation']) max_ref = rdflib.URIRef(constants.SUBFIELDS_NS['Max']) min_ref = rdflib.URIRef(constants.SUBFIELDS_NS['Min']) max_description = 'Maximum value (e.g. Max_Cooling_Air_Flow_Setpoint)' aggregation_class = (aggregation, rdflib.RDF.type, rdflib.OWL.Class) max_class = (max_ref, rdflib.RDF.type, rdflib.OWL.Class) min_class = (min_ref, rdflib.RDF.type, rdflib.OWL.Class) # Assertions self.assertIn(capacity_class, g_subfields) self.assertIn(point_type_class, g_subfields) self.assertIn((capacity, rdflib.RDFS.comment, rdflib.Literal(capacity_description)), g_subfields) self.assertIn(measurement_descriptor_class, g_subfields) self.assertIn(absolute_class, g_subfields) self.assertIn((absolute, rdflib.RDFS.comment, rdflib.Literal(absolute_description)), g_subfields) self.assertIn(measurement_class, g_subfields) self.assertIn(concentration_class, g_subfields) self.assertIn((concentration, rdflib.RDFS.comment, rdflib.Literal(concentration_description)), g_subfields) self.assertIn(descriptor_class, g_subfields) self.assertIn(air_class, g_subfields) self.assertIn( (air, rdflib.RDFS.comment, rdflib.Literal(air_description)), g_subfields) self.assertIn(component_class, g_subfields) self.assertIn(coil_class, g_subfields) self.assertIn( (coil, rdflib.RDFS.comment, rdflib.Literal(coil_description)), g_subfields) self.assertIn(aggregation_class, g_subfields) self.assertIn(min_class, g_subfields) self.assertIn(max_class, g_subfields) self.assertIn( (max_ref, rdflib.RDFS.comment, rdflib.Literal(max_description)), g_subfields) # SubClasses assertions self.assertIn((point_type, rdflib.RDFS.subClassOf, subfield_type), g_subfields) self.assertIn((capacity, rdflib.RDFS.subClassOf, point_type), g_subfields) self.assertIn((aggregation, rdflib.RDFS.subClassOf, subfield_type), g_subfields) self.assertIn((min_ref, rdflib.RDFS.subClassOf, aggregation), g_subfields) self.assertIn((max_ref, rdflib.RDFS.subClassOf, aggregation), g_subfields)
def testCreatesStandardFieldNameCompositionInGraph(self): # Prepare data # Build First Graph with the method graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind('owl', rdflib.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) graph.namespace_manager = namespace_manager list_composition = ['Cooling', 'Air', 'Flowrate', 'Sensor'] standard_field_name = 'cooling_air_flowrate_sensor_1' is_composed_of_property = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['isComposedOf'], baseType=infixowl.OWL_NS.ObjectProperty, graph=graph) point_type = infixowl.Class( identifier=constants.SUBFIELDS_NS['Point_type'], graph=graph) infixowl.Class(identifier=constants.SUBFIELDS_NS['Sensor'], graph=graph, subClassOf=[point_type]) field = infixowl.Class(identifier=constants.FIELDS_NS['Field'], graph=graph) infixowl.Class( identifier=constants.FIELDS_NS['Cooling_air_flowrate_sensor_1'], graph=graph, subClassOf=[field]) graph = rdf_helper.CreatesStandardFieldNameCompositionInGraph( list_composition=list_composition, standard_field_name=standard_field_name, is_composed_of_property=is_composed_of_property, graph=graph) # Build a second graph that is expected expected_graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager_expected = namespace.NamespaceManager(expected_graph) namespace_manager_expected.bind('owl', rdflib.OWL) namespace_manager_expected.bind('db', constants.DIGITAL_BUILDINGS_NS) expected_graph.namespace_manager = namespace_manager_expected field_expected = infixowl.Class( identifier=constants.FIELDS_NS['Field'], graph=expected_graph) point_type_expected = infixowl.Class( identifier=constants.SUBFIELDS_NS['Point_type'], graph=expected_graph) cooling_expected = infixowl.Class( identifier=constants.SUBFIELDS_NS['Cooling'], graph=expected_graph) air_expected = infixowl.Class(identifier=constants.SUBFIELDS_NS['Air'], graph=expected_graph) flowrate_expected = infixowl.Class( identifier=constants.SUBFIELDS_NS['Flowrate'], graph=expected_graph) sensor_expected = infixowl.Class( identifier=constants.SUBFIELDS_NS['Sensor'], graph=expected_graph, subClassOf=[point_type_expected]) sfn_expected = infixowl.Class( identifier=constants.FIELDS_NS['Cooling_air_flowrate_sensor_1'], graph=expected_graph, subClassOf=[field_expected]) is_composed_of_property_expected = infixowl.Property( identifier=constants.DIGITAL_BUILDINGS_NS['isComposedOf'], baseType=infixowl.OWL_NS.ObjectProperty, graph=expected_graph) concat = sensor_expected & flowrate_expected concat += air_expected concat += cooling_expected sfn_expected.subClassOf = [ is_composed_of_property_expected | infixowl.only | concat ] # Check if they are similar self.assertTrue(compare.similar(graph, expected_graph))
def Generate(resource_path): """Generates the RDF from Yaml. Handles the overall orchestration of the process. Args: resource_path: the path where to find the yaml files. Returns: graph: the rdf graph """ # Create global graph graph = rdflib.Graph() # Bind the OWL and Digital Buildings name spaces namespace_manager = namespace.NamespaceManager(graph) namespace_manager.bind('owl', namespace.OWL) namespace_manager.bind('db', constants.DIGITAL_BUILDINGS_NS) namespace_manager.bind('dcterms', 'http://purl.org/dc/terms/') graph.namespace_manager = namespace_manager # Initialize the ontology headers digital_building_ont = infixowl.Ontology(identifier=rdflib.URIRef( constants.DB), graph=graph) digital_building_ont.label = rdflib.Literal('Digital Buildings') graph.add((digital_building_ont.identifier, constants.DCTERMS['title'], rdflib.Literal('Digital Buildings Ontology'))) graph.add((digital_building_ont.identifier, constants.DCTERMS['license'], rdflib.Literal(constants.GITHUB_LICENSE))) graph.add((digital_building_ont.identifier, constants.DCTERMS['modified'], rdflib.Literal(rdf_helper.GetTimeNow()))) graph.add((digital_building_ont.identifier, rdflib.OWL['versionInfo'], rdflib.Literal(constants.ONT_VERSION))) graph.add((digital_building_ont.identifier, constants.DCTERMS['creator'], rdflib.Literal(constants.AUTHORS))) graph.add( (digital_building_ont.identifier, constants.DCTERMS['contributor'], rdflib.Literal(constants.CONTRIBUTORS))) graph.add((digital_building_ont.identifier, rdflib.OWL['seeAlso'], rdflib.Literal(constants.GITHUB))) graph.add( (digital_building_ont.identifier, constants.DCTERMS['description'], rdflib.Literal(constants.ONT_DESCRIPTION))) # Initialize the main classes graph = rdf_ont_init.GenerateGraph(graph) # Handle the Facilities file facilities_file = rdf_helper.ReadFile(resource_path + FACILITIES) yaml_facilities = yaml_handler.ImportYamlFiles(facilities_file) graph = rdflib_facilities_handler.GenerateGraph(yaml_facilities, graph) # Handle the Units file units_file = rdf_helper.ReadFile(resource_path + UNITS) yaml_units = yaml_handler.ImportYamlFiles(units_file) graph = rdflib_units_handler.GenerateGraph(yaml_units, graph) # Handle the Subfields file subfields_file = rdf_helper.ReadFile(resource_path + SUBFIELDS) yaml_subfields = yaml_handler.ImportYamlFiles(subfields_file) graph = rdflib_subfields_handler.GenerateGraph(yaml_subfields, graph) # # Handle the Abstract file abstract_file = rdf_helper.ReadFile(resource_path + ABSTRACT) yaml_abstract = yaml_handler.ImportYamlFiles(abstract_file) graph = rdflib_function_handler.GenerateGraph(yaml_abstract, graph) # # Handle the General Types file generaltypes_file = rdf_helper.ReadFile(resource_path + GENERALTYPES) yaml_generaltypes = yaml_handler.ImportYamlFiles(generaltypes_file) graph = rdflib_generaltypes_handler.GenerateGraph(yaml_generaltypes, graph) # Handle the states file states_file = rdf_helper.ReadFile(resource_path + STATES) yaml_states = yaml_handler.ImportYamlFiles(states_file) graph = rdflib_states_handler.GenerateGraph(yaml_states, graph) # Handle the HVAC files for hvac_type in carson_types: type_file = rdf_helper.ReadFile(resource_path + hvac_type) yaml_type = yaml_handler.ImportYamlFiles(type_file) graph = rdflib_carson_types_handler.GenerateGraph( yaml_type, graph, constants.HVAC_NS) return graph