Constraint.h
00001 /*
00002  * Constraint.h
00003  *
00004  *  Created on: Sep 16, 2008
00005  *  Author: isil
00006  */
00007 
00008 #ifndef CONSTRAINT_H_
00009 #define CONSTRAINT_H_
00010 
00011 #include "ConstraintSolver.h"
00012 #include "Term.h"
00013 
00014 #include <set>
00015 #include <iostream>
00016 #include <unordered_map>
00017 #include <unordered_set>
00018 #include <string>
00019 #include <functional>
00020 
00021 using namespace std;
00022 
00023 class CNode;
00024 
00025 class Constraint {
00026         friend class boost::serialization::access;
00027 private:
00028         friend struct std::hash<Constraint>;
00029         friend class ConstraintSolver;
00030         friend class Term;
00031         int id;
00032 
00033         template<class Archive>
00034         void save(Archive & ar, const unsigned int version) const
00035         {
00036 
00037                 CNode* nc = cs.get_nc((int&)id);
00038                 CNode* sc = cs.get_sc((int&)id);
00039                 ar & nc;
00040                 ar & sc;
00041 
00042 
00043         }
00044         template<class Archive>
00045         void load(Archive & ar, const unsigned int version)
00046         {
00047                 CNode* nc;
00048                 CNode* sc;
00049                 ar & nc;
00050                 ar & sc;
00051                 nc = CNode::uniquify_cnode(nc);
00052                 sc = CNode::uniquify_cnode(sc);
00053                 pair<CNode*, CNode*> key(nc, sc);
00054                 id = cs.get_id(key);
00055 
00056 
00057         }
00058         BOOST_SERIALIZATION_SPLIT_MEMBER()
00059 
00060         void get_msa_assignment(set<VariableTerm*>&
00061                         msa_vars, map<Term*, SatValue>& msa);
00062 
00063 
00064 
00065 public:
00066         static ConstraintSolver cs;
00067         Constraint();
00071         Constraint(bool val);
00072         Constraint(const Constraint& nc, const Constraint& sc);
00073         Constraint(Term* t1, Term* t2, atom_op_type op);
00074 
00075         Constraint(const Constraint & other);
00076         Constraint(string s);
00077         Constraint(const char* s);
00078 
00079         Constraint(CNode* n);
00080 
00081         static void set_geqz_attribute(Term* t);
00082         static void set_gtz_attribute(Term* t);
00083 
00084         bool sat() const;
00085         bool unsat() const;
00086         bool valid() const;
00087         bool equivalent(Constraint other) const;
00088 
00089 
00090         /*
00091          * These methods do not simplify.
00092          */
00093         bool sat_discard() const;
00094         bool unsat_discard() const;
00095         bool valid_discard() const;
00096 
00097 
00098         Constraint nc() const;
00099         Constraint sc() const;
00100 
00101         int nc_size() const;
00102         int sc_size() const;
00103 
00104         /*
00105          * Checks if the constraint is literally the constant true/false
00106          */
00107         bool is_true() const;
00108         bool is_false() const;
00109 
00110         static void clear();
00111 
00112 
00113 
00114         Constraint operator&(const Constraint & other) const;
00115         Constraint operator|(const Constraint & other) const;
00116 
00117         /*
00118          * Find a constraint C such that b&C=>this and C is consistent with everything
00119          * in the consistency_constraints set.
00120          */
00121         Constraint abduce(Constraint  b,
00122                         const set<Constraint> & consistency_constraints,
00123                         map<Term*, int>& costs) const;
00124 
00125         Constraint abduce(Constraint  b,
00126                         const set<Constraint> & consistency_constraints) const;
00127 
00128         Constraint abduce(Constraint  b) const;
00129 
00130         void operator&=(const Constraint & other);
00131         void operator|=(const Constraint & other);
00132 
00133 
00134         Constraint operator!() const ;
00135 
00136         void operator=(const Constraint &  other);
00137         bool operator==(const Constraint &  other) const;
00138         bool operator!=(const Constraint &  other) const;
00139         bool implies(const Constraint & other) const;
00140         bool operator<(const Constraint  & other) const;
00141 
00142         /*
00143          * Returns true if NC==SC, i.e. the current constraint has
00144          * no imprecision.
00145          */
00146         bool is_precise() const;
00147 
00148         /*
00149          * Functions for eliminating existentially quantified variables
00150          */
00151         void eliminate_evar(VariableTerm* var);
00152         void eliminate_evars(set<VariableTerm*>& vars);
00153 
00154         /*
00155          * Functions for eliminating universally quantified variables
00156          */
00157         void eliminate_uvar(VariableTerm* var);
00158         void eliminate_uvars(set<VariableTerm*>& vars);
00159 
00160         /*
00161          * Does this constraint contain a <, <=, >, >=?
00162          */
00163         bool contains_inequality();
00164 
00165         void fresh_id();
00166 
00167 
00168         /*
00169          * Functions for eliminating free variables
00170          */
00171         void eliminate_free_var(VariableTerm* var);
00172         void eliminate_free_vars(set<VariableTerm*>& vars);
00173 
00174 
00175         /*
00176          * Yields the set of all terms used in this constraint
00177          */
00178         void get_terms(set<Term*>& terms, bool include_nested_terms);
00179 
00180         /*
00181          * Methods to manage background knowledge, i.e.
00182          * assumptions on the left side of the turnstile.
00183          */
00184         static void add_ground_axiom(Constraint key, Constraint c);
00185         static void add_quantified_axiom(Constraint key, Constraint c);
00186         static void set_background_knowledge(Constraint c);
00187 
00188         static void replace_term_in_axioms(Term* old_t, Term* new_t);
00189 
00190 
00191 
00192         static string background_knowledge_to_string();
00193         static Constraint get_general_background();
00194 
00195 
00196         /*
00197          * The return value R is a term t' such that (t=t') is
00198          * implied by this constraint. Returns NULL if no
00199          * such equality is implied by the constraint.
00200          */
00201         Term* find_equality(Term* t) const;
00202 
00203         void find_equalities(Term* t, set<Term*> & eqs) const;
00204 
00205 
00206         void replace_term(Term* to_replace, Term* replacement);
00207         void replace_terms(map<Term*, Term*> & replacements);
00208         void replace_constraint(Constraint to_replace, Constraint replacement);
00209 
00210         void replace_terms(Term* (*sub_func)(Term* t, void* data), void* my_data)
00211         {
00212                 c_id old_id = id;
00213                 id = cs.replace_terms(old_id, sub_func, my_data);
00214         }
00215 
00216         void get_free_variables(set<Term*>& vars);
00217         bool contains_term(Term* var);
00218         // Does this constraint contain any of these terms?
00219         bool contains_term(set<Term*>& terms);
00220 
00221 
00222 
00223         static void clear_background();
00224 
00225         /*
00226          * If this method is called, background knowledge is not
00227          * taken into account when determining satisfiability/validity.
00228          */
00229         static void disable_background();
00230 
00231         /*
00232          * If this constraint contains artificial variables used to enforce,
00233          * for example, existence and uniqueness, this function
00234          * eliminates these fake variables and replaces them by
00235          * what they stand for.
00236          */
00237         void propagate_background();
00238 
00239         /*
00240          * Assume that c holds, i.e. set all leaves from c that occur inside you
00241          * to true.
00242          */
00243         void assume(Constraint c);
00244         /*
00245          * Are t1 and t2 related by an equality in this constraint?
00246          */
00247         bool has_equality_relation(Term* t1, Term* t2);
00248 
00249 
00250         void get_disjunctive_equalities(Term* var,
00251                         map<Term*, Constraint> & equalities);
00252 
00253         void divide(long int c, Term* t);
00254 
00255 
00256         /*
00257          * Gives a satisfying assignment to all terms in the constraint.
00258          * e.g. <drf(a), 3>,...
00259          */
00260         bool get_assignment(set<pair<string, string> > & assignments);
00261         bool get_assignment(map<Term*, SatValue> & assignments);
00262 
00263 
00264         void replace_terms(Term* (*sub_func)(Term* t));
00265 
00266 
00267 
00268         string to_string() const;
00269         string debug_string();
00270 
00271         int msa(map<Term*, SatValue>& msa);
00272         int  msa(set<VariableTerm*> & msa) const;
00273         int msa(set<VariableTerm*> & msa, set<Constraint>& bg) const;
00274         int msa(map<Term*, SatValue> & msa, set<Constraint>& bg);
00275         int  msa(set<VariableTerm*> & msa,
00276                         map<VariableTerm*, int>& costs) const;
00277         int  msa(set<VariableTerm*> & msa, set<Constraint>& bg,
00278                         map<VariableTerm*, int>& costs) const;
00279         int  msa(map<Term*, SatValue> & msa, set<Constraint>& bg,
00280                         map<VariableTerm*, int>& costs);
00281 
00282         pair<CNode*, CNode*> get_cnodes();
00283 
00284 
00285         void to_dnf(set<Constraint> & dnf);
00286         void to_cnf(set<Constraint> & cnf);
00287 
00288 };
00289 
00290 ostream& operator <<(ostream &os,const Constraint &_obj);
00291 
00292 namespace std {
00293 template <>
00294 struct hash<Constraint> {
00295 
00296         size_t operator() (const Constraint & x) const {
00297                 Constraint & c = (Constraint &)x;
00298                 return hash<int>()(c.id);
00299         }
00300 };
00301 
00302 
00303 }
00304 
00305 
00306 #endif /* CONSTRAINT_H_ */