-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# 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/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
import typing | ||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel, Field | ||
from yaml import safe_load | ||
|
||
from andromede.model.parsing import InputModel | ||
|
||
|
||
def parse_yaml_components(input_components: typing.TextIO) -> "InputComponent": | ||
tree = safe_load(input_components) | ||
return InputComponent.model_validate(tree["study"]) | ||
|
||
|
||
# Design note: actual parsing and validation is delegated to pydantic models | ||
def _to_kebab(snake: str) -> str: | ||
return snake.replace("_", "-") | ||
|
||
|
||
class InputPortConnections(BaseModel): | ||
id: str | ||
component1: str | ||
port_1: str | ||
component2: str | ||
port_2: str | ||
|
||
|
||
class InputComponent(BaseModel): | ||
nodes: List[InputModel] = Field(default_factory=list) | ||
components: List[InputModel] = Field(default_factory=list) | ||
connections: List[InputPortConnections] = Field(default_factory=list) | ||
|
||
class Config: | ||
alias_generator = _to_kebab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright (c) 2024, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# 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/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
from typing import List, Optional | ||
|
||
from andromede.model.parsing import InputModel | ||
from andromede.model.resolve_library import _resolve_model_identifier | ||
from andromede.study import Component, PortRef | ||
from andromede.study.components import Components, components | ||
from andromede.study.parsing import InputComponent, InputPortConnections | ||
|
||
|
||
def resolve_components_and_cnx(input_comp: InputComponent) -> Components: | ||
""" | ||
Resolves: | ||
- components to be used for study | ||
- connections between components""" | ||
components_list = [_resolve_component(m, m.id) for m in input_comp.components] | ||
components_list.extend(_resolve_component(n, n.id) for n in input_comp.nodes) | ||
connections = [] | ||
|
||
for cnx in input_comp.connections: | ||
resolved_cnx = _resolve_connections(cnx, components_list) | ||
connections.extend(resolved_cnx) | ||
|
||
return components(components_list, connections) | ||
|
||
|
||
def _resolve_component(model_for_component: InputModel, component_id: str) -> Component: | ||
return Component( | ||
model=_resolve_model_identifier(model_for_component), id=component_id | ||
) | ||
|
||
|
||
def _resolve_connections( | ||
connection: InputPortConnections, | ||
components_list: List[Component], | ||
) -> List[PortRef]: | ||
cnx_component1 = connection.component1 | ||
cnx_component2 = connection.component2 | ||
port1 = connection.port_1 | ||
port2 = connection.port_2 | ||
|
||
component_1 = _get_component_by_id(components_list, cnx_component1) | ||
component_2 = _get_component_by_id(components_list, cnx_component2) | ||
assert component_1 is not None and component_2 is not None | ||
port_ref_1 = PortRef(component_1, port1) | ||
port_ref_2 = PortRef(component_2, port2) | ||
return [port_ref_1, port_ref_2] | ||
|
||
|
||
def _get_component_by_id( | ||
components_list: List[Component], component_id: str | ||
) -> Optional[Component]: | ||
components_dict = {component.id: component for component in components_list} | ||
return components_dict.get(component_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pathlib import Path | ||
|
||
from andromede.study.parsing import parse_yaml_components | ||
from andromede.study.resolve_components import resolve_components_and_cnx | ||
|
||
|
||
def test_parsing_components_ok(data_dir: Path): | ||
compo_file = data_dir / "components.yml" | ||
|
||
with compo_file.open() as c: | ||
input_compo = parse_yaml_components(c) | ||
assert len(input_compo.components) == 2 | ||
assert len(input_compo.nodes) == 1 | ||
assert len(input_compo.connections) == 2 | ||
|
||
result = resolve_components_and_cnx(input_compo) | ||
|
||
assert len(result.components) == 3 | ||
assert len(result.connections) == 4 |