ParserEventPaje.cpp 21.5 KB
Newer Older
Mathieu Faverge's avatar
Mathieu Faverge committed
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
/*
** This file is part of the ViTE project.
**
** This software is governed by the CeCILL-A license under French law
** and abiding by the rules of distribution of free software. You can
** use, modify and/or redistribute the software under the terms of the
** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
** URL: "http://www.cecill.info".
** 
** As a counterpart to the access to the source code and rights to copy,
** modify and redistribute granted by the license, users are provided
** only with a limited warranty and the software's author, the holder of
** the economic rights, and the successive licensors have only limited
** liability.
** 
** In this respect, the user's attention is drawn to the risks associated
** with loading, using, modifying and/or developing or reproducing the
** software by the user in light of its specific status of free software,
** that may mean that it is complicated to manipulate, and that also
** therefore means that it is reserved for developers and experienced
** professionals having in-depth computer knowledge. Users are therefore
** encouraged to load and test the software's suitability as regards
** their requirements in conditions enabling the security of their
** systems and/or data to be ensured and, more generally, to use and
** operate it in the same conditions as regards security.
** 
** The fact that you are presently reading this means that you have had
** knowledge of the CeCILL-A license and that you accept its terms.
**
**
31
** ViTE developers are (for version 0.* to 1.0):
Mathieu Faverge's avatar
Mathieu Faverge committed
32 33 34 35 36 37 38 39 40 41 42
**
**        - COULOMB Kevin
**        - FAVERGE Mathieu
**        - JAZEIX Johnny
**        - LAGRASSE Olivier
**        - MARCOUEILLE Jule
**        - NOISETTE Pascal
**        - REDONDY Arthur
**        - VUCHENER Clément 
**
*/
43
#include <string>
44 45 46 47 48 49 50 51 52 53
#include <map>
#include <set>
#include <queue>
#include <list>
/* -- */
#include "common/Errors.hpp"
/* -- */
#include "trace/values/Values.hpp"
#include "trace/EntityValue.hpp"
#include "trace/EntityTypes.hpp"
54
#include "trace/Entitys.hpp"
55 56
#include "trace/Trace.hpp"
/* -- */
57 58 59 60
#include <fstream>
#include "parser/PajeFileManager.hpp" // temporary
#include "parser/PajeDefinition.hpp"
#include "parser/ParserDefinitionPaje.hpp"
61 62
#include "parser/ParserEventPaje.hpp"
/* -- */
63 64
using namespace std;

65 66
ParserEventPaje::ParserEventPaje(ParserDefinitionPaje *defs) {
    _Definitions = defs;
67 68
}

