<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
Define the circuit class
"""

import scr.logic.components.component as cmp
import scr.logic.nodes.node as nd
import scr.logic.refrigerants.refrigerant as ref
from scr.logic.base_classes import Element, Identifier
from scr.logic.restricted_inputs import StrRestricted
from scr.logic.errors import IdDuplicatedError, ValuePropertyError, CircuitBuilderError, BuildError
from scr.logic.warnings import CircuitBuilderWarning


class Circuit(Element):
    BASIC_PROPERTIES = 'basic properties'
    COMPONENTS = 'components'
    COMPONENT_TYPE = 'type'
    IDENTIFIER = 'id'
    INLET_NODES = 'inlet nodes'
    NAME = 'name'
    NODES = 'nodes'
    OPTIONAL_PROPERTIES = 'optional properties'
    OUTLET_NODES = 'outlet nodes'
    REFRIGERANT = 'refrigerant'
    REF_LIB = 'refrigerant_library'

    def __init__(self, name, id_, refrigerant, refrigerant_library):
        super().__init__(name, id_)
        self._ref_lib = refrigerant_library
        self._refrigerant = ref.Refrigerant.build(self.get_refrigerant_library(), refrigerant)
        self._nodes = {}
        self._components = {}
        self._mass_flows = []

    def _add_component(self, component_object):
        # Add component object to circuit.
        self._components[component_object.get_id()] = component_object

    def _add_node(self, node_object):
        # Add node object to circuit.
        self._nodes[node_object.get_id()] = node_object

    def configure(self):
        # Configure circuit to solve it later
        self._mass_flows = self._create_mass_flows()
        for node_id in self.get_nodes():
            node = self.get_node(node_id)
            # A list of all mass_flows of te circuit is passes to node. Later, will be configurated which mass flow it
            # is the correct.
            node.configure(self.get_components(), self.get_mass_flows())

        for component_id in self.get_components():
            component = self.get_component(component_id)
            component.configure(self.get_nodes())

        self._link_nodes_mass_flows()

    def _create_mass_flows(self):
        separate_components = self.search_components_by_type(cmp.Component.SEPARATOR_FLOW)
        return [0.0] * (2 * len(separate_components) + 1)

    def _link_nodes_mass_flows(self):
        # Search components that modify flows
        mix_components = self.search_components_by_type(cmp.Component.MIXER_FLOW)
        separate_components = self.search_components_by_type(cmp.Component.SEPARATOR_FLOW)
        flow_components = {**separate_components, **mix_components}
        # Create and fill id_mass_flow in nodes.
        if len(flow_components) == 0:
            id_mass_flow = 0
            id_component = self.get_components_id()[0]
            component = self.get_component(id_component)
            id_node = component.get_id_outlet_nodes()[0]
            outlet_node = component.get_outlet_node(id_node)
            self._fill_id_mass_flow_nodes(id_mass_flow, outlet_node, {id_component: component})
        else:
            id_mass_flow = -1
            for id_component in mix_components:
                id_mass_flow += 1
                component = self.get_component(id_component)
                # A mix component only have one outlet
                id_outlet_node = component.get_id_outlet_nodes()[0]
                outlet_node = component.get_outlet_node(id_outlet_node)
                self._fill_id_mass_flow_nodes(id_mass_flow, outlet_node, flow_components)

            for id_component in separate_components:
                component = self.get_component(id_component)
                outlet_nodes = component.get_outlet_nodes()
                for id_node in outlet_nodes:
                    node = component.get_outlet_node(id_node)
                    id_mass_flow += 1
                    self._fill_id_mass_flow_nodes(id_mass_flow, node, flow_components)

    def _fill_id_mass_flow_nodes(self, id_mass_flow, node, stop_components):
        while True:
            node.set_id_mass_flow(id_mass_flow)
            # This components only have one outlet because is not a flow component.
            inlet_component = node.get_inlet_component_attached()
            id_inlet_component = inlet_component.get_id()
            if id_inlet_component in stop_components:
                break
            else:
                node = inlet_component.get_outlet_node(inlet_component.get_id_outlet_nodes()[0])

    def _load_components(self, input_circuit):
        components = {}
        nodes = self.get_nodes()
        for component in input_circuit[self.COMPONENTS]:
            identifier = component[self.IDENTIFIER]
            if identifier in components:
                raise IdDuplicatedError("There are components duplicated. Identifier %i is duplicated", identifier)
            components[identifier] = cmp.Component.build(component, nodes)
        return components

    def _load_nodes(self, input_circuit, ref_lib):
        nodes = {}
        refrigerant = self.get_refrigerant()
        for node in input_circuit[self.NODES]:
            identifier = node[self.IDENTIFIER]
            if identifier in nodes:
                raise IdDuplicatedError("There are nodes duplicated.%i is duplicated", identifier)
            nodes[identifier] = nd.Node.build(node, refrigerant, ref_lib)
        return nodes

    def _fill_id_mass_flow_inlet_nodes(self, prior_id_mass_flow, prior_prior_id_mass_flow, component,
                                       total_id_mass_flow):
        # TODO  check if prior_id_mass_flow is not affected by the recurrence
        inlet_nodes = component.get_inlet_nodes()
        outlet_nodes = component.get_outlet_nodes()
        total_next_outlet_nodes = len(outlet_nodes)
        if total_next_outlet_nodes > 1:
            id_mass_flow = prior_prior_id_mass_flow.pop()
        else:
            id_mass_flow = prior_id_mass_flow
        total_nodes = len(inlet_nodes)
        for i in inlet_nodes:
            node = inlet_nodes[i]
            if total_nodes > 1:
                prior_prior_id_mass_flow.append(prior_id_mass_flow)
                id_mass_flow = total_id_mass_flow
                total_id_mass_flow += 1
            if not node.is_mass_flow_init():
                node.set_id_mass_flow(id_mass_flow)
                next_components = node.get_components_attached()
                for next_component in next_components:
                    next_component = self.get_component(next_component)
                    self._fill_id_mass_flow_inlet_nodes(id_mass_flow, prior_prior_id_mass_flow, next_component,
                                                        total_id_mass_flow)

    def get_component(self, id_component):
        return self.get_components()[id_component]<commit_msg>Eliminated unused circuit methods<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
Define the circuit class
"""

