Nantes Université

Skip to content
Extraits de code Groupes Projets
mparser.cpp 18,2 ko
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
#include <fstream>
#include <sstream>
#include "model.h"
using namespace std;

const wchar_t COMMENT_CHAR    = L'#';
const wchar_t PARAMNAME_SEP   = L':';
const wchar_t PARAM_ORDER     = L'@';
const wchar_t PARAM_REF_BEGIN = L'<';
const wchar_t PARAM_REF_END   = L'>';
const wchar_t WEIGHT_BEGIN    = L'(';
const wchar_t WEIGHT_END      = L')';
const wchar_t SET_BEGIN       = L'{';
const wchar_t SET_END         = L'}';
const wchar_t SET_ORDER       = L'@';
const wchar_t SET_SEP         = L','; // default separator of param names in submodel/cluster definition

const wchar_t RESULT_PARAM_PREFIX = L'$';

// note: keep it consistent with cpsyntax
// TODO: change the way we detect the constraints, this is error-prone
wstring CONSTRAINT_PATTERN1 = L"IF";
wstring CONSTRAINT_PATTERN2 = L"IF*[*]*";
wstring CONSTRAINT_PATTERN3 = L"[*]*";
wstring CONSTRAINT_PATTERN4 = L"(*[*]*";
wstring CONSTRAINT_PATTERN5 = L"IF*ISNEGATIVE";
wstring CONSTRAINT_PATTERN6 = L"IF*ISNEGATIVE*(*";
wstring CONSTRAINT_PATTERN7 = L"IF*ISPOSITIVE";
wstring CONSTRAINT_PATTERN8 = L"IF*ISPOSITIVE*(*";

//
//
//
bool lineIsComment( wstring& line )
{
    wstring trimmedLine = trim( line );
    if ( trimmedLine.empty() ) return( false );
    return( trimmedLine.at( 0 ) == COMMENT_CHAR );
}

//
// detects whether a line is a constraint
// TODO: have better detection here
//
bool lineIsConstraint( wstring& line )
{
    wstring trimmed = line;
    toUpper( trimmed );
    trimmed = trim( trimmed );

    // if the line contains just "IF", it is a constraint
    if( 0 == stringCompare( trimmed, CONSTRAINT_PATTERN1, false ) )
    {
        return( true );
    }

    // if the line matches any of the other patterns, it's a constraint
    return ( patternMatch( CONSTRAINT_PATTERN2, trimmed )
          || patternMatch( CONSTRAINT_PATTERN3, trimmed )
          || patternMatch( CONSTRAINT_PATTERN4, trimmed )
          || patternMatch( CONSTRAINT_PATTERN5, trimmed )
          || patternMatch( CONSTRAINT_PATTERN6, trimmed )
          || patternMatch( CONSTRAINT_PATTERN7, trimmed )
          || patternMatch( CONSTRAINT_PATTERN8, trimmed ) );
}

//
// detects whether a line is a submodel or a cluster definition
// must begin with { and must have } somewhere
//
bool lineIsParamSet( wstring& line )
{
    wstring trimmed = trim( line );

    if( trimmed.empty() )
    {
        return( false );
    }

    if( trimmed[ 0 ] != SET_BEGIN )
    {
        return( false );
    }
    
    size_t setend = trimmed.find( SET_END );
    if( wstring::npos == setend )
    {
        return( false );
    }
    
    return( true );
}

//
// reads one line from a file
//
bool readLineFromFile( wifstream& file, wstring& line )
{
    line = L"";
    if( file.eof() )
        return( false );

    wchar_t c;
    while( true )
    {
        file.get( c );
        if( file.eof()
         || c == L'\n'
         || c == 0 ) return( true );
        line += c;
    }
    return( true );
}

