From 11e732f9b19d79a1e7a96fa646dfcd435e10e040 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:45:22 +0800 Subject: [PATCH] Refine reaction api --- api/__init__.py | 2 +- api/s19_reactions.py | 118 +++++++++++++++++++++++-------------------- test_tjnetwork.py | 92 ++++++++++++++------------------- tjnetwork.py | 12 ++--- 4 files changed, 107 insertions(+), 117 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 79addc4..5443d37 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -65,7 +65,7 @@ from .s17_quality import get_quality_schema, get_quality, set_quality from .s18_sources import SOURCE_TYPE_CONCEN, SOURCE_TYPE_MASS, SOURCE_TYPE_FLOWPACED, SOURCE_TYPE_SETPOINT from .s18_sources import get_source_schema, get_source, set_source, add_source, delete_source -from .s19_reactions import get_global_reaction_schema, get_global_reaction, set_global_reaction +from .s19_reactions import get_reaction_schema, get_reaction, set_reaction from .s19_reactions import get_pipe_reaction_schema, get_pipe_reaction, set_pipe_reaction from .s19_reactions import get_tank_reaction_schema, get_tank_reaction, set_tank_reaction diff --git a/api/s19_reactions.py b/api/s19_reactions.py index 4f9c6f2..be46b66 100644 --- a/api/s19_reactions.py +++ b/api/s19_reactions.py @@ -1,76 +1,84 @@ from .operation import * -def get_global_reaction_schema(name: str) -> dict[str, dict[str, Any]]: - return { 'order_bulk' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'order_wall' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'order_tank' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'global_bulk' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'global_wall' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'limiting_potential' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'roughness_correlation' : {'type': 'float' , 'optional': True , 'readonly': False} } +element_schema = {'type': 'str' , 'optional': True , 'readonly': False} -def get_global_reaction(name: str) -> dict[str, Any]: - gr = read(name, f"select * from reactions_global") +def get_reaction_schema(name: str) -> dict[str, dict[str, Any]]: + return { 'ORDER BULK' : element_schema, + 'ORDER WALL' : element_schema, + 'ORDER TANK' : element_schema, + 'GLOBAL BULK' : element_schema, + 'GLOBAL WALL' : element_schema, + 'LIMITING POTENTIAL' : element_schema, + 'ROUGHNESS CORRELATION' : element_schema } + + +def get_reaction(name: str) -> dict[str, Any]: + ts = read_all(name, f"select * from reactions") d = {} - d['order_bulk'] = float(gr['order_bulk']) if gr['order_bulk'] != None else None - d['order_wall'] = float(gr['order_wall']) if gr['order_wall'] != None else None - d['order_tank'] = float(gr['order_tank']) if gr['order_tank'] != None else None - d['global_bulk'] = float(gr['global_bulk']) if gr['global_bulk'] != None else None - d['global_wall'] = float(gr['global_wall']) if gr['global_wall'] != None else None - d['limiting_potential'] = float(gr['limiting_potential']) if gr['limiting_potential'] != None else None - d['roughness_correlation'] = float(gr['roughness_correlation']) if gr['roughness_correlation'] != None else None + for e in ts: + d[e['key']] = str(e['value']) return d -class GlobalReaction(object): - def __init__(self, input: dict[str, Any]) -> None: - self.type = 'global_reaction' - self.order_bulk = float(input['order_bulk']) if 'order_bulk' in input and input['order_bulk'] != None else None - self.order_wall = float(input['order_wall']) if 'order_wall' in input and input['order_wall'] != None else None - self.order_tank = float(input['order_tank']) if 'order_tank' in input and input['order_tank'] != None else None - self.global_bulk = float(input['global_bulk']) if 'global_bulk' in input and input['global_bulk'] != None else None - self.global_wall = float(input['global_wall']) if 'global_wall' in input and input['global_wall'] != None else None - self.limiting_potential = float(input['limiting_potential']) if 'limiting_potential' in input and input['limiting_potential'] != None else None - self.roughness_correlation = float(input['roughness_correlation']) if 'roughness_correlation' in input and input['roughness_correlation'] != None else None +def set_reaction_cache(name: str, cs: ChangeSet) -> SqlChangeSet: + raw_old = get_reaction(name) - self.f_type = f"'{self.type}'" - self.f_order_bulk = self.order_bulk if self.order_bulk != None else 'null' - self.f_order_wall = self.order_wall if self.order_wall != None else 'null' - self.f_order_tank = self.order_tank if self.order_tank != None else 'null' - self.f_global_bulk = self.global_bulk if self.global_bulk != None else 'null' - self.f_global_wall = self.global_wall if self.global_wall != None else 'null' - self.f_limiting_potential = self.limiting_potential if self.limiting_potential != None else 'null' - self.f_roughness_correlation = self.roughness_correlation if self.roughness_correlation != None else 'null' - - - def as_dict(self) -> dict[str, Any]: - return { 'type': self.type, 'order_bulk': self.order_bulk, 'order_wall': self.order_wall, 'order_tank': self.order_tank, 'global_bulk': self.global_bulk, 'global_wall': self.global_wall, 'limiting_potential': self.limiting_potential, 'roughness_correlation': self.roughness_correlation } - - -def set_global_reaction_cache(name: str, cs: ChangeSet) -> SqlChangeSet: - old = GlobalReaction(get_global_reaction(name)) - raw_new = get_global_reaction(name) + old = {} + new = {} new_dict = cs.operations[0] - schema = get_global_reaction_schema(name) - for key, value in schema.items(): - if key in new_dict and not value['readonly']: - raw_new[key] = new_dict[key] - new = GlobalReaction(raw_new) + schema = get_reaction_schema(name) + for key in schema.keys(): + if key in new_dict: + old[key] = str(raw_old[key]) + new[key] = str(new_dict[key]) - redo_sql = f"update reactions_global set order_bulk = {new.f_order_bulk}, order_wall = {new.f_order_wall}, order_tank = {new.f_order_tank}, global_bulk = {new.f_global_bulk}, global_wall = {new.f_global_wall}, limiting_potential = {new.f_limiting_potential}, roughness_correlation = {new.f_roughness_correlation} where _no = 0;" - undo_sql = f"update reactions_global set order_bulk = {old.f_order_bulk}, order_wall = {old.f_order_wall}, order_tank = {old.f_order_tank}, global_bulk = {old.f_global_bulk}, global_wall = {old.f_global_wall}, limiting_potential = {old.f_limiting_potential}, roughness_correlation = {old.f_roughness_correlation} where _no = 0;" + redo_cs = g_update_prefix | { 'type' : 'reaction' } - redo_cs = g_update_prefix | new.as_dict() - undo_cs = g_update_prefix | old.as_dict() + redo_sql = '' + for key, value in new.items(): + if redo_sql != '': + redo_sql += '\n' + redo_sql += f"update reactions set value = '{value}' where key = '{key}';" + redo_cs |= { key: value } + + undo_cs = g_update_prefix | { 'type' : 'reaction' } + + undo_sql = '' + for key, value in old.items(): + if undo_sql != '': + undo_sql += '\n' + undo_sql += f"update reactions set value = '{value}' where key = '{key}';" + undo_cs |= { key: value } return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) -def set_global_reaction(name: str, cs: ChangeSet) -> ChangeSet: - return execute_command(name, set_global_reaction_cache(name, cs)) +def set_reaction(name: str, cs: ChangeSet) -> ChangeSet: + return execute_command(name, set_reaction_cache(name, cs)) + + +def inp_in_reaction(section: list[str]) -> ChangeSet: + cs = g_update_prefix | { 'type' : 'reaction' } + for s in section: + line = s.upper().strip() + for key in get_reaction_schema('').keys(): + if line.startswith(key): + value = line.removeprefix(key).strip() + cs |= { key : value } + return ChangeSet(cs) + + +def inp_out_reaction(name: str) -> list[str]: + lines = [] + objs = read_all(name, f"select * from reactions") + for obj in objs: + key = obj['key'] + value = obj['value'] + lines.append(f'{key} {value}') + return lines def get_pipe_reaction_schema(name: str) -> dict[str, dict[str, Any]]: diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 4d3a099..a8c4e7b 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -2519,78 +2519,60 @@ class TestApi: # 19 reaction - def test_global_reaction(self): - p = 'test_global_reaction' + def test_reaction(self): + p = 'test_reaction' self.enter(p) - gr = get_global_reaction(p) - assert gr['order_bulk'] == None - assert gr['order_wall'] == None - assert gr['order_tank'] == None - assert gr['global_bulk'] == None - assert gr['global_wall'] == None - assert gr['limiting_potential'] == None - assert gr['roughness_correlation'] == None + gr = get_reaction(p) + assert gr['ORDER BULK'] == '1' + assert gr['ORDER WALL'] == '1' + assert gr['ORDER TANK'] == '1' + assert gr['GLOBAL BULK'] == '0' + assert gr['GLOBAL WALL'] == '0' + assert gr['LIMITING POTENTIAL'] == '0' + assert gr['ROUGHNESS CORRELATION'] == '0' - set_global_reaction(p, ChangeSet({ 'order_bulk' : 10.0 })) - gr = get_global_reaction(p) - assert gr['order_bulk'] == 10.0 - assert gr['order_wall'] == None - assert gr['order_tank'] == None - assert gr['global_bulk'] == None - assert gr['global_wall'] == None - assert gr['limiting_potential'] == None - assert gr['roughness_correlation'] == None + set_reaction(p, ChangeSet({ 'ORDER BULK' : '10' })) + gr = get_reaction(p) + assert gr['ORDER BULK'] == '10' + assert gr['ORDER WALL'] == '1' + assert gr['ORDER TANK'] == '1' + assert gr['GLOBAL BULK'] == '0' + assert gr['GLOBAL WALL'] == '0' + assert gr['LIMITING POTENTIAL'] == '0' + assert gr['ROUGHNESS CORRELATION'] == '0' - set_global_reaction(p, ChangeSet({ 'order_bulk' : None })) - gr = get_global_reaction(p) - assert gr['order_bulk'] == None - assert gr['order_wall'] == None - assert gr['order_tank'] == None - assert gr['global_bulk'] == None - assert gr['global_wall'] == None - assert gr['limiting_potential'] == None - assert gr['roughness_correlation'] == None + set_reaction(p, ChangeSet({ 'ORDER BULK' : '1' })) + gr = get_reaction(p) + assert gr['ORDER BULK'] == '1' + assert gr['ORDER WALL'] == '1' + assert gr['ORDER TANK'] == '1' + assert gr['GLOBAL BULK'] == '0' + assert gr['GLOBAL WALL'] == '0' + assert gr['LIMITING POTENTIAL'] == '0' + assert gr['ROUGHNESS CORRELATION'] == '0' self.leave(p) - def test_global_reaction_op(self): - p = 'test_global_reaction_op' + def test_reaction_op(self): + p = 'test_reaction_op' self.enter(p) - cs = set_global_reaction(p, ChangeSet({ 'order_bulk' : 10.0 })).operations[0] + cs = set_reaction(p, ChangeSet({ 'ORDER BULK' : '10' })).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_reaction' - assert cs['order_bulk'] == 10.0 - assert cs['order_wall'] == None - assert cs['order_tank'] == None - assert cs['global_bulk'] == None - assert cs['global_wall'] == None - assert cs['limiting_potential'] == None - assert cs['roughness_correlation'] == None + assert cs['type'] == 'reaction' + assert cs['ORDER BULK'] == '10' cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_reaction' - assert cs['order_bulk'] == None - assert cs['order_wall'] == None - assert cs['order_tank'] == None - assert cs['global_bulk'] == None - assert cs['global_wall'] == None - assert cs['limiting_potential'] == None - assert cs['roughness_correlation'] == None + assert cs['type'] == 'reaction' + assert cs['ORDER BULK'] == '1' cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_reaction' - assert cs['order_bulk'] == 10.0 - assert cs['order_wall'] == None - assert cs['order_tank'] == None - assert cs['global_bulk'] == None - assert cs['global_wall'] == None - assert cs['limiting_potential'] == None - assert cs['roughness_correlation'] == None + assert cs['type'] == 'reaction' + assert cs['ORDER BULK'] == '10' self.leave(p) diff --git a/tjnetwork.py b/tjnetwork.py index 07f1bf8..dc10683 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -552,14 +552,14 @@ def delete_source(name: str, cs: ChangeSet) -> ChangeSet: # reaction 19.[REACTIONS] ############################################################ -def get_global_reaction_schema(name: str) -> dict[str, dict[str, Any]]: - return api.get_global_reaction_schema(name) +def get_reaction_schema(name: str) -> dict[str, dict[str, Any]]: + return api.get_reaction_schema(name) -def get_global_reaction(name: str) -> dict[str, Any]: - return api.get_global_reaction(name) +def get_reaction(name: str) -> dict[str, Any]: + return api.get_reaction(name) -def set_global_reaction(name: str, cs: ChangeSet) -> ChangeSet: - return api.set_global_reaction(name, cs) +def set_reaction(name: str, cs: ChangeSet) -> ChangeSet: + return api.set_reaction(name, cs) def get_pipe_reaction_schema(name: str) -> dict[str, dict[str, Any]]: return api.get_pipe_reaction_schema(name)