Skip to content

Commit

Permalink
(#318) implemented Symbol parser
Browse files Browse the repository at this point in the history
@TonitaN, когда-нибудь я солью SemDet. Чем позже, тем больнее
  • Loading branch information
xendalm committed Jun 23, 2024
1 parent 0e8fafb commit 837672b
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 266 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(chipollino)
# Add sub directories
add_subdirectory(libs/Fraction)
add_subdirectory(libs/AutomatonToImage)
add_subdirectory(libs/AutomataParser)
add_subdirectory(libs/AutomatonParser)
add_subdirectory(libs/Objects)
add_subdirectory(libs/Tester)
add_subdirectory(libs/Logger)
Expand Down
258 changes: 129 additions & 129 deletions apps/MetamorphicTestsApp/src/MetamorphicTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <string>
#include <unordered_set>

#include "MetamorphicTestsApp/MetamorphicTests.h"
#include "AutomataParser/Parser.h"
#include "AutomatonParser/Parser.h"
#include "InputGenerator/AutomatonGenerator.h"
#include "InputGenerator/RegexGenerator.h"
#include "MetamorphicTestsApp/MetamorphicTests.h"
#include "Objects/BackRefRegex.h"
#include "Objects/FiniteAutomaton.h"
#include "Objects/MemoryFiniteAutomaton.h"
Expand Down Expand Up @@ -178,8 +178,58 @@ TEST(TestMFA, ToTxt) {
}
}

TEST(TestNFA, ToMFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
MemoryFiniteAutomaton mfa1 = BackRefRegex(rgx_str).to_mfa();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa1, mfa1.to_symbolic_fa().to_mfa()));
MemoryFiniteAutomaton mfa2 = BackRefRegex(rgx_str).to_mfa_additional();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa2, mfa2.to_symbolic_fa().to_mfa()));
}
}

TEST(TestMFA, ToFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = rg.generate_regex();
SCOPED_TRACE("Regex: " + rgx_str);
Regex r = Regex(rgx_str);
FiniteAutomaton fa = r.to_glushkov();
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_action_fa()));
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_symbolic_fa()));
}
}

TEST(TestMFA, Bisimilar) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 1);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

ASSERT_TRUE(MemoryFiniteAutomaton::action_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::symbolic_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::bisimilar(mfa, mfa).value());
}
}

TEST(TestMFA, MergeBisimilar) {
RegexGenerator rg(6, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

MetamorphicTests::cmp_automatons(mfa.merge_bisimilar(), mfa);
}
}

