Ejemplo n.º 1
0
def prepare_partial_key_dictionary():
    prd = PartialRetrievalDictionary()
    oer = Observer("tester")
    p0 = Processor("A1")
    p1 = Processor("A2")
    p2 = Processor("B")
    p3 = Processor("C")
    prd.put({"_type": "Processor", "_name": "A1"}, p0)
    prd.put({"_type": "Processor", "_name": "A2"}, p1)
    prd.put({"_type": "Processor", "_name": "B"}, p2)
    prd.put({"_type": "Processor", "_name": "C"}, p3)
    prd.put({
        "_type": "PartOf",
        "_parent": "A1",
        "_child": "B"
    }, ProcessorsRelationPartOfObservation(p0, p2, oer))
    prd.put({
        "_type": "PartOf",
        "_parent": "A2",
        "_child": "B"
    }, ProcessorsRelationPartOfObservation(p1, p2, oer))
    prd.put({
        "_type": "PartOf",
        "_parent": "B",
        "_child": "C"
    }, ProcessorsRelationPartOfObservation(p2, p3, oer))
    return prd
Ejemplo 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)