Modelib: Fichier source de ConstraintBuilder.cpp

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# ConstraintBuilder.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    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 }