Skip to content

Commit f391339

Browse files
author
Marin Visscher
committed
Enable duplication to functional-sqlite
1 parent de2f785 commit f391339

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

bw_functional/convert.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def convert(cls, database_dict: dict):
2525
return converted
2626

2727
@classmethod
28-
def convert_process(cls, key, ds):
28+
def convert_process(cls, key, ds, convert_exchanges=True):
2929
ds["type"] = "process"
3030
production = [x for x in enumerate(ds["exchanges"]) if x[1]["type"] == "production"]
3131
if len(production) > 1:
@@ -34,7 +34,8 @@ def convert_process(cls, key, ds):
3434
act = bd.get_activity(key)
3535
raise ValueError("Cannot convert a process with multiple production exchanges to functional_sqlite.", act)
3636

37-
cls.convert_exchanges(key, ds)
37+
if convert_exchanges:
38+
cls.convert_exchanges(key, ds)
3839

3940
if not production:
4041
function_key, function = cls.create_function(key, ds)
@@ -53,7 +54,9 @@ def create_function(key, ds, amount=1.0, name=None):
5354

5455
function = {
5556
"type": "product" if amount > 0 else "waste",
56-
"name": function_name,
57+
"name": ds.get("name"),
58+
"reference product": function_name,
59+
"product": function_name,
5760
"exchanges": [],
5861
"database": ds["database"],
5962
"code": function_code,
@@ -81,6 +84,28 @@ def convert_exchanges(key, ds) -> None:
8184
continue
8285
exc["input"] = (database, exc["input"][1] + "_function")
8386

87+
@classmethod
88+
def duplicate_node(cls, node: bd.Node, target_database_name: str) -> list[bd.Node]:
89+
database = FunctionalSQLiteDatabase(node.get("database"))
90+
database_dict = database.as_dict()
91+
92+
converted_dict = cls.convert_process(node.key, node.as_dict())
93+
converted_dict = database.relabel_data(converted_dict, node.get("database"), node.get("database"))
94+
95+
new_nodes = []
96+
for key, ds in converted_dict.items():
97+
if "id" in ds:
98+
del ds["id"]
99+
100+
ds["key"] = key
101+
ds["database"] = node.get("database")
102+
103+
new_node = bd.Node(**ds)
104+
new_node.save()
105+
new_nodes.append(new_node)
106+
107+
return new_nodes
108+
84109

85110
def convert_functional_sqlite_to_sqlite(database_dict: dict):
86111
return FunctionalSQLiteToSQLite.convert(database_dict)

0 commit comments

Comments
 (0)