TEST(IsDeterministic, Test_is_deterministic) {
string test_path = "./test_data/MetamorphicTest/test1.txt";
string test_path = "./test_data/MetamorphicTest/test.txt";
for (int i = 0; i < RegexNumber; i++) {
AutomatonGenerator a(FA_type::NFA);
a.write_to_file(test_path);
Expand All @@ -191,7 +241,7 @@ TEST(IsDeterministic, Test_is_deterministic) {
}

TEST(AutomatonGenerator, Test_MergeBisim_equivalent) {
string test_path = "./test_data/MetamorphicTest/test1.txt";
string test_path = "./test_data/MetamorphicTest/test.txt";
for (int i = 0; i < RegexNumber; i++) {
AutomatonGenerator a(FA_type::NFA, 5);
a.write_to_file(test_path);
Expand All @@ -216,53 +266,53 @@ TEST(AutomatonGenerator, Test_MergeBisim_equivalent) {

/*
TEST(Statistics, Test_statistics) {
string test_path = "./TestData/MetamorphicTest/test1.txt";
std::vector<int> OX;
std::vector<float> OY;
AutomatonGenerator::set_initial_state_not_terminal(true);
for (int term = 5; term <= 100; term = term + 5) {
AutomatonGenerator::set_final_probability(term);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::NFA);
a.write_to_file(test_path);
Parser parser;
FiniteAutomaton FA;
try {
FA = parser.parse_NFA(test_path);
} catch (const std::runtime_error& re) {
std::ifstream t(test_path);
stringstream buffer;
buffer << t.rdbuf();
string file = buffer.str();
throw(std::runtime_error(file));
}
if (FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) <<
"%" << std::endl; OX.push_back(term); OY.push_back(float(count) / float(ALL));
}
std::cout << "OX = [";
for (int i = 0; i < OX.size() - 1; i++) {
std::cout << OX[i] << ",";
}
std::cout << OX[OX.size() - 1] << "]\n";
std::cout << "OY = [";
for (int i = 0; i < OY.size() - 1; i++) {
std::cout << OY[i] << ",";
}
std::cout << OY[OY.size() - 1] << "]\n";
string test_path = "./TestData/MetamorphicTest/test.txt";
std::vector<int> OX;
std::vector<float> OY;
AutomatonGenerator::set_initial_state_not_terminal(true);
for (int term = 5; term <= 100; term = term + 5) {
AutomatonGenerator::set_final_probability(term);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::NFA);
a.write_to_file(test_path);
Parser parser;
FiniteAutomaton FA;
try {
FA = parser.parse_NFA(test_path);
} catch (const std::runtime_error& re) {
std::ifstream t(test_path);
stringstream buffer;
buffer << t.rdbuf();
string file = buffer.str();
throw(std::runtime_error(file));
}
if (FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) <<
"%" << std::endl; OX.push_back(term); OY.push_back(float(count) / float(ALL));
}
std::cout << "OX = [";
for (int i = 0; i < OX.size() - 1; i++) {
std::cout << OX[i] << ",";
}
std::cout << OX[OX.size() - 1] << "]\n";
std::cout << "OY = [";
for (int i = 0; i < OY.size() - 1; i++) {
std::cout << OY[i] << ",";
}
std::cout << OY[OY.size() - 1] << "]\n";
}
TEST(AutomatonGenerator, Test_Arden_Glushkov_equivalent) {
int ALL = 50;
for (int i = 0; i < ALL; i++) {
string test_path = "./TestData/MetamorphicTest/test1.txt";
string test_path = "./TestData/MetamorphicTest/test.txt";
AutomatonGenerator a(FA_type::NFA, 5);
a.write_to_file(test_path);
Parser parser;
Expand All @@ -286,7 +336,7 @@ ard.minimize().to_txt() << "\n" << FA.to_regex().to_txt();
TEST(AutomatonGenerator, Test_Arden_Glushkov_Ambiguity_equivalent) {
int ALL = 50;
for (int i = 0; i < ALL; i++) {
string test_path = "./TestData/MetamorphicTest/test1.txt";
string test_path = "./TestData/MetamorphicTest/test.txt";
AutomatonGenerator a(FA_type::NFA, 5);
a.write_to_file(test_path);
Parser parser;
Expand All @@ -307,92 +357,42 @@ ard.minimize().to_txt() << "\n" << FA.to_regex().to_txt();
}
TEST(Statistics, Test_dfa) {
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::DFA);
a.write_to_file("./TestData/tmp/test.txt");
auto FA = Parser::parse_DFA("./TestData/tmp/test.txt");
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100<<
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::DFA);
a.write_to_file("./TestData/tmp/test.txt");
auto FA = Parser::parse_DFA("./TestData/tmp/test.txt");
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100<<
"%" << std::endl;
}
}
}
TEST(Statistics, Test_fa) {
std::cout << "TEST\n";
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::FA);
std::cout << "write_to_file START\n";
a.write_to_file("./TestData/tmp/test.txt");
std::cout << "write_to_file DONE\n";
auto FA = Parser::parse_FA("./TestData/tmp/test.txt");
std::cout << i << " " << std::endl;
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
std::cout << i << " " << std::endl;
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100
std::cout << "TEST\n";
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::FA);
std::cout << "write_to_file START\n";
a.write_to_file("./TestData/tmp/test.txt");
std::cout << "write_to_file DONE\n";
auto FA = Parser::parse_FA("./TestData/tmp/test.txt");
std::cout << i << " " << std::endl;
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
std::cout << i << " " << std::endl;
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100
<< "%" << std::endl;
}
}*/

TEST(TestNFA, ToMFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
MemoryFiniteAutomaton mfa1 = BackRefRegex(rgx_str).to_mfa();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa1, mfa1.to_symbolic_fa().to_mfa()));
MemoryFiniteAutomaton mfa2 = BackRefRegex(rgx_str).to_mfa_additional();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa2, mfa2.to_symbolic_fa().to_mfa()));
}
}

TEST(TestMFA, ToFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = rg.generate_regex();
SCOPED_TRACE("Regex: " + rgx_str);
Regex r = Regex(rgx_str);
FiniteAutomaton fa = r.to_glushkov();
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_action_fa()));
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_symbolic_fa()));
}
}

TEST(TestMFA, Bisimilar) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 1);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

ASSERT_TRUE(MemoryFiniteAutomaton::action_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::symbolic_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::bisimilar(mfa, mfa).value());
}
}

TEST(TestMFA, MergeBisimilar) {
RegexGenerator rg(6, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

MetamorphicTests::cmp_automatons(mfa.merge_bisimilar(), mfa);
}
}
}*/
Loading

0 comments on commit 837672b

Please sign in to comment.