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

[lib] Tests: CLUnfolder; see more...

- Add solution4 test (simple final property)
- Add full tests for variant properties (DIMACS + text)
parent 2e666f08
......@@ -54,6 +54,7 @@ from cadbiom.models.clause_constraints.mcl.CLUnfolder import CLUnfolder
from cadbiom.models.clause_constraints.mcl.MCLAnalyser import MCLAnalyser
from cadbiom.models.clause_constraints.mcl.MCLQuery import MCLSimpleQuery
from cadbiom.models.clause_constraints.mcl.MCLSolutions import MCLException
class ErrorReporter(object):
......@@ -546,6 +547,18 @@ def test_init_forward_unfolding_solution_3(feed_mclanalyser, textual_properties,
init_forward_unfolding_solution_3(mcla)
def test_init_forward_unfolding_solution_4(feed_mclanalyser, textual_properties):
mcla = feed_mclanalyser
# Note: The second set creates auxiliary clauses that we could not insert
# in DIMACS form in the query so we use the text form for it.
query = MCLSimpleQuery(*textual_properties[2])
mcla.unfolder.init_with_query(query)
init_forward_unfolding_solution_4(mcla)
def init_forward_unfolding_solution_1(mcla):
"""
- Test first part of init_forward_unfolding: init of constraints
......@@ -770,6 +783,159 @@ def init_forward_unfolding_solution_3(mcla):
assert unfolder.final_constraints == [[3, -47], [11, -47], [-3, -11, 47], [47], [3]]
def init_forward_unfolding_solution_4(mcla):
"""
- Test first part of init_forward_unfolding: init of constraints
- Test second part of init_forward_unfolding: shift of initialized constraints
Query:
start, invariant, final: ("", "", "C")
Solutions: F E L D, I E D F
"""
unfolder = mcla.unfolder
## Init constraints ########################################################
# __no_frontier_init not already initialized
# (initial_constraints and invariant_constraints must be initialized beforehand)
assert unfolder._CLUnfolder__no_frontier_init == []
init_forward_unfolding_part_1(mcla.unfolder)
print("initial_constraints:", unfolder.initial_constraints)
print("invariant_constraints:", unfolder.invariant_constraints)
print("final_constraints:", unfolder.final_constraints)
assert unfolder.shift_step == 46
expected_initial_constraints = [[-1], [-2], [-3], [-8], [-10], [-11], [-13], [-15]]
assert unfolder._CLUnfolder__no_frontier_init == expected_initial_constraints
assert unfolder.initial_constraints == expected_initial_constraints
## Shift constraints #######################################################
init_forward_unfolding_part_2(unfolder)
print("initial_constraints:", unfolder.initial_constraints)
print("invariant_constraints:", unfolder.invariant_constraints)
print("final_constraints:", unfolder.final_constraints)
assert unfolder.final_constraints == [[49]]
def test_init_forward_unfolding_variant_constraints(feed_mclanalyser):
"""Test variant_prop and dim_variant attributes of a query
.. todo:: This test contains some stupid results from the generator of
clauses.
=> clauses redondantes en cas d'égalité des events dans 1 formule
(Ex: "h2 and h2")
=> clauses redondantes en cas d'égalité des events entre l'attribut
au format texte et celui au format DIMACS.
=> On peut mettre n'importe quel nom d'event, aucun test n'est fait
pour vérifier qu'il existe dans le modèle.
"""
mcla = feed_mclanalyser
unfolder = mcla.unfolder
# Test values of events
print(mcla.unfolder._CLUnfolder__var_code_table)
h2_value = mcla.unfolder.var_dimacs_code("_h2")
assert h2_value == 16
h3_value = mcla.unfolder.var_dimacs_code("_h3")
assert h3_value == 17
h0_value = mcla.unfolder.var_dimacs_code("_h_0")
assert h0_value == 22
query = MCLSimpleQuery(None, None, None)
query.variant_prop = ["_h2", "_h_0"]
unfolder.init_with_query(query)
unfolder.init_forward_unfolding()
# Check copy of query attribute
assert unfolder._CLUnfolder__variant_property == query.variant_prop
# Check conversion of query attribute
expected = [[[16]], [[22]]]
assert unfolder._CLUnfolder__precomputed_variant_constraints == expected
# Check merge of 2 equivalent attributes
# TODO: => stupid result
query.dim_variant = [[[16]], [[22]]]
unfolder.init_with_query(query)
unfolder.init_forward_unfolding()
expected = [[[16], [16]], [[22], [22]]]
assert unfolder._CLUnfolder__precomputed_variant_constraints == expected
# Check merge of 2 different attributes
query.dim_variant = [[[16]], [[22]], []]
unfolder.init_with_query(query)
# Exception is expected
with pytest.raises(MCLException, match=r".*Incoherent variant properties.*"):
unfolder.init_forward_unfolding()
##
# Check the input of unknown DIMACS literals
query.variant_prop = None
query.dim_variant = [[[16, -47], [17, -47], [-16, -17, 47], [47]], [[22]]]
# With check_query enabled
with pytest.raises(ValueError, match=r".*47.*"):
unfolder.init_with_query(query)
# With check_query disabled
# => this causes an incorrect initialization
unfolder.init_with_query(query, check_query=False)
unfolder.init_forward_unfolding()
##
## aucun test sur les noms des events
# TODO: => stupid result
query.variant_prop = ["PoUeT"]
query.dim_variant = None
mcla.unfolder.init_with_query(query)
mcla.unfolder.init_forward_unfolding()
# => no problem
## formule ici a priori difficile à parser...
## clauses fonctionnelles mais ineptes...
# TODO: => stupid result
query.variant_prop = ["_h2", "_h2 and _h2"]
query.dim_variant = None
mcla.unfolder.init_with_query(query)
mcla.unfolder.init_forward_unfolding()
expected = [[[16]], [[16, -47], [16, -47], [-16, -16, 47], [47]]]
assert unfolder._CLUnfolder__precomputed_variant_constraints == expected
assert unfolder.shift_step == 47
##
# Check the compilation of a complex textual trajectory (a real solution)
query.variant_prop = ["_h2 and _h3", "_h_0"]
query.dim_variant = None
unfolder.init_with_query(query)
unfolder.init_forward_unfolding()
expected = [[[16, -47], [17, -47], [-16, -17, 47], [47]], [[22]]]
assert unfolder._CLUnfolder__precomputed_variant_constraints == expected
# Creation of the auxiliary variable: 47
print(unfolder._CLUnfolder__aux_list)
print(unfolder._CLUnfolder__aux_code_table)
# It is important to test this, because an auxiliary variable was previously
# added and we check the full reset of the unfolder here...
assert unfolder.shift_step == 47
assert unfolder._CLUnfolder__aux_list == ["_lit47"]
assert unfolder._CLUnfolder__aux_code_table == {"_lit47": 47}
def test_shift(feed_mclanalyser, textual_properties):
"""Test shift of constraints during the solutions search"""
......
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