diff --git a/api/del_cmd_raw.py b/api/del_cmd_raw.py index 5531435..1953fda 100644 --- a/api/del_cmd_raw.py +++ b/api/del_cmd_raw.py @@ -3,7 +3,6 @@ from .sections import * from .s0_base import * -from .s2_junctions import unset_junction_by_pattern from .s3_reservoirs import unset_reservoir_by_pattern from .s4_tanks import unset_tank_by_curve from .s6_pumps import unset_pump_by_curve, unset_pump_by_pattern @@ -164,7 +163,6 @@ def delete_pattern_cascade_batch_cmd(name: str, cs: ChangeSet) -> ChangeSet: if row == None: return result - result.merge(unset_junction_by_pattern(name, id)) result.merge(unset_reservoir_by_pattern(name, id)) result.merge(unset_pump_by_pattern(name, id)) result.merge(unset_demand_by_pattern(name, id)) diff --git a/api/parser.py b/api/parser.py index e1d4324..296d75a 100644 --- a/api/parser.py +++ b/api/parser.py @@ -185,6 +185,8 @@ def _read_inp(file: dict[str, list[str]]) -> ChangeSet: coords = inp_in_coord(section) for s in ['JUNCTIONS', 'RESERVOIRS', 'TANKS']: for node in file_cs[s].operations: + if node['type'] == 'demand': + continue if node['id'] in coords: coord = coords[node['id']] node |= { 'x' : coord['x'], 'y' : coord['y'] } @@ -204,10 +206,6 @@ def _read_inp(file: dict[str, list[str]]) -> ChangeSet: elif name == 'END': pass # :) - # if demand section is empty, fill it with junction data - demands_cs = fill_demand(file_cs['JUNCTIONS'], file_cs['DEMANDS']) - file_cs['DEMANDS'].merge(demands_cs) - # release file file = {} diff --git a/api/s2_junctions.py b/api/s2_junctions.py index c14677e..5c034fb 100644 --- a/api/s2_junctions.py +++ b/api/s2_junctions.py @@ -8,8 +8,6 @@ def get_junction_schema(name: str) -> dict[str, dict[str, Any]]: 'x' : {'type': 'float' , 'optional': False , 'readonly': False}, 'y' : {'type': 'float' , 'optional': False , 'readonly': False}, 'elevation' : {'type': 'float' , 'optional': False , 'readonly': False}, - 'demand' : {'type': 'float' , 'optional': True , 'readonly': False}, - 'pattern' : {'type': 'str' , 'optional': True , 'readonly': False}, 'links' : {'type': 'str_list' , 'optional': False , 'readonly': True } } @@ -21,8 +19,6 @@ def get_junction(name: str, id: str) -> dict[str, Any]: d['x'] = float(xy['x']) d['y'] = float(xy['y']) d['elevation'] = float(j['elevation']) - d['demand'] = float(j['demand']) if j['demand'] != None else None - d['pattern'] = str(j['pattern']) if j['pattern'] != None else None d['links'] = get_node_links(name, id) return d @@ -34,18 +30,14 @@ class Junction(object): self.x = float(input['x']) self.y = float(input['y']) self.elevation = float(input['elevation']) - self.demand = float(input['demand']) if 'demand' in input and input['demand'] != None else None - self.pattern = str(input['pattern']) if 'pattern' in input and input['pattern'] != None else None self.f_type = f"'{self.type}'" self.f_id = f"'{self.id}'" self.f_coord = f"'({self.x}, {self.y})'" self.f_elevation = self.elevation - self.f_demand = self.demand if self.demand != None else 'null' - self.f_pattern = f"'{self.pattern}'" if self.pattern != None else 'null' def as_dict(self) -> dict[str, Any]: - return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation, 'demand': self.demand, 'pattern': self.pattern } + return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation } def as_id_dict(self) -> dict[str, Any]: return { 'type': self.type, 'id': self.id } @@ -62,11 +54,11 @@ def set_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet: raw_new[key] = new_dict[key] new = Junction(raw_new) - redo_sql = f"update junctions set elevation = {new.f_elevation}, demand = {new.f_demand}, pattern = {new.f_pattern} where id = {new.f_id};" + redo_sql = f"update junctions set elevation = {new.f_elevation} where id = {new.f_id};" redo_sql += f"\nupdate coordinates set coord = {new.f_coord} where node = {new.f_id};" undo_sql = f"update coordinates set coord = {old.f_coord} where node = {old.f_id};" - undo_sql += f"\nupdate junctions set elevation = {old.f_elevation}, demand = {old.f_demand}, pattern = {old.f_pattern} where id = {old.f_id};" + undo_sql += f"\nupdate junctions set elevation = {old.f_elevation} where id = {old.f_id};" redo_cs = g_update_prefix | new.as_dict() undo_cs = g_update_prefix | old.as_dict() @@ -82,7 +74,7 @@ def add_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet: new = Junction(cs.operations[0]) redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});" - redo_sql += f"\ninsert into junctions (id, elevation, demand, pattern) values ({new.f_id}, {new.f_elevation}, {new.f_demand}, {new.f_pattern});" + redo_sql += f"\ninsert into junctions (id, elevation) values ({new.f_id}, {new.f_elevation});" redo_sql += f"\ninsert into coordinates (node, coord) values ({new.f_id}, {new.f_coord});" undo_sql = f"delete from coordinates where node = {new.f_id};" @@ -107,7 +99,7 @@ def delete_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet: redo_sql += f"\ndelete from _node where id = {old.f_id};" undo_sql = f"insert into _node (id, type) values ({old.f_id}, {old.f_type});" - undo_sql += f"\ninsert into junctions (id, elevation, demand, pattern) values ({old.f_id}, {old.f_elevation}, {old.f_demand}, {old.f_pattern});" + undo_sql += f"\ninsert into junctions (id, elevation) values ({old.f_id}, {old.f_elevation});" undo_sql += f"\ninsert into coordinates (node, coord) values ({old.f_id}, {old.f_coord});" redo_cs = g_delete_prefix | old.as_id_dict() @@ -150,6 +142,7 @@ def inp_in_junction(section: list[str]) -> ChangeSet: continue obj = InpJunction(s) cs.append(g_add_prefix | {'type': 'junction', 'id': obj.id, 'elevation': obj.elevation, 'demand': obj.demand, 'pattern': obj.pattern}) + cs.append(g_update_prefix | { 'type': 'demand', 'junction': obj.id, 'demands': [{'demand': obj.demand, 'pattern': obj.pattern, 'category': None}] }) return cs @@ -159,18 +152,6 @@ def inp_out_junction(name: str) -> list[str]: for obj in objs: id = obj['id'] elev = obj['elevation'] - demand = obj['demand'] if obj['demand'] != None else '' - pattern = obj['pattern'] if obj['pattern'] != None else '' desc = ';' - lines.append(f'{id} {elev} {demand} {pattern} {desc}') + lines.append(f'{id} {elev} {desc}') return lines - - -def unset_junction_by_pattern(name: str, pattern: str) -> ChangeSet: - cs = ChangeSet() - - rows = read_all(name, f"select id from junctions where pattern = '{pattern}'") - for row in rows: - cs.append(g_update_prefix | {'type': 'junction', 'id': row['id'], 'pattern': None}) - - return cs diff --git a/api/s9_demands.py b/api/s9_demands.py index 355b4fe..a90e046 100644 --- a/api/s9_demands.py +++ b/api/s9_demands.py @@ -1,5 +1,4 @@ from .database import * -from .s2_junctions import * def get_demand_schema(name: str) -> dict[str, dict[str, Any]]: return { 'junction' : {'type': 'str' , 'optional': False , 'readonly': True }, @@ -40,7 +39,7 @@ def set_demand_cmd(name: str, cs: ChangeSet) -> DbChangeSet: redo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" new['demands'].append({ 'demand': demand, 'pattern': pattern, 'category': category }) - _undo_sql = f"delete from demands where junction = {f_junction};" + undo_sql = f"delete from demands where junction = {f_junction};" for r in old['demands']: demand = float(r['demand']) pattern = str(r['pattern']) if 'pattern' in r and r['pattern'] != None else None @@ -48,37 +47,12 @@ def set_demand_cmd(name: str, cs: ChangeSet) -> DbChangeSet: f_demand = demand f_pattern = f"'{pattern}'" if pattern != None else 'null' f_category = f"'{category}'" if category != None else 'null' - _undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" + undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" - redo_cs = [] - redo_cs.append(g_update_prefix | { 'type': 'demand' } | new) - undo_cs = [] - undo_cs.append(g_update_prefix | { 'type': 'demand' } | old) + redo_cs = g_update_prefix | { 'type': 'demand' } | new + undo_cs = g_update_prefix | { 'type': 'demand' } | old - cmd = None - if len(cs.operations[0]['demands']) > 0: - r = cs.operations[0]['demands'][0] - demand = float(r['demand']) - pattern = str(r['pattern']) if 'pattern' in r and r['pattern'] != None else None - cmd = set_junction_cmd(name, ChangeSet({'id': junction, 'demand': demand, 'pattern': pattern})) - else: - cmd = set_junction_cmd(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None})) - - undo_sql = '' - if cmd != None: - redo_sql += '\n' - redo_sql += cmd.redo_sql - - undo_sql += cmd.undo_sql - undo_sql += '\n' - undo_sql += _undo_sql - - redo_cs += cmd.redo_cs - - undo_cs += cmd.undo_cs - undo_cs.reverse() - - return DbChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) + return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs]) def set_demand(name: str, cs: ChangeSet) -> ChangeSet: @@ -123,29 +97,6 @@ def inp_in_demand(section: list[str]) -> ChangeSet: return cs -def fill_demand(junction_cs : ChangeSet, demand_cs : ChangeSet) -> ChangeSet: - cs = ChangeSet() - - for j_cs in junction_cs.operations: - if 'demand' not in j_cs: - continue - - in_demand = False - for d_cs in demand_cs.operations: - if j_cs['id'] == d_cs['junction']: - in_demand = True - break - - if not in_demand: - obj_cs : dict[str, Any] = g_update_prefix | {'type': 'demand', 'junction' : j_cs['id'], 'demands' : []} - j_demand = j_cs['demand'] - j_pattern = j_cs['pattern'] if 'pattern' in j_cs else None - obj_cs['demands'].append({'demand': j_demand, 'pattern' : j_pattern, 'category': None}) - cs.append(obj_cs) - - return cs - - def inp_out_demand(name: str) -> list[str]: lines = [] objs = read_all(name, f"select * from demands order by _order") @@ -168,11 +119,11 @@ def delete_demand_by_junction(name: str, junction: str) -> ChangeSet: def unset_demand_by_pattern(name: str, pattern: str) -> ChangeSet: cs = ChangeSet() - rows = read_all(name, f"select distinct id from junctions where pattern = '{pattern}'") + rows = read_all(name, f"select distinct junction from demands where pattern = '{pattern}'") for row in rows: - ds = get_demand(name, row['id']) + ds = get_demand(name, row['junction']) for d in ds['demands']: d['pattern'] = None - cs.append(g_update_prefix | {'type': 'demand', 'junction': row['id'], 'demands': ds['demands']}) + cs.append(g_update_prefix | {'type': 'demand', 'junction': row['junction'], 'demands': ds['demands']}) return cs diff --git a/script/sql/create/2.junctions.sql b/script/sql/create/2.junctions.sql index 85e71fc..fd09e4b 100644 --- a/script/sql/create/2.junctions.sql +++ b/script/sql/create/2.junctions.sql @@ -4,8 +4,6 @@ create table junctions ( id varchar(32) primary key references _node(id) , elevation numeric not null -, demand numeric -, pattern varchar(32) references _pattern(id) ); -- unset pattern when delete pattern diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 6f05844..322530c 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -254,8 +254,6 @@ class TestApi: assert j0['x'] == 0.0 assert j0['y'] == 10.0 assert j0['elevation'] == 20.0 - assert j0['demand'] == None - assert j0['pattern'] == None assert j0['links'] == [] set_junction(p, ChangeSet({'id': 'j0', 'x': 100.0, 'y': 200.0})) @@ -267,10 +265,6 @@ class TestApi: j0 = get_junction(p, 'j0') assert j0['elevation'] == 100.0 - set_junction(p, ChangeSet({'id': 'j0', 'demand': 100.0})) - j0 = get_junction(p, 'j0') - assert j0['demand'] == 100.0 - # TODO: pattern add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0})) @@ -314,8 +308,6 @@ class TestApi: assert cs['x'] == 0.0 assert cs['y'] == 10.0 assert cs['elevation'] == 20.0 - assert cs['demand'] == None - assert cs['pattern'] == None cs = execute_undo(p).operations[0] assert cs['operation'] == 'delete' @@ -329,8 +321,6 @@ class TestApi: assert cs['x'] == 0.0 assert cs['y'] == 10.0 assert cs['elevation'] == 20.0 - assert cs['demand'] == None - assert cs['pattern'] == None cs = execute_undo(p, True).operations[0] assert cs['operation'] == 'delete' @@ -360,8 +350,6 @@ class TestApi: assert cs['x'] == 0.0 assert cs['y'] == 10.0 assert cs['elevation'] == 20.0 - assert cs['demand'] == 100.0 - assert cs['pattern'] == None cs = execute_redo(p).operations[0] assert cs['operation'] == 'delete' @@ -375,8 +363,6 @@ class TestApi: assert cs['x'] == 0.0 assert cs['y'] == 10.0 assert cs['elevation'] == 20.0 - assert cs['demand'] == 100.0 - assert cs['pattern'] == None cs = execute_redo(p) assert len(cs.operations) == 0 @@ -391,8 +377,6 @@ class TestApi: assert cs['x'] == 100.0 assert cs['y'] == 200.0 assert cs['elevation'] == 20.0 - assert cs['demand'] == 100.0 - assert cs['pattern'] == None cs = execute_undo(p).operations[0] assert cs['operation'] == 'update' @@ -401,8 +385,6 @@ class TestApi: assert cs['x'] == 0.0 assert cs['y'] == 10.0 assert cs['elevation'] == 20.0 - assert cs['demand'] == 100.0 - assert cs['pattern'] == None self.leave(p) @@ -1699,12 +1681,8 @@ class TestApi: assert ds[1]['pattern'] == None assert ds[1]['category'] == None - assert get_junction(p, 'j1')['demand'] == 10.0 - set_demand(p, ChangeSet({'junction': 'j1', 'demands': []})) - assert get_junction(p, 'j1')['demand'] == None - d = get_demand(p, 'j1') assert d['junction'] == 'j1' assert d['demands'] == [] @@ -1733,20 +1711,10 @@ class TestApi: assert ds[1]['demand'] == 20.0 assert ds[1]['pattern'] == None assert ds[1]['category'] == None - cs = result.operations[1] - assert cs['operation'] == API_UPDATE - assert cs['type'] == JUNCTION - assert cs['id'] == 'j1' - assert cs['demand'] == 10.0 result = execute_undo(p) cs = result.operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == JUNCTION - assert cs['id'] == 'j1' - assert cs['demand'] == None - cs = result.operations[1] - assert cs['operation'] == API_UPDATE assert cs['type'] == 'demand' assert cs['junction'] == 'j1' assert len(cs['demands']) == 0 @@ -1764,11 +1732,6 @@ class TestApi: assert ds[1]['demand'] == 20.0 assert ds[1]['pattern'] == None assert ds[1]['category'] == None - cs = result.operations[1] - assert cs['operation'] == API_UPDATE - assert cs['type'] == JUNCTION - assert cs['id'] == 'j1' - assert cs['demand'] == 10.0 self.leave(p) @@ -1971,14 +1934,13 @@ class TestApi: self.enter(p) add_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})) - add_junction(p, ChangeSet({'id': 'j0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0, 'pattern': 'p0'})) + add_junction(p, ChangeSet({'id': 'j0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0})) add_reservoir(p, ChangeSet({'id': 'r0', 'x': 0.0, 'y': 10.0, 'head': 20.0, 'pattern': 'p0'})) add_pump(p, ChangeSet({'id': 'pump0', 'node1': 'j0', 'node2': 'r0', 'power': 0.0, 'pattern': 'p0'})) set_demand(p, ChangeSet({'junction': 'j0', 'demands': [{'demand': 10.0, 'pattern': 'p0', 'category': 'x'}, {'demand': 20.0, 'pattern': 'p0', 'category': None}]})) set_pump_energy(p, ChangeSet({'pump' : 'pump0', 'pattern': 'p0'})) add_source(p, ChangeSet({'node': 'j0', 's_type': SOURCE_TYPE_CONCEN, 'strength': 10.0, 'pattern': 'p0' })) assert is_pattern(p, 'p0') - assert get_junction(p, 'j0')['pattern'] == 'p0' assert get_reservoir(p, 'r0')['pattern'] == 'p0' assert get_pump(p, 'pump0')['pattern'] == 'p0' assert get_demand(p, 'j0')['demands'] == [{'demand': 10.0, 'pattern': 'p0', 'category': 'x'}, {'demand': 20.0, 'pattern': 'p0', 'category': None}] @@ -1987,7 +1949,6 @@ class TestApi: delete_pattern(p, ChangeSet({'id': 'p0'})) assert is_pattern(p, 'p0') == False - assert get_junction(p, 'j0')['pattern'] == None assert get_reservoir(p, 'r0')['pattern'] == None assert get_pump(p, 'pump0')['pattern'] == None assert get_demand(p, 'j0')['demands'] == [{'demand': 10.0, 'pattern': None, 'category': 'x'}, {'demand': 20.0, 'pattern': None, 'category': None}] @@ -1996,7 +1957,6 @@ class TestApi: execute_undo(p) assert is_pattern(p, 'p0') - assert get_junction(p, 'j0')['pattern'] == 'p0' assert get_reservoir(p, 'r0')['pattern'] == 'p0' assert get_pump(p, 'pump0')['pattern'] == 'p0' assert get_demand(p, 'j0')['demands'] == [{'demand': 10.0, 'pattern': 'p0', 'category': 'x'}, {'demand': 20.0, 'pattern': 'p0', 'category': None}] @@ -2005,7 +1965,6 @@ class TestApi: execute_redo(p) assert is_pattern(p, 'p0') == False - assert get_junction(p, 'j0')['pattern'] == None assert get_reservoir(p, 'r0')['pattern'] == None assert get_pump(p, 'pump0')['pattern'] == None assert get_demand(p, 'j0')['demands'] == [{'demand': 10.0, 'pattern': None, 'category': 'x'}, {'demand': 20.0, 'pattern': None, 'category': None}]