//
// read one parameter, these are in the following format:
// param [@ N] : val1, ~val2, val3a | val3b, val4
//
bool CModelData::readParameter( wstring& line )
{
    CModelParameter parameter;

    // param name can be separated by : or ,
    wstring::size_type paramSep = line.find( PARAMNAME_SEP );
    if( paramSep == wstring::npos )
    {
        paramSep = line.find( ValuesDelim );
        if( paramSep == wstring::npos )
        {
            PrintMessage( InputDataError, L"Parameter", (wchar_t*) line.c_str(), L"should have at least one value defined" );
            return( false );
        }
    }

    wstring name = trim( line.substr( 0, paramSep ));
    
    unsigned int order = UNDEFINED_ORDER;
    
    //check if this param has custom-order defined
    wstrings nameAndOrder;
    split( name, PARAM_ORDER, nameAndOrder );

    double d;
    if( nameAndOrder.size() == 2 && stringToNumber( nameAndOrder[ 1 ], d ))
    {
        name  = trim( nameAndOrder[ 0 ]);
        if( d > 0 )
        {
            order = static_cast< unsigned int >( d );
        }
    }

    parameter.Name  = name;
    parameter.Order = order;

    if ( ! parameter.Name.empty() && parameter.Name[ 0 ] == RESULT_PARAM_PREFIX )
    {
        parameter.IsResultParameter = true;
    }

    // now get the values
    wstring rawValues = line.substr( paramSep + 1, line.length() - paramSep - 1 );

    wstrings values;
    split( rawValues, ValuesDelim, values );

    for( wstrings::iterator i_val = values.begin(); i_val != values.end(); i_val++ )
    {
        *i_val = trim( *i_val );

        //
        // if it is in a form <text> it is a reference to another parameter
        // find an existing parameter and add all its values here instead
        //
        vector< CModelParameter >::iterator refParam;
        if ( ! i_val->empty() 
          && *(i_val->begin())  == PARAM_REF_BEGIN
          && *(i_val->rbegin()) == PARAM_REF_END
          &&( refParam = FindParameterByName( static_cast<wstring&> (i_val->substr( 1, i_val->length() - 2 )))) !=
                         Parameters.end() )
        {
            __push_back( parameter.Values, refParam->Values.begin(), refParam->Values.end() );
        }
        else
        {
            //
            // value weight
            // Param: Val1 (3), Val21|Val22 (2), Val3
            //
            int weight = 1;
            
            size_t weightBegin = i_val->find_last_of( WEIGHT_BEGIN );
            size_t weightEnd   = i_val->find_last_of( WEIGHT_END );
            
            // '(' must exist, ')' must be the last character
            if ( weightBegin != -1 && weightEnd == i_val->length() - 1 ) 
            {
                wstring weightStr = trim( static_cast<wstring&> (i_val->substr( weightBegin + 1, weightEnd - weightBegin - 1 )));
                double weightDbl = 0;

                // anything after @ must be a positive integer
                if ( stringToNumber( weightStr, weightDbl ) && ( static_cast< unsigned int > (weightDbl) ) > 0 )
                {
                    weight = static_cast< unsigned int > (weightDbl);

                    // trim the weight off the value
                    i_val->erase( weightBegin, wstring::npos );
                    *i_val = trim( *i_val );
                }
            }

            //
            // names
            //
            wstrings names;
            split( *i_val, NamesDelim, names );

            bool positive = true;
            for ( wstrings::iterator i_name = names.begin(); i_name != names.end(); i_name++ )
            {
                *i_name = trim( *i_name );
                
                // only the first name determines the negativity of a value
                if ( i_name->length() > 0
                 &&  i_name == names.begin()
                 &&(*i_name)[ 0 ] == InvalidPrefix )
                {
                    positive = false;
                    *i_name = trim( static_cast<wstring&> (i_name->substr( 1, i_name->length() - 1 )));
                }
            }

            if ( ! positive ) 
            {
                m_hasNegativeValues = true;
            }
            CModelValue value( names, weight, positive );
            parameter.Values.push_back( value );
        }
    }

    Parameters.push_back( parameter );
    return( true );
}

//
//
//
void CModelData::getUnmatchedParameterNames( wstrings& paramsOfSubmodel, wstrings& unmatchedParams )
{
    for( auto & cparam : paramsOfSubmodel )
    {
        bool found = false;
        for( auto & param : Parameters )
        {
            if ( 0 == stringCompare( cparam, param.Name, CaseSensitive ))
            {
                found = true;
                break;
            }
        }
        if ( ! found )
        {
            unmatchedParams.push_back( cparam );
        }
    }
}

