Support batch command for existing APIs

This commit is contained in:
WQY\qiong
2022-11-12 18:50:20 +08:00
parent 7648b2d116
commit 6bd9afdc2e
2 changed files with 400 additions and 58 deletions

View File

@@ -5,30 +5,127 @@ from .s4_tanks import *
from .s5_pipes import *
from .s6_pumps import *
from .s7_valves import *
from .s8_tags import *
from .s9_demands import *
from .s10_status import *
from .s11_patterns import *
from .s12_curves import *
from .s13_controls import *
from .s14_rules import *
from .s15_energy import *
from .s16_emitters import *
from .s17_quality import *
from .s18_sources import *
from .s19_reactions import *
from .s20_mixing import *
from .s21_times import *
from .s22_report import *
from .s23_options import *
from .s24_coordinates import *
from .s25_vertices import *
from .s26_labels import *
from .s27_backdrop import *
from .s28_end import *
_s1_title = 'title'
_s2_junction = 'junction'
_s3_reservoir = 'reservoir'
_s4_tank = 'tank'
_s5_pipe = 'pipe'
_s6_pump = 'pump'
_s7_valve = 'valve'
_s8_tag = 'tag'
_s9_demand = 'demand'
_s10_status = 'status'
_s11_pattern = 'patten'
_s12_curve = 'curve'
_s13_control = 'control'
_s14_rule = 'rule'
_s15_global_energy = 'global_energy'
_s15_pump_energy = 'pump_energy'
_s16_emitter = 'emitter'
_s17_quality = 'quality'
_s18_source = 'source'
_s19_global_reaction = 'global_reaction'
_s19_pipe_reaction = 'pipe_reaction'
_s19_tank_reaction = 'tank_reaction'
_s20_mixing = 'mixing'
_s21_time = 'time'
_s22_report = 'report'
_s23_option = 'option'
_s24_coordinate = 'coordinate'
_s25_vertex = 'vertex'
_s26_label = 'label'
_s27_backdrop = 'backdrop'
_s28_end = 'end'
def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet:
type = cs.operations[0]['type']
if type == JUNCTION:
if type == _s1_title:
return ChangeSet()
if type == _s2_junction:
return add_junction(name, cs)
elif type == RESERVOIR:
elif type == _s3_reservoir:
return add_reservoir(name, cs)
elif type == TANK:
elif type == _s4_tank:
return add_tank(name, cs)
elif type == PIPE:
elif type == _s5_pipe:
return add_pipe(name, cs)
elif type == PUMP:
elif type == _s6_pump:
return add_pump(name, cs)
elif type == VALVE:
elif type == _s7_valve:
return add_valve(name, cs)
elif type == _s8_tag:
return ChangeSet()
elif type == _s9_demand:
return ChangeSet()
elif type == _s10_status:
return ChangeSet()
elif type == _s11_pattern:
return add_pattern(name, cs)
elif type == _s12_curve:
return add_curve(name, cs)
elif type == _s13_control:
return ChangeSet()
elif type == _s14_rule:
return ChangeSet()
elif type == _s15_global_energy:
return ChangeSet()
elif type == _s15_pump_energy:
return ChangeSet()
elif type == _s16_emitter:
return ChangeSet()
elif type == _s17_quality:
return ChangeSet()
elif type == _s18_source:
return add_source(name, cs)
elif type == _s19_global_reaction:
return ChangeSet()
elif type == _s19_pipe_reaction:
return ChangeSet()
elif type == _s19_tank_reaction:
return ChangeSet()
elif type == _s20_mixing:
return add_mixing(name, cs)
elif type == _s21_time:
return ChangeSet()
elif type == _s22_report:
return ChangeSet()
elif type == _s23_option:
return ChangeSet()
elif type == _s24_coordinate:
return ChangeSet()
elif type == _s25_vertex:
return add_vertex(name, cs)
elif type == _s26_label:
return add_label(name, cs)
elif type == _s27_backdrop:
return ChangeSet()
elif type == _s28_end:
return ChangeSet()
return ChangeSet()
@@ -36,34 +133,68 @@ def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet:
def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet:
type = cs.operations[0]['type']
if type == 'title':
if type == _s1_title:
return set_title(name, cs)
if type == JUNCTION:
if type == _s2_junction:
return set_junction(name, cs)
elif type == RESERVOIR:
elif type == _s3_reservoir:
return set_reservoir(name, cs)
elif type == TANK:
elif type == _s4_tank:
return set_tank(name, cs)
elif type == PIPE:
elif type == _s5_pipe:
return set_pipe(name, cs)
elif type == PUMP:
elif type == _s6_pump:
return set_pump(name, cs)
elif type == VALVE:
elif type == _s7_valve:
return set_valve(name, cs)
elif type == 'demand':
return set_demand(name, cs)
elif type == 'status':
elif type == _s8_tag:
return set_tag(name, cs)
elif type == _s9_demand: # exception, batch command ...
return ChangeSet()
elif type == _s10_status:
return set_status(name, cs)
elif type == PATTERN:
elif type == _s11_pattern:
return set_pattern(name, cs)
elif type == CURVE:
elif type == _s12_curve:
return set_curve(name, cs)
elif type == 'emitter':
elif type == _s13_control:
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_pump_energy:
return set_pump_energy(name, cs)
elif type == _s16_emitter:
return set_emitter(name, cs)
elif type == 'time':
elif type == _s17_quality:
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_pipe_reaction:
return set_pipe_reaction(name, cs)
elif type == _s19_tank_reaction:
return set_tank_reaction(name, cs)
elif type == _s20_mixing:
return set_mixing(name, cs)
elif type == _s21_time:
return set_time(name, cs)
elif type == 'option':
elif type == _s22_report: # no api now
return ChangeSet()
elif type == _s23_option:
return set_option(name, cs)
elif type == _s24_coordinate: # do not support update here
return ChangeSet()
elif type == _s25_vertex:
return set_vertex(name, cs)
elif type == _s26_label:
return set_label(name, cs)
elif type == _s27_backdrop:
return set_backdrop(name, cs)
elif type == _s28_end: # end
return ChangeSet()
return ChangeSet()
@@ -71,18 +202,68 @@ def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet:
def execute_delete_command(name: str, cs: ChangeSet) -> ChangeSet:
type = cs.operations[0]['type']
if type == JUNCTION:
if type == _s1_title:
return ChangeSet()
if type == _s2_junction:
return delete_junction(name, cs)
elif type == RESERVOIR:
elif type == _s3_reservoir:
return delete_reservoir(name, cs)
elif type == TANK:
elif type == _s4_tank:
return delete_tank(name, cs)
elif type == PIPE:
elif type == _s5_pipe:
return delete_pipe(name, cs)
elif type == PUMP:
elif type == _s6_pump:
return delete_pump(name, cs)
elif type == VALVE:
elif type == _s7_valve:
return delete_valve(name, cs)
elif type == _s8_tag:
return ChangeSet()
elif type == _s9_demand:
return ChangeSet()
elif type == _s10_status:
return ChangeSet()
elif type == _s11_pattern:
return delete_pattern(name, cs)
elif type == _s12_curve:
return delete_curve(name, cs)
elif type == _s13_control:
return ChangeSet()
elif type == _s14_rule:
return ChangeSet()
elif type == _s15_global_energy:
return ChangeSet()
elif type == _s15_pump_energy:
return ChangeSet()
elif type == _s16_emitter:
return ChangeSet()
elif type == _s17_quality:
return ChangeSet()
elif type == _s18_source:
return delete_source(name, cs)
elif type == _s19_global_reaction:
return ChangeSet()
elif type == _s19_pipe_reaction:
return ChangeSet()
elif type == _s19_tank_reaction:
return ChangeSet()
elif type == _s20_mixing:
return delete_mixing(name, cs)
elif type == _s21_time:
return ChangeSet()
elif type == _s22_report:
return ChangeSet()
elif type == _s23_option:
return ChangeSet()
elif type == _s24_coordinate:
return ChangeSet()
elif type == _s25_vertex:
return delete_vertex(name, cs)
elif type == _s26_label:
return delete_label(name, cs)
elif type == _s27_backdrop:
return ChangeSet()
elif type == _s28_end:
return ChangeSet()
return ChangeSet()
@@ -108,18 +289,68 @@ def execute_batch_commands(name: str, cs: ChangeSet) -> ChangeSet:
def cache_add_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
type = cs.operations[0]['type']
if type == JUNCTION:
if type == _s1_title:
return None
if type == _s2_junction:
return add_junction_cache(name, cs)
elif type == RESERVOIR:
elif type == _s3_reservoir:
return add_reservoir_cache(name, cs)
elif type == TANK:
elif type == _s4_tank:
return add_tank_cache(name, cs)
elif type == PIPE:
elif type == _s5_pipe:
return add_pipe_cache(name, cs)
elif type == PUMP:
elif type == _s6_pump:
return add_pump_cache(name, cs)
elif type == VALVE:
elif type == _s7_valve:
return add_valve_cache(name, cs)
elif type == _s8_tag:
return None
elif type == _s9_demand:
return None
elif type == _s10_status:
return None
elif type == _s11_pattern:
return add_pattern_cache(name, cs)
elif type == _s12_curve:
return add_curve_cache(name, cs)
elif type == _s13_control:
return None
elif type == _s14_rule:
return None
elif type == _s15_global_energy:
return None
elif type == _s15_pump_energy:
return None
elif type == _s16_emitter:
return None
elif type == _s17_quality:
return None
elif type == _s18_source:
return add_source_cache(name, cs)
elif type == _s19_global_reaction:
return None
elif type == _s19_pipe_reaction:
return None
elif type == _s19_tank_reaction:
return None
elif type == _s20_mixing:
return add_mixing_cache(name, cs)
elif type == _s21_time:
return None
elif type == _s22_report:
return None
elif type == _s23_option:
return None
elif type == _s24_coordinate:
return None
elif type == _s25_vertex:
return add_vertex_cache(name, cs)
elif type == _s26_label:
return add_label_cache(name, cs)
elif type == _s27_backdrop:
return None
elif type == _s28_end:
return None
return None
@@ -127,34 +358,68 @@ def cache_add_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
def cache_update_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
type = cs.operations[0]['type']
if type == 'title':
if type == _s1_title:
return set_title_cache(name, cs)
if type == JUNCTION:
if type == _s2_junction:
return set_junction_cache(name, cs)
elif type == RESERVOIR:
elif type == _s3_reservoir:
return set_reservoir_cache(name, cs)
elif type == TANK:
elif type == _s4_tank:
return set_tank_cache(name, cs)
elif type == PIPE:
elif type == _s5_pipe:
return set_pipe_cache(name, cs)
elif type == PUMP:
elif type == _s6_pump:
return set_pump_cache(name, cs)
elif type == VALVE:
elif type == _s7_valve:
return set_valve_cache(name, cs)
elif type == 'demand':
return set_demand_cache(name, cs)
elif type == 'status':
elif type == _s8_tag:
return set_tag_cache(name, cs)
elif type == _s9_demand: # exception, batch command ...
return None
elif type == _s10_status:
return set_status_cache(name, cs)
elif type == PATTERN:
elif type == _s11_pattern:
return set_pattern_cache(name, cs)
elif type == CURVE:
elif type == _s12_curve:
return set_curve_cache(name, cs)
elif type == 'emitter':
elif type == _s13_control:
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_pump_energy:
return set_pump_energy_cache(name, cs)
elif type == _s16_emitter:
return set_emitter_cache(name, cs)
elif type == 'time':
elif type == _s17_quality:
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_pipe_reaction:
return set_pipe_reaction_cache(name, cs)
elif type == _s19_tank_reaction:
return set_tank_reaction_cache(name, cs)
elif type == _s20_mixing:
return set_mixing_cache(name, cs)
elif type == _s21_time:
return set_time_cache(name, cs)
elif type == 'option':
elif type == _s22_report: # no api now
return None
elif type == _s23_option:
return set_option_cache(name, cs)
elif type == _s24_coordinate: # do not support update here
return None
elif type == _s25_vertex:
return set_vertex_cache(name, cs)
elif type == _s26_label:
return set_label_cache(name, cs)
elif type == _s27_backdrop:
return set_backdrop_cache(name, cs)
elif type == _s28_end: # end
return None
return None
@@ -162,18 +427,68 @@ def cache_update_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
def cache_delete_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
type = cs.operations[0]['type']
if type == JUNCTION:
if type == _s1_title:
return None
if type == _s2_junction:
return delete_junction_cache(name, cs)
elif type == RESERVOIR:
elif type == _s3_reservoir:
return delete_reservoir_cache(name, cs)
elif type == TANK:
elif type == _s4_tank:
return delete_tank_cache(name, cs)
elif type == PIPE:
elif type == _s5_pipe:
return delete_pipe_cache(name, cs)
elif type == PUMP:
elif type == _s6_pump:
return delete_pump_cache(name, cs)
elif type == VALVE:
elif type == _s7_valve:
return delete_valve_cache(name, cs)
elif type == _s8_tag:
return None
elif type == _s9_demand:
return None
elif type == _s10_status:
return None
elif type == _s11_pattern:
return delete_pattern_cache(name, cs)
elif type == _s12_curve:
return delete_curve_cache(name, cs)
elif type == _s13_control:
return None
elif type == _s14_rule:
return None
elif type == _s15_global_energy:
return None
elif type == _s15_pump_energy:
return None
elif type == _s16_emitter:
return None
elif type == _s17_quality:
return None
elif type == _s18_source:
return delete_source_cache(name, cs)
elif type == _s19_global_reaction:
return None
elif type == _s19_pipe_reaction:
return None
elif type == _s19_tank_reaction:
return None
elif type == _s20_mixing:
return delete_mixing_cache(name, cs)
elif type == _s21_time:
return None
elif type == _s22_report:
return None
elif type == _s23_option:
return None
elif type == _s24_coordinate:
return None
elif type == _s25_vertex:
return delete_vertex_cache(name, cs)
elif type == _s26_label:
return delete_label_cache(name, cs)
elif type == _s27_backdrop:
return None
elif type == _s28_end:
return None
return None
@@ -189,21 +504,33 @@ 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))
elif operation == API_UPDATE:
r = cache_update_command(name, ChangeSet(op))
if op['type'] == 'demand':
r = set_demand_cache(name, ChangeSet(op))
elif operation == API_DELETE:
r = cache_delete_command(name, ChangeSet(op))
if r == None:
return ChangeSet()
redo_sql_s.append(r.redo_sql)
undo_sql_s.append(r.undo_sql)
redo_cs_s.append(r.redo_cs)
undo_cs_s.append(r.undo_cs)
if op['type'] == 'demand':
redo_sql_s.append(r.redo_sql)
undo_sql_s.append(r.undo_sql)
for d in r.redo_cs:
redo_cs_s.append(d)
for d in r.undo_cs.reverse(): # need reverse again...
undo_cs_s.append(r.undo_cs)
else:
redo_sql_s.append(r.redo_sql)
undo_sql_s.append(r.undo_sql)
redo_cs_s.append(r.redo_cs)
undo_cs_s.append(r.undo_cs)
except:
pass

View File

@@ -43,6 +43,21 @@ def set_vertex_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
def add_vertex_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
result = set_vertex_cache(name, cs)
result.redo_cs |= g_add_prefix
result.undo_cs |= g_delete_prefix
return result
def delete_vertex_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
cs.operations[0]['coords'] = []
result = set_vertex_cache(name, cs)
result.redo_cs |= g_delete_prefix
result.undo_cs |= g_add_prefix
return result
def set_vertex(name: str, cs: ChangeSet) -> ChangeSet:
result = set_vertex_cache(name, cs)
result.redo_cs |= g_update_prefix