Skip to content

Commit

Permalink
afd to text
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacioDias committed Apr 10, 2024
1 parent 15d4da8 commit b5a0ecb
Show file tree
Hide file tree
Showing 38 changed files with 1,176 additions and 1,066 deletions.
8 changes: 5 additions & 3 deletions TP/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace std;

int main() {
/**
string filename = "archivo.txt"; // Nombre del archivo
string filename = "archivo.txt"; // Nombre del archivo
ifstream file(filename); // Abrir el archivo
Parser parser = *new Parser();
Expand Down Expand Up @@ -34,7 +34,7 @@ int main() {
*/

NotDeterministicFiniteAutomata ndfa;
ndfa.setInitialState(0);
ndfa.setInitialState(10);
ndfa.setE({1,2}); //a, b
ndfa.setK({10,11,12,13});
ndfa.addPath(10,1,11);
Expand All @@ -44,6 +44,8 @@ int main() {
ndfa.addPath(13,1,13);
ndfa.addPath(13,2,13);
ndfa.addFinalState(12);
cout << Parser::ndfaToString(ndfa);
//cout << Parser::ndfaToString(ndfa);
DeterministicFiniteAutomata dfa = ndfa.nfaToDfa();
cout << Parser::dfaToString(dfa);
return 0;
}
14 changes: 14 additions & 0 deletions TP/automata/dfa/DeterministicFiniteAutomata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,19 @@ bool DeterministicFiniteAutomata :: isFinalNode(const set<int>& node) {
return false;
}

set<int> DeterministicFiniteAutomata::calculateWaysToGo(const set<int>& set1, const set<int>& set2) {
set<int> ret;
for(int letter : this->E) {
pair<set<int>,int> pair;
pair.first = set1;
pair.second = letter;
set<int> delta = calculateDelta(pair);
if(!delta.empty() && CollectionsOperators::contained(set2, delta))
ret.insert(letter);
}
return ret;
}




3 changes: 3 additions & 0 deletions TP/automata/dfa/DeterministicFiniteAutomata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class DeterministicFiniteAutomata {
bool belongs(const string& numbers);
set<set<int>> getF();
set<int> getInitialState();

set<int> calculateWaysToGo(const set<int>& set1, const set<int>& set2);

private:
bool isFinalNode(const set<int>& node);

Expand Down
50 changes: 25 additions & 25 deletions TP/automata/ndfa/NotDeterministicFiniteAutomata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ bool NotDeterministicFiniteAutomata :: repOk() {
bool invariant = CollectionsOperators::contained(this->F, this->K);
return invariant &= CollectionsOperators::belongs(this->q0, this->K);
}
set<int> NotDeterministicFiniteAutomata :: calculateWaysToGo(int from, int destination) {
set<int> ret;
for(int letter : this->E) {
pair<int,int> pair;
pair.first = from;
pair.second = letter;
set<int> delta = calculateDelta(pair);
if(!delta.empty() && delta.count(destination) > 0)
ret.insert(letter);
}
return ret;
}
DeterministicFiniteAutomata NotDeterministicFiniteAutomata :: nfaToDfa() {
DeterministicFiniteAutomata convertedAutomata = *new DeterministicFiniteAutomata();
set<int> q0AsSet;
Expand Down Expand Up @@ -87,30 +99,6 @@ void NotDeterministicFiniteAutomata :: calculateNewK(set<set<int>> newK, Determi
unvisitedNodes.erase(currentNode);
}
}
set<int> NotDeterministicFiniteAutomata :: calculateWaysToGo(int from, int destination) {
set<int> ret;
for(int letter : this->E) {
pair<int,int> pair;
pair.first = from;
pair.second = letter;
set<int> delta = calculateDelta(pair);
if(!delta.empty() && delta.count(destination) > 0)
ret.insert(letter);
}
return ret;
}
set<set<int>> NotDeterministicFiniteAutomata :: calculateFinal(set<set<int>> k) {
set<set<int>> newF;
for(const auto& currentSet : k) {
for(auto currentNumber : currentSet) {
if(getF().count(currentNumber) > 0) {
newF.insert(currentSet);
break;
}
}
}
return newF;
}
set<int> NotDeterministicFiniteAutomata ::getSymbolClosure(const set<int>& Q) {
set<int> result;
set<int> visited_states;
Expand Down Expand Up @@ -139,4 +127,16 @@ set<int> NotDeterministicFiniteAutomata :: move(const set<int>& Q, int a) {
CollectionsOperators::insertAll(ret, actualSet);
}
return ret;
}
}
set<set<int>> NotDeterministicFiniteAutomata :: calculateFinal(set<set<int>> k) {
set<set<int>> newF;
for(const auto& currentSet : k) {
for(auto currentNumber : currentSet) {
if(getF().count(currentNumber) > 0) {
newF.insert(currentSet);
break;
}
}
}
return newF;
}
47 changes: 40 additions & 7 deletions TP/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,50 @@ void Parser::fileManagement(const string& line) {
} else if(!isRankdirLine(line) || line == "}" && readLastLine)
throw runtime_error("Formato invalido del archivo.");
}
string Parser::dfaToString(DeterministicFiniteAutomata dfa) {
string ret = "digraph{\ninic[shape=point];\ninic->";
ret += "{\"" + toStringSet(dfa.getInitialState()) + "\"};\n";
toStringStatesDFA(dfa, ret);
toStringFinalStateDFA(dfa, ret);
ret += "\n}";
return ret;
}
string Parser::toStringSet(const set<int>& set) {
string ret;
for(auto elem : set)
ret += to_string(elem) + ",";
ret.pop_back();
return ret;
}
void Parser::toStringStatesDFA(DeterministicFiniteAutomata dfa, std::string &ret) {
for(auto set1 : dfa.getK()) {
for(auto set2 : dfa.getK()) {
set<int> label = dfa.calculateWaysToGo(set1, set2);
if(!label.empty()) {
ret += toStringSet(set1) + " -> " + toStringSet(set2) + " [label = \"";
for(auto elem : label)
ret += to_string(elem) + ",";
ret.pop_back();
ret += "\"];\n";
}
}
}
}
void Parser :: toStringFinalStateDFA(DeterministicFiniteAutomata dfa, string& ret) {
for(set<int> finalState : dfa.getF())
ret += toStringSet(finalState) + "[shape=doublecircle];\n";
ret.pop_back();
}