69 70 71 72
ParserEventPaje::~ParserEventPaje(){
    _containers.clear();
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
void ParserEventPaje::store_event(const PajeLine       *line, 
				  Trace                &trace) {

    string      fvalue;
    string      alias;
    string      name;
    String      container_type;
    String      source_container_type;
    String      dest_container_type;
    String      entity_type;
    Date        time;
    String      type;
    String      container;
    String      value_string;
    Double      value_double;
    String      source_container;
    String      dest_container;
    String      key;
91 92 93

    map<std::string, Value *> extra_fields;

94 95 96 97 98 99 100 101 102 103 104
    const vector<PajeFieldName> *FNames = _Definitions->get_FieldNames();
    vector< Field >       *fields;
    PajeDefinition        *def;
    int                    i, trid;
    int                    defsize;
    int                    idname, idtype;
    
    // We check if we have an event identifier
    if(sscanf(line->_tokens[0], "%u", &trid) != 1){
	Error::set(Error::_EXPECT_ID_DEF, line->_id, Error::_WARNING);
	return;
105
    }
106 107 108 109 110 111
    
    // We check if the trid is available
    def = _Definitions->getDefFromTrid(trid);
    if ( def == NULL ) {
        Error::set(Error::_UNKNOWN_EVENT_DEF, line->_id, Error::_ERROR);
        return;
112 113
    }

114 115
    fields  = &(def->_fields);
    defsize = fields->size();
116

117 118 119 120 121
    // We check if we have enough data for this event
    if ( defsize > (line->_nbtks - 1) ) {
	Error::set(Error::_LINE_TOO_SHORT_EVENT, line->_id, 
		   Error::_WARNING);
	return;
122
    }
123 124 125 126

    // Warning if we have extra data
    if ( defsize < (line->_nbtks - 1) ) {
        Error::set(Error::_EXTRA_TOKEN, line->_id, Error::_WARNING);
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
    // Dispatch the tokens in the good fields 
    for(i=0; i < defsize; i++) {

	fvalue = line->_tokens[i+1];
        idname = (*fields)[i]._idname;
        idtype = (*fields)[i]._idtype;

	
	// Store the fvalue in the correct field
	switch( idname ) {
	case _PajeFN_Alias : 
	    alias = fvalue;
	    break; 
	    
	case _PajeFN_Name : 
	    name  = fvalue;
	    break;
	    
	case _PajeFN_ContainerType :
	    container_type = fvalue;
	    break;

	case _PajeFN_SourceContainerType : 
	    source_container_type = fvalue;
	    break;

	case _PajeFN_DestContainerType : 
	    dest_container_type = fvalue;
	    break;
	    
	case _PajeFN_EntityType : 
	    entity_type = fvalue;
	    break;

	case _PajeFN_Time : 
	    time = fvalue;
	    if(!time.is_correct()) {
		Error::set(Error::_INCOMPATIBLE_VALUE_IN_EVENT + 
			   fvalue + " (expecting a \"date\")", 
			   line->_id, 
			   Error::_WARNING);
		return;
	    }
	    break;
		
	case _PajeFN_Type :
	    type = fvalue;
	    break;

	case _PajeFN_Container :
	    container = fvalue;
	    break;
	    
	case _PajeFN_Value :
	    if( idtype == _FieldType_Double ) {
		value_double = fvalue;
		
		if(!value_double.is_correct()) {
		    Error::set(Error::_INCOMPATIBLE_VALUE_IN_EVENT + fvalue + " (expecting a \"double\")", 
			       line->_id, Error::_WARNING);
		    return;
		}
	    }
	    else {
		value_string = fvalue;
	    }
	    break;

	case _PajeFN_SourceContainer :
	    source_container = fvalue;
	    break;
	    
	case _PajeFN_DestContainer :
	    dest_container = fvalue;
	    break;
	    
	case _PajeFN_Key :
	    key = fvalue;
	    break;
	    
	default :
	    Value *value = NULL;
	    switch( idtype ) {
	    case _FieldType_String : 
		value = new String(fvalue);
		break;
  		
	    case _FieldType_Double :
		value = new Double(fvalue);
		break;
		
	    case _FieldType_Hex :
		value = new Hex(fvalue);
		break;
  		
	    case _FieldType_Date :
		value = new Date(fvalue);
		break;
		
	    case _FieldType_Int :
		value = new Integer(fvalue);
		break;
		
	    case _FieldType_Color :
		value = new Color(fvalue);
		break;
		
	    default:
		Error::set(Error::_FIELD_TYPE_UNKNOWN, line->_id, Error::_WARNING);
		return;
	    }

// 	    if(!value->is_correct()) { // Check if the value is correct or not
// 		Error::set(Error::_INCOMPATIBLE_VALUE_IN_EVENT + fvalue + " (expecting a \"" + ftype + "\")", 
// 			   line->_id, Error::_WARNING);
// 		return;
// 	    }
	    
	    extra_fields[(*FNames)[idname]._name] = value;
	}
    }
250

251
    Name alias_name;
252

253 254
    if ( (alias != "") && (name == "" ) ){
	name = alias;
255
    }
256 257
    if ( (name != "") && (alias == "") ) {
	alias = name;
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
    alias_name.set_alias(alias);
    alias_name.set_name(name);

    switch( def->_id ) {
    case _PajeDefineContainerType : 
      {
	  ContainerType *temp_container_type = trace.search_container_type(container_type);
	  if( (temp_container_type == NULL) && (container_type.to_string() != "0") ){
	      Error::set(Error::_UNKNOWN_CONTAINER_TYPE + container_type.to_string(), line->_id, Error::_ERROR);
	  }
	  else{
	      trace.define_container_type(alias_name, temp_container_type, extra_fields);
	  }
      }
      break;
      
    case _PajeCreateContainer : 
      {
	  ContainerType *temp_container_type = trace.search_container_type(type);
	  Container *temp_container = trace.search_container(container);
	  if(temp_container_type == NULL && type.to_string() != "0"){
	      Error::set(Error::_UNKNOWN_CONTAINER_TYPE + type.to_string(), line->_id, Error::_ERROR);
	  }
	  else if(temp_container == NULL && container.to_string() != "0"){
	      Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
	  }
	  else{
	      trace.create_container(time, alias_name, temp_container_type, temp_container, extra_fields);
	      // We store the container in the map
	      _containers[alias_name.to_string()] = trace.search_container(alias_name.to_string());
	  }
      }
      break;
292
    
293 294
    case _PajeDestroyContainer : 
      {
295 296
        Container *temp_container = trace.search_container(alias_name.to_string());
        ContainerType *temp_container_type = trace.search_container_type(type);
297 298
        if(temp_container == NULL && alias_name.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + alias_name.to_string(), line->_id, Error::_ERROR);
299
        }
300 301
        else if(temp_container_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + type.to_string(), line->_id, Error::_ERROR);
302 303
        }
        else{
304
            trace.destroy_container(time, temp_container, temp_container_type, extra_fields);
305
        }
306 307
      }
      break;
308

309 310
    case _PajeDefineEventType : 
      {
311
        ContainerType *temp_container_type = trace.search_container_type(container_type);
312 313
        if(temp_container_type == NULL && container_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + container_type.to_string(), line->_id, Error::_ERROR);
314 315
        }
        else{
316
            trace.define_event_type(alias_name, temp_container_type, extra_fields);
317
        }
318 319
      }
      break;
320

321 322
    case _PajeDefineStateType : 
      {
323
        ContainerType *temp_container_type = trace.search_container_type(container_type);
324 325
        if(temp_container_type == NULL && container_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + container_type.to_string(), line->_id, Error::_ERROR);
326 327
        }
        else{
328
            trace.define_state_type(alias_name, temp_container_type, extra_fields);
329
        }
330 331
      }
      break;
332

333 334
    case _PajeDefineVariableType : 
      {
335
        ContainerType *temp_container_type = trace.search_container_type(container_type);
336 337
        if(temp_container_type == NULL && container_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + container_type.to_string(), line->_id, Error::_ERROR);
338 339
        }
        else{
340
            trace.define_variable_type(alias_name, temp_container_type, extra_fields);
341
        }
342 343
      }
      break;
344

345 346
    case _PajeDefineLinkType : 
      {
347 348 349
        ContainerType *temp_container_type = trace.search_container_type(container_type);
        ContainerType *temp_source_container_type = trace.search_container_type(source_container_type);
        ContainerType *temp_dest_container_type = trace.search_container_type(dest_container_type);
350 351
        if(temp_container_type == NULL && container_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + container_type.to_string(), line->_id, Error::_ERROR);
352
        }
353 354
        else if(temp_source_container_type == NULL && source_container_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + source_container_type.to_string(), line->_id, Error::_ERROR);
355
        }
356 357
        else if(temp_dest_container_type == NULL && dest_container_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER_TYPE + dest_container_type.to_string(), line->_id, Error::_ERROR);
358 359
        }
        else{
360
            trace.define_link_type(alias_name, temp_container_type, temp_source_container_type, temp_dest_container_type, extra_fields);
361
        }
362 363
      }
      break;
364

365 366
    case _PajeDefineEntityValue : 
      {
367
        EntityType *temp_entity_type = trace.search_entity_type(entity_type);
368 369
        if(temp_entity_type == NULL && entity_type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_ENTITY_TYPE + entity_type.to_string(), line->_id, Error::_ERROR);
370 371
        }
        else{
372
            trace.define_entity_value(alias_name, temp_entity_type, extra_fields);
373
        }
374 375
      }
      break;
376

377 378
    case _PajeSetState : 
      {
379
        StateType   *temp_state_type   = trace.search_state_type(type);
380
        EntityValue *temp_entity_value = trace.search_entity_value(value_string, temp_state_type);
381 382 383 384 385 386 387 388 389 390

        Container   *temp_container = NULL;
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }

391 392
        if(temp_state_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_STATE_TYPE + type.to_string(), line->_id, Error::_ERROR);
393
        }
394 395
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
396 397
        }
        else{
398
            trace.set_state(time, temp_state_type, temp_container, temp_entity_value, extra_fields);
399
        }
400 401
      }
      break;
402

403 404
    case _PajePushState : 
      {
405
        StateType   *temp_state_type = trace.search_state_type(type);
406
        EntityValue *temp_entity_value = trace.search_entity_value(value_string, temp_state_type);
407 408 409 410 411 412 413 414 415 416

        Container   *temp_container = NULL; 
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }

417 418
        if(temp_state_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_STATE_TYPE + type.to_string(), line->_id, Error::_ERROR);
419
        }
420 421
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
422 423
        }
        else{
424
            trace.push_state(time, temp_state_type, temp_container, temp_entity_value, extra_fields);
425
        }
426 427
      }
      break;
428

429 430
    case _PajePopState : 
      {
431
        StateType *temp_state_type = trace.search_state_type(type);
432 433 434 435 436 437 438 439 440
        Container *temp_container = NULL; 
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }

441 442
        if(temp_state_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_STATE_TYPE + type.to_string(), line->_id, Error::_ERROR);
443
        }
444 445
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
446 447
        }
        else{
448
            trace.pop_state(time, temp_state_type, temp_container, extra_fields);
449
        }
450 451
      }
      break;
452

453 454
    case _PajeNewEvent : 
      {
455
        EventType   *temp_event_type = trace.search_event_type(type);
456
        EntityValue *temp_entity_value = trace.search_entity_value(value_string, temp_event_type);
457 458 459 460 461 462 463 464 465 466

        Container   *temp_container = NULL; 
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }

