SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ModuleInput.cpp
Go to the documentation of this file.
1 /**
2  * @file ModuleInput.cpp
3  *
4  * @author Florian Corzilius
5  * @version: 2014-05-16
6  */
7 
8 #include "ModuleInput.h"
9 
10 #include <carl-formula/model/Assignment.h>
11 
12 // Main smtrat namespace.
13 namespace smtrat
14 {
15  unsigned ModuleInput::satisfiedBy( const RationalAssignment& _assignment ) const
16  {
17  unsigned result = 1;
18  for( const FormulaWithOrigins& fwo : *this )
19  {
20  switch( carl::satisfied_by(fwo.formula(), Model(_assignment)) )
21  {
22  case 0:
23  return 0;
24  case 1:
25  break;
26  default:
27  if( result != 2 ) result = 2;
28  }
29  }
30  return result;
31  }
32 
33  unsigned ModuleInput::satisfiedBy( const Model& _assignment ) const
34  {
35  unsigned result = 1;
36  for( const FormulaWithOrigins& fwo : *this )
37  {
38  auto res = carl::substitute(fwo.formula(), _assignment);
39  SMTRAT_LOG_DEBUG("smtrat.module", "Checking whether model satisfies " << fwo.formula() << " -> " << res);
40  if (res.is_false()) return 0;
41  if (!res.is_true()) result = 2;
42  }
43  return result;
44  }
45 
47  {
48  auto res = mFormulaPositionMap.find( _formula );
49  if( res == mFormulaPositionMap.end() )
50  {
51  return end();
52  }
53  else
54  {
55  return res->second;
56  }
57  }
58 
60  {
61  auto res = mFormulaPositionMap.find( _formula );
62  if( res == mFormulaPositionMap.end() )
63  {
64  return end();
65  }
66  else
67  {
68  return res->second;
69  }
70  }
71 
73  {
74  auto res = mFormulaPositionMap.find( _formula );
75  if( res == mFormulaPositionMap.end() )
76  {
77  return end();
78  }
79  else
80  {
81  return res->second;
82  }
83  }
84 
86  {
87  auto res = mFormulaPositionMap.find( _formula );
88  if( res == mFormulaPositionMap.end() )
89  {
90  return end();
91  }
92  else
93  {
94  return res->second;
95  }
96  }
97 
99  {
100  assert( _formula != end() );
101  mPropertiesUpdated = false;
102  mFormulaPositionMap.erase( _formula->formula() );
103  return super::erase( _formula );
104  }
105 
106  bool ModuleInput::removeOrigin( iterator _formula, const FormulaT& _origin )
107  {
108  assert( _formula != end() );
109  if( !_formula->hasOrigins() ) return true;
110  auto& origs = *_formula->mOrigins;
111  auto iter = origs.begin();
112  while( iter != origs.end() )
113  {
114  if( *iter == _origin || (iter->type() == carl::FormulaType::AND && iter->contains( _origin )) )
115  {
116  if (iter != --origs.end())
117  {
118  *iter = origs.back();
119  origs.pop_back();
120  }
121  else
122  {
123  origs.pop_back();
124  break;
125  }
126  }
127  else
128  {
129  ++iter;
130  }
131  }
132  if( origs.empty() )
133  {
134  _formula->mOrigins = nullptr;
135  return true;
136  }
137  return false;
138  }
139 
140  bool ModuleInput::removeOrigins( iterator _formula, const std::shared_ptr<std::vector<FormulaT>>& _origins )
141  {
142  assert( _formula != end() );
143  if( !_formula->hasOrigins() ) return true;
144  if( _formula->mOrigins == _origins )
145  {
146  _formula->mOrigins = nullptr;
147  return true;
148  }
149  auto& origs = *_formula->mOrigins;
150  for( auto& orig : *_origins )
151  {
152  auto iter = std::find( origs.begin(), origs.end(), orig );
153  if( iter != origs.end() )
154  {
155  *iter = origs.back();
156  origs.pop_back();
157  }
158  }
159  if( origs.empty() )
160  {
161  _formula->mOrigins = nullptr;
162  return true;
163  }
164  return false;
165  }
166 
168  {
169  mProperties = carl::Condition();
170  mProperties |= carl::PROP_IS_PURE_CONJUNCTION | carl::PROP_IS_LITERAL_CONJUNCTION | carl::PROP_IS_IN_CNF | carl::PROP_IS_IN_NNF;
171  for( const FormulaWithOrigins& fwo : *this )
172  {
173  carl::Condition subFormulaConds = fwo.formula().properties();
174  if( !(carl::PROP_IS_A_CLAUSE<=subFormulaConds) )
175  {
176  mProperties &= ~carl::PROP_IS_PURE_CONJUNCTION;
177  mProperties &= ~carl::PROP_IS_LITERAL_CONJUNCTION;
178  mProperties &= ~carl::PROP_IS_IN_CNF;
179  }
180  else if( !(carl::PROP_IS_AN_ATOM<=subFormulaConds) )
181  mProperties &= ~carl::PROP_IS_PURE_CONJUNCTION;
182  if( !(carl::PROP_IS_IN_NNF<=subFormulaConds) )
183  mProperties &= ~carl::PROP_IS_IN_NNF;
184  mProperties |= (subFormulaConds & carl::WEAK_CONDITIONS);
185  }
186  mPropertiesUpdated = true;
187  }
188 
189  std::pair<ModuleInput::iterator,bool> ModuleInput::add( const FormulaT& _formula, bool _hasSingleOrigin, const FormulaT& _origin, const std::shared_ptr<std::vector<FormulaT>>& _origins, bool _mightBeConjunction )
190  {
191  if( _mightBeConjunction && _formula.type() == carl::FormulaType::AND )
192  {
193  std::pair<iterator,bool> res = std::pair<iterator,bool>(end(), false);
194  auto formulaIter = _formula.subformulas().begin();
195  while( !res.second && formulaIter != _formula.subformulas().end() )
196  {
197  res = add( *formulaIter, _hasSingleOrigin, _origin, _origins );
198  ++formulaIter;
199  }
200  while( formulaIter != _formula.subformulas().end() )
201  {
202  add( *formulaIter, _hasSingleOrigin, _origin, _origins );
203  ++formulaIter;
204  }
205  return res;
206  }
207  else
208  {
209  iterator iter = find( _formula );
210  if( iter == end() )
211  {
212  mPropertiesUpdated = false;
213  if( _hasSingleOrigin )
214  {
215  std::shared_ptr<std::vector<FormulaT>> vecOfOrigs = std::shared_ptr<std::vector<FormulaT>>( new std::vector<FormulaT>() );
216  vecOfOrigs->push_back( _origin );
217  emplace_back( _formula, std::move( vecOfOrigs ) );
218  }
219  else
220  {
221  emplace_back( _formula, _origins );
222  }
223  iterator pos = --end();
224  mFormulaPositionMap.insert( make_pair( _formula, pos ) );
225  return make_pair( pos, true );
226  }
227  else
228  {
229  if( _hasSingleOrigin )
230  {
231  if( !iter->hasOrigins() )
232  {
233  iter->mOrigins = std::shared_ptr<std::vector<FormulaT>>( new std::vector<FormulaT>() );
234  }
235  iter->mOrigins->push_back( _origin );
236  return make_pair( iter, false );
237  }
238  if( _origins != nullptr )
239  {
240  if( iter->hasOrigins() )
241  {
242  iter->mOrigins = std::shared_ptr<std::vector<FormulaT>>( new std::vector<FormulaT>( *iter->mOrigins ) );
243  iter->mOrigins->insert( iter->mOrigins->end(), _origins->begin(), _origins->end() );
244  }
245  else
246  {
247  iter->mOrigins = _origins;
248  }
249  }
250  return make_pair( iter, false );
251  }
252  }
253  }
254 
255  template<typename AnnotationType>
256  void annotateFormula( const FormulaT&, const std::vector<AnnotationType>& )
257  {
258  }
259 } // namespace smtrat
Stores a formula along with its origins.
Definition: ModuleInput.h:22
const FormulaT & formula() const
Definition: ModuleInput.h:86
super::const_iterator const_iterator
Passing through the list::const_iterator.
Definition: ModuleInput.h:146
carl::Condition mProperties
Store some properties about the conjunction of the stored formulas.
Definition: ModuleInput.h:152
iterator find(const FormulaT &_formula)
Definition: ModuleInput.cpp:46
bool removeOrigins(iterator _formula, const std::shared_ptr< FormulasT > &_origins)
void updateProperties()
Updates all properties of the formula underlying this module input.
unsigned satisfiedBy(const Model &_assignment) const
Definition: ModuleInput.cpp:33
carl::FastMap< FormulaT, iterator > mFormulaPositionMap
Maps all formulas occurring (in the origins) at pos i in this module input to i. This is for a faster...
Definition: ModuleInput.h:156
bool mPropertiesUpdated
A flag indicating whether the properties of this module input are updated.
Definition: ModuleInput.h:154
iterator erase(iterator _formula)
Definition: ModuleInput.cpp:98
bool removeOrigin(iterator _formula, const FormulaT &_origin)
super::iterator iterator
Passing through the list::iterator.
Definition: ModuleInput.h:144
std::pair< iterator, bool > add(const FormulaT &_formula, bool _mightBeConjunction=true)
Definition: ModuleInput.h:430
static bool find(V &ts, const T &t)
Definition: Alg.h:47
bool substitute(const FormulaT &constr, const carl::Variable var, const carl::vs::Term< Poly > &term, FormulaT &result)
Definition: VSHelper.h:138
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Assignment< Rational > RationalAssignment
Definition: types.h:45
carl::Model< Rational, Poly > Model
Definition: model.h:13
void annotateFormula(const FormulaT &, const std::vector< AnnotationType > &)
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35