libgsjj  1
Deterministic finite automata learning library
SATNonCNFMethod.cpp
Go to the documentation of this file.
2 
3 #include <fstream>
4 #include <iostream>
5 #include <functional>
6 
7 #include "MapleCOMSPS_LRB/utils/System.h"
8 using namespace Minisat;
9 
10 namespace gsjj {
11  namespace passive {
12  namespace NonCNF {
13  SATNonCNFMethod::SATNonCNFMethod(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) :
14  Method(SpSet, SmSet, SSet, prefixesSet, alphabetSet, n),
15  mustStop(nullptr) {
16  }
17 
19 
20  }
21 
23  const std::string& in = getInputFile();
24  const std::string& out = getOutputFile();
25  std::ofstream output(in);
26  if (createFormula(output)) {
27  output.close();
28  m_triedSolve = true;
29  const char* argv[] = {
30  "./limboole",
31  "-s",
32  in.c_str(),
33  "-o",
34  out.c_str(),
35  };
36 
37  if (mustStop && *mustStop) {
38  return false;
39  }
40 
41  m_cpuTimeStart = cpuTime();
42  limboole(5, argv, mustStop);
43  m_cpuTimeEnd = cpuTime();
44 
45  return m_hasSolution = isSatisfiable();
46  }
47  else {
48  output.close();
49  return m_hasSolution = (m_Sp.size() == 0 || m_Sm.size() == 0);
50  }
51  }
52 
53  std::unique_ptr<DFA<char>> SATNonCNFMethod::constructDFA() {
54  if (!m_triedSolve) {
55  if (!solve()) {
56  return nullptr;
57  }
58  }
59 
60  if (hasSolution()) {
61  if (m_Sp.size() == 0 || m_Sm.size() == 0) {
62  std::unique_ptr<DFA<char>> dfa = std::make_unique<DFA<char>>(0, m_Sp.size() != 0);
63  for (const char &a : m_alphabet) {
64  dfa->addTransition(0, a, 0);
65  }
66  return dfa;
67  }
68  std::ifstream file(getOutputFile());
69  auto dfa = toDFA(file);
70  file.close();
71  return dfa;
72  }
73  return nullptr;
74  }
75 
76  void SATNonCNFMethod::setStopTrigger(const std::chrono::seconds &timeLimit, std::atomic_bool &stopTrigger, const bool *stopPointer) {
77  mustStop = stopPointer;
78  }
79 
81  std::ifstream file(getOutputFile());
82  std::string line;
83  std::getline(file, line);
84  if (line.find("UNSATISFIABLE") != std::string::npos) {
85  return false;
86  }
87  else {
88  return true;
89  }
90  }
91 
92  std::string SATNonCNFMethod::getInputFile() const {
93  // TODO: find a way for Windows
94  return "/tmp/" + getFileName() + std::to_string(m_numberStates);
95  }
96 
97  std::string SATNonCNFMethod::getOutputFile() const {
98  return getInputFile() + "-test";
99  }
100  }
101  }
102 }
const std::set< char > m_alphabet
Definition: Method.h:121
The namespace of the library.
Definition: binary.h:11
double m_cpuTimeStart
Definition: Method.h:126
const std::set< std::string > m_Sm
The set of words to reject .
Definition: Method.h:105
virtual void setStopTrigger(const std::chrono::seconds &timeLimit, std::atomic_bool &stopTrigger, const bool *stopPointer) override
Sets the stop trigger for this method.
virtual std::unique_ptr< DFA< char > > toDFA(std::istream &stream) const =0
Effectively creates the DFA.
virtual bool solve() override
Solves the SAT/SMT problem induced by the method.
bool hasSolution() const
If solve() has been called and if there exists a solution, return True.
Definition: Method.h:60
virtual std::string getFileName() const =0
Gives the name of the file in which the boolean formula must be written.
const unsigned int m_numberStates
The number of states in the DFA to construct.
Definition: Method.h:109
The base class of all passive methods.
Definition: Method.h:40
virtual std::unique_ptr< DFA< char > > constructDFA() override
Constructs the DFA induced by the variables according to the method.
const std::set< std::string > m_Sp
The set of words to accept .
Definition: Method.h:101
virtual bool createFormula(std::ostream &stream)=0
Creates the boolean formula (must not be in CNF) and writes it in the stream.
bool isSatisfiable() const
Reads the output of the SAT solver and checks if the formula was satisfiable or not.