GEGELATI
Loading...
Searching...
No Matches
tpgGraphDotImporter.h
1
39#ifndef TPG_GRAPH_DOT_IMPORTER_H
40#define TPG_GRAPH_DOT_IMPORTER_H
41
42#include <cstdio>
43#include <fstream>
44#include <inttypes.h>
45#include <map>
46#include <memory>
47#include <regex>
48#include <stdexcept>
49#include <string>
50
51#include "learn/learningEnvironment.h"
52#include "tpg/tpgAction.h"
53#include "tpg/tpgEdge.h"
54#include "tpg/tpgGraph.h"
55#include "tpg/tpgTeam.h"
56#include "tpg/tpgVertex.h"
57#include "util/counterReset.h"
58
59namespace File {
65 {
66 protected:
70 std::ifstream pFile;
71
77 std::string lastLine;
78
83
88
96 std::map<uint64_t, const TPG::TPGVertex*> vertexID;
97
102 std::map<const TPG::TPGAction*, std::vector<uint64_t>> actionClasses;
103
111 std::map<uint64_t, std::shared_ptr<Program::Program>> programID;
112
120 std::map<uint64_t, const TPG::TPGAction*> actionID;
121
126 static const std::string lineSeparator;
127
147 static const std::string teamRegex;
148
168 static const std::string programRegex;
169
190 static const std::string instructionRegex;
191
212 static const std::string actionRegex;
213
234 static const std::string linkProgramInstructionRegex;
235
263 static const std::string linkProgramActionRegex;
264
291 static const std::string linkProgramTeamRegex;
292
315 static const std::string linkActionProgramRegex;
316
337 static const std::string addLinkProgramRegex;
338
346 void readOperands(std::string& str, Program::Line& line);
347
352 void readLine(std::smatch& matches);
353
357 void readProgram(std::smatch& matches);
358
368 bool getExportedVersion(int& major, int& minor, int& patch);
369
376 void dumpTPGGraphHeader();
377
381 void readTeam(std::smatch& matches);
382
386 void readAction(std::smatch& matches);
387
391 void readLinkTeamProgramAction(std::smatch& matches);
392
396 void readLinkActionProgram(std::smatch& matches);
397
401 void readLinkTeamProgramTeam(std::smatch& matches);
402
407 void readLinkTeamProgram(std::smatch& matches);
408
415 bool readLineFromFile();
416
417 public:
419 static const int supportedMajorVersion = 2;
421 static const int supportedMinorVersion = 0;
423 static const int supportedPatchVersion = 0;
424
437 TPGGraphDotImporter(const char* filePath, Environment environment,
438 TPG::TPGGraph& tpgref)
439 : env{environment}, tpg{tpgref}
440 {
441 pFile.open(filePath);
442 if (!pFile.is_open()) {
443 throw std::runtime_error("Could not open file " +
444 std::string(filePath));
445 }
446 importGraph();
447 };
448
453 static const unsigned int MAX_READ_SIZE = 131072;
454
461 {
462 if (pFile.is_open()) {
463 pFile.close();
464 }
465 }
466
475 void setNewFilePath(const char* newFilePath);
476
480 void importGraph();
481 };
482}; // namespace File
483
484#endif
The Environment class contains all information needed to execute a Program.
Definition environment.h:86
Class used to import a TPG graph from a dot file. It should be able to import a whole Learning agent ...
Definition tpgGraphDotImporter.h:65
void readLinkTeamProgram(std::smatch &matches)
reads a link declaration and creates a team to program's destination edge.
Definition tpgGraphDotImporter.cpp:367
void importGraph()
Creates a TPG Graph from its description in a .dot file.
Definition tpgGraphDotImporter.cpp:402
void readLinkTeamProgramTeam(std::smatch &matches)
reads a link declaration and creates a team to team edge
Definition tpgGraphDotImporter.cpp:341
TPGGraphDotImporter(const char *filePath, Environment environment, TPG::TPGGraph &tpgref)
Constructor for the importer.
Definition tpgGraphDotImporter.h:437
std::map< uint64_t, const TPG::TPGAction * > actionID
Map associating pointers to TPGVertex representing actions to the corresponding action.
Definition tpgGraphDotImporter.h:120
void readAction(std::smatch &matches)
reads and creates a TPGAction.
Definition tpgGraphDotImporter.cpp:247
static const std::string linkActionProgramRegex
contains the regex to identify a Team -> Program -> Action Link
Definition tpgGraphDotImporter.h:315
bool getExportedVersion(int &major, int &minor, int &patch)
reads the version of the dot file and returns it in the parameters.
Definition tpgGraphDotImporter.cpp:198
void readOperands(std::string &str, Program::Line &line)
Reads the content of the operands and puts it in the line passed in parameter.
Definition tpgGraphDotImporter.cpp:60
static const std::string lineSeparator
string used to spot the end of a line in the program description.
Definition tpgGraphDotImporter.h:126
TPG::TPGGraph & tpg
TPGGraph imported from dot file.
Definition tpgGraphDotImporter.h:87
std::ifstream pFile
File in which the dot content is read during import.
Definition tpgGraphDotImporter.h:70
~TPGGraphDotImporter()
Definition tpgGraphDotImporter.h:460
void readTeam(std::smatch &matches)
reads and creates a TPGTeam.
Definition tpgGraphDotImporter.cpp:236
std::string lastLine
last Line read from file
Definition tpgGraphDotImporter.h:77
void readLinkActionProgram(std::smatch &matches)
reads a link declaration and creates a action edge
Definition tpgGraphDotImporter.cpp:292
void readProgram(std::smatch &matches)
Create a program from its dot content and import its constants.
Definition tpgGraphDotImporter.cpp:155
static const int supportedPatchVersion
The patch version supported by the importer.
Definition tpgGraphDotImporter.h:423
static const int supportedMajorVersion
The major version supported by the importer.
Definition tpgGraphDotImporter.h:419
static const std::string linkProgramInstructionRegex
contains the regex to identify a Program -> Instruction Link
Definition tpgGraphDotImporter.h:234
static const std::string addLinkProgramRegex
contains the regex to identify a Team -> Program Link the outgoing program vertex must already have b...
Definition tpgGraphDotImporter.h:337
static const std::string teamRegex
Contains the regex to identify a team declaration.
Definition tpgGraphDotImporter.h:147
static const std::string instructionRegex
contains the regex to identify an instruction declaration
Definition tpgGraphDotImporter.h:190
static const std::string linkProgramTeamRegex
contains the regex to identify a Team -> Program -> Team Link
Definition tpgGraphDotImporter.h:291
Environment env
The environment in which the TPGGRAPH will be built.
Definition tpgGraphDotImporter.h:82
void readLine(std::smatch &matches)
Reads the content of a line and puts it in the line passed in parameter.
Definition tpgGraphDotImporter.cpp:88
void dumpTPGGraphHeader()
dumps the header of the dot file
Definition tpgGraphDotImporter.cpp:219
std::map< uint64_t, std::shared_ptr< Program::Program > > programID
Map associating pointers to Program to an integer ID.
Definition tpgGraphDotImporter.h:111
std::map< uint64_t, const TPG::TPGVertex * > vertexID
Map associating pointers to TPGVertex to an integer ID.
Definition tpgGraphDotImporter.h:96
static const std::string actionRegex
contains the regex to identify an action declaration
Definition tpgGraphDotImporter.h:212
void readLinkTeamProgramAction(std::smatch &matches)
reads a link declaration and creates a team to action edge
Definition tpgGraphDotImporter.cpp:316
bool readLineFromFile()
Definition tpgGraphDotImporter.cpp:453
static const std::string linkProgramActionRegex
contains the regex to identify a Team -> Program -> Action Link
Definition tpgGraphDotImporter.h:263
static const int supportedMinorVersion
The minor version supported by the importer.
Definition tpgGraphDotImporter.h:421
std::map< const TPG::TPGAction *, std::vector< uint64_t > > actionClasses
Map associating pointers to TPGAction to a vector of integer action class.
Definition tpgGraphDotImporter.h:102
static const std::string programRegex
Contains the regex to identify a program declaration.
Definition tpgGraphDotImporter.h:168
void setNewFilePath(const char *newFilePath)
Set a new file for the importer.
Definition tpgGraphDotImporter.cpp:513
static const unsigned int MAX_READ_SIZE
Maximum number of characters that can be read in a single line.
Definition tpgGraphDotImporter.h:453
Definition line.h:49
Class for storing a Tangled-Program-Graph.
Definition tpgGraph.h:58
Definition parametersParser.h:51