string Parser::ndfaToString(NotDeterministicFiniteAutomata ndfa) {
//inicio del automata y estado inicial
string ret = "digraph{\ninic[shape=point];\ninic -> ";
string ret = "digraph{\ninic[shape=point];\ninic -> ";
ret += to_string(ndfa.getInitialState()) + ";\n";

toStringStates(ndfa, ret); // transiciones
toStringFinalState(ndfa, ret);
toStringStatesNDFA(ndfa, ret);
toStringFinalStateNDFA(ndfa, ret);
ret += "\n}";
return ret;
}
void Parser::toStringStates(NotDeterministicFiniteAutomata ndfa, string& ret) {
void Parser::toStringStatesNDFA(NotDeterministicFiniteAutomata ndfa, string& ret) {
for(int number : ndfa.getK()) {
for(int number2 : ndfa.getK()) {
set<int> label = ndfa.calculateWaysToGo(number,number2);
Expand All @@ -123,7 +156,7 @@ void Parser::toStringStates(NotDeterministicFiniteAutomata ndfa, string& ret) {
}
}
}
void Parser :: toStringFinalState(NotDeterministicFiniteAutomata ndfa, string& ret) {
void Parser :: toStringFinalStateNDFA(NotDeterministicFiniteAutomata ndfa, string& ret) {
for(int finalState : ndfa.getF())
ret += to_string(finalState) + "[shape=doublecircle];\n";
ret.pop_back();
Expand Down
11 changes: 7 additions & 4 deletions TP/parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ class Parser {
static bool validateFinalStateLine(const string& line);
bool isRankdirLine(const string& line);
static string ndfaToString(NotDeterministicFiniteAutomata ndfa);
static void writeToFile(const string& filename, const string& content);
static string dfaToString(DeterministicFiniteAutomata dfa);
static void writeToFile(const string& filename, const string& content);

private:
static bool checkDelta(const string& line);
NotDeterministicFiniteAutomata nda;
bool readLastLine;
static void toStringStates(NotDeterministicFiniteAutomata ndfa, string& ret);
static void toStringFinalState(NotDeterministicFiniteAutomata ndfa, string& ret);

static void toStringStatesNDFA(NotDeterministicFiniteAutomata ndfa, string& ret);
static void toStringFinalStateNDFA(NotDeterministicFiniteAutomata ndfa, string& ret);
static void toStringStatesDFA(DeterministicFiniteAutomata dfa, string& ret);
static void toStringFinalStateDFA(DeterministicFiniteAutomata dfa, string& ret);
static string toStringSet(const set<int>& set);
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}
],
"type" : "INTERNAL",
"value" : "27"
"value" : "28"
},
{
"name" : "CMAKE_CACHE_PATCH_VERSION",
Expand All @@ -91,7 +91,7 @@
}
],
"type" : "INTERNAL",
"value" : "8"
"value" : "1"
},
{
"name" : "CMAKE_CODEBLOCKS_COMPILER_ID",
Expand Down Expand Up @@ -151,7 +151,7 @@
}
],
"type" : "INTERNAL",
"value" : "/snap/clion/265/bin/cmake/linux/x64/bin/cmake"
"value" : "/snap/clion/274/bin/cmake/linux/x64/bin/cmake"
},
{
"name" : "CMAKE_CPACK_COMMAND",
Expand All @@ -163,7 +163,7 @@
}
],
"type" : "INTERNAL",
"value" : "/snap/clion/265/bin/cmake/linux/x64/bin/cpack"
"value" : "/snap/clion/274/bin/cmake/linux/x64/bin/cpack"
},
{
"name" : "CMAKE_CTEST_COMMAND",
Expand All @@ -175,7 +175,7 @@
}
],
"type" : "INTERNAL",
"value" : "/snap/clion/265/bin/cmake/linux/x64/bin/ctest"
"value" : "/snap/clion/274/bin/cmake/linux/x64/bin/ctest"
},
{
"name" : "CMAKE_CXX_COMPILER",
Expand Down Expand Up @@ -975,7 +975,7 @@
}
],
"type" : "INTERNAL",
"value" : "/snap/clion/265/bin/cmake/linux/x64/share/cmake-3.27"
"value" : "/snap/clion/274/bin/cmake/linux/x64/share/cmake-3.28"
},
{
"name" : "CMAKE_SHARED_LINKER_FLAGS",
Expand Down
Loading

0 comments on commit b5a0ecb

Please sign in to comment.