from typing import Any from ..core.database import ( ChangeSet, DatabaseCommand, execute_command, g_add_prefix, g_delete_prefix, g_update_prefix, read_all, sql_literal, try_read, ) PIPE_STATUS_OPEN = 'OPEN' PIPE_STATUS_CLOSED = 'CLOSED' PIPE_STATUS_CV = 'CV' def get_pipe_schema(name: str) -> dict[str, dict[str, Any]]: return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True }, 'node1' : {'type': 'str' , 'optional': False , 'readonly': False}, 'node2' : {'type': 'str' , 'optional': False , 'readonly': False}, 'length' : {'type': 'float' , 'optional': False , 'readonly': False}, 'diameter' : {'type': 'float' , 'optional': False , 'readonly': False}, 'roughness' : {'type': 'float' , 'optional': False , 'readonly': False}, 'minor_loss' : {'type': 'float' , 'optional': False , 'readonly': False}, 'status' : {'type': 'str' , 'optional': False , 'readonly': False} } def get_pipe(name: str, id: str) -> dict[str, Any]: p = try_read(name, "select l.id, l.start_node_id as node1, l.end_node_id as node2, p.length, p.diameter, p.roughness, p.minor_loss, p.status from network.links l join network.pipes p on p.link_id = l.id where l.id = %s", (id,)) if p == None: return {} d = {} d['id'] = str(p['id']) d['node1'] = str(p['node1']) d['node2'] = str(p['node2']) d['length'] = float(p['length']) d['diameter'] = float(p['diameter']) d['roughness'] = float(p['roughness']) d['minor_loss'] = float(p['minor_loss']) d['status'] = str(p['status']) return d # DingZQ, 2025-03-29 def get_all_pipes(name: str) -> list[dict[str, Any]]: rows = read_all( name, "SELECT id, start_node_id AS node1, end_node_id AS node2, length, " "diameter, roughness, minor_loss, status FROM gis.pipes ORDER BY id", ) if rows == None: return [] result = [] for row in rows: d = {} d['id'] = str(row['id']) d['node1'] = str(row['node1']) d['node2'] = str(row['node2']) d['length'] = float(row['length']) d['diameter'] = float(row['diameter']) d['roughness'] = float(row['roughness']) d['minor_loss'] = float(row['minor_loss']) d['status'] = str(row['status']) result.append(d) return result def get_pipes_by_property( name: str, fields: list[str] | None = None, property_conditions: dict[str, Any] | None = None, ) -> list[dict[str, Any]]: if not fields: fields = [ 'id', 'node1', 'node2', 'length', 'diameter', 'roughness', 'minor_loss', 'status', ] rows = read_all( name, "SELECT id, start_node_id AS node1, end_node_id AS node2, length, " "diameter, roughness, minor_loss, status FROM gis.pipes ORDER BY id", ) if rows == None: return [] result = [] for row in rows: if property_conditions: matched = True for key, value in property_conditions.items(): if row[key] != value: matched = False break if not matched: continue d = {} for field in fields: value = row[field] if field in ('length', 'diameter', 'roughness', 'minor_loss') and value is not None: d[field] = float(value) elif field in ('id', 'node1', 'node2', 'status') and value is not None: d[field] = str(value) else: d[field] = value result.append(d) return result class Pipe(object): def __init__(self, input: dict[str, Any]) -> None: self.type = 'pipe' self.id = str(input['id']) self.node1 = str(input['node1']) self.node2 = str(input['node2']) self.length = float(input['length']) self.diameter = float(input['diameter']) self.roughness = float(input['roughness']) self.minor_loss = float(input['minor_loss']) self.status = str(input['status']) self.f_type = sql_literal(self.type) self.f_id = sql_literal(self.id) self.f_node1 = sql_literal(self.node1) self.f_node2 = sql_literal(self.node2) self.f_length = sql_literal(self.length) self.f_diameter = sql_literal(self.diameter) self.f_roughness = sql_literal(self.roughness) self.f_minor_loss = sql_literal(self.minor_loss) self.f_status = sql_literal(self.status) def as_dict(self) -> dict[str, Any]: return { 'type': self.type, 'id': self.id, 'node1': self.node1, 'node2': self.node2, 'length': self.length, 'diameter': self.diameter, 'roughness': self.roughness, 'minor_loss': self.minor_loss, 'status': self.status } def _set_pipe(name: str, cs: ChangeSet) -> DatabaseCommand: raw_new = get_pipe(name, cs.operations[0]['id']) new_dict = cs.operations[0] schema = get_pipe_schema(name) for key, value in schema.items(): if key in new_dict and not value['readonly']: raw_new[key] = new_dict[key] new = Pipe(raw_new) statement = f"update network.links set start_node_id = {new.f_node1}, end_node_id = {new.f_node2} where id = {new.f_id};" statement += f"\nupdate network.pipes set length = {new.f_length}, diameter = {new.f_diameter}, roughness = {new.f_roughness}, minor_loss = {new.f_minor_loss}, status = {new.f_status} where link_id = {new.f_id};" change = g_update_prefix | new.as_dict() return DatabaseCommand(statement, [change]) def set_pipe(name: str, cs: ChangeSet) -> ChangeSet: if 'id' not in cs.operations[0]: return ChangeSet() if get_pipe(name, cs.operations[0]['id']) == {}: return ChangeSet() return execute_command(name, _set_pipe(name, cs)) def _add_pipe(name: str, cs: ChangeSet) -> DatabaseCommand: new = Pipe(cs.operations[0]) statement = f"insert into network.links (id, link_type, start_node_id, end_node_id) values ({new.f_id}, {new.f_type}, {new.f_node1}, {new.f_node2});" statement += f"\ninsert into network.pipes (link_id, length, diameter, roughness, minor_loss, status) values ({new.f_id}, {new.f_length}, {new.f_diameter}, {new.f_roughness}, {new.f_minor_loss}, {new.f_status});" change = g_add_prefix | new.as_dict() return DatabaseCommand(statement, [change]) def add_pipe(name: str, cs: ChangeSet) -> ChangeSet: if 'id' not in cs.operations[0]: return ChangeSet() if get_pipe(name, cs.operations[0]['id']) != {}: return ChangeSet() return execute_command(name, _add_pipe(name, cs)) def _delete_pipe(name: str, cs: ChangeSet) -> DatabaseCommand: element_id = str(cs.operations[0]['id']) f_id = sql_literal(element_id) statement = f"delete from network.links where id = {f_id};" change = g_delete_prefix | {'type': 'pipe', 'id': element_id} return DatabaseCommand(statement, [change]) def delete_pipe(name: str, cs: ChangeSet) -> ChangeSet: if 'id' not in cs.operations[0]: return ChangeSet() if get_pipe(name, cs.operations[0]['id']) == {}: return ChangeSet() return execute_command(name, _delete_pipe(name, cs)) #-------------------------------------------------------------- # [EPA2][EPA3] # [IN] # id node1 node2 length diam rcoeff (lcoeff status) ;desc # [OUT] # id node1 node2 length diam rcoeff lcoeff (status) ;desc #-------------------------------------------------------------- def inp_in_pipe(line: str) -> str: tokens = line.split() num = len(tokens) has_desc = tokens[-1].startswith(';') num_without_desc = (num - 1) if has_desc else num id = str(tokens[0]) node1 = str(tokens[1]) node2 = str(tokens[2]) length = float(tokens[3]) diameter = float(tokens[4]) roughness = float(tokens[5]) minor_loss = float(tokens[6]) # status is must-have, here fix input status = str(tokens[7].upper()) if num_without_desc >= 8 else PIPE_STATUS_OPEN desc = str(tokens[-1]) if has_desc else None return str(f"insert into network.links (id, link_type, start_node_id, end_node_id) values ({sql_literal(id)}, 'pipe', {sql_literal(node1)}, {sql_literal(node2)});insert into network.pipes (link_id, length, diameter, roughness, minor_loss, status) values ({sql_literal(id)}, {sql_literal(length)}, {sql_literal(diameter)}, {sql_literal(roughness)}, {sql_literal(minor_loss)}, {sql_literal(status)});") def inp_out_pipe(name: str) -> list[str]: lines = [] objs = read_all(name, 'select l.id, l.start_node_id as node1, l.end_node_id as node2, p.length, p.diameter, p.roughness, p.minor_loss, p.status from network.links l join network.pipes p on p.link_id = l.id order by l.id') for obj in objs: id = obj['id'] node1 = obj['node1'] node2 = obj['node2'] length = obj['length'] diameter = obj['diameter'] roughness = obj['roughness'] minor_loss = obj['minor_loss'] status = obj['status'] desc = ';' lines.append(f'{id} {node1} {node2} {length} {diameter} {roughness} {minor_loss} {status} {desc}') return lines