Clean and support v3 inp in

This commit is contained in:
Joey Wang
2023-03-22 13:00:16 +08:00
parent faec3cef73
commit 0db93b0637
3 changed files with 47 additions and 54 deletions

View File

@@ -13,8 +13,8 @@ from .s7_valves import inp_in_valve
from .s8_tags import inp_in_tag
from .s9_demands import inp_in_demand
from .s10_status import inp_in_status
from .s11_patterns import inp_in_pattern
from .s12_curves import CURVE_TYPE_PUMP, inp_in_curve
from .s11_patterns import pattern_v3_types, inp_in_pattern
from .s12_curves import curve_types, inp_in_curve
from .s13_controls import inp_in_control
from .s14_rules import inp_in_rule
from .s15_energy import inp_in_energy
@@ -35,9 +35,12 @@ from .s27_backdrop import inp_in_backdrop
_S = 'S'
_L = 'L'
def _inp_in_option(section: list[str], version: str = '3') -> str:
return inp_in_option_v3(section) if version == '3' else inp_in_option(section)
_handler = {
TITLE : (_S, inp_in_title),
JUNCTIONS : (_L, inp_in_junction),
JUNCTIONS : (_L, inp_in_junction), # line, demand_outside
RESERVOIRS : (_L, inp_in_reservoir),
TANKS : (_L, inp_in_tank),
PIPES : (_L, inp_in_pipe),
@@ -46,7 +49,7 @@ _handler = {
TAGS : (_L, inp_in_tag),
DEMANDS : (_L, inp_in_demand),
STATUS : (_L, inp_in_status),
PATTERNS : (_L, inp_in_pattern),
PATTERNS : (_L, inp_in_pattern), # line, fixed
CURVES : (_L, inp_in_curve),
CONTROLS : (_L, inp_in_control),
RULES : (_L, inp_in_rule),
@@ -58,7 +61,7 @@ _handler = {
MIXING : (_L, inp_in_mixing),
TIMES : (_S, inp_in_time),
REPORT : (_S, inp_in_report),
OPTIONS : (_S, inp_in_option),
OPTIONS : (_S, _inp_in_option), # line, version
COORDINATES : (_L, inp_in_coord),
VERTICES : (_L, inp_in_vertex),
LABELS : (_L, inp_in_label),
@@ -171,6 +174,7 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
if t[0] == _S:
sections[s] = []
variable_patterns = []
current_pattern = None
current_curve = None
curve_type_desc_line = None
@@ -209,22 +213,38 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
sections[s].append(line)
else:
if line.startswith(';'):
line = line.removeprefix(';')
if s == PATTERNS: # ;desc
pass
elif s == CURVES: # ;type: desc
curve_type_desc_line = line
if version != '3': #v2
line = line.removeprefix(';')
if s == PATTERNS: # ;desc
pass
elif s == CURVES: # ;type: desc
curve_type_desc_line = line
continue
if s == PATTERNS:
tokens = line.split()
if tokens[1].upper() in pattern_v3_types: #v3
sql_batch.add(f"insert into _pattern (id) values ('{tokens[0]}');")
current_pattern = tokens[0]
if tokens[1].upper() == 'VARIABLE':
variable_patterns.append(tokens[0])
continue
if current_pattern != tokens[0]:
sql_batch.add(f"insert into _pattern (id) values ('{tokens[0]}');")
current_pattern = tokens[0]
elif s == CURVES:
tokens = line.split()
if tokens[1].upper() in curve_types: #v3
sql_batch.add(f"insert into _curve (id, type) values ('{tokens[0]}', '{tokens[1].upper()}');")
current_curve = tokens[0]
continue
if current_curve != tokens[0]:
type = CURVE_TYPE_PUMP
type = curve_types[0]
if curve_type_desc_line != None:
type = curve_type_desc_line.split(':')[0].strip()
sql_batch.add(f"insert into _curve (id, type) values ('{tokens[0]}', '{type}');")
@@ -233,11 +253,10 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
if s == JUNCTIONS:
sql_batch.add(handler(line, demand_outside))
elif s == PATTERNS:
sql_batch.add(handler(line, current_pattern not in variable_patterns))
elif s == OPTIONS:
if version == '3':
sql_batch.add(inp_in_option_v3(line))
else:
sql_batch.add(inp_in_option(line))
sql_batch.add(handler(line, version))
else:
sql_batch.add(handler(line))

View File

@@ -1,5 +1,9 @@
from .database import *
PATTERN_V3_TYPE_FIXED = 'FIXED'
PATTERN_V3_TYPE_VARIABLE = 'VARIABLE'
pattern_v3_types = [PATTERN_V3_TYPE_FIXED, PATTERN_V3_TYPE_VARIABLE]
def get_pattern_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
@@ -107,11 +111,15 @@ def delete_pattern(name: str, cs: ChangeSet) -> ChangeSet:
#--------------------------------------------------------------
def inp_in_pattern(line: str) -> str:
def inp_in_pattern(line: str, fixed: bool = True) -> str:
tokens = line.split()
sql = ''
for token in tokens[1:]:
sql += f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});"
if fixed:
for token in tokens[1:]:
sql += f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});"
else:
for token in tokens[1::2]:
sql += f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});"
return sql
@@ -125,42 +133,6 @@ def inp_out_pattern(name: str) -> list[str]:
return lines
'''def inp_in_pattern_v3(section: list[str]) -> ChangeSet:
patterns: dict[str, list[float]] = {}
variable_patterns: list[str] = []
count = len(section)
for i in range(0, count):
if section[i].startswith(';'):
continue
tokens = section[i].split()
# for EPA3, ignore time of variable pattern...
if tokens[1] == 'VARIABLE':
variable_patterns.append(tokens[0])
continue
elif tokens[1] == 'FIXED':
continue
if tokens[0] not in patterns:
patterns[tokens[0]] = []
if tokens[0] not in variable_patterns:
for token in tokens[1:]:
patterns[tokens[0]].append(float(token))
else:
for token in tokens[1::2]:
patterns[tokens[0]].append(float(token))
cs = ChangeSet()
for id, factors in patterns.items():
cs.append(g_add_prefix | {'type': 'pattern', 'id' : id, 'factors' : factors})
#print(descs)
return cs'''
def inp_out_pattern_v3(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from patterns order by _order")

View File

@@ -5,6 +5,8 @@ CURVE_TYPE_EFFICIENCY = 'EFFICIENCY'
CURVE_TYPE_VOLUME = 'VOLUME'
CURVE_TYPE_HEADLOSS = 'HEADLOSS'
curve_types = [CURVE_TYPE_PUMP, CURVE_TYPE_EFFICIENCY, CURVE_TYPE_VOLUME, CURVE_TYPE_HEADLOSS]
def get_curve_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
'c_type' : {'type': 'str' , 'optional': False , 'readonly': False},