Refine pattern
This commit is contained in:
@@ -16,52 +16,78 @@ def get_pattern(name: str, id: str) -> dict[str, Any]:
|
|||||||
|
|
||||||
def set_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
def set_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||||
id = cs.operations[0]['id']
|
id = cs.operations[0]['id']
|
||||||
|
f_id = f"'{id}'"
|
||||||
|
|
||||||
old = get_pattern(name, id)
|
old = get_pattern(name, id)
|
||||||
new = { 'id': id, 'factors': [] }
|
|
||||||
|
|
||||||
f_id = f"'{id}'"
|
new = { 'id': id }
|
||||||
|
if 'factors' in cs.operations[0]:
|
||||||
|
new['factors'] = cs.operations[0]['factors']
|
||||||
|
else:
|
||||||
|
new['factors'] = old['factors']
|
||||||
|
|
||||||
# TODO: transaction ?
|
# TODO: transaction ?
|
||||||
redo_sql = f"delete from patterns where id = {f_id};"
|
redo_sql = f"delete from patterns where id = {f_id};"
|
||||||
redo_sql += f"\ndelete from _pattern where id = {f_id};"
|
for f_factor in new['factors']:
|
||||||
if len(cs.operations[0]['factors']) > 0:
|
|
||||||
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});"
|
redo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||||
new['factors'].append(factor)
|
|
||||||
|
|
||||||
undo_sql = f"delete from patterns where id = {f_id};"
|
undo_sql = f"delete from patterns where id = {f_id};"
|
||||||
undo_sql += f"\ndelete from _pattern where id = {f_id};"
|
|
||||||
if len(old['factors']) > 0:
|
|
||||||
undo_sql += f"\ninsert into _pattern (id) values ({f_id});"
|
|
||||||
for f_factor in old['factors']:
|
for f_factor in old['factors']:
|
||||||
undo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
undo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||||
|
|
||||||
redo_cs = { 'type': 'pattern' } | new
|
redo_cs = g_update_prefix | { 'type': 'pattern' } | new
|
||||||
undo_cs = { 'type': 'pattern' } | old
|
undo_cs = g_update_prefix | { 'type': 'pattern' } | old
|
||||||
|
|
||||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||||
|
|
||||||
|
|
||||||
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
result = set_pattern_cache(name, cs)
|
return execute_command(name, set_pattern_cache(name, cs))
|
||||||
result.redo_cs |= g_update_prefix
|
|
||||||
result.undo_cs |= g_update_prefix
|
|
||||||
return execute_command(name, result)
|
def add_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||||
|
id = cs.operations[0]['id']
|
||||||
|
f_id = f"'{id}'"
|
||||||
|
|
||||||
|
new = { 'id': id, 'factors': cs.operations[0]['factors'] }
|
||||||
|
|
||||||
|
# TODO: transaction ?
|
||||||
|
redo_sql = f"insert into _pattern (id) values ({f_id});"
|
||||||
|
for f_factor in new['factors']:
|
||||||
|
redo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||||
|
|
||||||
|
undo_sql = f"delete from patterns where id = {f_id};"
|
||||||
|
undo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||||
|
|
||||||
|
redo_cs = g_add_prefix | { 'type': 'pattern' } | new
|
||||||
|
undo_cs = g_delete_prefix | { 'type': 'pattern' } | { 'id': id }
|
||||||
|
|
||||||
|
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||||
|
|
||||||
|
|
||||||
def add_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
def add_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
result = set_pattern_cache(name, cs)
|
return execute_command(name, add_pattern_cache(name, cs))
|
||||||
result.redo_cs |= g_add_prefix
|
|
||||||
result.undo_cs |= g_delete_prefix
|
|
||||||
return execute_command(name, result)
|
def delete_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||||
|
id = cs.operations[0]['id']
|
||||||
|
f_id = f"'{id}'"
|
||||||
|
|
||||||
|
old = get_pattern(name, id)
|
||||||
|
|
||||||
|
redo_sql = f"delete from patterns where id = {f_id};"
|
||||||
|
redo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||||
|
|
||||||
|
# TODO: transaction ?
|
||||||
|
undo_sql = f"insert 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_delete_prefix | { 'type': 'pattern' } | { 'id': id }
|
||||||
|
undo_cs = g_add_prefix | { 'type': 'pattern' } | old
|
||||||
|
|
||||||
|
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||||
|
|
||||||
|
|
||||||
def delete_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
def delete_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
cs.operations[0]['factors'] = []
|
return execute_command(name, delete_pattern_cache(name, cs))
|
||||||
result = set_pattern_cache(name, cs)
|
|
||||||
result.redo_cs |= g_delete_prefix
|
|
||||||
result.undo_cs |= g_add_prefix
|
|
||||||
return execute_command(name, result)
|
|
||||||
|
|||||||
@@ -1594,24 +1594,31 @@ class TestApi:
|
|||||||
self.enter(p)
|
self.enter(p)
|
||||||
|
|
||||||
assert is_pattern(p, 'p0') == False
|
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]}))
|
add_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]}))
|
||||||
|
|
||||||
assert is_pattern(p, 'p0')
|
assert is_pattern(p, 'p0')
|
||||||
p0 = get_pattern(p, 'p0')
|
p0 = get_pattern(p, 'p0')
|
||||||
assert p0['id'] == 'p0'
|
assert p0['id'] == 'p0'
|
||||||
assert p0['factors'] == [1.0, 2.0, 3.0]
|
assert p0['factors'] == [1.0, 2.0, 3.0]
|
||||||
|
|
||||||
|
set_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0]}))
|
||||||
|
|
||||||
|
assert is_pattern(p, 'p0')
|
||||||
|
p0 = get_pattern(p, 'p0')
|
||||||
|
assert p0['id'] == 'p0'
|
||||||
|
assert p0['factors'] == [1.0, 2.0]
|
||||||
|
|
||||||
set_pattern(p, ChangeSet({'id' : 'p0', 'factors': []}))
|
set_pattern(p, ChangeSet({'id' : 'p0', 'factors': []}))
|
||||||
|
|
||||||
assert is_pattern(p, 'p0') == False
|
assert is_pattern(p, 'p0')
|
||||||
p0 = get_pattern(p, 'p0')
|
p0 = get_pattern(p, 'p0')
|
||||||
assert p0['id'] == 'p0'
|
assert p0['id'] == 'p0'
|
||||||
assert p0['factors'] == []
|
assert p0['factors'] == []
|
||||||
|
|
||||||
|
delete_pattern(p, ChangeSet({'id' : 'p0'}))
|
||||||
|
assert is_pattern(p, 'p0') == False
|
||||||
|
|
||||||
self.leave(p)
|
self.leave(p)
|
||||||
|
|
||||||
|
|
||||||
@@ -1619,23 +1626,74 @@ class TestApi:
|
|||||||
p = 'test_pattern_op'
|
p = 'test_pattern_op'
|
||||||
self.enter(p)
|
self.enter(p)
|
||||||
|
|
||||||
cs = set_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})).operations[0]
|
cs = add_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})).operations[0]
|
||||||
assert cs['operation'] == API_UPDATE
|
assert cs['operation'] == API_ADD
|
||||||
assert cs['type'] == PATTERN
|
assert cs['type'] == PATTERN
|
||||||
assert cs['id'] == 'p0'
|
assert cs['id'] == 'p0'
|
||||||
assert cs['factors'] == [1.0, 2.0, 3.0]
|
assert cs['factors'] == [1.0, 2.0, 3.0]
|
||||||
|
|
||||||
cs = execute_undo(p).operations[0]
|
cs = execute_undo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_DELETE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
|
||||||
|
cs = execute_redo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_ADD
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == [1.0, 2.0, 3.0]
|
||||||
|
|
||||||
|
cs = set_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0]})).operations[0]
|
||||||
assert cs['operation'] == API_UPDATE
|
assert cs['operation'] == API_UPDATE
|
||||||
assert cs['type'] == PATTERN
|
assert cs['type'] == PATTERN
|
||||||
assert cs['id'] == 'p0'
|
assert cs['id'] == 'p0'
|
||||||
assert cs['factors'] == []
|
assert cs['factors'] == [1.0, 2.0]
|
||||||
|
|
||||||
|
cs = execute_undo(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]
|
||||||
|
|
||||||
cs = execute_redo(p).operations[0]
|
cs = execute_redo(p).operations[0]
|
||||||
assert cs['operation'] == API_UPDATE
|
assert cs['operation'] == API_UPDATE
|
||||||
assert cs['type'] == PATTERN
|
assert cs['type'] == PATTERN
|
||||||
assert cs['id'] == 'p0'
|
assert cs['id'] == 'p0'
|
||||||
assert cs['factors'] == [1.0, 2.0, 3.0]
|
assert cs['factors'] == [1.0, 2.0]
|
||||||
|
|
||||||
|
cs = set_pattern(p, ChangeSet({'id' : 'p0', 'factors': []})).operations[0]
|
||||||
|
assert cs['operation'] == API_UPDATE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == []
|
||||||
|
|
||||||
|
cs = execute_undo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_UPDATE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == [1.0, 2.0]
|
||||||
|
|
||||||
|
cs = execute_redo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_UPDATE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == []
|
||||||
|
|
||||||
|
cs = delete_pattern(p, ChangeSet({'id' : 'p0'})).operations[0]
|
||||||
|
assert cs['operation'] == API_DELETE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
|
||||||
|
cs = execute_undo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_ADD
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
assert cs['factors'] == []
|
||||||
|
|
||||||
|
cs = execute_redo(p).operations[0]
|
||||||
|
assert cs['operation'] == API_DELETE
|
||||||
|
assert cs['type'] == PATTERN
|
||||||
|
assert cs['id'] == 'p0'
|
||||||
|
|
||||||
self.leave(p)
|
self.leave(p)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user