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
59 namespace File {
64 class TPGGraphDotImporter
65 {
66 protected:
70 std::ifstream pFile;
71
77 std::string lastLine;
78
82 Environment env;
83
87 TPG::TPGGraph& tpg;
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
460 ~TPGGraphDotImporter()
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
Environment
The Environment class contains all information needed to execute a Program.
Definition environment.h:86
File::TPGGraphDotImporter
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
File::TPGGraphDotImporter::readLinkTeamProgram
void readLinkTeamProgram(std::smatch &matches)
reads a link declaration and creates a team to program's destination edge.
Definition tpgGraphDotImporter.cpp:367
File::TPGGraphDotImporter::importGraph
void importGraph()
Creates a TPG Graph from its description in a .dot file.
Definition tpgGraphDotImporter.cpp:402
File::TPGGraphDotImporter::readLinkTeamProgramTeam
void readLinkTeamProgramTeam(std::smatch &matches)
reads a link declaration and creates a team to team edge
Definition tpgGraphDotImporter.cpp:341
File::TPGGraphDotImporter::TPGGraphDotImporter
TPGGraphDotImporter(const char *filePath, Environment environment, TPG::TPGGraph &tpgref)
Constructor for the importer.
Definition tpgGraphDotImporter.h:437
File::TPGGraphDotImporter::actionID
std::map< uint64_t, const TPG::TPGAction * > actionID
Map associating pointers to TPGVertex representing actions to the corresponding action.
Definition tpgGraphDotImporter.h:120
File::TPGGraphDotImporter::readAction
void readAction(std::smatch &matches)
reads and creates a TPGAction.
Definition tpgGraphDotImporter.cpp:247
File::TPGGraphDotImporter::linkActionProgramRegex
static const std::string linkActionProgramRegex
contains the regex to identify a Team -> Program -> Action Link
Definition tpgGraphDotImporter.h:315
File::TPGGraphDotImporter::getExportedVersion
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
File::TPGGraphDotImporter::readOperands
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
File::TPGGraphDotImporter::lineSeparator
static const std::string lineSeparator
string used to spot the end of a line in the program description.
Definition tpgGraphDotImporter.h:126
File::TPGGraphDotImporter::tpg
TPG::TPGGraph & tpg
TPGGraph imported from dot file.
Definition tpgGraphDotImporter.h:87
File::TPGGraphDotImporter::pFile
std::ifstream pFile
File in which the dot content is read during import.
Definition tpgGraphDotImporter.h:70
File::TPGGraphDotImporter::~TPGGraphDotImporter
~TPGGraphDotImporter()
Definition tpgGraphDotImporter.h:460
File::TPGGraphDotImporter::readTeam
void readTeam(std::smatch &matches)
reads and creates a TPGTeam.
Definition tpgGraphDotImporter.cpp:236
File::TPGGraphDotImporter::lastLine
std::string lastLine
last Line read from file
Definition tpgGraphDotImporter.h:77
File::TPGGraphDotImporter::readLinkActionProgram
void readLinkActionProgram(std::smatch &matches)
reads a link declaration and creates a action edge
Definition tpgGraphDotImporter.cpp:292
File::TPGGraphDotImporter::readProgram
void readProgram(std::smatch &matches)
Create a program from its dot content and import its constants.
Definition tpgGraphDotImporter.cpp:155
File::TPGGraphDotImporter::supportedPatchVersion
static const int supportedPatchVersion
The patch version supported by the importer.
Definition tpgGraphDotImporter.h:423
File::TPGGraphDotImporter::supportedMajorVersion
static const int supportedMajorVersion
The major version supported by the importer.
Definition tpgGraphDotImporter.h:419
File::TPGGraphDotImporter::linkProgramInstructionRegex
static const std::string linkProgramInstructionRegex
contains the regex to identify a Program -> Instruction Link
Definition tpgGraphDotImporter.h:234
File::TPGGraphDotImporter::addLinkProgramRegex
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
File::TPGGraphDotImporter::teamRegex
static const std::string teamRegex
Contains the regex to identify a team declaration.
Definition tpgGraphDotImporter.h:147
File::TPGGraphDotImporter::instructionRegex
static const std::string instructionRegex
contains the regex to identify an instruction declaration
Definition tpgGraphDotImporter.h:190
File::TPGGraphDotImporter::linkProgramTeamRegex
static const std::string linkProgramTeamRegex
contains the regex to identify a Team -> Program -> Team Link
Definition tpgGraphDotImporter.h:291
File::TPGGraphDotImporter::env
Environment env
The environment in which the TPGGRAPH will be built.
Definition tpgGraphDotImporter.h:82
File::TPGGraphDotImporter::readLine
void readLine(std::smatch &matches)
Reads the content of a line and puts it in the line passed in parameter.
Definition tpgGraphDotImporter.cpp:88
File::TPGGraphDotImporter::dumpTPGGraphHeader
void dumpTPGGraphHeader()
dumps the header of the dot file
Definition tpgGraphDotImporter.cpp:219
File::TPGGraphDotImporter::programID
std::map< uint64_t, std::shared_ptr< Program::Program > > programID
Map associating pointers to Program to an integer ID.
Definition tpgGraphDotImporter.h:111
File::TPGGraphDotImporter::vertexID
std::map< uint64_t, const TPG::TPGVertex * > vertexID
Map associating pointers to TPGVertex to an integer ID.
Definition tpgGraphDotImporter.h:96
File::TPGGraphDotImporter::actionRegex
static const std::string actionRegex
contains the regex to identify an action declaration
Definition tpgGraphDotImporter.h:212
File::TPGGraphDotImporter::readLinkTeamProgramAction
void readLinkTeamProgramAction(std::smatch &matches)
reads a link declaration and creates a team to action edge
Definition tpgGraphDotImporter.cpp:316
File::TPGGraphDotImporter::readLineFromFile
bool readLineFromFile()
Definition tpgGraphDotImporter.cpp:453
File::TPGGraphDotImporter::linkProgramActionRegex
static const std::string linkProgramActionRegex
contains the regex to identify a Team -> Program -> Action Link
Definition tpgGraphDotImporter.h:263
File::TPGGraphDotImporter::supportedMinorVersion
static const int supportedMinorVersion
The minor version supported by the importer.
Definition tpgGraphDotImporter.h:421
File::TPGGraphDotImporter::actionClasses
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
File::TPGGraphDotImporter::programRegex
static const std::string programRegex
Contains the regex to identify a program declaration.
Definition tpgGraphDotImporter.h:168
File::TPGGraphDotImporter::setNewFilePath
void setNewFilePath(const char *newFilePath)
Set a new file for the importer.
Definition tpgGraphDotImporter.cpp:513
File::TPGGraphDotImporter::MAX_READ_SIZE
static const unsigned int MAX_READ_SIZE
Maximum number of characters that can be read in a single line.
Definition tpgGraphDotImporter.h:453
Program::Line
Definition line.h:49
TPG::TPGGraph
Class for storing a Tangled-Program-Graph.
Definition tpgGraph.h:58
File
Definition parametersParser.h:51