import scr.logic.components.component as cmp
import scr.logic.nodes.node as nd
import scr.logic.refrigerants.refrigerant as ref
from scr.logic.base_classes import Element, Identifier
from scr.logic.restricted_inputs import StrRestricted
from scr.logic.errors import IdDuplicatedError, ValuePropertyError, CircuitBuilderError, BuildError
from scr.logic.warnings import CircuitBuilderWarning


class Circuit(Element):
    BASIC_PROPERTIES = 'basic properties'
    COMPONENTS = 'components'
    COMPONENT_TYPE = 'type'
    IDENTIFIER = 'id'
    INLET_NODES = 'inlet nodes'
    NAME = 'name'
    NODES = 'nodes'
    OPTIONAL_PROPERTIES = 'optional properties'
    OUTLET_NODES = 'outlet nodes'
    REFRIGERANT = 'refrigerant'
    REF_LIB = 'refrigerant_library'

    def __init__(self, name, id_, refrigerant, refrigerant_library):
        super().__init__(name, id_)
        self._ref_lib = refrigerant_library
        self._refrigerant = ref.Refrigerant.build(self.get_refrigerant_library(), refrigerant)
        self._nodes = {}
        self._components = {}
        self._mass_flows = []

    def _add_component(self, component_object):
        # Add component object to circuit.
        self._components[component_object.get_id()] = component_object

    def _add_node(self, node_object):
        # Add node object to circuit.
        self._nodes[node_object.get_id()] = node_object

    def configure(self):
        # Configure circuit to solve it later
        self._mass_flows = self._create_mass_flows()
        for node_id in self.get_nodes():
            node = self.get_node(node_id)
            # A list of all mass_flows of te circuit is passes to node. Later, will be configurated which mass flow it
            # is the correct.
            node.configure(self.get_components(), self.get_mass_flows())

        for component_id in self.get_components():
            component = self.get_component(component_id)
            component.configure(self.get_nodes())

        self._link_nodes_mass_flows()

    def _create_mass_flows(self):
        separate_components = self.search_components_by_type(cmp.Component.SEPARATOR_FLOW)
        return [0.0] * (2 * len(separate_components) + 1)

    def _link_nodes_mass_flows(self):
        # Search components that modify flows
        mix_components = self.search_components_by_type(cmp.Component.MIXER_FLOW)
        separate_components = self.search_components_by_type(cmp.Component.SEPARATOR_FLOW)
        flow_components = {**separate_components, **mix_components}
        # Create and fill id_mass_flow in nodes.
        if len(flow_components) == 0:
            id_mass_flow = 0
            id_component = self.get_components_id()[0]
            component = self.get_component(id_component)
            id_node = component.get_id_outlet_nodes()[0]
            outlet_node = component.get_outlet_node(id_node)
            self._fill_id_mass_flow_nodes(id_mass_flow, outlet_node, {id_component: component})
        else:
            id_mass_flow = -1
            for id_component in mix_components:
                id_mass_flow += 1
                component = self.get_component(id_component)
                # A mix component only have one outlet
                id_outlet_node = component.get_id_outlet_nodes()[0]
                outlet_node = component.get_outlet_node(id_outlet_node)
                self._fill_id_mass_flow_nodes(id_mass_flow, outlet_node, flow_components)

            for id_component in separate_components:
                component = self.get_component(id_component)
                outlet_nodes = component.get_outlet_nodes()
                for id_node in outlet_nodes:
                    node = component.get_outlet_node(id_node)
                    id_mass_flow += 1
                    self._fill_id_mass_flow_nodes(id_mass_flow, node, flow_components)

    def _fill_id_mass_flow_nodes(self, id_mass_flow, node, stop_components):
        while True:
            node.set_id_mass_flow(id_mass_flow)
            # This components only have one outlet because is not a flow component.
            inlet_component = node.get_inlet_component_attached()
            id_inlet_component = inlet_component.get_id()
            if id_inlet_component in stop_components:
                break
            else:
                node = inlet_component.get_outlet_node(inlet_component.get_id_outlet_nodes()[0])

    def _fill_id_mass_flow_inlet_nodes(self, prior_id_mass_flow, prior_prior_id_mass_flow, component,
                                       total_id_mass_flow):
        # TODO  check if prior_id_mass_flow is not affected by the recurrence
        inlet_nodes = component.get_inlet_nodes()
        outlet_nodes = component.get_outlet_nodes()
        total_next_outlet_nodes = len(outlet_nodes)
        if total_next_outlet_nodes > 1:
            id_mass_flow = prior_prior_id_mass_flow.pop()
        else:
            id_mass_flow = prior_id_mass_flow
        total_nodes = len(inlet_nodes)
        for i in inlet_nodes:
            node = inlet_nodes[i]
            if total_nodes > 1:
                prior_prior_id_mass_flow.append(prior_id_mass_flow)
                id_mass_flow = total_id_mass_flow
                total_id_mass_flow += 1
            if not node.is_mass_flow_init():
                node.set_id_mass_flow(id_mass_flow)
                next_components = node.get_components_attached()
                for next_component in next_components:
                    next_component = self.get_component(next_component)
                    self._fill_id_mass_flow_inlet_nodes(id_mass_flow, prior_prior_id_mass_flow, next_component,
                                                        total_id_mass_flow)

    def get_component(self, id_component):
        return self.get_components()[id_component]
{'fraction_non_alphanumeric': 0.0705756276791182, 'fraction_numerical': 0.003061849357011635, 'mean_word_length': 2.5755336617405584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1638400', 'n_tokens_mistral': 3911, 'n_tokens_neox': 3747, 'n_words': 810}