//
//
//
bool CModelData::readParamSet( wstring& line )
{
    const wstring STD_MSG = L"Submodel definition is incorrect: " + line;

    wstringstream ist( line );

    // it's always in a form of { paramName1 @ N, paramName2 @ N, ... } @ N but "@ N" is optional

    wstring s;
    ist >> s;

    wstring::iterator next = line.begin();

    // {
    wstring::iterator begin = findFirstNonWhitespace( next, line.end() );
    if( begin == line.end() || *begin != SET_BEGIN )
    {
        PrintMessage( InputDataError, (wchar_t*) STD_MSG.data() );
        return( false );
    }
    ++begin;

    // find }
    wstring::iterator end;
    end = find( begin, line.end(), SET_END );
    if ( end == line.end() )
    {
        PrintMessage( InputDataError, (wchar_t*) STD_MSG.data() );
        return( false );
    }

    // params in the middle
    wstring setp;
    setp.assign( begin, end );
    setp = trim( setp );
    if ( setp.empty() )
    {
        PrintMessage( InputDataError, (wchar_t*) STD_MSG.data() );
        return( false );
    }

    //
    // Two attempts to resolve submodel names:
    // 1. Use a comma as a separator
    // 2. If 1 fails to produce matching names, use ModelData.ValuesDelim as a separator
    //

    // first figure out whether "," or a delimiter specified by /d option applies
    wstrings setParams;
    
    split( setp, SET_SEP, setParams );
    transform( setParams.begin(), setParams.end(), setParams.begin(), trim );

    wstrings unmatched;
    getUnmatchedParameterNames( setParams, unmatched );

    if( !unmatched.empty() )
    {
        setParams.clear();
        unmatched.clear();
        split( setp, ValuesDelim, setParams );
        transform( setParams.begin(), setParams.end(), setParams.begin(), trim );

        getUnmatchedParameterNames( setParams, unmatched );
        if( !unmatched.empty() )
        {
            PrintMessage( InputDataWarning, L"Submodel defintion", (wchar_t*) trim( line ).data(), L"contains unknown parameter. Skipping..." );
            return( true ); // just a warning so don't exit
        }
    }

    // remove duplicates
    sort( setParams.begin(), setParams.end(), stringCaseInsensitiveLess );
    wstrings::iterator newEnd = unique( setParams.begin(), setParams.end(), stringCaseInsensitiveEquals );
    if( setParams.end() != newEnd )
    {
        PrintMessage( InputDataWarning, L"Submodel defintion", (wchar_t*) trim( line ).data(), L"contains duplicate parameters. Removing duplicates..." );
        setParams.erase( newEnd, setParams.end() );
    }

    CModelSubmodel submodel;

    // match to names, set up the structure
    for( auto & cparam : setParams )
    {
        bool found = false;
        unsigned int index = 0;
        for( auto & param : Parameters )
        {
            if ( 0 == stringCompare( cparam, param.Name, CaseSensitive ))
            {
                found = true;
                break;
            }
            ++index;
        }
        // at this point we should always match the name
        assert( found );

        submodel.Parameters.push_back( index );
    }

    // @
    ++end;
    wstring::iterator at = findFirstNonWhitespace( end, line.end() );
    
    // anything other than @, quit
    if ( at != line.end() && *at != SET_ORDER )
    {
        PrintMessage( InputDataError, (wchar_t*) STD_MSG.data() );
        return( false );
    }

    if (  at == line.end() )
    {
        // if this is the end then order will be assigned later
        NOOP
    }
    else
    {
        ++at;

        // number
        wstring numberText;
        numberText.assign( at, line.end() );

        double number;
        bool ret = stringToNumber( numberText, number );
        
        int order = 0;
        if( ret )
        {
            order = static_cast<int> (number);
            if( order <= 0 )
            {
                order = 0;
                ret = false;
            }
        }
        if ( !ret )
        {
            PrintMessage( InputDataError, (wchar_t*) STD_MSG.data() );
            return( false );
        }

        submodel.Order = order;
    }

    Submodels.push_back( submodel );
    return ( true );
}

//
//
//
wifstream CModelData::openFile( wstring& filePath )
{
    // change name to ANSI
    string ansiFileName;
    ansiFileName.reserve( filePath.size() );
    for( auto c : filePath )
    {
        ansiFileName += static_cast< char > ( c );
    }

    // open file into input stream
    wifstream file( ansiFileName.c_str() );
    if( !file )
    {
        PrintMessage( InputDataError, L"Couldn't open file:", (wchar_t*) filePath.data() );
    }

    return( file );
}

