forked from jacyara/GenESyS-Reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create.cpp
105 lines (89 loc) · 4.38 KB
/
Create.cpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Create.cpp
* Author: cancian
*
* Created on 21 de Junho de 2018, 20:12
*/
#include "Create.h"
#include "Model.h"
#include "EntityType.h"
//#include <typeinfo>
Create::Create(Model* model) : SourceModelComponent(model) {
_name = "Create " + std::to_string(Util::GenerateNewIdOfType<Create>());
}
Create::Create(const Create& orig) : SourceModelComponent(orig) {
}
Create::~Create() {
}
std::string Create::show() {
return SourceModelComponent::show();
}
void Create::_execute(Entity* entity) {
/*
std::map<std::string, AttributeValue*>::iterator it = entity->getAttributeValues()->find('Entity.ArrivalTime');
if (it != entity->getAttributeValues()::end()) {
(*it)->second->setValue(this->_model->getSimulationTime());
}
*/
double tnow = _model->getSimulatedTime();
entity->setAttributeValue("Entity.ArrivalTime", tnow); // ->find("Entity.ArrivalTime")->second->setValue(tnow);
double timeBetweenCreations, timeScale, newArrivalTime;
for (unsigned int i = 0; i<this->_entitiesPerCreation; i++) {
this->_entitiesCreatedSoFar++;
Entity* newEntity = new Entity();
_model->getEntities()->insert(newEntity);
timeBetweenCreations = _model->parseExpression(this->_timeBetweenCreationsExpression);
timeScale = Util::TimeUnitConvert(this->_timeBetweenCreationsTimeUnit, _model->getReplicationLengthTimeUnit());
newArrivalTime = tnow + timeBetweenCreations*timeScale;
Event* newEvent = new Event(newArrivalTime, newEntity, this);
_model->getEvents()->insert(newEvent);
_model->traceSimulation(Util::TraceLevel::TL_blockInternal, tnow, entity, this, "Arrival of entity " + std::to_string(newEntity->getId()) + " schedule for time " + std::to_string(newArrivalTime));
//_model->trace(Util::TraceLevel::TL_blockInternal, "Arrival of entity "+std::to_string(entity->getId()) + " schedule for time " +std::to_string(newArrivalTime));
}
_model->sendEntityToComponent(entity, this->getNextComponents()->first(), 0.0);
}
void Create::_loadInstance(std::list<std::string> words) {
// the first word (CREATE) have to be deleted before. It begins on the second word
std::list<std::string>::iterator it = words.begin();
this->_entitiesPerCreation = std::stoi((*it++));
this->_firstCreation = std::stoi((*it++));
this->_timeBetweenCreationsExpression = (*it++);
this->_timeBetweenCreationsTimeUnit = std::stoi((*it++)); // bad enum convertion!
this->_maxCreations = std::stoi((*it++));
this->_entityType = (*it++);
this->_collectStatistics = std::stoi(*it++);
}
std::list<std::string>* Create::_saveInstance() {
std::list<std::string>* words = new std::list<std::string>();
words->insert(words->end(), Util::TypeOf<Create>());
words->insert(words->end(), std::to_string(this->_entitiesPerCreation));
words->insert(words->end(), std::to_string(this->_firstCreation));
words->insert(words->end(), (this->_timeBetweenCreationsExpression));
words->insert(words->end(), std::to_string(this->_timeBetweenCreationsTimeUnit));
words->insert(words->end(), std::to_string(this->_maxCreations));
words->insert(words->end(), (this->_entityType));
words->insert(words->end(), std::to_string(this->_collectStatistics));
return words;
}
bool Create::_verifySymbols(std::string* errorMessage) {
//Genesys.AuxFunctions.VerifySymbol(thismodule.Name, 'Time Between Creations', thisModule.aTimeBetweenCreations, cEXPRESSION, true);
// bool res = _model->verifySymbol(this->_name, "Time Between Creations", this->_timeBetweenCreationsExpression, "EXPRESSION", true); // Todo: typeid(Expression)
/*Checking Time Between Creations*/
bool result = true;
this->_model->parseExpression(this->_timeBetweenCreationsExpression, &result, errorMessage);
/*Checking Entity*/
// get the list of all EntityType from model infrastrucure and check if it exists
if (_model->getInfrastructure(Util::TypeOf<EntityType>(), this->_entityType) == nullptr) {
// the _entityType does not exists yet, so create it
EntityType* newEntityType = new EntityType(_model);
newEntityType->setName(this->_entityType);
// insert the new EntittyType into the infrastructure list
_model->getInfrastructures(Util::TypeOf<EntityType>())->insert(newEntityType);
}
return result;
}