GEGELATI
Loading...
Searching...
No Matches
programEngine.h
1
39#ifndef PROGRAMENGINE_H
40#define PROGRAMENGINE_H
41
42#include "data/primitiveTypeArray.h"
43#include "data/untypedSharedPtr.h"
44#include "program/program.h"
45
46namespace Program {
55 {
56 protected:
60
62 ProgramEngine() = delete;
63
66 registers; // If the type of registers attribute is
67 // changed one day
68 // make sure to update the Program::identifyIntrons()
69 // method as it create its own
70 // Data::PrimitiveTypeArray<double> to keep track of
71 // accessed addresses.
72
74 std::vector<std::reference_wrapper<const Data::DataHandler>>
76
78 std::vector<std::reference_wrapper<const Data::DataHandler>>
80
83
84 protected:
94 : programCounter{0}, registers{env.getParams().nbRegisters},
96 {
97 // Setup the data sources
98 dataScsConstsAndRegs.push_back(this->registers);
99
100 if (env.getParams().nbProgramConstant > 0) {
101 dataScsConstsAndRegs.push_back(env.getFakeDataSources().at(1));
102 }
103
104 // Cannot use insert here because it dataSourcesAndRegisters
105 // requires constnessand dataSrc data are not const...
106 for (auto data : env.getDataSources()) {
107 dataScsConstsAndRegs.push_back(data.get());
108 }
109 }
110
126 template <class T>
128 const std::vector<std::reference_wrapper<T>>& dataSrc)
129 : programCounter{0},
130 registers{prog.getEnvironment().getParams().nbRegisters},
131 program{NULL}
132 {
133 // Check that T is either convertible to a const DataHandler
134 static_assert(
135 std::is_convertible<T&, const Data::DataHandler&>::value);
136 // Setup the data sources
137 this->dataScsConstsAndRegs.push_back(this->registers);
138
139 if (prog.getEnvironment().getParams().nbProgramConstant > 0) {
140 this->dataScsConstsAndRegs.push_back(
141 prog.cGetConstantHandler());
142 }
143
144 // Cannot use insert here because it dataSourcesAndRegisters
145 // requires constnessand dataSrc data are not const...
146 for (std::reference_wrapper<T> data : dataSrc) {
147 this->dataScsConstsAndRegs.push_back(data.get());
148 this->dataSources.push_back(data.get());
149 }
150
151 // Set the Program
152 this->setProgram(prog);
153 };
154
165 : ProgramEngine(prog, prog.getEnvironment().getDataSources()){};
166
171 virtual void processLine() = 0;
172
173 public:
183 void setProgram(const Program& prog);
184
194 template <class T>
195 void setDataSources(
196 const std::vector<std::reference_wrapper<T>>& dataSrc);
197
204 const std::vector<std::reference_wrapper<const Data::DataHandler>>&
205 getDataSources() const;
206
217 const bool next();
218
228 const Line& getCurrentLine() const;
229
242
258 const void fetchCurrentOperands(
259 std::vector<Data::UntypedSharedPtr>& operands) const;
260
276 uint64_t getOperandLocation(uint64_t idxOp) const;
277
286 virtual void iterateThroughtProgram(const bool ignoreException);
287
296 virtual std::vector<double> getRegisterValues(uint64_t nbRegisters);
297 };
298
299 template <class T>
301 const std::vector<std::reference_wrapper<T>>& dataSrc)
302 {
303 // Check that T is either convertible to a const DataHandler
304 static_assert(std::is_convertible<T&, const Data::DataHandler&>::value);
305
306 // Replace the references in attributes
307 this->dataSources = dataSrc;
308 // we need this offset to push the constant at the first
309 size_t offset =
311 ? 2
312 : 1;
313 if (offset == 2) {
314 this->dataScsConstsAndRegs.at(1) =
316 }
317 for (size_t idx = 0; idx < this->dataSources.size(); idx++) {
318 this->dataScsConstsAndRegs.at(idx + offset) = dataSrc.at(idx);
319 }
320
321 // Set program to check compatibility with new data source
322 this->setProgram(*this->program);
323 }
324} // namespace Program
325
326#endif // PROGRAMENGINE_H
DataHandler for 2D arrays of primitive types.
Definition primitiveTypeArray2D.h:69
The Environment class contains all information needed to execute a Program.
Definition environment.h:86
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the Environment.
Definition environment.cpp:204
const Learn::LearningParameters & getParams() const
Get the instance of parameters used.
Definition environment.cpp:168
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getFakeDataSources() const
Definition environment.cpp:210
This abstract class is the base class for any instruction to be used in a Program.
Definition instruction.h:61
Definition line.h:49
This abstract class is the base class for any program engine (generation and execution)
Definition programEngine.h:55
const Program * program
Definition programEngine.h:59
virtual void processLine()=0
operator parenthesis used when iterating through the program with the function iterationThroughtProgr...
ProgramEngine(const Environment &env)
Constructor of the class.
Definition programEngine.h:93
uint64_t programCounter
Program counter of the execution engine.
Definition programEngine.h:82
const void fetchCurrentOperands(std::vector< Data::UntypedSharedPtr > &operands) const
Get the operands for the current Instruction.
Definition programEngine.cpp:126
void setDataSources(const std::vector< std::reference_wrapper< T > > &dataSrc)
Method for changing the dataSources on which the Program will be executed.
Definition programEngine.h:300
const bool next()
Increments the programCounter and checks for the end of the Program.
Definition programEngine.cpp:98
const std::vector< std::reference_wrapper< const Data::DataHandler > > & getDataSources() const
Get the DataHandler of the ProgramExecutionEngine.
Definition programEngine.cpp:93
virtual void iterateThroughtProgram(const bool ignoreException)
Function that iterates through the lines of the program and execute the function processLine().
Definition programEngine.cpp:167
virtual std::vector< double > getRegisterValues(uint64_t nbRegisters)
Return the current registers value of the program indicated.
Definition programEngine.cpp:194
std::vector< std::reference_wrapper< const Data::DataHandler > > dataScsConstsAndRegs
Data sources (including registers) used in the Program.
Definition programEngine.h:79
uint64_t getOperandLocation(uint64_t idxOp) const
Get the location for the current Instruction.
Definition programEngine.cpp:150
void setProgram(const Program &prog)
Method for changing the Program executed by a ProgramExecutionEngin.
Definition programEngine.cpp:43
std::vector< std::reference_wrapper< const Data::DataHandler > > dataSources
Data sources from the environment used for archiving a program.
Definition programEngine.h:75
ProgramEngine()=delete
Default constructor is deleted.
const Line & getCurrentLine() const
Get the Program Line corresponding to the current programCounter.
Definition programEngine.cpp:109
Data::PrimitiveTypeArray< double > registers
Registers used for the Program execution.
Definition programEngine.h:66
const Instructions::Instruction & getCurrentInstruction() const
Get the Instruction corresponding to the current programCounter.
Definition programEngine.cpp:114
ProgramEngine(const Program &prog, const std::vector< std::reference_wrapper< T > > &dataSrc)
Constructor of the class.
Definition programEngine.h:127
ProgramEngine(const Program &prog)
Constructor of the class.
Definition programEngine.h:164
const Data::ConstantHandler & cGetConstantHandler() const
get a const reference to the constantHandler object of the Program
Definition program.cpp:236
const Environment & getEnvironment() const
Get the environment associated to the Program at construction.
Definition program.cpp:126
Definition line.h:44
size_t nbProgramConstant
Number of Constants available in a program.
Definition learningParameters.h:144