Remove experiment script

This commit is contained in:
王琼钰
2022-09-15 22:22:52 +08:00
committed by coding.net
parent ee0d0000f1
commit e197b0d0e3
69 changed files with 0 additions and 916 deletions

View File

@@ -1,27 +0,0 @@
-- get_title()
create function tj.get_title() returns text as
$$
declare
title text;
begin
select value into title from tj.title;
return title;
end;
$$ language plpgsql;
-- set_title()
create function tj.set_title(new_title text) returns void as
$$
declare
old_title text;
redo text;
undo text;
begin
select tj.get_title() into old_title;
update tj.title set value = new_title where value = old_title;
redo := concat('update tj.title set value = ''', new_title, ''' where value = ''', old_title, '''');
undo := concat('update tj.title set value = ''', old_title, ''' where value = ''', new_title, '''');
perform tj.add_operation(redo, undo);
end;
$$ language plpgsql;

View File

@@ -1,109 +0,0 @@
create function tj.add_operation(redo text, undo text) returns void as
$$
declare
parent_id int;
curr_id int;
begin
select id into parent_id from tj.current_operation;
insert into tj.operation (id, redo, undo, parent) values (default, redo, undo, parent_id);
select max(id) into curr_id from tj.operation;
update tj.current_operation set id = curr_id where id = parent_id;
end;
$$ language plpgsql;
create function tj.have_transaction() returns boolean as
$$
declare
tran_count int;
begin
select count(*) into tran_count from tj.transaction_operation;
return tran_count > 0;
end;
$$ language plpgsql;
create function tj.execute_undo(discard boolean) returns void as
$$
declare
curr_id int;
have_tran boolean;
tran_id int;
strict_mode boolean;
undo_sql text;
parent_id int;
begin
select id into curr_id from tj.current_operation;
select tj.have_transaction() into have_tran;
if have_tran then
select strict into strict_mode from tj.transaction_operation;
if strict_mode then
return; -- strict mode disallow undo
else
select id into tran_id from tj.transaction_operation;
if tran_id >= curr_id then
return; -- # normal mode disallow undo start point, and there is foreign key constraint
end if;
end if;
end if;
select undo into undo_sql from tj.operation where id = curr_id;
if undo_sql = '' then
return;
end if;
select parent into parent_id from tj.operation where id = curr_id;
if discard then
update tj.operation set redo_child = null where id = parent_id;
else
update tj.operation set redo_child = curr_id where id = parent_id;
end if;
execute undo_sql;
update tj.current_operation set id = parent_id where id = curr_id;
if discard then
delete from tj.transaction_operation where id = curr_id;
delete from tj.snapshot_operation where id = curr_id;
delete from tj.operation where id = curr_id;
end if;
end;
$$ language plpgsql;
create function tj.undo() returns void as
$$
declare
begin
perform tj.execute_undo(false);
end;
$$ language plpgsql;
create function tj.discard_undo() returns void as
$$
declare
begin
perform tj.execute_undo(true);
end;
$$ language plpgsql;
create function tj.redo() returns void as
$$
declare
curr_id int;
child_id int;
redo_sql text;
begin
select id into curr_id from tj.current_operation;
select redo_child into child_id from tj.operation where id = curr_id;
if child_id = null then
return;
end if;
select redo into redo_sql from tj.operation where id = child_id;
execute redo_sql;
update tj.current_operation set id = child_id where id = curr_id;
end;
$$ language plpgsql;

View File

@@ -1,9 +0,0 @@
create function tj.have_project(in_name text) returns boolean as
$$
declare
db_count int;
begin
select count(*) into db_count from pg_database where datname = in_name;
return db_count > 0;
end;
$$ language plpgsql;

View File

@@ -1,3 +0,0 @@
drop function if exists tj.set_title;
drop function if exists tj.get_title;

View File

@@ -1,9 +0,0 @@
drop function if exists tj.redo;
drop function if exists tj.discard_undo;
drop function if exists tj.undo;
drop function if exists tj.execute_undo;
drop function if exists tj.have_transaction;
drop function if exists tj.add_operation;

View File

@@ -1,6 +0,0 @@
create function xxx() returns void as
$$
declare
begin
end;
$$ language plpgsql;

View File

@@ -1,35 +0,0 @@
create type tj.node_type as enum
(
'junction'
, 'reservoir'
, 'tank'
);
create type tj.link_type as enum
(
'pipe'
, 'pump'
, 'valve'
);
create table tj.node
(
id varchar(32) primary key
, type tj.node_type not null
);
create table tj.link
(
id varchar(32) primary key
, type tj.link_type not null
);
create table tj.curve
(
id varchar(32) primary key
);
create table tj.pattern
(
id varchar(32) primary key
);

View File

@@ -1,8 +0,0 @@
-- [title]
create table tj.title
(
value text
);
insert into tj.title (value) values ('');

View File

@@ -1,33 +0,0 @@
-- [status]
create type tj.status_pipe_pump_status as enum
(
'open'
, 'closed'
);
create table tj.status_pipe
(
id varchar(32) primary key references tj.pipes(id)
, status tj.status_pipe_pump_status not null
);
create table tj.status_pump
(
id varchar(32) primary key references tj.pumps(id)
, status tj.status_pipe_pump_status not null
);
create type tj.status_valve_status as enum ('open', 'closed', 'active');
create table tj.status_valve
(
id varchar(32) primary key references tj.valves(id)
, status tj.status_valve_status not null
);
create table tj.status_link
(
id varchar(32) primary key references tj.link(id)
, setting numeric not null
);

View File

@@ -1,7 +0,0 @@
-- [patterns]
create table tj.patterns
(
id varchar(32) references tj.pattern(id) not null
, multipliers numeric not null
);

View File

@@ -1,8 +0,0 @@
-- [curves]
create table tj.curves
(
id varchar(32) references tj.curve(id) not null
, x numeric not null
, y numeric not null
);

View File

@@ -1,30 +0,0 @@
-- [controls]
create type tj.controls_1_prep as enum ('above', 'below');
-- link linkid status if node nodeid above / below value
create table tj.controls_1
(
linkid varchar(32) primary key references tj.link(id)
, status text not null -- open / closed, a pump speed setting, or a control valve setting
, nodeid varchar(32) references tj.node(id) not null
, prep tj.controls_1_prep not null
, value numeric not null
);
-- link linkid status at time time
create table tj.controls_2
(
linkid varchar(32) primary key references tj.link(id)
, status text not null -- open / closed, a pump speed setting, or a control valve setting
, time interval hour
);
-- link linkid status at clocktime clocktime am / pm
create table tj.controls_3
(
linkid varchar(32) primary key references tj.link(id)
, status text not null -- open / closed, a pump speed setting, or a control valve setting
, clocktimehour interval hour -- get am/pm from it
, clocktimemin interval minute
);

View File

@@ -1,6 +0,0 @@
-- [rules]
create table tj.rules
(
content text primary key
);

View File

@@ -1,29 +0,0 @@
-- [energy]
create type tj.energy_param as enum
(
'price'
, 'pattern'
, 'effic'
);
-- global price / pattern / effic value
create table tj.energy_global
(
param tj.energy_param not null
, value numeric not null
);
-- pump pumpid price / pattern / effic value
create table tj.energy_pump
(
id varchar(32) references tj.pumps(id) not null
, param tj.energy_param not null
, value numeric not null
);
-- demand charge value
create table tj.energy_demand_charge
(
value numeric not null
);

View File

@@ -1,7 +0,0 @@
-- [emitters]
create table tj.emitters
(
junction varchar(32) primary key references tj.junctions(id)
, coefficient numeric not null
);

View File

@@ -1,7 +0,0 @@
-- [quality]
create table tj.quality
(
node varchar(32) primary key references tj.node(id)
, initialqual numeric not null
);

View File

@@ -1,17 +0,0 @@
-- [sources]
create type tj.sources_type as enum
(
'concen'
, 'mass'
, 'flowpaced'
, 'setpoint'
);
create table tj.sources
(
node varchar(32) primary key references tj.node(id)
, type tj.sources_type not null
, strength numeric not null
, timepattern varchar(32) references tj.pattern(id)
);

View File

@@ -1,55 +0,0 @@
-- [reactions]
create type tj.reactions_order_param as enum
(
'bulk'
, 'wall'
, 'tank'
);
create table tj.reactions_order
(
key tj.reactions_order_param not null
, value numeric not null
);
create type tj.reactions_global_param as enum
(
'bulk'
, 'wall'
);
create table tj.reactions_global
(
key tj.reactions_global_param not null
, value numeric not null
);
create type tj.reactions_pipe_param as enum
(
'bulk'
, 'wall'
);
create table tj.reactions_pipe
(
key tj.reactions_pipe_param not null
, pipe varchar(32) references tj.pipes(id) not null
, value numeric not null
);
create table tj.reactions_tank
(
tank varchar(32) references tj.tanks(id) not null
, value numeric not null
);
create table tj.reactions_limiting_potential
(
value numeric not null
);
create table tj.reactions_roughness_correlation
(
value numeric not null
);

View File

@@ -1,9 +0,0 @@
-- [junctions]
create table tj.junctions
(
id varchar(32) primary key references tj.node(id)
, elevation numeric not null
, demand numeric
, pattern varchar(32) references tj.pattern(id)
);

View File

@@ -1,16 +0,0 @@
-- [mixing]
create type tj.mixing_model as enum
(
'mixed'
, '2comp'
, 'fifo'
, 'lifo'
);
create table tj.mixing
(
tank varchar(32) primary key references tj.tanks(id)
, model tj.mixing_model not null
, value numeric
);

View File

@@ -1,9 +0,0 @@
-- [times]
-- todo: constraint
create table tj.times
(
key text not null
, value text not null
);

View File

@@ -1,9 +0,0 @@
-- [report]
-- todo: constraint
create table tj.report
(
key text not null
, value text not null
);

View File

@@ -1,9 +0,0 @@
-- [options]
-- todo: constraint
create table tj.options
(
key text not null
, value text not null
);

View File

@@ -1,10 +0,0 @@
-- [coordinates]
create table tj.coordinates
(
node varchar(32) primary key references tj.node(id)
, coord point not null
);
create index tj_coordinates_spgist on tj.coordinates using spgist(coord);
create index tj_coordinates_gist on tj.coordinates using gist(coord);

View File

@@ -1,8 +0,0 @@
-- [vertices]
create table tj.vertices
(
link varchar(32) references tj.link(id) not null
, x numeric not null
, y numeric not null
);

View File

@@ -1,9 +0,0 @@
-- [labels]
create table tj.labels
(
x numeric not null
, y numeric not null
, label text not null
, anchornode varchar(32) references tj.node(id)
);

View File

@@ -1,6 +0,0 @@
-- [backdrop]
create table tj.backdrop
(
content text primary key
);

View File

@@ -1 +0,0 @@
-- [END]

View File

@@ -1,8 +0,0 @@
-- [reservoirs]
create table tj.reservoirs
(
id varchar(32) primary key references tj.node(id)
, head numeric not null
, pattern varchar(32) references tj.pattern(id)
);

View File

@@ -1,20 +0,0 @@
-- [tanks]
create type tj.tanks_overflow as enum
(
'yes'
, 'no'
);
create table tj.tanks
(
id varchar(32) primary key references tj.node(id)
, elevation numeric not null
, initlevel numeric not null
, minlevel numeric not null
, maxlevel numeric not null
, diameter numeric not null
, minvol numeric not null
, volcurve varchar(32) references tj.curve(id)
, overflow tj.tanks_overflow
);

View File

@@ -1,20 +0,0 @@
-- [pipes]
create type tj.pipes_status as enum
(
'open'
, 'closed'
, 'cv'
);
create table tj.pipes
(
id varchar(32) primary key references tj.link(id)
, node1 varchar(32) references tj.node(id) not null
, node2 varchar(32) references tj.node(id) not null
, length numeric not null
, diameter numeric not null
, roughness numeric not null
, minorloss numeric not null
, status tj.pipes_status not null
);

View File

@@ -1,32 +0,0 @@
-- [pumps]
create table tj.pumps
(
id varchar(32) primary key references tj.link(id)
, node1 varchar(32) references tj.node(id) not null
, node2 varchar(32) references tj.node(id) not null
);
create table tj.pumps_property_power
(
id varchar primary key references tj.pumps(id)
, value varchar(32) not null
);
create table tj.pumps_property_speed
(
id varchar primary key references tj.pumps(id)
, value varchar(32) not null
);
create table tj.pumps_property_head
(
id varchar primary key references tj.pumps(id)
, head varchar(32) references tj.curve(id) not null
);
create table tj.pumps_property_pattern
(
id varchar primary key references tj.pumps(id)
, pattern varchar(32) references tj.pattern(id) not null
);

View File

@@ -1,22 +0,0 @@
-- [valves]
create type tj.valves_type as enum
(
'prv'
, 'psv'
, 'pbv'
, 'fcv'
, 'tcv'
, 'gpv'
);
create table tj.valves
(
id varchar(32) primary key references tj.link(id)
, node1 varchar(32) references tj.node(id) not null
, node2 varchar(32) references tj.node(id) not null
, diameter numeric not null
, type tj.valves_type not null
, setting numeric not null
, minorloss numeric not null
);

View File

@@ -1,13 +0,0 @@
-- [tags]
create table tj.tags_node
(
id varchar(32) primary key references tj.node(id)
, tag text not null
);
create table tj.tags_link
(
id varchar(32) primary key references tj.link(id)
, tag text not null
);

View File

@@ -1,9 +0,0 @@
-- [demands]
create table tj.demands
(
junction varchar(32) references tj.junctions(id) not null
, demand numeric not null
, pattern varchar(32) references tj.pattern(id)
, category text not null
);

View File

@@ -1 +0,0 @@
create schema tj;

View File

@@ -1,29 +0,0 @@
create table tj.operation
(
id serial primary key
, redo text not null
, undo text not null
, parent integer references tj.operation(id)
, redo_child integer references tj.operation(id)
);
insert into tj.operation (id, redo, undo) values (0, '', '');
create table tj.current_operation
(
id integer primary key references tj.operation(id)
);
insert into tj.current_operation (id) values (0);
create table tj.snapshot_operation
(
id integer primary key references tj.operation(id)
, tag text not null unique
);
create table tj.transaction_operation
(
id integer primary key references tj.operation(id)
, strict boolean not null default false
);

View File

@@ -1,11 +0,0 @@
drop table if exists tj.pattern;
drop table if exists tj.curve;
drop table if exists tj.link;
drop table if exists tj.node;
drop type if exists tj.link_type;
drop type if exists tj.node_type;

View File

@@ -1,3 +0,0 @@
-- [title]
drop table if exists tj.title;

View File

@@ -1,13 +0,0 @@
-- [status]
drop table if exists tj.status_link;
drop table if exists tj.status_valve;
drop type if exists tj.status_valve_status;
drop table if exists tj.status_pump;
drop table if exists tj.status_pipe;
drop type if exists tj.status_pipe_pump_status;

View File

@@ -1,3 +0,0 @@
-- [patterns]
drop table if exists tj.patterns;

View File

@@ -1,3 +0,0 @@
-- [curves]
drop table if exists tj.curves;

View File

@@ -1,9 +0,0 @@
-- [controls]
drop table if exists tj.controls_3;
drop table if exists tj.controls_2;
drop table if exists tj.controls_1;
drop type if exists tj.controls_1_prep;

View File

@@ -1,3 +0,0 @@
-- [rules]
drop table if exists tj.rules;

View File

@@ -1,9 +0,0 @@
-- [energy]
drop table if exists tj.energy_demand_charge;
drop table if exists tj.energy_pump;
drop table if exists tj.energy_global;
drop type if exists tj.energy_param;

View File

@@ -1,3 +0,0 @@
-- [emitters]
drop table if exists tj.emitters;

View File

@@ -1,3 +0,0 @@
-- [quality]
drop table if exists tj.quality;

View File

@@ -1,5 +0,0 @@
-- [sources]
drop table if exists tj.sources;
drop type if exists tj.sources_type;

View File

@@ -1,19 +0,0 @@
-- [reactions]
drop table if exists tj.reactions_roughness_correlation;
drop table if exists tj.reactions_limiting_potential;
drop table if exists tj.reactions_tank;
drop table if exists tj.reactions_pipe;
drop type if exists tj.reactions_pipe_param;
drop table if exists tj.reactions_global;
drop type if exists tj.reactions_global_param;
drop table if exists tj.reactions_order;
drop type if exists tj.reactions_order_param;

View File

@@ -1,3 +0,0 @@
-- [junctions]
drop table if exists tj.junctions;

View File

@@ -1,5 +0,0 @@
-- [mixing]
drop table if exists tj.mixing;
drop type if exists tj.mixing_model;

View File

@@ -1,3 +0,0 @@
-- [times]
drop table if exists tj.times;

View File

@@ -1,3 +0,0 @@
-- [report]
drop table if exists tj.report;

View File

@@ -1,3 +0,0 @@
-- [options]
drop table if exists tj.options;

View File

@@ -1,7 +0,0 @@
-- [coordinates]
drop index if exists tj_coordinates_gist;
drop index if exists tj_coordinates_spgist;
drop table if exists tj.coordinates;

View File

@@ -1,3 +0,0 @@
-- [vertices]
drop table if exists tj.vertices;

View File

@@ -1,3 +0,0 @@
-- [labels]
drop table if exists tj.labels;

View File

@@ -1,3 +0,0 @@
-- [backdrop]
drop table if exists tj.backdrop;

View File

@@ -1 +0,0 @@
-- [END]

View File

@@ -1,3 +0,0 @@
-- [reservoirs]
drop table if exists tj.reservoirs;

View File

@@ -1,5 +0,0 @@
-- [tanks]
drop table if exists tj.tanks;
drop type if exists tj.tanks_overflow;

View File

@@ -1,5 +0,0 @@
-- [pipes]
drop table if exists tj.pipes;
drop type if exists tj.pipes_status;

View File

@@ -1,11 +0,0 @@
-- [pumps]
drop table if exists tj.pumps_property_pattern;
drop table if exists tj.pumps_property_head;
drop table if exists tj.pumps_property_speed;
drop table if exists tj.pumps_property_power;
drop table if exists tj.pumps;

View File

@@ -1,5 +0,0 @@
-- [valves]
drop table if exists tj.valves;
drop type if exists tj.valves_type;

View File

@@ -1,5 +0,0 @@
-- [tags]
drop table if exists tj.tags_link;
drop table if exists tj.tags_node;

View File

@@ -1,3 +0,0 @@
-- [demands]
drop table if exists tj.demands;

View File

@@ -1 +0,0 @@
drop schema if exists tj;

View File

@@ -1,7 +0,0 @@
drop table if exists tj.transaction_operation;
drop table if exists tj.snapshot_operation;
drop table if exists tj.current_operation;
drop table if exists tj.operation;

View File

@@ -1,108 +0,0 @@
import psycopg as pg
sql_create = [
"table/create/namespace.sql",
"table/create/0.base.sql",
"table/create/1.title.sql",
"table/create/2.junctions.sql",
"table/create/3.reservoirs.sql",
"table/create/4.tanks.sql",
"table/create/5.pipes.sql",
"table/create/6.pumps.sql",
"table/create/7.valves.sql",
"table/create/8.tags.sql",
"table/create/9.demands.sql",
"table/create/10.status.sql",
"table/create/11.patterns.sql",
"table/create/12.curves.sql",
"table/create/13.controls.sql",
"table/create/14.rules.sql",
"table/create/15.energy.sql",
"table/create/16.emitters.sql",
"table/create/17.quality.sql",
"table/create/18.sources.sql",
"table/create/19.reactions.sql",
"table/create/20.mixing.sql",
"table/create/21.times.sql",
"table/create/22.report.sql",
"table/create/23.options.sql",
"table/create/24.coordinates.sql",
"table/create/25.vertices.sql",
"table/create/26.labels.sql",
"table/create/27.backdrop.sql",
"table/create/28.end.sql",
"table/create/operation.sql",
"api/create/operation.sql",
"api/create/1.title.sql"
]
sql_drop = [
"api/drop/1.title.sql",
"api/drop/operation.sql",
"table/drop/operation.sql",
"table/drop/28.end.sql",
"table/drop/27.backdrop.sql",
"table/drop/26.labels.sql",
"table/drop/25.vertices.sql",
"table/drop/24.coordinates.sql",
"table/drop/23.options.sql",
"table/drop/22.report.sql",
"table/drop/21.times.sql",
"table/drop/20.mixing.sql",
"table/drop/19.reactions.sql",
"table/drop/18.sources.sql",
"table/drop/17.quality.sql",
"table/drop/16.emitters.sql",
"table/drop/15.energy.sql",
"table/drop/14.rules.sql",
"table/drop/13.controls.sql",
"table/drop/12.curves.sql",
"table/drop/11.patterns.sql",
"table/drop/10.status.sql",
"table/drop/9.demands.sql",
"table/drop/8.tags.sql",
"table/drop/7.valves.sql",
"table/drop/6.pumps.sql",
"table/drop/5.pipes.sql",
"table/drop/4.tanks.sql",
"table/drop/3.reservoirs.sql",
"table/drop/2.junctions.sql",
"table/drop/1.title.sql",
"table/drop/0.base.sql",
"table/drop/namespace.sql"
]
def create_template():
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute("create database tj_project")
with pg.connect(conninfo="dbname=tj_project") as conn:
with conn.cursor() as cur:
for sql in sql_create:
with open(sql, "r") as f:
cur.execute(f.read())
print(f'executed {sql}')
conn.commit()
def have_template():
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute("select * from pg_database where datname = 'tj_project'")
return cur.rowcount > 0
def delete_template():
with pg.connect(conninfo="dbname=tj_project") as conn:
with conn.cursor() as cur:
for sql in sql_drop:
with open(sql, "r") as f:
cur.execute(f.read())
print(f'executed {sql}')
conn.commit()
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute("drop database tj_project")
if __name__ == "__main__":
if (have_template()):
delete_template()
create_template()