From b0815f75ebe3ffbe9c17e1154978039ef5ed3b24 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:02:45 +0800 Subject: [PATCH 01/10] Refine energy table --- script/sql/create/15.energy.sql | 17 +++++++++-------- script/sql/drop/15.energy.sql | 4 +++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/script/sql/create/15.energy.sql b/script/sql/create/15.energy.sql index 93c5481..9fd7bd5 100644 --- a/script/sql/create/15.energy.sql +++ b/script/sql/create/15.energy.sql @@ -1,16 +1,17 @@ -- [ENERGY] -create table energy_global +create table energy ( - _no integer primary key -, price numeric not null -, pattern varchar(32) references _pattern(id) -, effic numeric not null -, demand_charge numeric not null + key text primary key +, value text not null ); -insert into energy_global (_no, price, pattern, effic, demand_charge) - values (0, 0.0, null, 75, 0.0); +insert into energy (key, value) values + ('GLOBAL PRICE', '0.0') +, ('GLOBAL PATTERN', '') +, ('GLOBAL EFFIC', '75') +, ('DEMAND CHARGE', '0.0') +; create table energy_pump_price ( diff --git a/script/sql/drop/15.energy.sql b/script/sql/drop/15.energy.sql index cbf857d..d5a8d35 100644 --- a/script/sql/drop/15.energy.sql +++ b/script/sql/drop/15.energy.sql @@ -1,7 +1,9 @@ -- [ENERGY] drop table if exists energy_pump_effic; + drop table if exists energy_pump_pattern; + drop table if exists energy_pump_price; -drop table if exists energy_global; +drop table if exists energy; From dff73445c59645addb1f4959a866816c7b7bf0ef Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:05:31 +0800 Subject: [PATCH 02/10] Update energy value --- script/sql/create/15.energy.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/sql/create/15.energy.sql b/script/sql/create/15.energy.sql index 9fd7bd5..fd9d7f2 100644 --- a/script/sql/create/15.energy.sql +++ b/script/sql/create/15.energy.sql @@ -7,10 +7,10 @@ create table energy ); insert into energy (key, value) values - ('GLOBAL PRICE', '0.0') + ('GLOBAL PRICE', '0') , ('GLOBAL PATTERN', '') , ('GLOBAL EFFIC', '75') -, ('DEMAND CHARGE', '0.0') +, ('DEMAND CHARGE', '0') ; create table energy_pump_price From 8e8592882713ccfe0d0f069feda970db8f8b3ab2 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:08:15 +0800 Subject: [PATCH 03/10] Refine reactions table --- script/sql/create/19.reactions.sql | 22 ++++++++++++---------- script/sql/drop/19.reactions.sql | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/script/sql/create/19.reactions.sql b/script/sql/create/19.reactions.sql index a318dc3..16a69ce 100644 --- a/script/sql/create/19.reactions.sql +++ b/script/sql/create/19.reactions.sql @@ -1,18 +1,20 @@ -- [REACTIONS] -create table reactions_global +create table reactions ( - _no integer primary key -, order_bulk numeric -, order_wall numeric -, order_tank numeric -, global_bulk numeric -, global_wall numeric -, limiting_potential numeric -, roughness_correlation numeric + key text primary key +, value text not null ); -insert into reactions_global values (0, null, null, null, null, null, null, null); +insert into reactions (key, value) values + ('ORDER BULK', '1') +, ('ORDER WALL', '1') +, ('ORDER TANK', '1') +, ('GLOBAL BULK', '0') +, ('GLOBAL WALL', '0') +, ('LIMITING POTENTIAL', '0') +, ('ROUGHNESS CORRELATION', '0') +; create table reactions_pipe_bulk ( diff --git a/script/sql/drop/19.reactions.sql b/script/sql/drop/19.reactions.sql index 5fae80d..4fc30fa 100644 --- a/script/sql/drop/19.reactions.sql +++ b/script/sql/drop/19.reactions.sql @@ -6,4 +6,4 @@ drop table if exists reactions_pipe_wall; drop table if exists reactions_pipe_bulk; -drop table if exists reactions_global; +drop table if exists reactions; From 75a825da379a525f4b36f405eaae94211bca5c0c Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:09:00 +0800 Subject: [PATCH 04/10] Refine times table --- script/sql/create/21.times.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/sql/create/21.times.sql b/script/sql/create/21.times.sql index ae150f9..0259dc1 100644 --- a/script/sql/create/21.times.sql +++ b/script/sql/create/21.times.sql @@ -2,7 +2,7 @@ create table times ( - key text not null + key text primary key , value text not null ); From 08fde4a2d44c7940c2b66c3d9dd4e597979b0be0 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:09:31 +0800 Subject: [PATCH 05/10] Refine report table --- script/sql/create/22.report.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/sql/create/22.report.sql b/script/sql/create/22.report.sql index 8f8273d..3065f75 100644 --- a/script/sql/create/22.report.sql +++ b/script/sql/create/22.report.sql @@ -2,7 +2,7 @@ create table report ( - key text not null + key text primary key , value text not null ); From 2891c1a4db60a10e4bf81db82e4de47aabef389a Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:09:47 +0800 Subject: [PATCH 06/10] Refine options table --- script/sql/create/23.options.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/sql/create/23.options.sql b/script/sql/create/23.options.sql index d973e6d..f88b536 100644 --- a/script/sql/create/23.options.sql +++ b/script/sql/create/23.options.sql @@ -4,7 +4,7 @@ create table options ( - key text not null + key text primary key , value text not null ); From 2e15bdce4ba1c40b98ceec43429d639697e14f8e Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:13:04 +0800 Subject: [PATCH 07/10] Parse [TAGS] --- api/s8_tags.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/api/s8_tags.py b/api/s8_tags.py index 67b53b0..bed1862 100644 --- a/api/s8_tags.py +++ b/api/s8_tags.py @@ -6,7 +6,7 @@ TAG_TYPE_LINK = 'LINK' def get_tag_schema(name: str) -> dict[str, dict[str, Any]]: return { 't_type' : {'type': 'str' , 'optional': False , 'readonly': False}, 'id' : {'type': 'str' , 'optional': False , 'readonly': False}, - 'tag' : {'type': 'str' , 'optional': True , 'readonly': False},} + 'tag' : {'type': 'str' , 'optional': True , 'readonly': False},} def get_tag(name: str, t_type: str, id: str) -> dict[str, Any]: @@ -77,3 +77,44 @@ def set_tag_cache(name: str, cs: ChangeSet) -> SqlChangeSet: def set_tag(name: str, cs: ChangeSet) -> ChangeSet: return execute_command(name, set_tag_cache(name, cs)) + + +class InpTag: + def __init__(self, line: str) -> None: + tokens = line.split() + + num = len(tokens) + has_desc = tokens[-1].startswith(';') + num_without_desc = (num - 1) if has_desc else num + + self.t_type = str(tokens[0]) + self.id = str(tokens[1]) + self.tag = str(tokens[2]) + + +def inp_in_tag(section: list[str]) -> ChangeSet: + cs = ChangeSet() + for s in section: + # skip comment + if s.startswith(';'): + continue + obj = InpTag(s) + cs.append({'operation': API_UPDATE, 'type': 'tag', 't_type': obj.t_type, 'id': obj.id, 'tag': obj.tag}) + return cs + + +def inp_out_tag(name: str) -> list[str]: + lines = [] + objs = read_all(name, 'select * from tags_node') + for obj in objs: + t_type = TAG_TYPE_NODE + id = obj['id'] + tag = obj['tag'] + lines.append(f'{t_type} {id} {tag}') + objs = read_all(name, 'select * from tags_link') + for obj in objs: + t_type = TAG_TYPE_LINK + id = obj['id'] + tag = obj['tag'] + lines.append(f'{t_type} {id} {tag}') + return lines From 45c69033a872b783a420103bb55bb43dcc04f3a2 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:38:05 +0800 Subject: [PATCH 08/10] Refine energy api --- api/__init__.py | 2 +- api/s15_energy.py | 102 +++++++++++++++----------- test_tjnetwork.py | 182 ++++++++++++++++------------------------------ tjnetwork.py | 12 +-- 4 files changed, 128 insertions(+), 170 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index e179134..79addc4 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -55,7 +55,7 @@ from .s13_controls import get_control_schema, get_control, set_control from .s14_rules import get_rule_schema, get_rule, set_rule -from .s15_energy import get_global_energy_schema, get_global_energy, set_global_energy +from .s15_energy import get_energy_schema, get_energy, set_energy from .s15_energy import get_pump_energy_schema, get_pump_energy, set_pump_energy from .s16_emitters import get_emitter_schema, get_emitter, set_emitter diff --git a/api/s15_energy.py b/api/s15_energy.py index ca7ed8e..4884984 100644 --- a/api/s15_energy.py +++ b/api/s15_energy.py @@ -1,63 +1,81 @@ from .operation import * -def get_global_energy_schema(name: str) -> dict[str, dict[str, Any]]: - return { 'price' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'pattern' : {'type': 'str' , 'optional': True , 'readonly': False}, - 'effic' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'demand_charge' : {'type': 'float' , 'optional': True , 'readonly': False} } +element_schema = {'type': 'str' , 'optional': True , 'readonly': False} -def get_global_energy(name: str) -> dict[str, Any]: - ge = read(name, f"select * from energy_global") +def get_energy_schema(name: str) -> dict[str, dict[str, Any]]: + return { 'GLOBAL PRICE' : element_schema, + 'GLOBAL PATTERN' : element_schema, + 'GLOBAL EFFIC' : element_schema, + 'DEMAND CHARGE' : element_schema } + + +def get_energy(name: str) -> dict[str, Any]: + ts = read_all(name, f"select * from energy") d = {} - d['price'] = float(ge['price']) - d['pattern'] = str(ge['pattern']) if ge['pattern'] != None else None - d['effic'] = float(ge['effic']) - d['demand_charge'] = float(ge['demand_charge']) + for e in ts: + d[e['key']] = str(e['value']) return d -class GlobalEnergy(object): - def __init__(self, input: dict[str, Any]) -> None: - self.type = 'global_energy' - self.price = float(input['price']) if 'price' in input and input['price'] != None else 0.0 - self.pattern = str(input['pattern']) if 'pattern' in input and input['pattern'] != None else None - self.effic = float(input['effic']) if 'effic' in input and input['effic'] != None else 75 - self.demand_charge = float(input['demand_charge']) if 'demand_charge' in input and input['demand_charge'] != None else 0.0 +def set_energy_cache(name: str, cs: ChangeSet) -> SqlChangeSet: + raw_old = get_energy(name) - self.f_type = f"'{self.type}'" - self.f_price = self.price - self.f_pattern = f"'{self.pattern}'" if self.pattern != None else 'null' - self.f_effic = self.effic - self.f_demand_charge = self.demand_charge - - def as_dict(self) -> dict[str, Any]: - return { 'type': self.type, 'price': self.price, 'pattern': self.pattern, 'effic': self.effic, 'demand_charge': self.demand_charge } - - -def set_global_energy_cache(name: str, cs: ChangeSet) -> SqlChangeSet: - old = GlobalEnergy(get_global_energy(name)) - raw_new = get_global_energy(name) + old = {} + new = {} new_dict = cs.operations[0] - schema = get_global_energy_schema(name) - for key, value in schema.items(): - if key in new_dict and not value['readonly']: - raw_new[key] = new_dict[key] - new = GlobalEnergy(raw_new) + schema = get_energy_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 energy_global set price = {new.f_price}, pattern = {new.f_pattern}, effic = {new.f_effic}, demand_charge = {new.f_demand_charge} where _no = 0;" - undo_sql = f"update energy_global set price = {old.f_price}, pattern = {old.f_pattern}, effic = {old.f_effic}, demand_charge = {old.f_demand_charge} where _no = 0;" + redo_cs = g_update_prefix | { 'type' : 'energy' } - 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 energy set value = '{value}' where key = '{key}';" + redo_cs |= { key: value } + + undo_cs = g_update_prefix | { 'type' : 'energy' } + + undo_sql = '' + for key, value in old.items(): + if undo_sql != '': + undo_sql += '\n' + undo_sql += f"update energy set value = '{value}' where key = '{key}';" + undo_cs |= { key: value } return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) -def set_global_energy(name: str, cs: ChangeSet) -> ChangeSet: - return execute_command(name, set_global_energy_cache(name, cs)) +def set_energy(name: str, cs: ChangeSet) -> ChangeSet: + return execute_command(name, set_energy_cache(name, cs)) + + +def inp_in_energy(section: list[str]) -> ChangeSet: + cs = g_update_prefix | { 'type' : 'energy' } + for s in section: + line = s.upper().strip() + for key in get_energy_schema('').keys(): + if line.startswith(key): + value = line.removeprefix(key).strip() + cs |= { key : value } + return ChangeSet(cs) + + +def inp_out_energy(name: str) -> list[str]: + lines = [] + objs = read_all(name, f"select * from energy") + for obj in objs: + key = obj['key'] + value = obj['value'] + lines.append(f'{key} {value}') + return lines def get_pump_energy_schema(name: str) -> dict[str, dict[str, Any]]: diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 0603442..4d3a099 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1921,173 +1921,113 @@ class TestApi: # 15 energy - def test_global_energy(self): - p = 'test_global_energy' + def test_energy(self): + p = 'test_energy' self.enter(p) - ge = get_global_energy(p) - assert ge['price'] == 0.0 - assert ge['pattern'] == None - assert ge['effic'] == float(75) - assert ge['demand_charge'] == 0.0 + ge = get_energy(p) + assert ge['GLOBAL PRICE'] == '0' + assert ge['GLOBAL PATTERN'] == '' + assert ge['GLOBAL EFFIC'] == '75' + assert ge['DEMAND CHARGE'] == '0' - set_global_energy(p, ChangeSet({ 'price' : 10.0 })) - ge = get_global_energy(p) - assert ge['price'] == 10.0 - assert ge['pattern'] == None - assert ge['effic'] == float(75) - assert ge['demand_charge'] == 0.0 + set_energy(p, ChangeSet({ 'GLOBAL PRICE' : '10' })) + ge = get_energy(p) + assert ge['GLOBAL PRICE'] == '10' + assert ge['GLOBAL PATTERN'] == '' + assert ge['GLOBAL EFFIC'] == '75' + assert ge['DEMAND CHARGE'] == '0' add_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})) - set_global_energy(p, ChangeSet({ 'pattern' : 'p0' })) - ge = get_global_energy(p) - assert ge['price'] == 10.0 - assert ge['pattern'] == 'p0' - assert ge['effic'] == float(75) - assert ge['demand_charge'] == 0.0 + set_energy(p, ChangeSet({ 'GLOBAL PATTERN' : 'p0' })) + ge = get_energy(p) + assert ge['GLOBAL PRICE'] == '10' + assert ge['GLOBAL PATTERN'] == 'p0' + assert ge['GLOBAL EFFIC'] == '75' + assert ge['DEMAND CHARGE'] == '0' - set_global_energy(p, ChangeSet({ 'effic' : 0.0 })) - ge = get_global_energy(p) - assert ge['price'] == 10.0 - assert ge['pattern'] == 'p0' - assert ge['effic'] == 0.0 - assert ge['demand_charge'] == 0.0 + set_energy(p, ChangeSet({ 'GLOBAL EFFIC' : '0' })) + ge = get_energy(p) + assert ge['GLOBAL PRICE'] == '10' + assert ge['GLOBAL PATTERN'] == 'p0' + assert ge['GLOBAL EFFIC'] == '0' + assert ge['DEMAND CHARGE'] == '0' - set_global_energy(p, ChangeSet({ 'demand_charge' : 10.0 })) - ge = get_global_energy(p) - assert ge['price'] == 10.0 - assert ge['pattern'] == 'p0' - assert ge['effic'] == 0.0 - assert ge['demand_charge'] == 10.0 + set_energy(p, ChangeSet({ 'DEMAND CHARGE' : '10' })) + ge = get_energy(p) + assert ge['GLOBAL PRICE'] == '10' + assert ge['GLOBAL PATTERN'] == 'p0' + assert ge['GLOBAL EFFIC'] == '0' + assert ge['DEMAND CHARGE'] == '10' self.leave(p) - def test_global_energy_op(self): - p = 'test_global_energy_op' + def test_energy_op(self): + p = 'test_energy_op' self.enter(p) add_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})) - cs = set_global_energy(p, ChangeSet({ 'price' : 10.0 })).operations[0] + cs = set_energy(p, ChangeSet({ 'GLOBAL PRICE' : '10' })).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == None - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL PRICE'] == '10' cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 0.0 - assert cs['pattern'] == None - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL PRICE'] == '0' cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == None - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL PRICE'] == '10' - cs = set_global_energy(p, ChangeSet({ 'pattern' : 'p0' })).operations[0] + cs = set_energy(p, ChangeSet({ 'GLOBAL PATTERN' : 'p0' })).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL PATTERN'] == 'p0' cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == None - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL PATTERN'] == '' cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL PATTERN'] == 'p0' - cs = set_global_energy(p, ChangeSet({ 'effic' : 0.0 })).operations[0] + cs = set_energy(p, ChangeSet({ 'GLOBAL EFFIC' : '0' })).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == 0.0 - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL EFFIC'] == '0' cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL EFFIC'] == '75' cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == 0.0 - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['GLOBAL EFFIC'] == '0' - cs = set_global_energy(p, ChangeSet({ 'demand_charge' : 10.0 })).operations[0] + cs = set_energy(p, ChangeSet({ 'DEMAND CHARGE' : '10' })).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == 0.0 - assert cs['demand_charge'] == 10.0 + assert cs['type'] == 'energy' + assert cs['DEMAND CHARGE'] == '10' cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == 0.0 - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['DEMAND CHARGE'] == '0' cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == 0.0 - assert cs['demand_charge'] == 10.0 - - cs = set_global_energy(p, ChangeSet({ 'price' : 0.0, 'pattern' : None, 'effic' : float(75), 'demand_charge' : 0.0 })).operations[0] - assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 0.0 - assert cs['pattern'] == None - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 - - cs = execute_undo(p).operations[0] - assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 10.0 - assert cs['pattern'] == 'p0' - assert cs['effic'] == 0.0 - assert cs['demand_charge'] == 10.0 - - cs = execute_redo(p).operations[0] - assert cs['operation'] == API_UPDATE - assert cs['type'] == 'global_energy' - assert cs['price'] == 0.0 - assert cs['pattern'] == None - assert cs['effic'] == float(75) - assert cs['demand_charge'] == 0.0 + assert cs['type'] == 'energy' + assert cs['DEMAND CHARGE'] == '10' self.leave(p) diff --git a/tjnetwork.py b/tjnetwork.py index 0a46c1e..07f1bf8 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -478,14 +478,14 @@ def set_rule(name: str, cs: ChangeSet) -> ChangeSet: # energy 15.[ENERGY] ############################################################ -def get_global_energy_schema(name: str) -> dict[str, dict[str, Any]]: - return api.get_global_energy_schema(name) +def get_energy_schema(name: str) -> dict[str, dict[str, Any]]: + return api.get_energy_schema(name) -def get_global_energy(name: str) -> dict[str, Any]: - return api.get_global_energy(name) +def get_energy(name: str) -> dict[str, Any]: + return api.get_energy(name) -def set_global_energy(name: str, cs: ChangeSet) -> ChangeSet: - return api.set_global_energy(name, cs) +def set_energy(name: str, cs: ChangeSet) -> ChangeSet: + return api.set_energy(name, cs) def get_pump_energy_schema(name: str) -> dict[str, dict[str, Any]]: return api.get_pump_energy_schema(name) From 11e732f9b19d79a1e7a96fa646dfcd435e10e040 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:45:22 +0800 Subject: [PATCH 09/10] 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) From 9c0cbc885bbdef162ba9a3f7e4148661dc5e3541 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Mon, 14 Nov 2022 20:45:45 +0800 Subject: [PATCH 10/10] Fix api name --- api/command.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/api/command.py b/api/command.py index deed8b2..7c3dc27 100644 --- a/api/command.py +++ b/api/command.py @@ -42,12 +42,12 @@ _s11_pattern = 'patten' _s12_curve = 'curve' _s13_control = 'control' _s14_rule = 'rule' -_s15_global_energy = 'global_energy' +_s15_energy = 'energy' _s15_pump_energy = 'pump_energy' _s16_emitter = 'emitter' _s17_quality = 'quality' _s18_source = 'source' -_s19_global_reaction = 'global_reaction' +_s19_reaction = 'reaction' _s19_pipe_reaction = 'pipe_reaction' _s19_tank_reaction = 'tank_reaction' _s20_mixing = 'mixing' @@ -92,7 +92,7 @@ def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet: return ChangeSet() elif type == _s14_rule: return ChangeSet() - elif type == _s15_global_energy: + elif type == _s15_energy: return ChangeSet() elif type == _s15_pump_energy: return ChangeSet() @@ -102,7 +102,7 @@ def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet: return ChangeSet() elif type == _s18_source: return add_source(name, cs) - elif type == _s19_global_reaction: + elif type == _s19_reaction: return ChangeSet() elif type == _s19_pipe_reaction: return ChangeSet() @@ -161,8 +161,8 @@ def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet: return set_control(name, cs) elif type == _s14_rule: return set_rule(name, cs) - elif type == _s15_global_energy: - return set_global_energy(name, cs) + elif type == _s15_energy: + return set_energy(name, cs) elif type == _s15_pump_energy: return set_pump_energy(name, cs) elif type == _s16_emitter: @@ -171,8 +171,8 @@ def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet: return set_quality(name, cs) elif type == _s18_source: return set_source(name, cs) - elif type == _s19_global_reaction: - return set_global_reaction(name, cs) + elif type == _s19_reaction: + return set_reaction(name, cs) elif type == _s19_pipe_reaction: return set_pipe_reaction(name, cs) elif type == _s19_tank_reaction: @@ -230,7 +230,7 @@ def execute_delete_command(name: str, cs: ChangeSet) -> ChangeSet: return ChangeSet() elif type == _s14_rule: return ChangeSet() - elif type == _s15_global_energy: + elif type == _s15_energy: return ChangeSet() elif type == _s15_pump_energy: return ChangeSet() @@ -240,7 +240,7 @@ def execute_delete_command(name: str, cs: ChangeSet) -> ChangeSet: return ChangeSet() elif type == _s18_source: return delete_source(name, cs) - elif type == _s19_global_reaction: + elif type == _s19_reaction: return ChangeSet() elif type == _s19_pipe_reaction: return ChangeSet() @@ -317,7 +317,7 @@ def cache_add_command(name: str, cs: ChangeSet) -> SqlChangeSet | None: return None elif type == _s14_rule: return None - elif type == _s15_global_energy: + elif type == _s15_energy: return None elif type == _s15_pump_energy: return None @@ -327,7 +327,7 @@ def cache_add_command(name: str, cs: ChangeSet) -> SqlChangeSet | None: return None elif type == _s18_source: return add_source_cache(name, cs) - elif type == _s19_global_reaction: + elif type == _s19_reaction: return None elif type == _s19_pipe_reaction: return None @@ -386,8 +386,8 @@ def cache_update_command(name: str, cs: ChangeSet) -> SqlChangeSet | None: return set_control_cache(name, cs) elif type == _s14_rule: return set_rule_cache(name, cs) - elif type == _s15_global_energy: - return set_global_energy_cache(name, cs) + elif type == _s15_energy: + return set_energy_cache(name, cs) elif type == _s15_pump_energy: return set_pump_energy_cache(name, cs) elif type == _s16_emitter: @@ -396,8 +396,8 @@ def cache_update_command(name: str, cs: ChangeSet) -> SqlChangeSet | None: return set_quality_cache(name, cs) elif type == _s18_source: return set_source_cache(name, cs) - elif type == _s19_global_reaction: - return set_global_reaction_cache(name, cs) + elif type == _s19_reaction: + return set_reaction_cache(name, cs) elif type == _s19_pipe_reaction: return set_pipe_reaction_cache(name, cs) elif type == _s19_tank_reaction: @@ -455,7 +455,7 @@ def cache_delete_command(name: str, cs: ChangeSet) -> SqlChangeSet | None: return None elif type == _s14_rule: return None - elif type == _s15_global_energy: + elif type == _s15_energy: return None elif type == _s15_pump_energy: return None @@ -465,7 +465,7 @@ def cache_delete_command(name: str, cs: ChangeSet) -> SqlChangeSet | None: return None elif type == _s18_source: return delete_source_cache(name, cs) - elif type == _s19_global_reaction: + elif type == _s19_reaction: return None elif type == _s19_pipe_reaction: return None @@ -504,7 +504,6 @@ def execute_batch_command(name: str, cs: ChangeSet) -> ChangeSet: operation = op['operation'] r = None - demand = None if operation == API_ADD: r = cache_add_command(name, ChangeSet(op))