metaSMT 2
metaSMT/backend/PicoSAT.hpp
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "../tags/SAT.hpp"
00004 #include "../result_wrapper.hpp"
00005 
00006 extern "C"
00007 {
00008 #include <picosat.h>
00009 }
00010 #include <vector>
00011 #include <exception>
00012 #include <iostream>
00013 
00014 #include <boost/fusion/sequence/intrinsic.hpp>
00015 #include <boost/fusion/support/is_sequence.hpp>
00016 #include <boost/variant.hpp>
00017 #include <boost/any.hpp> 
00018 #include <boost/foreach.hpp>
00019  
00020 
00021 namespace metaSMT {
00022   namespace solver {
00023 
00024     class PicoSAT
00025     {
00026       public:
00027         PicoSAT ()
00028         {
00029 //          if ( in_use == USED )
00030 //            throw std:runtime_error ("Picosat already in use. Only single instances supported."); 
00031          
00032           picosat_init (); 
00033 //          in_use = USED;
00034         }
00035 
00036         ~PicoSAT ()
00037         {
00038           picosat_reset (); 
00039  //         in_use = UNUSED;
00040         }
00041          
00042 
00043         int toLit ( SAT::tag::lit_tag lit )
00044         {
00045           return lit.id; 
00046         }
00047 
00048         void clause ( std::vector < SAT::tag::lit_tag > const& clause)
00049         {
00050           BOOST_FOREACH ( SAT::tag::lit_tag const& lit, clause )
00051             picosat_add ( toLit ( lit ) );
00052           picosat_add ( 0 ); 
00053         }
00054 
00055 
00056         void assertion ( SAT::tag::lit_tag lit )
00057         {
00058           picosat_add ( toLit ( lit ) ); 
00059           picosat_add ( 0 ); 
00060         }
00061 
00062         void assumption ( SAT::tag::lit_tag lit )
00063         {
00064           picosat_assume ( toLit ( lit ) ); 
00065         }
00066 
00067 
00068         bool solve ( )
00069         {
00070           switch (picosat_sat (-1))
00071           {
00072             case PICOSAT_UNSATISFIABLE:
00073               return false;
00074             case PICOSAT_SATISFIABLE:
00075               return true;
00076             case PICOSAT_UNKNOWN:
00077               throw std::runtime_error ( "unknown return type of picosat_sat "); 
00078             default:
00079               throw std::runtime_error ( "unsupported return type of picosat_sat "); 
00080           }
00081         }
00082 
00083         result_wrapper read_value ( SAT::tag::lit_tag lit ) 
00084         {
00085            
00086           switch ( picosat_deref ( toLit ( lit ) ) )
00087           {
00088             case -1:
00089               return result_wrapper ( '0' );
00090             case 1:
00091               return result_wrapper ( '1' );
00092             case 0:
00093               return result_wrapper ( 'X' );
00094             default:
00095               std::cerr << "Unknown result." << std::endl;
00096               return result_wrapper ( 'X' ); 
00097           }
00098         }
00099 
00100       private:
00101 //         enum { UNUSED, USED }; 
00102 //         static int in_use = UNUSED; 
00103     };
00104   } /* solver */
00105 } /* metaSMT */
00106 // vim: ts=2 sw=2 et
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines