8 #include <boost/algorithm/string.hpp> 10 #include "MapleCOMSPS_LRB/utils/System.h" 18 BinaryNonCNFMethod::BinaryNonCNFMethod(
const std::set<std::string> &SpSet,
const std::set<std::string> &SmSet,
const std::set<std::string> &SSet,
const std::set<std::string> &prefixesSet,
const std::set<char> &alphabetSet,
unsigned int n) :
27 return "binaryNonCNF";
30 std::string BinaryNonCNFMethod::phi_different(std::string pref1, std::string pref2){
32 std::string toWrite =
"(";
33 for (
unsigned int i = 0; i < m; i++){
37 "x_" + pref1 +
"_" + std::to_string(i) +
39 "x_" + pref2 +
"_" + std::to_string(i) +
43 "!x_" + pref1 +
"_" + std::to_string(i) +
45 "x_" + pref2 +
"_" + std::to_string(i) +
55 std::string BinaryNonCNFMethod::phi_not_q_i(std::string pref,
unsigned int i){
58 std::string toWrite =
"(";
59 for (
unsigned int j = 0; j < m; j++){
61 toWrite = toWrite +
"x_" + pref +
"_" + std::to_string(j);
64 toWrite = toWrite +
"!" +
"x_" + pref +
"_" + std::to_string(j);
81 std::string formula =
"";
86 if (pref.size() > 0) {
87 const std::string &u = pref.substr(0, pref.length() - 1);
88 const char &a = pref.at(pref.length() - 1);
90 for (
const auto &pref2 : m_prefixes){
91 if(pref2.size() > 0 && a == pref2.at(pref2.length() - 1)){
92 const std::string &v = pref2.substr(0, pref2.length() - 1);
94 std::string phi_dif_u = phi_different(u, v);
96 std::string phi_dif_ua = phi_different(pref, pref2);
98 formula = formula +
"(" + phi_dif_u +
" | !" + phi_dif_ua +
")\n";
104 formula = formula.substr(0, formula.length() - 3) +
"\n\n";
109 for (
const auto &pos :
m_Sp){
110 for (
const auto &neg :
m_Sm){
111 formula += phi_different(pos, neg) +
" &";
115 formula = formula.substr(0, formula.length() - 3) +
"\n\n";
119 for (
const auto &pref : m_prefixes){
121 std::string phi_q = phi_not_q_i(pref, i);
122 formula +=
"(" + phi_q +
")";
126 formula = formula.substr(0, formula.length() - 3);
137 std::map<std::pair<std::string, unsigned int>,
unsigned int> m_state_bit;
139 while(std::getline(stream, line)){
141 if (line.at(0) ==
'x'){
142 std::vector<std::string> results;
143 std::vector<std::string> results_2;
145 boost::split(results, line, [](
char c){
return c ==
' ';});
147 boost::split(results_2, results[0], [](
char c){
return c ==
'_';});
148 auto pair = std::make_pair(results_2[1], std::stoi(results_2[2]));
149 m_state_bit.emplace(state(pair, std::stoi(results[2])));
154 std::map<std::string, unsigned int> words_states;
168 std::vector<bool> binary_number;
169 for (
unsigned int i = 0; i < m; i++){
170 binary_number.push_back(m_state_bit.at(std::make_pair(u, i)));
173 words_states.emplace(u, bin);
176 std::unique_ptr<DFA<char>> dfa = std::make_unique<DFA<char>>(words_states.find(
"") -> second,
m_Sp.find(
"") !=
m_Sp.end());
178 for(
const auto &u : m_prefixes){
179 unsigned int state = words_states.find(u) -> second;
181 if(std::find(
m_Sp.begin(),
m_Sp.end(), u) !=
m_Sp.end()){
182 dfa->addState(state,
true);
185 dfa->addState(state,
false);
192 for (
const auto &ua : m_prefixes) {
193 if (ua.size() > 0 && ua.at(ua.size() - 1) == a){
194 std::string u = ua.substr(0, ua.size() - 1);
196 if (words_states.at(u) == i && words_states.at(ua) == j) {
198 dfa->addTransition(i, a, j);
205 dfa->addTransition(i, a, i);
const std::set< char > m_alphabet
The namespace of the library.
virtual std::string getFileName() const override
Gives the name of the file in which the boolean formula must be written.
const std::set< std::string > m_Sm
The set of words to reject .
const std::set< std::string > m_prefixes
This file declares the function to convert a number in its decimal form to its binary form and vice-v...
static bool s_registered
Useful to make sure the template is correctly used by the compiler.
virtual bool createFormula(std::ostream &stream) override
Creates the boolean formula (must not be in CNF) and writes it in the stream.
std::vector< bool > decimal_to_binary(unsigned int dec_number, unsigned int size)
Converts a number in decimal form to binary form.
static std::string getFactoryName()
virtual std::unique_ptr< DFA< char > > toDFA(std::istream &stream) const override
Effectively creates the DFA.
const unsigned int m_numberStates
The number of states in the DFA to construct.
Base class for every method that relies on a non-CNF SAT solver.
unsigned int binary_to_decimal(const std::vector< bool > &binary_number)
Converts a number in binary form to decimal form.
const std::set< std::string > m_Sp
The set of words to accept .