Esempio n. 1
0
    def _get_interface_from_field(self, field_name: str,
                                  processor: Processor) -> Factor:
        interface_name = self._fields[field_name]

        if interface_name is None:
            raise CommandExecutionError(
                f"No interface has been defined for field '{field_name}'.")

        interface = processor.factors_find(interface_name)
        if not interface:
            raise CommandExecutionError(
                f"The interface '{interface_name}' has not been found in "
                f"processor '{processor.name}'.")

        return interface
Esempio n. 2
0
def prepare_model(state) -> NoReturn:
    """
    Modify the state so that:
    * Implicit references of Interfaces to subcontexts are materialized
      * Creating processors
      * Creating interfaces in these processors
      * Creating relationships in these processors

    :param state:
    """

    # TODO: currently when an interface is defined as a Scale from two or more interfaces, the computed values are
    #  added while the intuition tells us that only one scale should be defined. We have to give a warning message
    #  if this situation happens.

    # Registry and the other objects also
    glb_idx, _, _, _, _ = get_case_study_registry_objects(state)
    # Prepare a Query to obtain ALL interfaces
    query = BasicQuery(state)
    filt = {}
    objs = query.execute([Factor], filt)
    for iface in objs[Factor]:  # type: Factor
        if strcmp(
                iface.processor.instance_or_archetype, 'Archetype') or strcmp(
                    iface.processor.instance_or_archetype, 'No'):
            continue

        # If the Interface is connected to a "Subcontext" different than the owning Processor
        if iface.opposite_processor_type:
            if iface.opposite_processor_type.lower(
            ) != iface.processor.subsystem_type.lower():
                # Check if the interface has flow relationships
                # TODO An alternative is to search "observations" of type FactorsRelationDirectedFlowObservation
                #      in the same "iface"

                if iface.orientation.lower() == "input":
                    parameter = {"target": iface}
                else:
                    parameter = {"source": iface}

                relations = glb_idx.get(
                    FactorsRelationDirectedFlowObservation.partial_key(
                        **parameter))

                # If it does not have flow relationships:
                #  * define default Processor name and retrieve it (or if it does not exist, create it)
                #  * create an Interface into that Processor and a Flow Relationship
                if len(relations) == 0:
                    # Define the name of a Processor in the same context but in different subcontext
                    p_name = iface.processor.processor_system + "_" + iface.opposite_processor_type
                    p = glb_idx.get(Processor.partial_key(p_name))
                    if len(p) == 0:
                        attributes = {
                            'subsystem_type': iface.opposite_processor_type,
                            'processor_system':
                            iface.processor.processor_system,
                            'functional_or_structural': 'Functional',
                            'instance_or_archetype': 'Instance'
                            # 'stock': None
                        }

                        p = Processor(p_name, attributes=attributes)
                        glb_idx.put(p.key(), p)
                    else:
                        p = p[0]

                    attributes = {
                        'sphere':
                        'Technosphere'
                        if iface.opposite_processor_type.lower()
                        in ["local", "external"] else 'Biosphere',
                        'roegen_type':
                        iface.roegen_type,
                        'orientation':
                        "Input"
                        if iface.orientation.lower() == "output" else "Output",
                        'opposite_processor_type':
                        iface.processor.subsystem_type
                    }

                    # Create Interface (if it does not exist)
                    if not p.factors_find(iface.taxon.name):
                        f = Factor.create_and_append(
                            name=iface.taxon.name,
                            processor=p,
                            in_processor_type=FactorInProcessorType(
                                external=False,
                                incoming=iface.orientation.lower() ==
                                "output"),
                            attributes=attributes,
                            taxon=iface.taxon)

                        glb_idx.put(f.key(), f)

                    # Create Flow Relationship
                    if iface.orientation.lower() == "output":
                        source = iface
                        target = f
                    else:
                        source = f
                        target = iface

                    fr = FactorsRelationDirectedFlowObservation.create_and_append(
                        source=source, target=target, observer=None)
                    glb_idx.put(fr.key(), fr)