diff --git a/src/solver/uniform_solver/uniform_solver.jl b/src/solver/uniform_solver/uniform_solver.jl index 95105fb..7c83435 100644 --- a/src/solver/uniform_solver/uniform_solver.jl +++ b/src/solver/uniform_solver/uniform_solver.jl @@ -154,8 +154,12 @@ function post!(solver::UniformSolver, constraint::AbstractLocalConstraint) return end #if the was not deactivated after initial propagation, it can be added to the list of constraints - @assert constraint ∉ keys(solver.isactive) "Attempted to post a constraint that was already posted before" - solver.isactive[constraint] = StateInt(solver.sm, 1) + if (constraint ∈ keys(solver.isactive)) + @assert solver.isactive[constraint] == 0 "Attempted to post a constraint that is already active" + else + solver.isactive[constraint] = StateInt(solver.sm, 0) #initializing the state int as 0 will deactivate it on backtrack + end + set_value!(solver.isactive[constraint], 1) end diff --git a/test/test_state_manager.jl b/test/test_state_manager.jl index f1a5e95..a1bbe7f 100644 --- a/test/test_state_manager.jl +++ b/test/test_state_manager.jl @@ -96,4 +96,13 @@ @test get_value(a) == 10 @test get_value(b) == 100 end + + @testset "behavior needed for constraint unposting" begin + sm = HerbConstraints.StateManager() + save_state!(sm) + a = StateInt(sm, 0) # initialize a new state int, set its value to 0 (post a new constraint) + set_value!(a, 1) # immediately update the state int to 1 (activate the newly posted constraint) + restore!(sm) + @test get_value(a) == 0 # on backtrack, the new constraint should be deactivated + end end