From 3b208116058c69dab26f3b2db7f72ec5a1927bb4 Mon Sep 17 00:00:00 2001 From: wqy Date: Sun, 25 Sep 2022 00:54:11 +0800 Subject: [PATCH] Update pipe api and test --- api/__init__.py | 4 +- api/s4_tanks.py | 115 ---------------------------------------- api/s5_pipes.py | 130 +++++++++++++++++++++++++++++++++++++++++++++- test_tjnetwork.py | 66 +++++++++++------------ tjnetwork.py | 34 +++--------- 5 files changed, 171 insertions(+), 178 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 03be181..507f708 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -26,9 +26,7 @@ from .s4_tanks import OVERFLOW_YES, OVERFLOW_NO from .s4_tanks import get_tank_schema, add_tank, get_tank, set_tank, delete_tank from .s5_pipes import PIPE_STATUS_OPEN, PIPE_STATUS_CLOSED, PIPE_STATUS_CV -from .s5_pipes import add_pipe, delete_pipe -from .s5_pipes import set_pipe_node1, set_pipe_node2, set_pipe_length, set_pipe_diameter, set_pipe_roughness, set_pipe_minor_loss, set_pipe_status -from .s5_pipes import get_pipe_property_names, get_pipe_properties +from .s5_pipes import add_pipe, get_pipe, set_pipe, delete_pipe from .s6_pumps import add_pump, delete_pump from .s6_pumps import set_pump_node1, set_pump_node2 diff --git a/api/s4_tanks.py b/api/s4_tanks.py index 73bc786..d9c1b07 100644 --- a/api/s4_tanks.py +++ b/api/s4_tanks.py @@ -125,118 +125,3 @@ def delete_tank(name: str, id: str) -> ChangeSet: write(name, sql) add_operation(name, sql.replace("'", "''"), undo, 'delete_tank', API_DELETE, TANK, id) return get_current_change_set(name) - - - -''' -def add_tank(name: str, id: str, x: float, y: float, elevation: float, init_level: float = 0, min_level: float = 0, max_level: float = 0, diameter: float = 0, min_vol: float = 0) -> ChangeSet: - sql = f"insert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol) values ('{id}', {elevation}, {init_level}, {min_level}, {max_level}, {diameter}, {min_vol});" - undo_sql = f'delete from tanks where id = "{id}";' - return add_node(name, TANK, id, x, y, sql, undo_sql) - - -def _get_tank(name: str, id: str) -> Row | None: - return query(name, f"select elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow from tanks where id = '{id}'") - - -def delete_tank(name: str, id: str) -> ChangeSet: - if not is_tank(name, id): - return ChangeSet() - - row = _get_tank(name, id) - if row == None: - return ChangeSet() - - elevation = row['elevation'] - init_level = row['init_level'] - min_level = row['min_level'] - max_level = row['max_level'] - diameter = row['diameter'] - min_vol = row['min_vol'] - vol_curve = decorate(row['vol_curve'], 'str', True) - overflow = decorate(row['overflow'], 'str', True) - - sql = f"delete from tanks where id = '{id}';" - undo_sql = f'insert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow) values ("{id}", {elevation}, {init_level}, {min_level}, {max_level}, {diameter}, {min_vol}, {vol_curve}, {overflow});' - - return delete_node(name, TANK, id, sql, undo_sql) - - -def _set_tank(name: str, id: str, key: str, key_type: str, value: str, optional: bool = False) -> ChangeSet: - if not is_tank(name, id): - return ChangeSet() - - row = _get_tank(name, id) - if row == None: - return ChangeSet() - - return update(name, TANK, 'tanks', 'id', id, key, key_type, row[key], value, optional) - - -def set_tank_elevation(name: str, id: str, elevation: float) -> ChangeSet: - return _set_tank(name, id, 'elevation', 'float', elevation) - - -def set_tank_init_level(name: str, id: str, init_level: float) -> ChangeSet: - return _set_tank(name, id, 'init_level', 'float', init_level) - - -def set_tank_min_level(name: str, id: str, min_level: float) -> ChangeSet: - return _set_tank(name, id, 'min_level', 'float', min_level) - - -def set_tank_max_level(name: str, id: str, max_level: float) -> ChangeSet: - return _set_tank(name, id, 'max_level', 'float', max_level) - - -def set_tank_diameter(name: str, id: str, diameter: float) -> ChangeSet: - return _set_tank(name, id, 'diameter', 'float', diameter) - - -def set_tank_min_vol(name: str, id: str, min_vol: float) -> ChangeSet: - return _set_tank(name, id, 'min_vol', 'float', min_vol) - - -def set_tank_vol_curve(name: str, id: str, vol_curve: str) -> ChangeSet: - if not is_curve(name, vol_curve): - return ChangeSet() - - return _set_tank(name, id, 'vol_curve', 'str', vol_curve, True) - - -def set_tank_overflow(name: str, id: str, overflow: str) -> ChangeSet: - if overflow != OVERFLOW_YES and overflow != OVERFLOW_NO: - return ChangeSet() - - return _set_tank(name, id, 'overflow', 'str', overflow) - - -def set_tank_coord(name: str, id: str, x: float, y: float) -> ChangeSet: - if not is_tank(name, id): - return ChangeSet() - - return set_node_coord(name, TANK, id, x, y) - - -def get_tank_property_names(name: str) -> list[str]: - return ['elevation', 'init_level', 'min_level', 'max_level', 'diameter', 'min_vol', 'vol_curve', 'overflow', 'coord', 'links'] - - -def get_tank_properties(name: str, id: str) -> dict[str, Any] | None: - row = _get_tank(name, id) - if row == None: - return None - - ps: dict[str, str] = {} - ps['elevation'] = float(row['elevation']) if row != None else None - ps['init_level'] = float(row['init_level']) if row != None else None - ps['min_level'] = float(row['min_level']) if row != None else None - ps['max_level'] = float(row['max_level']) if row != None else None - ps['diameter'] = float(row['diameter']) if row != None else None - ps['min_vol'] = float(row['min_vol']) if row != None else None - ps['vol_curve'] = row['vol_curve'] if row != None and row['vol_curve'] != None else None - ps['overflow'] = row['overflow'] if row != None and row['overflow'] != None else None - ps['coord'] = get_node_coord(name, id) - ps['links'] = get_node_links(name, id) - return ps -''' diff --git a/api/s5_pipes.py b/api/s5_pipes.py index 07ccc6c..34ca786 100644 --- a/api/s5_pipes.py +++ b/api/s5_pipes.py @@ -2,7 +2,9 @@ from typing import Any from psycopg.rows import Row from .s0_base import * from .change_set import ChangeSet +from .s24_coordinates import * from .utility import * +from .schema import * PIPE_STATUS_OPEN = 'open' @@ -10,6 +12,131 @@ PIPE_STATUS_CLOSED = 'closed' PIPE_STATUS_CV = 'cv' +schema: dict[str, dict[str, Any]] = { \ + 'id' : define_property(str_type, False, True), \ + 'node1' : define_property(str_type), \ + 'node2' : define_property(str_type), \ + 'length' : define_property(float_type), \ + 'diameter' : define_property(float_type), \ + 'roughness' : define_property(float_type), \ + 'minor_loss': define_property(float_type), \ + 'status' : define_property(str_type)} + + +def get_pipe_schema(name: str) -> dict[str, dict[str, Any]]: + return schema + + +def _query_pipe(name: str, id: str) -> Row | None: + return read(name, f"select * from pipes where id = '{id}'") + + +def _get_pipe_node1(name: str, id: str) -> str | None: + row = _query_pipe(name, id) + return row['node1'] if row != None else None + + +def _get_pipe_node2(name: str, id: str) -> str | None: + row = _query_pipe(name, id) + return row['node2'] if row != None else None + + +def add_pipe(name: str, id: str, node1: str, node2: str, length: float = 0, diameter: float = 0, roughness: float = 0, minor_loss: float = 0, status: str = PIPE_STATUS_OPEN) -> ChangeSet: + if is_pipe(name, id): + return ChangeSet() + if not is_node(name, node1): + return ChangeSet() + if not is_node(name, node2): + return ChangeSet() + if node1 == node2: + return ChangeSet() + if status != PIPE_STATUS_OPEN and status != PIPE_STATUS_CLOSED and status != PIPE_STATUS_CV: + return ChangeSet() + + sql = f"insert into _link (id, type) values ('{id}', '{PIPE}');" + sql += f"\ninsert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) values ('{id}', '{node1}', '{node2}', {length}, {diameter}, {roughness}, {minor_loss}, '{status}');" + + undo = f"delete from pipes where id = ''{id}'';" + undo += f"\ndelete from _link where id = ''{id}'';" + + write(name, sql) + add_operation(name, sql.replace("'", "''"), undo, 'add_pipe', API_ADD, PIPE, id) + return get_current_change_set(name) + + +def get_pipe(name: str, id: str) -> dict[str, Any] | None: + row = _query_pipe(name, id) + if row == None: + return None + + ps: dict[str, str] = {} + ps['id'] = id + ps['node1'] = row['node1'] + ps['node2'] = row['node2'] + ps['length'] = float(row['length']) + ps['diameter'] = float(row['diameter']) + ps['roughness'] = float(row['roughness']) + ps['minor_loss'] = float(row['minor_loss']) + ps['status'] = row['status'] + return ps + + +def set_pipe(name: str, id: str, properties: dict[str, Any]) -> ChangeSet: + if not is_pipe(name, id): + return ChangeSet() + if 'node1' in properties: + if not is_node(name, properties['node1']) or _get_pipe_node2(name, id) == properties['node1']: + return ChangeSet() + if 'node2' in properties: + if not is_node(name, properties['node2']) or _get_pipe_node1(name, id) == properties['node2']: + return ChangeSet() + if 'node1' in properties and 'node2' in properties: + if properties['node1'] == properties['node2']: + return ChangeSet() + if 'status' in properties: + if properties['status'] != PIPE_STATUS_OPEN and properties['status'] != PIPE_STATUS_CLOSED and properties['status'] != PIPE_STATUS_CV: + return ChangeSet() + + old = Serialize(get_pipe(name, id), schema).to_storage() + + new = get_pipe(name, id) + ps: list[str] = [] + for key in properties: + if key in schema and schema[key]['readonly'] == False: + new[key] = properties[key] + ps.append(key) + new = Serialize(new, schema).to_execution() + + sql = f"update pipes set node1 = {new['node1']}, node2 = {new['node2']}, \ + length = {new['length']}, diameter = {new['diameter']}, roughness = {new['roughness']}, minor_loss = {new['minor_loss']}, status = {new['status']} where id = '{id}';" + undo = f"update pipes set node1 = {old['node1']}, node2 = {old['node2']}, \ + length = {old['length']}, diameter = {old['diameter']}, roughness = {old['roughness']}, minor_loss = {old['minor_loss']}, status = {old['status']} where id = ''{id}'';" + + write(name, sql) + add_operation(name, sql.replace("'", "''"), undo, 'set_pipe', API_UPDATE, PIPE, id, ps) + return get_current_change_set(name) + + +def delete_pipe(name: str, id: str) -> ChangeSet: + row = get_pipe(name, id) + if row == None: + return ChangeSet() + + old = Serialize(get_pipe(name, id), schema).to_storage() + + sql = f"delete from pipes where id = '{id}';" + sql += f"\ndelete from _link where id = '{id}';" + + undo = f"insert into _link (id, type) values (''{id}'', ''{PIPE}'');" + undo += f"\ninsert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) \ + values (''{id}'', {old['node1']}, {old['node2']}, {old['length']}, {old['diameter']}, {old['roughness']}, {old['minor_loss']}, {old['status']});" + + write(name, sql) + add_operation(name, sql.replace("'", "''"), undo, 'delete_pipe', API_DELETE, PIPE, id) + return get_current_change_set(name) + + +''' def add_pipe(name: str, id: str, node1: str, node2: str, length: float = 0, diameter: float = 0, roughness: float = 0, minor_loss: float = 0, status: str = PIPE_STATUS_OPEN) -> ChangeSet: if not is_node(name, node1): return ChangeSet() @@ -125,4 +252,5 @@ def get_pipe_properties(name: str, id: str) -> dict[str, Any] | None: ps['roughness'] = float(row['roughness']) if row != None else None ps['minor_loss'] = float(row['minor_loss']) if row != None else None ps['status'] = row['status'] if row != None else None - return ps \ No newline at end of file + return ps +''' \ No newline at end of file diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 9ededdd..0cb480f 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -314,7 +314,7 @@ class TestApi: self.leave(p) -''' + def test_pipe(self): p = "test_pipe" self.enter(p) @@ -351,77 +351,77 @@ class TestApi: pipes = get_links(p) assert len(pipes) == 1 - assert get_pipe_properties(p, 'p1')['node1'] == 'j1' - assert get_pipe_properties(p, 'p1')['node2'] == 'j2' - assert get_pipe_properties(p, 'p1')['length'] == 10.0 - assert get_pipe_properties(p, 'p1')['diameter'] == 10.0 - assert get_pipe_properties(p, 'p1')['roughness'] == 10.0 - assert get_pipe_properties(p, 'p1')['minor_loss'] == 10.0 - assert get_pipe_properties(p, 'p1')['status'] == PIPE_STATUS_CLOSED + assert get_pipe(p, 'p1')['node1'] == 'j1' + assert get_pipe(p, 'p1')['node2'] == 'j2' + assert get_pipe(p, 'p1')['length'] == 10.0 + assert get_pipe(p, 'p1')['diameter'] == 10.0 + assert get_pipe(p, 'p1')['roughness'] == 10.0 + assert get_pipe(p, 'p1')['minor_loss'] == 10.0 + assert get_pipe(p, 'p1')['status'] == PIPE_STATUS_CLOSED - change_set = set_pipe_node1(p, 'p1', 'j2') + change_set = set_pipe(p, 'p1', {'node1': 'j2'}) assert len(change_set.operations) == 0 - change_set = set_pipe_node2(p, 'p1', 'j1') + change_set = set_pipe(p, 'p1', {'node2': 'j1'}) assert len(change_set.operations) == 0 - change_set = set_pipe_status(p, 'p1', "XXX") + change_set = set_pipe(p, 'p1', {'status': 'XXX'}) assert len(change_set.operations) == 0 - change_set = set_pipe_node1(p, 'p1', 'j3') + change_set = set_pipe(p, 'p1', {'node1': 'j3'}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'node1' + assert change_set.operations[0]['properties'] == ['node1'] - change_set = set_pipe_node2(p, 'p1', 'j4') + change_set = set_pipe(p, 'p1', {'node2': 'j4'}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'node2' + assert change_set.operations[0]['properties'] == ['node2'] - change_set = set_pipe_length(p, 'p1', 100.0) + change_set = set_pipe(p, 'p1', {'length': 100.0}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'length' + assert change_set.operations[0]['properties'] == ['length'] - change_set = set_pipe_diameter(p, 'p1', 100.0) + change_set = set_pipe(p, 'p1', {'diameter': 100.0}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'diameter' + assert change_set.operations[0]['properties'] == ['diameter'] - change_set = set_pipe_roughness(p, 'p1', 100.0) + change_set = set_pipe(p, 'p1', {'roughness': 100.0}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'roughness' + assert change_set.operations[0]['properties'] == ['roughness'] - change_set = set_pipe_minor_loss(p, 'p1', 100.0) + change_set = set_pipe(p, 'p1', {'minor_loss': 100.0}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'minor_loss' + assert change_set.operations[0]['properties'] == ['minor_loss'] - change_set = set_pipe_status(p, 'p1', PIPE_STATUS_OPEN) + change_set = set_pipe(p, 'p1', {'status': PIPE_STATUS_OPEN}) assert len(change_set.operations) == 1 assert change_set.operations[0]['operation'] == 'update' assert change_set.operations[0]['type'] == PIPE assert change_set.operations[0]['id'] == 'p1' - assert change_set.operations[0]['properties'] == 'status' + assert change_set.operations[0]['properties'] == ['status'] - assert get_pipe_properties(p, 'p1')['node1'] == 'j3' - assert get_pipe_properties(p, 'p1')['node2'] == 'j4' - assert get_pipe_properties(p, 'p1')['length'] == 100.0 - assert get_pipe_properties(p, 'p1')['diameter'] == 100.0 - assert get_pipe_properties(p, 'p1')['roughness'] == 100.0 - assert get_pipe_properties(p, 'p1')['minor_loss'] == 100.0 - assert get_pipe_properties(p, 'p1')['status'] == PIPE_STATUS_OPEN + assert get_pipe(p, 'p1')['node1'] == 'j3' + assert get_pipe(p, 'p1')['node2'] == 'j4' + assert get_pipe(p, 'p1')['length'] == 100.0 + assert get_pipe(p, 'p1')['diameter'] == 100.0 + assert get_pipe(p, 'p1')['roughness'] == 100.0 + assert get_pipe(p, 'p1')['minor_loss'] == 100.0 + assert get_pipe(p, 'p1')['status'] == PIPE_STATUS_OPEN change_set = add_pipe(p, 'p2', 'j1', 'j2', 10.0, 10.0, 10.0, 10.0, PIPE_STATUS_CLOSED) assert len(change_set.operations) == 1 @@ -454,7 +454,7 @@ class TestApi: self.leave(p) - +''' def test_pump(self): p = "test_pump" self.enter(p) diff --git a/tjnetwork.py b/tjnetwork.py index 12c8bd7..ec51b6b 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -212,39 +212,21 @@ def delete_tank(name: str, tank_id: str) -> ChangeSet: # pipe 4.[PIPES] ############################################################ -def get_pipe_property_names(name: str) -> list[str]: - return api.get_pipe_property_names(name) +def get_pipe_schema(name: str) -> dict[str, dict[str, Any]]: + return api.get_pipe_schema(name) def add_pipe(name: str, pipe_id: str, node1: str, node2: str, length: float = 0, diameter: float = 0, roughness: float = 0, minor_loss: float = 0, status: str = PIPE_STATUS_OPEN) -> ChangeSet: return api.add_pipe(name, pipe_id, node1, node2, length, diameter, roughness, minor_loss, status) +def get_pipe(name: str, pipe_id: str) -> dict[str, Any] | None: + return api.get_pipe(name, pipe_id) + +def set_pipe(name: str, pipe_id: str, properties: dict[str, Any]) -> ChangeSet: + return api.set_pipe(name, pipe_id, properties) + def delete_pipe(name: str, pipe_id: str) -> ChangeSet: return api.delete_pipe(name, pipe_id) -def set_pipe_node1(name: str, pipe_id: str, node1: str) -> ChangeSet: - return api.set_pipe_node1(name, pipe_id, node1) - -def set_pipe_node2(name: str, pipe_id: str, node2: str) -> ChangeSet: - return api.set_pipe_node2(name, pipe_id, node2) - -def set_pipe_length(name: str, pipe_id: str, length: float) -> ChangeSet: - return api.set_pipe_length(name, pipe_id, length) - -def set_pipe_diameter(name: str, pipe_id: str, diameter: float) -> ChangeSet: - return api.set_pipe_diameter(name, pipe_id, diameter) - -def set_pipe_roughness(name: str, pipe_id: str, roughness: float) -> ChangeSet: - return api.set_pipe_roughness(name, pipe_id, roughness) - -def set_pipe_minor_loss(name: str, pipe_id: str, minor_loss: float) -> ChangeSet: - return api.set_pipe_minor_loss(name, pipe_id, minor_loss) - -def set_pipe_status(name: str, pipe_id: str, status: float) -> ChangeSet: - return api.set_pipe_status(name, pipe_id, status) - -def get_pipe_properties(name: str, pipe_id: str) -> dict[str, Any] | None: - return api.get_pipe_properties(name, pipe_id) - ############################################################ # pump 4.[PUMPS]