Commit c03674b0 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[lib] Tests: CLU: black

parent cdac13c0
......@@ -97,6 +97,7 @@ from cadbiom.models.clause_constraints.mcl.MCLSolutions import MCLException
class ErrorReporter(object):
"""error reporter of the compil type"""
def __init__(self):
self.context = ""
self.error = False
......@@ -107,13 +108,13 @@ class ErrorReporter(object):
just printing
"""
self.error = True
print('\n>> '+self.context+" "+mess)
print("\n>> " + self.context + " " + mess)
def display_info(self, mess):
"""
just printing
"""
print('\n-- '+mess)
print("\n-- " + mess)
def set_context(self, cont):
"""
......@@ -121,6 +122,7 @@ class ErrorReporter(object):
"""
self.context = cont
def model1():
"""
A simple ChartModel with two nodes and a transition
......@@ -128,11 +130,12 @@ def model1():
# build dynamical system
model = ChartModel("Test")
root = model.get_root()
node_1 = root.add_simple_node('n1', 0, 0)
node_2 = root.add_simple_node('n2', 0, 0)
node_1 = root.add_simple_node("n1", 0, 0)
node_2 = root.add_simple_node("n2", 0, 0)
root.add_transition(node_1, node_2)
return model
def model2():
"""
ChartModel model1 + 3 nodes and 2 transitions and a cycle without start
......@@ -140,14 +143,15 @@ def model2():
# build dynamical system
model = model1()
root = model.get_root()
node_3 = root.add_simple_node('n3', 0, 0)
node_4 = root.add_simple_node('n4', 0, 0)
node_3 = root.add_simple_node("n3", 0, 0)
node_4 = root.add_simple_node("n4", 0, 0)
root.add_transition(node_3, node_4)
node_5 = root.add_simple_node('n5', 0, 0)
node_5 = root.add_simple_node("n5", 0, 0)
root.add_transition(node_4, node_5)
root.add_transition(node_5, node_3)
return model
def model3():
"""
ChartModel model2 + 1 node + 1 start node and a transition
......@@ -155,11 +159,12 @@ def model3():
# build dynamical system
model = model2()
root = model.get_root()
node_s = root.add_start_node('n3', 0, 0)
node_3 = model.get_simple_node('n3')
node_s = root.add_start_node("n3", 0, 0)
node_3 = model.get_simple_node("n3")
root.add_transition(node_s, node_3)
return model
def model4():
"""
ChartModel with:
......@@ -170,50 +175,53 @@ def model4():
"""
model = ChartModel("Test")
root = model.get_root()
node_1 = root.add_simple_node('n1', 0, 0)
node_2 = root.add_simple_node('n2', 0, 0)
node_1 = root.add_simple_node("n1", 0, 0)
node_2 = root.add_simple_node("n2", 0, 0)
tr0 = root.add_transition(node_1, node_2)
tr0.set_event('hh1')
tr0.set_event("hh1")
tr0.set_condition("n3 or n4")
node_3 = root.add_simple_node('n3', 0, 0)
node_4 = root.add_simple_node('n4', 0, 0)
node_3 = root.add_simple_node("n3", 0, 0)
node_4 = root.add_simple_node("n4", 0, 0)
root.add_transition(node_3, node_4)
node_5 = root.add_simple_node('n5', 0, 0)
node_5 = root.add_simple_node("n5", 0, 0)
tr1 = root.add_transition(node_4, node_5)
root.add_transition(node_5, node_3)
tr1.set_event("hh2")
tr1.set_condition("n1 and n3")
node_i = root.add_input_node('in1', 0, 0)
node_i = root.add_input_node("in1", 0, 0)
tri = root.add_transition(node_i, node_1)
tri.set_condition("n5")
# Frontier test: Add a start node on n3 or do not attempt any frontier
# place because there is a SCC composed of n3, n4, n5.
#node_s = root.add_start_node('s1', 0, 0)
#root.add_transition(node_s, node_3)
# node_s = root.add_start_node('s1', 0, 0)
# root.add_transition(node_s, node_3)
return model
def create_unfolder(model):
"""Return an unfolder for the given model"""
tvisit = TableVisitor(None) # no error display
tvisit = TableVisitor(None) # no error display
model.accept(tvisit)
cld = CLDynSys( tvisit.tab_symb, None)
cld = CLDynSys(tvisit.tab_symb, None)
reporter = ErrorReporter()
cvisit = GT2Clauses(cld, reporter, True)
model.accept(cvisit)
# unfolder
return CLUnfolder(cld)
@pytest.fixture()
def textual_properties():
"""start, invariant, final properties in textual form"""
return (
("M", "L", "C"), # No solution (because inhibitor M is activated)
("", "L", "C and K"), # Solution: D E F I L
("", "", "C"), # Solutions: F E L D, D E F I
("M", "L", "C"), # No solution (because inhibitor M is activated)
("", "L", "C and K"), # Solution: D E F I L
("", "", "C"), # Solutions: F E L D, D E F I
)
@pytest.fixture()
def numeric_properties():
"""start, invariant, final properties in DIMACS form"""
......@@ -224,6 +232,7 @@ def numeric_properties():
([], [[12]], [[3, -47], [11, -47], [-3, -11, 47], [47]]),
)
@pytest.fixture()
def feed_mclanalyser():
"""Setup MCLAnalyser with a bcx model
......@@ -249,6 +258,7 @@ class TestCLUnfolder(unittest.TestCase):
"""
Test public and some private methods (test_method form)
"""
def test_var_name(self):
"""
Test of variables at initial state uncoding and decoding
......@@ -262,27 +272,26 @@ class TestCLUnfolder(unittest.TestCase):
assert unfolder.get_var_number() == 2
# naming and coding variables
cn1 = unfolder.var_dimacs_code('n1')
cn2 = unfolder.var_dimacs_code('n2')
cn1 = unfolder.var_dimacs_code("n1")
cn2 = unfolder.var_dimacs_code("n2")
res = unfolder.get_var_name(cn1) == 'n1'
res = res and (unfolder.get_var_name(cn2) == 'n2')
self.assert_(res,'Error in variable name 1')
res = unfolder.get_var_name(cn1) == "n1"
res = res and (unfolder.get_var_name(cn2) == "n2")
self.assert_(res, "Error in variable name 1")
res = unfolder.get_var_name(7) == 'n1'
self.assert_(res,'Error in variable name 2')
res = unfolder.get_var_name(8) == 'n2'
self.assert_(res,'Error in variable name 3')
res = unfolder.get_var_name(7) == "n1"
self.assert_(res, "Error in variable name 2")
res = unfolder.get_var_name(8) == "n2"
self.assert_(res, "Error in variable name 3")
res = unfolder.get_var_indexed_name(7) == 'n1_3'
self.assert_(res,'Error in variable name 4')
res = unfolder.get_var_indexed_name(8) == 'n2_3'
self.assert_(res,'Error in variable name 5')
res = unfolder.get_var_indexed_name(7) == "n1_3"
self.assert_(res, "Error in variable name 4")
res = unfolder.get_var_indexed_name(8) == "n2_3"
self.assert_(res, "Error in variable name 5")
# Test number of variables
assert unfolder.get_system_var_number() == unfolder.get_var_number()
def test_frontier(self):
"""
Test frontier computation and encoding
......@@ -292,9 +301,9 @@ class TestCLUnfolder(unittest.TestCase):
unfolder = create_unfolder(model)
# test frontier: should be n1
frontier_value = unfolder.frontier_values[0]
res = unfolder.get_var_name(frontier_value) == 'n1'
res = unfolder.get_var_name(frontier_value) == "n1"
res = res and (len(unfolder.frontier_values) == 1)
self.assert_(res,'Error in frontier: model1')
self.assert_(res, "Error in frontier: model1")
## model2 (one cycle without start)
model = model2()
......@@ -302,9 +311,9 @@ class TestCLUnfolder(unittest.TestCase):
# test frontier: should be n1
frontier_value = unfolder.frontier_values[0]
res = unfolder.get_var_name(frontier_value) == 'n1'
res = unfolder.get_var_name(frontier_value) == "n1"
res = res and (len(unfolder.frontier_values) == 1)
self.assert_(res,'Error in frontier: model2')
self.assert_(res, "Error in frontier: model2")
## model3: same as model2 but with a start on n3
model = model3()
......@@ -313,16 +322,15 @@ class TestCLUnfolder(unittest.TestCase):
# test frontier - should be {n1, n3}
frontier_values = unfolder.frontier_values
res = len(frontier_values) == 2
res = res and unfolder.get_var_name(frontier_values[0]) == 'n1'
res = res and unfolder.get_var_name(frontier_values[1]) == 'n3'
self.assert_(res,'Error in frontier: model3')
res = res and unfolder.get_var_name(frontier_values[0]) == "n1"
res = res and unfolder.get_var_name(frontier_values[1]) == "n3"
self.assert_(res, "Error in frontier: model3")
## model4 - No frontiers (even if we can get input node in1 in the solutions)
model = model4()
unfolder = create_unfolder(model)
res = len(unfolder.frontier_values) == 0
self.assert_(res,'Error in frontier: model4')
self.assert_(res, "Error in frontier: model4")
def test_free_clocks_inputs(self):
"""
......@@ -332,35 +340,33 @@ class TestCLUnfolder(unittest.TestCase):
model = model3()
unfolder = create_unfolder(model)
lfc = unfolder.free_clocks
self.assertEqual(len(lfc), 0, 'Error in free clocks: model3')
self.assertEqual(len(lfc), 0, "Error in free clocks: model3")
lin = unfolder.inputs
self.assertEqual(len(lin), 0,'Error in inputs 1')
self.assertEqual(len(lin), 0, "Error in inputs 1")
# model4: two free clocks and one input
model = model4()
unfolder = create_unfolder(model)
lfc = unfolder.free_clocks
self.assertEqual(len(lfc), 2, 'Error in free clocks: model4')
self.assertEqual(len(lfc), 2, "Error in free clocks: model4")
found_names = {unfolder.get_var_name(clock) for clock in lfc}
self.assertEqual(
found_names,
{'hh2', 'hh1'},
'Error in free clocks names: model4'
found_names, {"hh2", "hh1"}, "Error in free clocks names: model4"
)
lin = unfolder.inputs
self.assertEqual(len(lin), 1, 'Error in inputs: model4')
self.assertEqual(len(lin), 1, "Error in inputs: model4")
found_names = {unfolder.get_var_name(inpt) for inpt in lin}
self.assertEqual(found_names, {'in1'}, 'Error in inputs names: model4')
self.assertEqual(found_names, {"in1"}, "Error in inputs names: model4")
def init_forward_unfolding_part_1(unfolder):
"""Initialization step only"""
unfolder._CLUnfolder__shift_direction = 'FORWARD'
unfolder._CLUnfolder__shift_direction = "FORWARD"
unfolder._CLUnfolder__current_step = 1
unfolder._CLUnfolder__shift_step = unfolder.shift_step_init # back to basic!
unfolder._CLUnfolder__aux_code_table = dict() # flush auxiliary variables
unfolder._CLUnfolder__aux_list = [] # idem
unfolder._CLUnfolder__aux_code_table = dict() # flush auxiliary variables
unfolder._CLUnfolder__aux_list = [] # idem
# Init properties to generate all variable num codes
unfolder._CLUnfolder__init_initial_constraint_0()
......@@ -380,10 +386,11 @@ def init_forward_unfolding_part_2(unfolder):
unfolder._CLUnfolder__shift_invariant()
# PS: __variant_constraints si already initialized for the first step.
def test_init_unfolder(feed_mclanalyser):
mcla = feed_mclanalyser
unfolder = mcla.unfolder # shortcut
unfolder = mcla.unfolder # shortcut
# Initialization step only
init_forward_unfolding_part_1(mcla.unfolder)
......@@ -458,7 +465,16 @@ def test_init_unfolder(feed_mclanalyser):
value: unfolder.get_var_name(value) for value in unfolder.free_clocks
}
print("Free clocks/events values mapping", clocks_values_mapping)
assert clocks_values_mapping == {16: '_h2', 17: '_h3', 18: '_h4', 19: '_h5', 20: '_h6', 21: '_h7', 22: '_h_0', 23: '_h_1'}
assert clocks_values_mapping == {
16: "_h2",
17: "_h3",
18: "_h4",
19: "_h5",
20: "_h6",
21: "_h7",
22: "_h_0",
23: "_h_1",
}
# Check input places
assert unfolder._CLUnfolder__inputs == frozenset()
......@@ -520,7 +536,9 @@ def test_init_forward_unfolding_solution_1_dimacs(feed_mclanalyser, numeric_prop
# Note: By reusing the unfolder here we bypass its locked status.
# This will not be authorized with textual properties.
# Exception is expected
with pytest.raises(AssertionError, match=r".*At index 6 diff: \[-13\] != \[-15\].*"):
with pytest.raises(
AssertionError, match=r".*At index 6 diff: \[-13\] != \[-15\].*"
):
init_forward_unfolding_solution_1(mcla)
......@@ -603,7 +621,9 @@ def test_init_forward_unfolding_solution_3(feed_mclanalyser, textual_properties,
## Test without disabling initial_constraints check
mcla.unfolder.init_with_query(query)
# Exception is expected
with pytest.raises(AssertionError, match=r".*At index 6 diff: \[-13\] != \[-15\].*"):
with pytest.raises(
AssertionError, match=r".*At index 6 diff: \[-13\] != \[-15\].*"
):
init_forward_unfolding_solution_3(mcla)
......@@ -780,8 +800,8 @@ def init_forward_unfolding_solution_2(mcla):
print("final_constraints:", unfolder.final_constraints)
# Auxiliary variables: "C and K" property is added
assert unfolder._CLUnfolder__aux_code_table == {'_lit47': 47}
assert unfolder._CLUnfolder__aux_list == ['_lit47']
assert unfolder._CLUnfolder__aux_code_table == {"_lit47": 47}
assert unfolder._CLUnfolder__aux_list == ["_lit47"]
# MCLA could be reused, so __dynamic_constraints could not be empty here
# artificial reset
......@@ -811,7 +831,7 @@ def init_forward_unfolding_solution_2(mcla):
# not K, _lit47
# C, K, not _lit47
# _lit47
#[[-3, 47], [-11, 47], [3, 11, -47], [47]]
# [[-3, 47], [-11, 47], [3, 11, -47], [47]]
## Shift constraints #######################################################
......@@ -1099,11 +1119,10 @@ def test_shift(feed_mclanalyser, textual_properties):
unfolder.shift()
print("dynamic_constraints:", unfolder.dynamic_constraints)
print("initial_constraints:", unfolder.initial_constraints) # Not changed
print("initial_constraints:", unfolder.initial_constraints) # Not changed
print("invariant_constraints:", unfolder.invariant_constraints)
print("variant_constraints:", unfolder.variant_constraints) # No data for this test
print("variant_constraints:", unfolder.variant_constraints) # No data for this test
print("final_constraints:", unfolder.final_constraints)
assert unfolder.current_step == 2
......@@ -1167,5 +1186,3 @@ def test_shift(feed_mclanalyser, textual_properties):
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment