Add pattern api and test
This commit is contained in:
@@ -13,7 +13,7 @@ from .operation import sync_with_server
|
|||||||
|
|
||||||
from .command import execute_batch_commands
|
from .command import execute_batch_commands
|
||||||
|
|
||||||
from .s0_base import JUNCTION, RESERVOIR, TANK, PIPE, PUMP, VALVE
|
from .s0_base import JUNCTION, RESERVOIR, TANK, PIPE, PUMP, VALVE, PATTERN, CURVE
|
||||||
from .s0_base import is_node, is_junction, is_reservoir, is_tank
|
from .s0_base import is_node, is_junction, is_reservoir, is_tank
|
||||||
from .s0_base import is_link, is_pipe, is_pump, is_valve
|
from .s0_base import is_link, is_pipe, is_pump, is_valve
|
||||||
from .s0_base import is_curve
|
from .s0_base import is_curve
|
||||||
@@ -43,4 +43,6 @@ from .s9_demands import get_demand_schema, get_demand, set_demand
|
|||||||
from .s10_status import LINK_STATUS_OPEN, LINK_STATUS_CLOSED, LINK_STATUS_ACTIVE
|
from .s10_status import LINK_STATUS_OPEN, LINK_STATUS_CLOSED, LINK_STATUS_ACTIVE
|
||||||
from .s10_status import get_status_schema, get_status, set_status
|
from .s10_status import get_status_schema, get_status, set_status
|
||||||
|
|
||||||
|
from .s11_patterns import get_pattern_schema, get_pattern, set_pattern
|
||||||
|
|
||||||
from .s24_coordinates import get_node_coord
|
from .s24_coordinates import get_node_coord
|
||||||
|
|||||||
@@ -3,17 +3,20 @@ from .connection import g_conn_dict as conn
|
|||||||
from .operation import *
|
from .operation import *
|
||||||
|
|
||||||
|
|
||||||
_NODE = "_node"
|
_NODE = '_node'
|
||||||
_LINK = "_link"
|
_LINK = '_link'
|
||||||
_CURVE = "_curve"
|
_CURVE = '_curve'
|
||||||
_PATTERN = "_pattern"
|
_PATTERN = '_pattern'
|
||||||
|
|
||||||
JUNCTION = "junction"
|
JUNCTION = 'junction'
|
||||||
RESERVOIR = "reservoir"
|
RESERVOIR = 'reservoir'
|
||||||
TANK = "tank"
|
TANK = 'tank'
|
||||||
PIPE = "pipe"
|
PIPE = 'pipe'
|
||||||
PUMP = "pump"
|
PUMP = 'pump'
|
||||||
VALVE = "valve"
|
VALVE = 'valve'
|
||||||
|
|
||||||
|
PATTERN = 'pattern'
|
||||||
|
CURVE = 'curve'
|
||||||
|
|
||||||
|
|
||||||
def _get_from(name: str, id: str, base_type: str) -> Row | None:
|
def _get_from(name: str, id: str, base_type: str) -> Row | None:
|
||||||
|
|||||||
44
api/s11_patterns.py
Normal file
44
api/s11_patterns.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from .operation import *
|
||||||
|
from .s0_base import *
|
||||||
|
|
||||||
|
|
||||||
|
def get_pattern_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
|
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||||
|
'factors' : {'type': 'float_list' , 'optional': False , 'readonly': False } }
|
||||||
|
|
||||||
|
|
||||||
|
def get_pattern(name: str, id: str) -> dict[str, Any]:
|
||||||
|
pas = read_all(name, f"select * from patterns where id = '{id}'")
|
||||||
|
ps = []
|
||||||
|
for r in pas:
|
||||||
|
ps.append(float(r['factor']))
|
||||||
|
return { 'id': id, 'factors': ps }
|
||||||
|
|
||||||
|
|
||||||
|
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
id = cs.operations[0]['id']
|
||||||
|
|
||||||
|
old = get_pattern(name, id)
|
||||||
|
new = { 'id': id, 'factors': [] }
|
||||||
|
|
||||||
|
f_id = f"'{id}'"
|
||||||
|
|
||||||
|
# TODO: transaction ?
|
||||||
|
redo_sql = f"delete from patterns where id = {f_id};"
|
||||||
|
redo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||||
|
redo_sql += f"\ninsert into _pattern (id) values ({f_id});"
|
||||||
|
for factor in cs.operations[0]['factors']:
|
||||||
|
f_factor = float(factor)
|
||||||
|
redo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||||
|
new['factors'].append(f_factor)
|
||||||
|
|
||||||
|
undo_sql = f"delete from patterns where id = {f_id};"
|
||||||
|
undo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||||
|
undo_sql += f"\ninsert into _pattern (id) values ({f_id});"
|
||||||
|
for f_factor in old['factors']:
|
||||||
|
undo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||||
|
|
||||||
|
redo_cs = g_update_prefix | { 'type': 'pattern' } | new
|
||||||
|
undo_cs = g_update_prefix | { 'type': 'pattern' } | old
|
||||||
|
|
||||||
|
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||||
@@ -29,6 +29,7 @@ def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
|
|
||||||
f_junction = f"'{junction}'"
|
f_junction = f"'{junction}'"
|
||||||
|
|
||||||
|
# TODO: transaction ?
|
||||||
redo_sql = f"delete from demands where junction = {f_junction};"
|
redo_sql = f"delete from demands where junction = {f_junction};"
|
||||||
for r in cs.operations[0]['demands']:
|
for r in cs.operations[0]['demands']:
|
||||||
demand = float(r['demand'])
|
demand = float(r['demand'])
|
||||||
|
|||||||
@@ -3,5 +3,5 @@
|
|||||||
create table patterns
|
create table patterns
|
||||||
(
|
(
|
||||||
id varchar(32) references _pattern(id) not null
|
id varchar(32) references _pattern(id) not null
|
||||||
, multipliers numeric not null
|
, factor numeric not null
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1312,6 +1312,50 @@ class TestApi:
|
|||||||
self.leave(p)
|
self.leave(p)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pattern(self):
|
||||||
|
p = 'test_demand'
|
||||||
|
self.enter(p)
|
||||||
|
|
||||||
|
assert is_pattern(p, 'p0') == False
|
||||||
|
p0 = get_pattern(p, 'p0')
|
||||||
|
assert p0['id'] == 'p0'
|
||||||
|
assert p0['factors'] == []
|
||||||
|
|
||||||
|
set_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]}))
|
||||||
|
|
||||||
|
assert is_pattern(p, 'p0')
|
||||||
|
p0 = get_pattern(p, 'p0')
|
||||||
|
assert p0['id'] == 'p0'
|
||||||
|
assert p0['factors'] == [1.0, 2.0, 3.0]
|
||||||
|
|
||||||
|
self.leave(p)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pattern_op(self):
|
||||||
|
p = 'test_pattern_op'
|
||||||
|
self.enter(p)
|
||||||
|
|
||||||
|
cs = set_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})).operations[0]
|
||||||
|
assert cs['operation'] == API_UPDATE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == [1.0, 2.0, 3.0]
|
||||||
|
|
||||||
|
cs = execute_undo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_UPDATE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == []
|
||||||
|
|
||||||
|
cs = execute_redo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_UPDATE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == [1.0, 2.0, 3.0]
|
||||||
|
|
||||||
|
self.leave(p)
|
||||||
|
|
||||||
|
|
||||||
def test_snapshot(self):
|
def test_snapshot(self):
|
||||||
p = "test_snapshot"
|
p = "test_snapshot"
|
||||||
self.enter(p)
|
self.enter(p)
|
||||||
|
|||||||
18
tjnetwork.py
18
tjnetwork.py
@@ -23,6 +23,8 @@ TANK = api.TANK
|
|||||||
PIPE = api.PIPE
|
PIPE = api.PIPE
|
||||||
PUMP = api.PUMP
|
PUMP = api.PUMP
|
||||||
VALVE = api.VALVE
|
VALVE = api.VALVE
|
||||||
|
PATTERN = api.PATTERN
|
||||||
|
CURVE = api.CURVE
|
||||||
|
|
||||||
OVERFLOW_YES = api.OVERFLOW_YES
|
OVERFLOW_YES = api.OVERFLOW_YES
|
||||||
OVERFLOW_NO = api.OVERFLOW_NO
|
OVERFLOW_NO = api.OVERFLOW_NO
|
||||||
@@ -295,7 +297,7 @@ def delete_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
|
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# demands 9.[DEMANDS]
|
# demand 9.[DEMANDS]
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def get_demand_schema(name: str) -> dict[str, dict[str, Any]]:
|
def get_demand_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
@@ -323,6 +325,20 @@ def set_status(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
return api.set_status(name, cs)
|
return api.set_status(name, cs)
|
||||||
|
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# pattern 11.[PATTERNS]
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def get_pattern_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
|
return api.get_pattern_schema(name)
|
||||||
|
|
||||||
|
def get_pattern(name: str, id: str) -> dict[str, Any]:
|
||||||
|
return api.get_pattern(name, id)
|
||||||
|
|
||||||
|
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return api.set_pattern(name, cs)
|
||||||
|
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# coord 24.[COORDINATES]
|
# coord 24.[COORDINATES]
|
||||||
############################################################
|
############################################################
|
||||||
|
|||||||
Reference in New Issue
Block a user