GEGELATI
tpgGraphDotImporter.h
1
38#ifndef TPG_GRAPH_DOT_IMPORTER_H
39#define TPG_GRAPH_DOT_IMPORTER_H
40
41#include <cstdio>
42#include <fstream>
43#include <inttypes.h>
44#include <map>
45#include <memory>
46#include <regex>
47#include <stdexcept>
48#include <string>
49
50#include "learn/learningEnvironment.h"
51#include "tpg/tpgAction.h"
52#include "tpg/tpgEdge.h"
53#include "tpg/tpgGraph.h"
54#include "tpg/tpgTeam.h"
55#include "tpg/tpgVertex.h"
56
57namespace File {
63 {
64 protected:
68 std::ifstream pFile;
69
75 std::string lastLine;
76
81
86
94 std::map<uint64_t, const TPG::TPGVertex*> vertexID;
95
103 std::map<uint64_t, std::shared_ptr<Program::Program>> programID;
104
112 std::map<uint64_t, const TPG::TPGVertex*> actionID;
113
120 std::map<uint64_t, uint64_t> actionLabel;
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
313 static const std::string addLinkProgramRegex;
314
322 void readOperands(std::string& str, Program::Line& line);
323
328 void readLine(std::smatch& matches);
329
333 void readProgram(std::smatch& matches);
334
341 void dumpTPGGraphHeader();
342
346 void readTeam(std::smatch& matches);
347
351 void readAction(std::smatch& matches);
352
356 void readLinkTeamProgramAction(std::smatch& matches);
357
361 void readLinkTeamProgramTeam(std::smatch& matches);
362
367 void readLinkTeamProgram(std::smatch& matches);
368
375 bool readLineFromFile();
376
377 public:
388 TPGGraphDotImporter(const char* filePath, Environment environment,
389 TPG::TPGGraph& tpgref)
390 : env{environment}, tpg{tpgref}
391 {
392 pFile.open(filePath);
393 if (!pFile.is_open()) {
394 throw std::runtime_error("Could not open file " +
395 std::string(filePath));
396 }
397 importGraph();
398 };
399
404 static const unsigned int MAX_READ_SIZE = 4096;
405
412 {
413 if (pFile.is_open()) {
414 pFile.close();
415 }
416 }
417
426 void setNewFilePath(const char* newFilePath);
427
431 void importGraph();
432 };
433}; // namespace File
434
435#endif
The Environment class contains all information needed to execute a Program.
Definition: environment.h:84
Class used to import a TPG graph from a dot file. It should be able to import a whole Learning agent ...
Definition: tpgGraphDotImporter.h:63
void readLinkTeamProgram(std::smatch &matches)
reads a link declaration and creates a team to program's destination edge.
Definition: tpgGraphDotImporter.cpp:275
void importGraph()
Creates a TPG Graph from its description in a .dot file.
Definition: tpgGraphDotImporter.cpp:309
void readLinkTeamProgramTeam(std::smatch &matches)
reads a link declaration and creates a team to team edge
Definition: tpgGraphDotImporter.cpp:249
TPGGraphDotImporter(const char *filePath, Environment environment, TPG::TPGGraph &tpgref)
Constructor for the importer.
Definition: tpgGraphDotImporter.h:388
void readAction(std::smatch &matches)
reads and creates a TPGAction.
Definition: tpgGraphDotImporter.cpp:203
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:57
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:85
std::ifstream pFile
File in which the dot content is read during import.
Definition: tpgGraphDotImporter.h:68
~TPGGraphDotImporter()
Definition: tpgGraphDotImporter.h:411
void readTeam(std::smatch &matches)
reads and creates a TPGTeam.
Definition: tpgGraphDotImporter.cpp:195
std::string lastLine
last Line read from file
Definition: tpgGraphDotImporter.h:75
void readProgram(std::smatch &matches)
Create a program from its dot content and import its constants.
Definition: tpgGraphDotImporter.cpp:151
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:313
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:80
std::map< uint64_t, const TPG::TPGVertex * > actionID
Map associating pointers to TPGVertex representing actions to the corresponding action.
Definition: tpgGraphDotImporter.h:112
void readLine(std::smatch &matches)
Reads the content of a line and puts it in the line passed in parameter.
Definition: tpgGraphDotImporter.cpp:85
void dumpTPGGraphHeader()
dumps the header of the dot file
Definition: tpgGraphDotImporter.cpp:186
std::map< uint64_t, std::shared_ptr< Program::Program > > programID
Map associating pointers to Program to an integer ID.
Definition: tpgGraphDotImporter.h:103
std::map< uint64_t, const TPG::TPGVertex * > vertexID
Map associating pointers to TPGVertex to an integer ID.
Definition: tpgGraphDotImporter.h:94
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:223
bool readLineFromFile()
Definition: tpgGraphDotImporter.cpp:329
static const std::string linkProgramActionRegex
contains the regex to identify a Team -> Program -> Action Link
Definition: tpgGraphDotImporter.h:263
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:385
static const unsigned int MAX_READ_SIZE
Maximum number of characters that can be read in a single line.
Definition: tpgGraphDotImporter.h:404
std::map< uint64_t, uint64_t > actionLabel
Map associating actions to the corresponding action ID.
Definition: tpgGraphDotImporter.h:120
Definition: line.h:48
Class for storing a Tangled-Program-Graph.
Definition: tpgGraph.h:54
Definition: parametersParser.h:51