diff --git a/maca_common/include/File.hpp b/maca_common/include/File.hpp index b804309de884594bc57beeb6a983d528c2cbb97b..3631ccd2be3ad00de532b3460643724a9d561b5d 100644 --- a/maca_common/include/File.hpp +++ b/maca_common/include/File.hpp @@ -49,10 +49,10 @@ class File /// @param filename The filename of the file. /// @param mode C's style opening mode. "r"ead or "w"rite. File(const std::string & filename, const std::string & mode); - /// @brief File is not moveable. + /// @brief Open the file again and place the reading cursor at the same point. /// /// @param model - File(File && model) = delete; + File(const File & model); /// @brief File is not copyable. /// /// @param model @@ -61,7 +61,6 @@ class File File & operator=(const File & model) = delete; /// @brief The destructor, in which we close the file. ~File(); - /// @brief Peek the next char in the file but without consuming it. /// /// @return The next char in the file. diff --git a/maca_common/include/LimitedArray.hpp b/maca_common/include/LimitedArray.hpp index b8339e2271ca3f61b1ca45b46257f1b23fb4e7fa..c6dcda01018b73bace679b4f7049dd90ee798fb8 100644 --- a/maca_common/include/LimitedArray.hpp +++ b/maca_common/include/LimitedArray.hpp @@ -91,6 +91,14 @@ class LimitedArray return res; } + int getNextOverridenRealIndex() const + { + if (lastElementRealIndex < (int)data.size()-1) + return -1; + + return lastElementRealIndex - (data.size()-1); + } + int getDataSize() const { return std::min((int)data.size(), nbElements); diff --git a/maca_common/include/ProgramOutput.hpp b/maca_common/include/ProgramOutput.hpp new file mode 100644 index 0000000000000000000000000000000000000000..12282c082a09c76207c95688c549720f7474bc72 --- /dev/null +++ b/maca_common/include/ProgramOutput.hpp @@ -0,0 +1,33 @@ +/// @file ProgramOutput.hpp +/// @author Franck Dary +/// @version 1.0 +/// @date 2019-02-12 + +#ifndef PROGRAMOUTPUT__H +#define PROGRAMOUTPUT__H + +#include <vector> +#include <string> +#include <cstdio> + +struct ProgramOutput +{ + private : + + std::vector<std::string> lines; + std::vector<float> entropies; + std::vector<unsigned int> lineIndexes; + std::vector<int> outputIndexes; + + public : + + static ProgramOutput instance; + + public : + + void print(FILE * output); + void addLine(const std::string & line, float entropy, unsigned int index); +}; + +#endif + diff --git a/maca_common/src/File.cpp b/maca_common/src/File.cpp index 42d7014c1e12cf0fbd61fee93c77189a8e2beeae..30a8f11ffe579baa87ff2e27fc05ea1e22fb30a9 100644 --- a/maca_common/src/File.cpp +++ b/maca_common/src/File.cpp @@ -7,6 +7,26 @@ File::File(FILE * file) this->file = file; } +File::File(const File & model) +{ + this->buffer = model.buffer; + this->endHasBeenReached = model.endHasBeenReached; + this->filename = model.filename; + + std::fpos_t otherPos; + std::fgetpos(model.file, &otherPos); + + file = fopen(filename.c_str(), "r"); + if (!file) + { + fprintf(stderr, "ERROR (%s) : cannot open file %s\n", ERRINFO, filename.c_str()); + + exit(1); + } + + std::fsetpos(file, &otherPos); +} + char File::peek() { if (!buffer.empty()) diff --git a/maca_common/src/ProgramOutput.cpp b/maca_common/src/ProgramOutput.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8705910e944e21be1548a5fbfe142df390ba4cc5 --- /dev/null +++ b/maca_common/src/ProgramOutput.cpp @@ -0,0 +1,21 @@ +#include "ProgramOutput.hpp" + +ProgramOutput ProgramOutput::instance; + +void ProgramOutput::print(FILE * output) +{ + for (auto & i : outputIndexes) + if (i != -1) + fprintf(output, "%s\n", lines[i].c_str()); +} + +void ProgramOutput::addLine(const std::string & line, float entropy, unsigned int index) +{ + lines.emplace_back(line); + entropies.emplace_back(entropy); + while (index > outputIndexes.size()-1) + outputIndexes.emplace_back(-1); + if (outputIndexes[index] == -1 || entropies[outputIndexes[index]] < entropy) + outputIndexes[index] = lines.size()-1; +} + diff --git a/transition_machine/include/Config.hpp b/transition_machine/include/Config.hpp index 64357dbb74ed4e1a3810a499ea34c7125540c2c9..bf764f230d3162f12f2b35bda0b0d4b1cb626d1f 100644 --- a/transition_machine/include/Config.hpp +++ b/transition_machine/include/Config.hpp @@ -116,6 +116,8 @@ class Config void copyPart(Tape & other, unsigned int from, unsigned int to); /// @brief Get the last tape index that will be overriden with the next read. int getNextOverridenDataIndex(); + /// @brief Get the last tape index that will be overriden with the next read. + int getNextOverridenRealIndex(); }; private : @@ -162,6 +164,10 @@ class Config /// @param bd The BD that describes the tapes of this Config. /// @param inputFilename The name of the input file. Config(BD & bd, const std::string inputFilename); + /// @brief Copy constructor. + /// + /// @param other The model. + Config(const Config & other); /// @brief Get a Tape by its name. /// /// @param name The name of the Tape. @@ -183,8 +189,9 @@ class Config /// @brief Print the tapes as the output of the program. /// /// @param output Where to print. - /// @param index Index of line to print. - void printAsOutput(FILE * output, int index); + /// @param dataIndex Index of line to print. + /// @param realIndex Index of line to print. + void printAsOutput(FILE * output, int dataIndex, int realIndex); /// @brief Print the Config without information loss. /// /// @param output Where to print. diff --git a/transition_machine/src/ActionBank.cpp b/transition_machine/src/ActionBank.cpp index 84b063d5505782693ac81e74387aada204ed97a7..31ece1e3c4b5744c299ec4232e524eea09b805f1 100644 --- a/transition_machine/src/ActionBank.cpp +++ b/transition_machine/src/ActionBank.cpp @@ -190,7 +190,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na auto undo = [](Config & c, Action::BasicAction & ba) {c.stackPush(std::stoi(ba.data));}; auto appliable = [](Config & c, Action::BasicAction &) - {return !c.stackEmpty() && !c.getTape("GOV")[c.stackTop()].empty();}; + {return !c.stackEmpty() && !c.getTape("GOV")[c.stackTop()-c.getHead()].empty();}; Action::BasicAction basicAction = {Action::BasicAction::Type::Pop, "", apply, undo, appliable}; @@ -367,7 +367,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na else { simpleBufferWrite(c, "GOV", std::to_string(rootIndex - s), s-b0); - ba.data += "+"+s-b0; + ba.data += "+"+std::to_string(s-b0); } } @@ -599,7 +599,7 @@ bool ActionBank::isRuleAppliable(Config & config, { if (!simpleBufferWriteAppliable(config, tapeName, relativeIndex)) return false; - return ruleIsAppliable(config.getTape(tapeName)[config.getHead()+relativeIndex], rule); + return ruleIsAppliable(config.getTape(tapeName)[relativeIndex], rule); } void ActionBank::writeRuleResult(Config & config, const std::string & fromTapeName, const std::string & targetTapeName, const std::string & rule, int relativeIndex) diff --git a/transition_machine/src/Config.cpp b/transition_machine/src/Config.cpp index 6642a3d407ade45ff2ccbe964bea79957ae04417..1d9f55db06c14251733adeb2e7e6ab62a58ca7a8 100644 --- a/transition_machine/src/Config.cpp +++ b/transition_machine/src/Config.cpp @@ -17,6 +17,23 @@ Config::Config(BD & bd, const std::string inputFilename) : bd(bd), hashHistory(1 tapes.emplace_back(bd.getNameOfLine(i), bd.lineIsKnown(i)); } +Config::Config(const Config & other) : bd(other.bd), hashHistory(other.hashHistory), pastActions(other.pastActions) +{ + this->currentStateName = other.currentStateName; + this->actionHistory = other.actionHistory; + this->entropyHistory = other.entropyHistory; + this->stack = other.stack; + this->stackHistory = other.stackHistory; + this->head = other.head; + this->outputFile = other.outputFile; + this->lastIndexPrinted = other.lastIndexPrinted; + this->tapes = other.tapes; + + this->inputFilename = other.inputFilename; + this->inputAllRead = other.inputAllRead; + this->file.reset(new File(*other.file.get())); +} + Config::Tape::Tape(const std::string & name, bool isKnown) : ref(ProgramParameters::readSize*4+1), hyp(ProgramParameters::readSize*4+1) { this->head = 0; @@ -62,7 +79,7 @@ void Config::readInput() exit(1); } - printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex()); + printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex(), tapes[0].getNextOverridenRealIndex()); for(unsigned int i = 0; i < cols.size(); i++) if(bd.hasLineOfInputCol(i)) @@ -83,7 +100,7 @@ void Config::readInput() if (haveRead < toRead || tapes[0].size() == ProgramParameters::tapeSize) { - printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex()); + printAsOutput(outputFile, tapes[0].getNextOverridenDataIndex(), tapes[0].getNextOverridenRealIndex()); inputAllRead = true; } @@ -176,23 +193,30 @@ void Config::printAsExample(FILE *) exit(1); } -void Config::printAsOutput(FILE * output, int index) +void Config::printAsOutput(FILE * output, int dataIndex, int realIndex) { - if (index == -1 || !output) + if (dataIndex == -1 || !output) return; - lastIndexPrinted = index; + lastIndexPrinted = dataIndex; unsigned int lastToPrint = 0; for (unsigned int j = 0; j < tapes.size(); j++) if(bd.mustPrintLine(j)) lastToPrint = j; + std::string toPrint = ""; for (unsigned int j = 0; j < tapes.size(); j++) { if(bd.mustPrintLine(j)) - fprintf(output, "%s%s", tapes[j][index-head].empty() ? "0" : tapes[j][index-head].c_str(), j == lastToPrint ? "\n" : "\t"); + { + toPrint += tapes[j][dataIndex-head].empty() ? "0" : tapes[j][dataIndex-head].c_str(); + toPrint += j == lastToPrint ? "" : "\t"; + //fprintf(output, "%s%s", tapes[j][index-head].empty() ? "0" : tapes[j][index-head].c_str(), j == lastToPrint ? "\n" : "\t"); + } } + + fprintf(stderr, "Print %d\n", realIndex); } void Config::moveHead(int mvt) @@ -528,15 +552,33 @@ int Config::Tape::getNextOverridenDataIndex() return ref.getNextOverridenDataIndex(); } +int Config::Tape::getNextOverridenRealIndex() +{ + return ref.getNextOverridenRealIndex(); +} + void Config::printTheRest() { if (!outputFile) return; + int tapeSize = tapes[0].size(); + int goalPrintIndex = lastIndexPrinted; + int realIndex = tapeSize - ((()-())+()); for (int i = goalPrintIndex+1; i < (tapes[0].dataSize()-(goalPrintIndex == -1 ? 1 : 0)); i++) - printAsOutput(outputFile, i); + { + printAsOutput(outputFile, i, 0); + fprintf(stderr, "i=%d tapeSize=%d dataSize=%d\n", i, tapeSize, tapes[0].dataSize()); + fprintf(stderr, "calcul = %d\n", realIndex); + realIndex++; + } for (int i = 0; i < goalPrintIndex; i++) - printAsOutput(outputFile, i); + { + printAsOutput(outputFile, i, 0); + fprintf(stderr, "i=%d tapeSize=%d dataSize=%d\n", i, tapeSize, tapes[0].dataSize()); + fprintf(stderr, "calcul = %d\n", realIndex); + realIndex++; + } }