46 #include<boost/program_options.hpp> 50 #include"../AutomataInterface.hh" 59 namespace Poptions = boost::program_options;
67 const string program(
"dd-trimstates");
68 const string briefcomment(
": this program removes some states from the given ddaut automaton and\n write as output the new automaton (output file or standard output).\n\n");
69 const string detailedcomment=R
"(dd-trimstates removes some states from the given ddaut automaton and 70 writes as output the new automaton (output file or standard output). The states to 71 remove can be characterised by a regular expression regexp so that if the label 72 of the state matches the regular expression, it is simply removed (and all the transitions 73 involved with this state). 75 Here is a first example: 77 dd-trimstates file.ddaut -r '.*o\.p=ko.*' 79 In this example, we ask to remove the states in file.ddaut labeled with string such 80 that it contains the substring 'o.p=ko'. dd-trimstates can also be used to remove 81 deadlocks in file.ddaut, as follows: 83 dd-trimstates file.ddaut --no-deadlock 85 It is also possible to remove deadlocks by automatically disabling some controllable events 86 (see Ramadge and Wonham theory). In the following example, dd-trimstates will simply disable 87 some controllable events from some states to ensure that the result has no deadlocks. A file 88 with a 'cont' suffix is a file that contains a list of events, one per line. 90 dd-trimstates file.ddaut --no-deadlock -c controllable.cont 92 It is also possible to combine both type of operations like: 94 dd-trimstates file.ddaut --no-deadlock -c controllable.cont -r '.*o\.p=ko.*' 96 In this case, the program will first remove the states '.*o\.p=ko.*' and then remove 97 deadlocks by disabling controllable events. 115 Poptions::options_description & desc,
116 Poptions::variables_map & vm)
119 (
"help,h",
"produce help message")
120 (
"file,f", Poptions::value< string >(),
"file (.ddaut format)")
121 (
"regexp,r", Poptions::value< string >(),
"regular expression on the name of the states inside a set of quotes: 'regexp' for details see\n http://www.cplusplus.com/reference/regex/ECMAScript/")
122 (
"output,o", Poptions::value< string >(),
"outfile name (.ddaut format)")
123 (
"controllable,c", Poptions::value< string >(),
"controllable event name (.cont format, single list of events, one per line)")
124 (
"no-deadlock",
"removing deadlocks")
125 (
"verbose",
"verbose mode")
127 Poptions::positional_options_description p;
130 Poptions::store(Poptions::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
131 Poptions::notify(vm);
142 std::set<std::string> & controllable)
144 if(!contName.empty())
146 ifstream file(contName.c_str());
149 std::istream_iterator<std::string> it(file);
150 std::istream_iterator<std::string> end;
153 controllable.insert(*it);
167 trimDdAutStates2(
const string & fileName,
const string & output,
const string & regexp,
const string & contName,
bool noDeadLock,
bool verbose)
173 ifstream file(fileName.c_str());
191 return printCommandLineError(
Msg(
"Error when converting the DdAutFileDescriptor from %1% to a Finite Automaton") % fileName);
195 std::cout <<
"Verbose mode is on.\n";
201 std::cout <<
"Trimming states whose label matches the following regular expression: " << regexp <<
"\n";
203 std::regex reg(regexp);
204 for(
const auto & stateProperty: sManager.
storage())
206 if(std::regex_match(stateProperty,reg))
210 std::cout <<
"Deleting state: " << stateProperty <<
"\n";
216 if(verbose && regexp.empty())
218 std::cout <<
"No regular expression found.\n";
225 std::cout <<
"Need to trim deadlock states.\n";
228 std::set<std::string> controllable;
232 std::cout <<
"Controllable events:\n";
238 if(controllable.empty())
244 auto it = controllable.begin();
245 while(!found && it!=controllable.end())
247 found= eManager.
getEvent(e).find(*it)!=std::string::npos;
254 std::cout << eManager.
getEvent(e) <<
" (-> " << *it <<
")\n";
260 std::list<DdAutFsm::StatePropertyId> stateIds;
261 std::list<std::pair<DdAutFsm::StatePropertyId, DdAutFsm::EventPropertyId>> disabled;
263 std::inserter(disabled,disabled.begin())))
269 for(
const auto & d : disabled)
271 std::cout <<
"Disabling " 277 for(
const auto &
id: stateIds)
287 std::cout <<
"Writing resulting finite-state machine herebelow:\n";
295 std::cout <<
"Writing resulting finite-state machine in " << output <<
"\n";
297 ofstream outfile(output.c_str());
298 if(outfile.is_open())
304 std::cout <<
"done.\n";
318 trimDdAutStates(
const string & fileName,
const string & output,
const string & regexp,
const string & contName,
bool noDeadLock,
bool verbose)
324 return printCommandLineError(
Msg(
"Expecting an output file with a name ending with .ddaut, but I read %1%") % output);
327 if(!contName.empty())
331 return printCommandLineError(
Msg(
"Expecting a file containing a list of controllable events with a name ending with .cont, but I read %1%") % contName);
338 return trimDdAutStates2(fileName, output,regexp,contName,noDeadLock,verbose);
357 std::string fileName, output, contName;
359 bool deadlock =
false;
364 Poptions::options_description desc(
"Options");
365 Poptions::variables_map vm;
373 fileName = vm[
"file"].as<std::string> ();
375 if(vm.count(
"output"))
377 output = vm[
"output"].as<std::string>();
379 if(vm.count(
"regexp"))
381 regexp = vm[
"regexp"].as<std::string>();
383 if(vm.count(
"no-deadlock"))
387 if(vm.count(
"verbose"))
391 if(vm.count(
"controllable"))
393 contName = vm[
"controllable"].as<std::string> ();
396 catch(Poptions::required_option & e)
400 catch(Poptions::error & e)
406 return trimDdAutStates(fileName, output,regexp, contName, deadlock, verbose);
size_t readControllableEvents(const string &contName, std::set< std::string > &controllable)
EventPropertyIdIterator eventEnd() const
Set of functions that trim states/transitions from StateMachines.
StatePropertyManager< DdAutStateLabel, DdAutStateId > DdAutStateManager
StatePropertyId statePropertyId(const StateProperty &stateProperty)
const size_t ERROR_UNHANDLED_EXCEPTION
size_t printCommandLineError(const string &msg)
bool faFromDescriptor(const DdAutFileDescriptor &descriptor, DdAutFA &fa, DdAutStateManager &sManager, DdAutEventManager &eManager)
vector< string > options(numberOfOptions)
A control pattern is a mapping between the set of Events of a StateMachine and a Boolean.
StateIterator stateEnd() const
bool faToDdAutFile(ostream &stream, const ConstManagedDdAutFA &mFa)
const string detailedcomment
bool match(const std::string &fileName, const std::string &suffix) const
DdAutFsm::EventPropertyId Event
void initialiseOptions(int argc, char *argv[], Poptions::options_description &desc, Poptions::variables_map &vm)
bool controlledTrimmingDeadlockState(Fsm &machine, ControlPattern< Fsm > &pattern, BeliefStateIterator bfBegin, BeliefStateIterator bfEnd, StateIdInsertIterator insertIt, DisableInsertIterator disableIt)
remove deadlock states of the machine by exploiting the events that are disabled in the current Contr...
bool disable(const Event &e)
Disable an event of the StateMachine.
virtual void deleteState(State state)
delete state
const Event & getEvent(EventId id) const
size_t trimDdAutStates(const string &fileName, const string &output, const string ®exp, const string &contName, bool noDeadLock, bool verbose)
FileSuffixes suffixes({"ddaut","cont"})
size_t trimDdAutStates2(const string &fileName, const string &output, const string ®exp, const string &contName, bool noDeadLock, bool verbose)
_EventPropertyId EventPropertyId
const string program("dd-trimstates")
EventPropertyIdIterator eventBegin() const
Diades::Utils::Verbose verbose(const char *msg)
EventManager< DdAutEventLabel, DdAutEventId > DdAutEventManager
StateIterator stateBegin() const
const string briefcomment(": this program removes some states from the given ddaut automaton and\ write as output the new automaton (output file or standard output).\")
StateMachine< DdAutStateId, DdAutEventId > DdAutFsm
const std::vector< Info > & storage() const
const StateProperty & getStateProperty(StatePropertyId id) const
int main(int argc, char **argv)
State getState(const StatePropertyId &sProperty) const
virtual bool readStream(std::istream &stream)
void printUsage(const po::options_description &desc)