@ -1,99 +1,92 @@
# include "src/solver/MathS atSmtSolver.h"
# include "src/solver/Maths atSmtSolver.h"
# include <vector>
# include "src/exceptions/UnexpectedException.h"
# include "src/exceptions/InvalidStateException.h"
namespace storm {
namespace solver {
MathSatSmtSolver : : MathS atSmtSolver ( Options options )
MathsatSmtSolver : : Maths atSmtSolver ( Options const & options )
# ifdef STORM_HAVE_MSAT
: lastCheckAssumptions ( false )
, lastResult ( CheckResult : : UNKNOWN )
: lastCheckAssumptions ( false ) , lastResult ( CheckResult : : Unknown )
# endif
{
# ifdef STORM_HAVE_MSAT
msat_config m_cfg = msat_create_config ( ) ;
if ( static_cast < int > ( options ) & static_cast < int > ( Options : : InterpolantComputation ) ) {
msat_set_option ( m_cfg , " interpolation " , " true " ) ;
msat_config config = msat_create_config ( ) ;
if ( options . enableInterpolantGeneration ) {
msat_set_option ( config , " interpolation " , " true " ) ;
}
if ( options . enableModelGeneration ) {
msat_set_option ( config , " model_generation " , " true " ) ;
}
if ( options . enableUnsatCoreGeneration ) {
msat_set_option ( config , " unsat_core_generation " , " true " ) ;
}
STORM_LOG_THROW ( ! MSAT_ERROR_CONFIG ( config ) , storm : : exceptions : : UnexpectedException , " Unable to create Mathsat configuration. " ) ;
msat_set_option ( m_cfg , " model_generation " , " true " ) ;
m_env = msat_create_env ( m_cfg ) ;
STORM_LOG_THROW ( ! MSAT_ERROR_ENV ( m_env ) , storm : : exceptions : : UnexpectedException , " Unable to create Mathsat environment. " ) ;
msat_destroy_config ( m_cfg ) ;
// Based on the configuration, build the environment, check for errors and destroy the configuration.
env = msat_create_env ( config ) ;
STORM_LOG_THROW ( ! MSAT_ERROR_ENV ( env ) , storm : : exceptions : : UnexpectedException , " Unable to create Mathsat environment. " ) ;
msat_destroy_config ( config ) ;
m_adapter = new storm : : adapters : : MathSatExpressionAdapter ( m_env , variableToDeclMap ) ;
expressionAdapter = std : : unique_ptr < storm : : adapters : : MathsatExpressionAdapter > ( new storm : : adapters : : MathsatExpressionAdapter ( env ) ) ;
# endif
}
MathSatSmtSolver : : ~ MathSatSmtSolver ( ) {
msat_destroy_env ( m_env ) ;
} ;
void MathSatSmtSolver : : push ( )
{
# ifdef STORM_HAVE_MSAT
msat_push_backtrack_point ( m_env ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
MathsatSmtSolver : : ~ MathsatSmtSolver ( ) {
STORM_LOG_THROW ( MSAT_ERROR_ENV ( env ) , storm : : exceptions : : UnexpectedException , " Illegal MathSAT environment. " ) ;
msat_destroy_env ( env ) ;
}
void MathSatSmtSolver : : pop ( )
{
void MathsatSmtSolver : : push ( ) {
# ifdef STORM_HAVE_MSAT
msat_pop _backtrack_point ( m_ env) ;
msat_push_backtrack_point ( env ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
}
void MathSatSmtSolver : : pop ( uint_fast64_t n )
{
void MathsatSmtSolver : : pop ( ) {
# ifdef STORM_HAVE_MSAT
for ( uint_fast64_t i = 0 ; i < n ; + + i ) {
this - > pop ( ) ;
}
msat_pop_backtrack_point ( env ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
}
void MathS atSmtSolver : : reset ( )
void Maths atSmtSolver : : reset ( )
{
# ifdef STORM_HAVE_MSAT
msat_reset_env ( m_ env) ;
msat_reset_env ( env ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
}
void MathSatSmtSolver : : assertExpression ( storm : : expressions : : Expression const & e )
void MathsatSmtSolver : : add ( storm : : expressions : : Expression const & e )
{
# ifdef STORM_HAVE_MSAT
msat_assert_formula ( m_ env, m_a dapter- > translateExpression ( e , true ) ) ;
msat_assert_formula ( env , expressionA dapter- > translateExpression ( e , true ) ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
}
SmtSolver : : CheckResult MathS atSmtSolver : : check ( )
SmtSolver : : CheckResult Maths atSmtSolver : : check ( )
{
# ifdef STORM_HAVE_MSAT
lastCheckAssumptions = false ;
switch ( msat_solve ( m_ env) ) {
switch ( msat_solve ( env ) ) {
case MSAT_SAT :
this - > lastResult = SmtSolver : : CheckResult : : SAT ;
this - > lastResult = SmtSolver : : CheckResult : : Sat ;
break ;
case MSAT_UNSAT :
this - > lastResult = SmtSolver : : CheckResult : : UNSAT ;
this - > lastResult = SmtSolver : : CheckResult : : Unsat ;
break ;
default :
this - > lastResult = SmtSolver : : CheckResult : : UNKNOWN ;
this - > lastResult = SmtSolver : : CheckResult : : Unknown ;
break ;
}
return this - > lastResult ;
@ -102,7 +95,7 @@ namespace storm {
# endif
}
SmtSolver : : CheckResult MathS atSmtSolver : : checkWithAssumptions ( std : : set < storm : : expressions : : Expression > const & assumptions )
SmtSolver : : CheckResult Maths atSmtSolver : : checkWithAssumptions ( std : : set < storm : : expressions : : Expression > const & assumptions )
{
# ifdef STORM_HAVE_MSAT
lastCheckAssumptions = true ;
@ -110,18 +103,18 @@ namespace storm {
mathSatAssumptions . reserve ( assumptions . size ( ) ) ;
for ( storm : : expressions : : Expression assumption : assumptions ) {
mathSatAssumptions . push_back ( this - > m_a dapter- > translateExpression ( assumption ) ) ;
mathSatAssumptions . push_back ( this - > expressionA dapter- > translateExpression ( assumption ) ) ;
}
switch ( msat_solve_with_assumptions ( m_ env, mathSatAssumptions . data ( ) , mathSatAssumptions . size ( ) ) ) {
switch ( msat_solve_with_assumptions ( env , mathSatAssumptions . data ( ) , mathSatAssumptions . size ( ) ) ) {
case MSAT_SAT :
this - > lastResult = SmtSolver : : CheckResult : : SAT ;
this - > lastResult = SmtSolver : : CheckResult : : Sat ;
break ;
case MSAT_UNSAT :
this - > lastResult = SmtSolver : : CheckResult : : UNSAT ;
this - > lastResult = SmtSolver : : CheckResult : : Unsat ;
break ;
default :
this - > lastResult = SmtSolver : : CheckResult : : UNKNOWN ;
this - > lastResult = SmtSolver : : CheckResult : : Unknown ;
break ;
}
return this - > lastResult ;
@ -130,7 +123,7 @@ namespace storm {
# endif
}
SmtSolver : : CheckResult MathS atSmtSolver : : checkWithAssumptions ( std : : initializer_list < storm : : expressions : : Expression > assumptions )
SmtSolver : : CheckResult Maths atSmtSolver : : checkWithAssumptions ( std : : initializer_list < storm : : expressions : : Expression > const & assumptions )
{
# ifdef STORM_HAVE_MSAT
lastCheckAssumptions = true ;
@ -138,18 +131,18 @@ namespace storm {
mathSatAssumptions . reserve ( assumptions . size ( ) ) ;
for ( storm : : expressions : : Expression assumption : assumptions ) {
mathSatAssumptions . push_back ( this - > m_a dapter- > translateExpression ( assumption ) ) ;
mathSatAssumptions . push_back ( this - > expressionA dapter- > translateExpression ( assumption ) ) ;
}
switch ( msat_solve_with_assumptions ( m_ env, mathSatAssumptions . data ( ) , mathSatAssumptions . size ( ) ) ) {
switch ( msat_solve_with_assumptions ( env , mathSatAssumptions . data ( ) , mathSatAssumptions . size ( ) ) ) {
case MSAT_SAT :
this - > lastResult = SmtSolver : : CheckResult : : SAT ;
this - > lastResult = SmtSolver : : CheckResult : : Sat ;
break ;
case MSAT_UNSAT :
this - > lastResult = SmtSolver : : CheckResult : : UNSAT ;
this - > lastResult = SmtSolver : : CheckResult : : Unsat ;
break ;
default :
this - > lastResult = SmtSolver : : CheckResult : : UNKNOWN ;
this - > lastResult = SmtSolver : : CheckResult : : Unknown ;
break ;
}
return this - > lastResult ;
@ -158,23 +151,22 @@ namespace storm {
# endif
}
storm : : expressions : : SimpleValuation MathS atSmtSolver : : getModel ( )
storm : : expressions : : SimpleValuation Maths atSmtSolver : : getModel ( )
{
# ifdef STORM_HAVE_MSAT
STORM_LOG_THROW ( this - > lastResult = = SmtSolver : : CheckResult : : SAT , storm : : exceptions : : InvalidStateException , " Requested Model but last check result was not SAT. " ) ;
return this - > MathSatModelToStorm ( ) ;
STORM_LOG_THROW ( this - > lastResult = = SmtSolver : : CheckResult : : Sat , storm : : exceptions : : InvalidStateException , " Requested Model but last check result was not SAT. " ) ;
return this - > convertMathsatModelToValuation ( ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
}
# ifdef STORM_HAVE_MSAT
storm : : expressions : : SimpleValuation MathSatSmtSolver : : MathSatModelToStorm ( ) {
storm : : expressions : : SimpleValuation MathsatSmtSolver : : convertMathsatModelToValuation ( ) {
storm : : expressions : : SimpleValuation stormModel ;
msat_model_iterator model = msat_create_model_iterator ( m_ env) ;
msat_model_iterator model = msat_create_model_iterator ( env ) ;
STORM_LOG_THROW ( ! MSAT_ERROR_MODEL_ITERATOR ( model ) , storm : : exceptions : : UnexpectedException , " MathSat returned an illegal model iterator. " ) ;
@ -182,7 +174,7 @@ namespace storm {
msat_term t , v ;
msat_model_iterator_next ( model , & t , & v ) ;
storm : : expressions : : Expression var_i_interp = this - > m_a dapter- > translateTerm ( v ) ;
storm : : expressions : : Expression var_i_interp = this - > expressionA dapter- > translateTerm ( v ) ;
char * name = msat_decl_get_name ( msat_term_get_decl ( t ) ) ;
switch ( var_i_interp . getReturnType ( ) ) {
@ -209,7 +201,7 @@ namespace storm {
}
# endif
std : : vector < storm : : expressions : : SimpleValuation > MathS atSmtSolver : : allSat ( std : : vector < storm : : expressions : : Expression > const & important )
std : : vector < storm : : expressions : : SimpleValuation > Maths atSmtSolver : : allSat ( std : : vector < storm : : expressions : : Expression > const & important )
{
# ifdef STORM_HAVE_MSAT
@ -229,13 +221,13 @@ namespace storm {
class AllsatValuationsCallbackUserData {
public :
AllsatValuationsCallbackUserData ( msat_env & env ,
storm : : adapters : : MathS atExpressionAdapter & adapter ,
std : : function < bool ( storm : : expressions : : SimpleValuation & ) > & callback )
storm : : adapters : : Maths atExpressionAdapter & adapter ,
std : : function < bool ( storm : : expressions : : SimpleValuation & ) > const & callback )
: env ( env ) , adapter ( adapter ) , callback ( callback ) {
}
msat_env & env ;
storm : : adapters : : MathS atExpressionAdapter & adapter ;
std : : function < bool ( storm : : expressions : : SimpleValuation & ) > & callback ;
storm : : adapters : : Maths atExpressionAdapter & adapter ;
std : : function < bool ( storm : : expressions : : SimpleValuation & ) > const & callback ;
} ;
int allsatValuationsCallback ( msat_term * model , int size , void * user_data ) {
@ -265,7 +257,7 @@ namespace storm {
# endif
uint_fast64_t MathS atSmtSolver : : allSat ( std : : vector < storm : : expressions : : Expression > const & important , std : : function < bool ( storm : : expressions : : SimpleValuation & ) > callback )
uint_fast64_t Maths atSmtSolver : : allSat ( std : : vector < storm : : expressions : : Expression > const & important , std : : function < bool ( storm : : expressions : : SimpleValuation & ) > const & callback )
{
# ifdef STORM_HAVE_MSAT
std : : vector < msat_term > msatImportant ;
@ -275,11 +267,11 @@ namespace storm {
if ( ! e . isVariable ( ) ) {
throw storm : : exceptions : : InvalidArgumentException ( ) < < " The important expressions for AllSat must be atoms, i.e. variable expressions. " ;
}
msatImportant . push_back ( m_a dapter- > translateExpression ( e ) ) ;
msatImportant . push_back ( expressionA dapter- > translateExpression ( e ) ) ;
}
AllsatValuationsCallbackUserData allSatUserData ( m_ env, * m_a dapter, callback ) ;
int numModels = msat_all_sat ( m_ env, msatImportant . data ( ) , msatImportant . size ( ) , & allsatValuationsCallback , & allSatUserData ) ;
AllsatValuationsCallbackUserData allSatUserData ( env , * expressionA dapter, callback ) ;
int numModels = msat_all_sat ( env , msatImportant . data ( ) , msatImportant . size ( ) , & allsatValuationsCallback , & allSatUserData ) ;
return numModels ;
# else
@ -287,7 +279,7 @@ namespace storm {
# endif
}
uint_fast64_t MathS atSmtSolver : : allSat ( std : : function < bool ( SmtSolver : : ModelReference & ) > callback , std : : vector < storm : : expressions : : Expression > const & important )
uint_fast64_t Maths atSmtSolver : : allSat ( std : : vector < storm : : expressions : : Expression > const & important , std : : function < bool ( SmtSolver : : ModelReference & ) > const & callback )
{
# ifdef STORM_HAVE_MSAT
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " Not Implemented. " ) ;
@ -296,9 +288,9 @@ namespace storm {
# endif
}
std : : vector < storm : : expressions : : Expression > MathS atSmtSolver : : getUnsatAssumptions ( ) {
std : : vector < storm : : expressions : : Expression > Maths atSmtSolver : : getUnsatAssumptions ( ) {
# ifdef STORM_HAVE_MSAT
if ( lastResult ! = SmtSolver : : CheckResult : : UNSAT ) {
if ( lastResult ! = SmtSolver : : CheckResult : : Unsat ) {
throw storm : : exceptions : : InvalidStateException ( ) < < " Unsat Assumptions was called but last state is not unsat. " ;
}
if ( ! lastCheckAssumptions ) {
@ -306,13 +298,13 @@ namespace storm {
}
size_t numUnsatAssumpations ;
msat_term * msatUnsatAssumptions = msat_get_unsat_assumptions ( m_ env, & numUnsatAssumpations ) ;
msat_term * msatUnsatAssumptions = msat_get_unsat_assumptions ( env , & numUnsatAssumpations ) ;
std : : vector < storm : : expressions : : Expression > unsatAssumptions ;
unsatAssumptions . reserve ( numUnsatAssumpations ) ;
for ( unsigned int i = 0 ; i < numUnsatAssumpations ; + + i ) {
unsatAssumptions . push_back ( this - > m_a dapter- > translateTerm ( msatUnsatAssumptions [ i ] ) ) ;
unsatAssumptions . push_back ( this - > expressionA dapter- > translateTerm ( msatUnsatAssumptions [ i ] ) ) ;
}
return unsatAssumptions ;
@ -321,26 +313,26 @@ namespace storm {
# endif
}
void MathS atSmtSolver : : setInterpolationGroup ( uint_fast64_t group ) {
void Maths atSmtSolver : : setInterpolationGroup ( uint_fast64_t group ) {
# ifdef STORM_HAVE_MSAT
auto groupIter = this - > interpolationGroups . find ( group ) ;
if ( groupIter = = this - > interpolationGroups . end ( ) ) {
int newGroup = msat_create_itp_group ( m_ env) ;
int newGroup = msat_create_itp_group ( env ) ;
auto insertResult = this - > interpolationGroups . insert ( std : : make_pair ( group , newGroup ) ) ;
if ( ! insertResult . second ) {
throw storm : : exceptions : : InvalidStateException ( ) < < " Internal error in MathSAT wrapper: Unable to insert newly created interpolation group. " ;
}
groupIter = insertResult . first ;
}
msat_set_itp_group ( m_ env, groupIter - > second ) ;
msat_set_itp_group ( env , groupIter - > second ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif
}
storm : : expressions : : Expression MathS atSmtSolver : : getInterpolant ( std : : vector < uint_fast64_t > groupsA ) {
storm : : expressions : : Expression Maths atSmtSolver : : getInterpolant ( std : : vector < uint_fast64_t > const & groupsA ) {
# ifdef STORM_HAVE_MSAT
if ( lastResult ! = SmtSolver : : CheckResult : : UNSAT ) {
if ( lastResult ! = SmtSolver : : CheckResult : : Unsat ) {
throw storm : : exceptions : : InvalidStateException ( ) < < " getInterpolant was called but last state is not unsat. " ;
}
if ( lastCheckAssumptions ) {
@ -356,11 +348,11 @@ namespace storm {
}
msatInterpolationGroupsA . push_back ( groupIter - > second ) ;
}
msat_term interpolant = msat_get_interpolant ( m_ env, msatInterpolationGroupsA . data ( ) , msatInterpolationGroupsA . size ( ) ) ;
msat_term interpolant = msat_get_interpolant ( env , msatInterpolationGroupsA . data ( ) , msatInterpolationGroupsA . size ( ) ) ;
STORM_LOG_THROW ( ! MSAT_ERROR_TERM ( interpolant ) , storm : : exceptions : : UnexpectedException , " Unable to retrieve an interpolant. " ) ;
return this - > m_a dapter- > translateTerm ( interpolant ) ;
return this - > expressionA dapter- > translateTerm ( interpolant ) ;
# else
STORM_LOG_THROW ( false , storm : : exceptions : : NotImplementedException , " StoRM is compiled without MathSat support. " ) ;
# endif