1#include "constraint_store.hpp"
2
3ConstraintStore::ConstraintStore(Problem* _p)
4    : p(_p), constraints_initalized(false)
5    { }
6
7void ConstraintStore::addConstraint(AbstractConstraint* con)
8    {
9        D_ASSERT(!constraints_initalized);
10        con->setId(toString(constraints.size()));
11        constraints.push_back(con);
12    }
13
14void ConstraintStore::initConstraints(bool rbase_building)
15    {
16        D_ASSERT(!constraints_initalized);
17        constraints_initalized = true;
18        for(auto con : constraints)
19        {
20            std::vector<TriggerType> v = con->triggers();
21            for(auto & j : v)
22              p->p_stack.addTrigger(con, j);
23
24            if(rbase_building)
25              con->signal_start_buildingRBase();
26            else
27              con->signal_start();
28
29            SplitState ss = p->con_queue.invokeQueue();
30            (void)ss; // Avoid warning
31            D_ASSERT(!ss.hasFailed());
32        }
33    }
34
35ConstraintStore::~ConstraintStore()
36    {
37        for(auto con : constraints)
38            delete con;
39    }
40
41bool ConstraintStore::initCalled() const
42    { return constraints_initalized; }
43
44bool ConstraintStore::verifySolution(const Permutation& p) const
45    {
46        // TODO: Investigate cases where this fails
47        static bool printed_warning = false;
48        for(int i : range1(constraints.size()))
49        {
50            if(!constraints[i]->verifySolution(p))
51            {
52                if(!printed_warning)
53                {
54                    printed_warning = true;
55                    std::cerr << "A solution has failed checking!\n";
56                    std::cerr << "This was caused by a " << constraints[i]->name() << " constraint\n";
57                    std::cerr << "Your answer will hopefully still be correct,\n";
58                    std::cerr << "But please report your problem\n";
59                }
60             //   assert(0);
61                return false;
62            }
63        }
64        return true;
65    }
66