00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "ConstraintBuilder.h"
00033 #include <iostream>
00034
00035 namespace Modelib {
00036
00037 using namespace std;
00038
00039
00040
00049 ConstraintBuilder::ConstraintBuilder( const Expr & e1, ConstraintBuilder::Operateur op, const Expr & e2)
00050 {
00051 expressions.push_back( e1 );
00052 expressions.push_back( e2 );
00053 operateurs.push_back( op );
00054 CheckIfIsBound();
00055 }
00056
00057
00058
00059
00070 bool ConstraintBuilder::CheckIfIsBound()
00071 {
00072 isBound = false;
00073 if(operateurs.size()==1 && expressions.size() == 2)
00074 {
00075 switch(operateurs.front())
00076 {
00077 case ConstraintBuilder::INFSTRICT:
00078 case ConstraintBuilder::SUPSTRICT:
00079 case ConstraintBuilder::INFERIEUR:
00080 case ConstraintBuilder::SUPERIEUR:
00081 isBound=(expressions.front().IsNum() && expressions.back().IsVar());
00082 isBound=isBound || (expressions.front().IsVar() && expressions.back().IsNum());
00083 break;
00084 default:
00085 isBound = false;
00086 break;
00087 }
00088 }
00089 if(operateurs.size()==2 && expressions.size() == 3)
00090 {
00091 if( operateurs.front() == operateurs.back()
00092 || operateurs.front() == operateurs.back() + 2
00093 || operateurs.front() + 2 == operateurs.back() )
00094 {
00095 switch(operateurs.front())
00096 {
00097 case ConstraintBuilder::INFERIEUR:
00098 case ConstraintBuilder::SUPERIEUR:
00099 case ConstraintBuilder::INFSTRICT:
00100 case ConstraintBuilder::SUPSTRICT:
00101 isBound=(expressions.front().IsNum() && expressions.back().IsNum());
00102 isBound=isBound && ( (++expressions.begin())->IsVar());
00103 break;
00104 default:
00105 isBound = false;
00106 break;
00107 }
00108 }
00109 }
00110 return isBound;
00111 }
00112
00113
00114
00123 void ConstraintBuilder::Add( ConstraintBuilder::Operateur op,
00124 const list<Expr> & exps,
00125 const list<ConstraintBuilder::Operateur> & ops)
00126 {
00127 operateurs.push_back( op );
00128
00129 for( list<Expr>::const_iterator iter = exps.begin();
00130 iter != exps.end();
00131 ++iter)
00132 expressions.push_back( *iter );
00133
00134 for( list<ConstraintBuilder::Operateur>::const_iterator oper = ops.begin();
00135 oper != ops.end();
00136 ++oper)
00137 operateurs.push_back( *oper );
00138 CheckIfIsBound();
00139 }
00140
00146 ConstraintBuilder & ConstraintBuilder::operator== ( const ConstraintBuilder & c)
00147 {
00148 Add(ConstraintBuilder::EGAL,c.expressions,c.operateurs);
00149 return *this;
00150 }
00151
00157 ConstraintBuilder & ConstraintBuilder::operator!= ( const ConstraintBuilder & c)
00158 {
00159 Add(ConstraintBuilder::DIFFERENT,c.expressions,c.operateurs);
00160 return *this;
00161 }
00162
00168 ConstraintBuilder & ConstraintBuilder::operator<= ( const ConstraintBuilder & c)
00169 {
00170 Add(ConstraintBuilder::INFERIEUR,c.expressions,c.operateurs);
00171 return *this;
00172 }
00173
00179 ConstraintBuilder & ConstraintBuilder::operator>= ( const ConstraintBuilder & c)
00180 {
00181 Add(ConstraintBuilder::SUPERIEUR,c.expressions,c.operateurs);
00182 return *this;
00183 }
00184
00190 ConstraintBuilder & ConstraintBuilder::operator< ( const ConstraintBuilder & c)
00191 {
00192 Add(ConstraintBuilder::INFSTRICT,c.expressions,c.operateurs);
00193 return *this;
00194 }
00195
00201 ConstraintBuilder & ConstraintBuilder::operator> ( const ConstraintBuilder & c)
00202 {
00203 Add(ConstraintBuilder::SUPSTRICT,c.expressions,c.operateurs);
00204 return *this;
00205 }
00206
00207
00208
00209
00215 ConstraintBuilder & ConstraintBuilder::operator== ( const Expr & e)
00216 {
00217 expressions.push_back( e );
00218 operateurs.push_back( ConstraintBuilder::EGAL );
00219 isBound = false;
00220 return *this;
00221 }
00222
00228 ConstraintBuilder & ConstraintBuilder::operator!= ( const Expr & e)
00229 {
00230 expressions.push_back( e );
00231 operateurs.push_back( ConstraintBuilder::DIFFERENT );
00232 isBound = false;
00233 return *this;
00234 }
00235
00241 ConstraintBuilder & ConstraintBuilder::operator<= ( const Expr & e)
00242 {
00243 expressions.push_back( e );
00244 operateurs.push_back( ConstraintBuilder::INFERIEUR );
00245 CheckIfIsBound();
00246 return *this;
00247 }
00248
00254 ConstraintBuilder & ConstraintBuilder::operator>= ( const Expr & e)
00255 {
00256 expressions.push_back( e );
00257 operateurs.push_back( ConstraintBuilder::SUPERIEUR );
00258 CheckIfIsBound();
00259 return *this;
00260 }
00261
00267 ConstraintBuilder & ConstraintBuilder::operator< ( const Expr & e)
00268 {
00269 expressions.push_back( e );
00270 operateurs.push_back( ConstraintBuilder::INFSTRICT );
00271 CheckIfIsBound();
00272 return *this;
00273 }
00274
00280 ConstraintBuilder & ConstraintBuilder::operator> ( const Expr & e)
00281 {
00282 expressions.push_back( e );
00283 operateurs.push_back( ConstraintBuilder::SUPSTRICT );
00284 CheckIfIsBound();
00285 return *this;
00286 }
00287
00288
00289
00290
00297 ConstraintBuilder operator== ( const Expr & e1, const Expr & e2)
00298 {
00299 return ConstraintBuilder( e1, ConstraintBuilder::EGAL, e2 );
00300 }
00301
00308 ConstraintBuilder operator!= ( const Expr & e1, const Expr & e2)
00309 {
00310 return ConstraintBuilder( e1, ConstraintBuilder::DIFFERENT, e2 );
00311 }
00312
00319 ConstraintBuilder operator<= ( const Expr & e1, const Expr & e2 )
00320 {
00321 return ConstraintBuilder( e1, ConstraintBuilder::INFERIEUR, e2 );
00322 }
00323
00330 ConstraintBuilder operator>= ( const Expr & e1, const Expr & e2)
00331 {
00332 return ConstraintBuilder( e1, ConstraintBuilder::SUPERIEUR, e2 );
00333 }
00334
00341 ConstraintBuilder operator< ( const Expr & e1, const Expr & e2 )
00342 {
00343 return ConstraintBuilder( e1, ConstraintBuilder::INFSTRICT, e2 );
00344 }
00345
00352 ConstraintBuilder operator> ( const Expr & e1, const Expr & e2)
00353 {
00354 return ConstraintBuilder( e1, ConstraintBuilder::SUPSTRICT, e2 );
00355 }
00356
00357
00358
00365 std::ostream & operator<< (std::ostream & flux, const ConstraintBuilder & c)
00366 {
00367 string op[] = { "==", "!=", "<=", ">=", " < ", " > " };
00368 list<Expr>::const_iterator iter = c.expressions.begin();
00369 list<ConstraintBuilder::Operateur>::const_iterator oper = c.operateurs.begin();
00370
00371 while( iter != c.expressions.end() )
00372 {
00373 flux << *iter;
00374 if(oper != c.operateurs.end())
00375 {
00376 flux << op[*oper];
00377 ++oper;
00378 }
00379 ++iter;
00380 }
00381 return flux;
00382 }
00383
00384
00385 }