Skip to content

Commit

Permalink
add the possibility to make library import another library
Browse files Browse the repository at this point in the history
the importing order is an depth-first travelsal of the tree going from every library to the root or an already imported library
the algo :
 - checks if there is cycles and raise error if so
 - can import models form 2 separated tree
 - doesnt check if ports are defined twice

no test available
the code isnt commented
  • Loading branch information
ChouaneLouis committed May 23, 2024
1 parent f45d135 commit db6eda1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
46 changes: 40 additions & 6 deletions src/andromede/main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,47 @@ class AntaresTimeSeriesImportError(Exception):
pass


def input_models(model_path: List[Path]) -> Library:
with model_path[0].open("r") as file:
lib = resolve_library(parse_yaml_library(file))
for path in model_path[1:]:
def input_models(model_paths: List[Path]) -> Library:
yaml_libraries = {}
for path in model_paths:
with path.open("r") as file:
lib = resolve_library(parse_yaml_library(file), [lib])
return lib
yaml_lib = parse_yaml_library(file)
if yaml_lib.id in yaml_libraries:
raise ValueError(f"the identifier: {yaml_lib.id} is defined twice")
yaml_libraries[yaml_lib.id] = yaml_lib

todo = list(yaml_libraries.values())
did = list()
import_stack = []
output_lib = Library(port_types={}, models={})

while todo:
next_lib = todo.pop()
if next_lib.id in did:
continue
else:
import_stack.append(next_lib)
while import_stack:
if import_stack[-1].dependence:
if import_stack[-1].dependence in did:
lib = resolve_library(import_stack[-1], [output_lib])

output_lib.models.update(lib.models)
output_lib.port_types.update(lib.port_types)

did.append(import_stack.pop().id)
elif yaml_libraries[import_stack[-1].dependence] in import_stack:
raise Exception("importing loop in yaml libraries")
else:
import_stack.append(yaml_libraries[import_stack[-1].dependence])
else:
lib = resolve_library(import_stack[-1], [output_lib])

output_lib.models.update(lib.models)
output_lib.port_types.update(lib.port_types)

did.append(import_stack.pop().id)
return output_lib


def input_database(study_path: Path, timeseries_path: Optional[Path]) -> DataBase:
Expand Down
1 change: 1 addition & 0 deletions src/andromede/model/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Config:

class InputLibrary(BaseModel):
id: str
dependence: Optional[str] = None
port_types: List[InputPortType] = Field(default_factory=list)
models: List[InputModel] = Field(default_factory=list)

Expand Down
2 changes: 0 additions & 2 deletions src/andromede/model/resolve_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ def resolve_library(
port_types.extend(lib.port_types.values())
port_types_dict = dict((p.id, p) for p in port_types)
models = [_resolve_model(m, port_types_dict) for m in input_lib.models]
for lib in preloaded_libraries:
models.extend(lib.models.values())
return library(port_types, models)


Expand Down
19 changes: 2 additions & 17 deletions tests/functional/input/models/lib_2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,12 @@
#
# This file is part of the Antares project.
library:
id: basic
id: basic 2
dependence: basic
description: Basic library

port-types:
- id: flow
description: A port which transfers power flow
fields:
- name: flow

models:


- id: node
description: A basic balancing node model
ports:
- name: injection_port
type: flow
binding-constraints:
- name: balance
expression: sum_connections(injection_port.flow) = 0

- id: spillage
description: A basic spillage model
parameters:
Expand Down

0 comments on commit db6eda1

Please sign in to comment.