libgsjj  1
Deterministic finite automata learning library
UnaryNonCNFMethod.cpp
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <vector>
5 #include <boost/algorithm/string.hpp>
6 
7 namespace gsjj {
8  namespace passive {
9  namespace NonCNF {
10  UnaryNonCNFMethod::UnaryNonCNFMethod(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) :
11  SATNonCNFMethod(SpSet, SmSet, SSet, prefixesSet, alphabetSet, n)
12  {
14  }
15 
17 
18  }
19 
21  return "unaryNonCNF";
22  }
23 
24  bool UnaryNonCNFMethod::createFormula(std::ostream &stream){
25  if (m_numberStates == 1) {
26  return false;
27  }
28  //Equation 4.3.1
29  std::string formula = "";
30  for (const auto &u : m_prefixes) {
31  formula += " ( ";
32  for (unsigned int q = 0 ; q < m_numberStates ; q++) {
33  formula += "x_" + u + "_" + std::to_string(q);
34  if (q != m_numberStates - 1){
35  formula += " | ";
36  }
37  }
38  formula += " ) &\n";
39  }
40 
41  formula = formula.substr(0, formula.length() - 3);
42  stream << formula;
43 
44  //Equation 4.3.2
45  formula = " &\n";
46  for (const auto &u : m_prefixes) {
47  formula += " ( ";
48  for (unsigned int q = 0 ; q < m_numberStates ; q++) {
49  for (unsigned int p = 0 ; p < m_numberStates ; p++) {
50  if (p != q) {
51  formula += " ! ( x_" + u + "_" + std::to_string(q) + " & x_" + u + "_" + std::to_string(p) + " )";
52  formula += " &";
53  }
54  }
55  }
56  formula = formula.substr(0, formula.length() - 2);
57  formula += " ) &\n";
58  }
59 
60  formula = formula.substr(0, formula.length() - 3);
61  stream << formula;
62 
63  //Equation 4.3.3
64  formula = " &\n";
65  for (const auto &ua : m_prefixes) {
66  for (const auto &va : m_prefixes) {
67  // We must have that ua and va end with the same symbol
68  // We must have at least one symbol as well
69  if (ua.size() > 0 && va.size() > 0 && ua.at(ua.size() - 1) == va.at(va.size() - 1)) {
70  std::string u = ua.substr(0, ua.size() - 1);
71  std::string v = va.substr(0, va.size() - 1);
72  // Now, we can create every clause
73  for (unsigned int p = 0 ; p < m_numberStates ; p++) {
74  for (unsigned int q = 0 ; q < m_numberStates ; q++) {
75  formula += "( ( x_" + u + "_" + std::to_string(p) + " & " +
76  "x_" + v + "_" + std::to_string(p) + " ) " + " -> ( "
77  "x_" + ua + "_" + std::to_string(q) + " <-> " +
78  "x_" + va + "_" + std::to_string(q) + " ) )";
79  formula += " &\n";
80  }
81  }
82  }
83  }
84  }
85 
86  formula = formula.substr(0, formula.length() - 3);
87  stream << formula;
88 
89  //Equation 4.3.4
90  formula = " &\n";
91  for (const auto &u : m_Sp) {
92  for (const auto &v : m_Sm) {
93  for (unsigned int q = 0 ; q < m_numberStates ; q++) {
94  formula += " ! ( x_" + u + "_" + std::to_string(q) + " & x_" + v + "_" + std::to_string(q) + " )";
95  formula += " &\n";
96  }
97  }
98  }
99  formula = formula.substr(0, formula.length() - 3);
100  stream << formula;
101 
102  return true;
103  }
104 
105  std::unique_ptr<DFA<char>> UnaryNonCNFMethod::toDFA(std::istream &stream) const {
106  //First, we have to read the solution
107  std::string line;
108  std::map<std::string, unsigned int> words_states;
109 
110  while(std::getline(stream, line)){
111  //If there is a variable on this line
112  if (line.at(0) == 'x'){
113  std::vector<std::string> results;
114  std::vector<std::string> results_2;
115  //We split the line to get var - = - assignationOfTheVar
116  boost::split(results, line, [](char c){return c == ' ';});
117  //We split the var to get x - pref - stateNumber
118  boost::split(results_2, results[0], [](char c){return c == '_';});
119  // auto pair = std::make_pair(results_2[1], std::stoi(results_2[2]));
120  // m_state_bit.emplace(state(pair, std::stoi(results[2])));
121  if (std::stoi(results[2]) == 1){
122  words_states.emplace(results_2[1], std::stoi(results_2[2]));
123  }
124  }
125  }
126 
127  std::unique_ptr<DFA<char>> dfa = std::make_unique<DFA<char>>(words_states.find("") -> second, m_Sp.find("") != m_Sp.end());
128 
129  for(const auto &u : m_prefixes){
130  unsigned int state = words_states.find(u) -> second;
131  //If the state is final
132  if(std::find(m_Sp.begin(), m_Sp.end(), u) != m_Sp.end()){
133  dfa->addState(state, true);
134  }
135  else{
136  dfa->addState(state, false);
137  }
138  }
139 
140  for (unsigned int i = 0 ; i < m_numberStates ; i++) {
141  for (const char &a : m_alphabet) {
142  bool found = false;
143  for (unsigned int j = 0 ; j < m_numberStates ; j++) {
144  for (const auto &ua : m_prefixes) {
145  if (ua.size() > 0 && ua.at(ua.size() - 1) == a){
146  std::string u = ua.substr(0, ua.size() - 1);
147 
148  if (words_states.at(u) == i && words_states.at(ua) == j) {
149  found = true;
150  dfa->addTransition(i, a, j);
151  }
152  }
153  }
154  }
155 
156  if (!found) {
157  dfa->addTransition(i, a, i);
158  }
159  }
160  }
161 
162  return dfa;
163  }
164 
165  std::string UnaryNonCNFMethod::getFileName() const {
166  return "unary";
167  }
168  }
169  }
170 }
const std::set< char > m_alphabet
Definition: Method.h:121
The namespace of the library.
Definition: binary.h:11
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 .
Definition: Method.h:105
const std::set< std::string > m_prefixes
Definition: Method.h:117
virtual bool createFormula(std::ostream &stream) override
Creates the boolean formula (must not be in CNF) and writes it in the stream.
static bool s_registered
Useful to make sure the template is correctly used by the compiler.
Definition: MethodFactory.h:97
const unsigned int m_numberStates
The number of states in the DFA to construct.
Definition: Method.h:109
Base class for every method that relies on a non-CNF SAT solver.
const std::set< std::string > m_Sp
The set of words to accept .
Definition: Method.h:101
std::unique_ptr< DFA< char > > toDFA(std::istream &stream) const override
Effectively creates the DFA.