467 468
        if(temp_event_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_EVENT_TYPE + type.to_string(), line->_id, Error::_ERROR);
469
        }
470 471
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
472 473
        }
        else{
474
            trace.new_event(time, temp_event_type, temp_container, temp_entity_value, extra_fields);
475
        }
476 477
      }
      break;
478

479 480
    case _PajeSetVariable : 
      {
481
        VariableType *temp_variable_type = trace.search_variable_type(type);
482 483 484 485 486 487 488 489 490 491

        Container    *temp_container = NULL;
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }
        
492 493
        if(temp_variable_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_VARIABLE_TYPE + type.to_string(), line->_id, Error::_ERROR);
494
        }
495 496
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
497 498
        }
        else{
499
            trace.set_variable(time, temp_variable_type, temp_container, value_double, extra_fields);
500
        }
501 502
      }
      break;
503

504 505
    case _PajeAddVariable : 
      {
506
        VariableType *temp_variable_type = trace.search_variable_type(type);
507 508 509 510 511 512 513 514 515
        Container    *temp_container = NULL;
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }

516 517
        if(temp_variable_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_VARIABLE_TYPE + type.to_string(), line->_id, Error::_ERROR);
518
        }
519 520
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
521 522
        }
        else{
523
            trace.add_variable(time, temp_variable_type, temp_container, value_double, extra_fields);
524
        }
525 526
      }
      break;
527

528 529
    case _PajeSubVariable : 
      {
530
        VariableType *temp_variable_type = trace.search_variable_type(type);
531 532 533 534 535 536 537 538 539
        Container    *temp_container = NULL;
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }

540 541
        if(temp_variable_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_VARIABLE_TYPE + type.to_string(), line->_id, Error::_ERROR);
542
        }
543 544
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
545 546
        }
        else{
547
            trace.sub_variable(time, temp_variable_type, temp_container, value_double, extra_fields);
548
        }
549 550
      }
      break;
551

552 553
    case _PajeStartLink : 
      {
554
        LinkType    *temp_link_type = trace.search_link_type(type);
555
        EntityValue *temp_entity_value = trace.search_entity_value(value_string, temp_link_type);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

        Container   *temp_container = NULL;
        Container   *temp_source_container = NULL;
        // temp_container
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }
        // temp_source_container
        if(_containers.find(source_container) != _containers.end()) {
            temp_source_container = _containers[source_container];
        }
        else {
            temp_source_container = trace.search_container(source_container);
            _containers[source_container] = temp_source_container;
        }


577 578
        if(temp_link_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_LINK_TYPE + type.to_string(), line->_id, Error::_ERROR);
579
        }
580 581
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
582
        }
583 584
        else if(temp_source_container == NULL && source_container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + source_container.to_string(), line->_id, Error::_ERROR);
585 586
        }
        else{
587
            trace.start_link(time, temp_link_type, temp_container, temp_source_container, temp_entity_value, key, extra_fields);
588
        }
589 590
      }
      break;
591

592 593
    case _PajeEndLink : 
      {
594
        LinkType    *temp_link_type = trace.search_link_type(type);
595
        EntityValue *temp_entity_value = trace.search_entity_value(value_string, temp_link_type);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

        Container   *temp_container = NULL;
        Container   *temp_dest_container = NULL;
        // temp_container
        if(_containers.find(container) != _containers.end()) {
            temp_container = _containers[container];
        }
        else {
            temp_container = trace.search_container(container);
            _containers[container] = temp_container;
        }
        // temp_dest_container
        if(_containers.find(dest_container) != _containers.end()) {
            temp_dest_container = _containers[dest_container];
        }
        else {
            temp_dest_container = trace.search_container(dest_container);
            _containers[dest_container] = temp_dest_container;
        }

616 617
        if(temp_link_type == NULL && type.to_string() != "0"){
            Error::set(Error::_UNKNOWN_LINK_TYPE + type.to_string(), line->_id, Error::_ERROR);
618
        }
619 620
        else if(temp_container == NULL && container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + container.to_string(), line->_id, Error::_ERROR);
621
        }
622 623
        else if(temp_dest_container == NULL && dest_container.to_string() != "0"){
            Error::set(Error::_UNKNOWN_CONTAINER + dest_container.to_string(), line->_id, Error::_ERROR);
624 625
        }
        else{
626
            trace.end_link(time, temp_link_type, temp_container, temp_dest_container, temp_entity_value, key, extra_fields);
627
        }
628 629
      }
      break;
630

631 632
    default:
        Error::set(Error::_UNKNOWN_EVENT_DEF + def->_name, line->_id, Error::_WARNING);
633 634 635
        return;
    }
}