Esempio n. 1
0
    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 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
Esempio n. 3
0
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
Esempio n. 4
0
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