//
//
//
bool CModelData::readModel( wstring& filePath )
{
    wifstream file = openFile( filePath );
    if( ! file ) return( false );

    wstring line;

    // read definition of parameters
    bool firstLine = true;
    while( true )
    {
        // skip not important stuff
        if ( lineIsEmpty( line ) || lineIsComment( line ))
        {
            if ( ! readLineFromFile( file, line )) return( true );
            continue;
        }

        if ( firstLine )
        {
            m_encoding = getEncodingType( line );
            if ( m_encoding != ANSI && m_encoding != UTF8 )
            {
                PrintMessage( InputDataError, L"Only ANSI and UTF-8 are supported" );
                return( false );
            }
            firstLine = false;
        }

        // continue reading until a submodel/cluster or a constraint
        if ( lineIsParamSet( line ) || lineIsConstraint( line )) break;

        if ( ! readParameter( line ))          return( false );
        if ( ! readLineFromFile( file, line )) return( true );
    }

    // read submodels
    if ( lineIsParamSet( line ))
    {
        while( true )
        {
            // skip not important stuff
            if ( lineIsEmpty( line ) || lineIsComment( line ))
            {
                if ( ! readLineFromFile( file, line )) return( true );
                continue;
            }

            // continue reading until a constraint
            if ( lineIsConstraint( line )) break;

            if ( ! readParamSet( line ))           return( false );
            if ( ! readLineFromFile( file, line )) return( true );
        }
    }

    // anything that's left is constraints
    while( true )
    {
        // if only a line is not empty or not a comment,
        //   it's got to be a part of constraints definition
        if ( ! ( lineIsEmpty( line ) || lineIsComment( line )))
        {
            ConstraintPredicates += line;
        }  
        if ( ! readLineFromFile( file, line )) return( true );
    }

    return( true );
}

//
// reads model file
//
bool CModelData::ReadModel( wstring& filePath )
{
    if( !readModel( filePath ))
    {
        return( false );
    }

    if( !ValidateParams() )
    {
        return( false );
    }

    return( true );
}

//
//
//
bool CModelData::ReadRowSeedFile( wstring& filePath )
{
    if( trim( filePath ).empty() ) return( true );

    wifstream file = openFile( filePath );
    if( !file ) return( false );

    wstring line;

    // parameter names

    bool fileEmpty = false;
    if ( readLineFromFile( file, line ))
    {
        if ( trim( line ).empty() ) fileEmpty = true;
    }
    else
    {
        fileEmpty = true;
    }

    if ( fileEmpty )
    {
        PrintMessage( RowSeedsWarning, L"Seeding file is empty" ); 
        return( true );
    }

    EncodingType encoding = getEncodingType( line );
    if ( encoding != ANSI && encoding != UTF8 )
    {
        PrintMessage( RowSeedsError, L"Only ANSI and UTF-8 are supported" );
        return( false );
    }

    vector< vector<CModelParameter>::iterator > parameters;

    wstrings params;
    split( line, RESULT_DELIMITER, params );
    for( auto & param : params )
    {
        vector<CModelParameter>::iterator found = FindParameterByName( param );
        if ( found == Parameters.end())
        {
            PrintMessage( RowSeedsWarning, L"Parameter", 
                                           (wchar_t*) param.data(),
                                           L"not found in the model. Skipping..." );
        }
        parameters.push_back( found );
    }

    // if any parameter equals to ModelData.Parameters.end()
    // this parameter could not be found in the model

    while( readLineFromFile( file, line ))
    {
        if ( trim(line).empty() ) break;

        wstrings values;
        split( line, RESULT_DELIMITER, values );

        unsigned int n_param = 0;
        CModelRowSeed rowSeed;
        for ( wstrings::iterator i_value  = values.begin(); 
                                 i_value != values.end(); 
                               ++i_value, ++n_param )
        {
            // There could be fewer parameter names (in the first line)
            // than there is values in the following lines. This has
            // to be detected and a warning issued
            if ( n_param < (unsigned int) parameters.size() && parameters[ n_param ] != Parameters.end() )
            {
                CModelParameter &param = *(parameters[ n_param ]);
                
                // remove the negative marker and match up the raw name
                if ( i_value->length() > 0  && (*i_value)[ 0 ] == InvalidPrefix )
                {
                    *i_value = trim( static_cast<wstring&> (i_value->substr( 1, i_value->length() - 1 )));
                }

                // if any value could not be found, the whole seed row is not invalid
                // we just remove that one offending value and the rest of the row can
                // stay intact; we cannot really warn about this as in a model with
                // submodels this is very normal
                int found = param.GetValueOrdinal( *i_value, CaseSensitive );
                if ( found == -1 )
                {
                    if ( ! i_value->empty() )
                    {
                        PrintMessage( RowSeedsWarning, L"Value", 
                                                    (wchar_t*) i_value->data(),
                                                    L"not found in the model. Skipping this value..." );
                    }
                }
                else
                {
                    // we don't care about result parameters as we should not seed we expected results
                    if ( ! param.IsResultParameter )
                    {
                        rowSeed.push_back( make_pair( param.Name, *i_value ));
                    }
                }
            }
        }
        if ( ! rowSeed.empty() )
        {
            RowSeeds.push_back( rowSeed );
        }
    }

    if( ! ValidateRowSeeds())
    {
        return( false );
    }

    return( true );
}