Drop demand & pattern from junction

This commit is contained in:
WQY\qiong
2023-03-01 22:59:40 +08:00
parent e83d3a83a5
commit 628258ef52
6 changed files with 18 additions and 133 deletions

View File

@@ -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))

View File

@@ -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 = {}

View File

@@ -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

View File

@@ -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