diff --git a/alib2algo/src/automaton/transform/ReverseFSM.cpp b/alib2algo/src/automaton/transform/ReverseFSM.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6775b53b0c9a56facaae2451583977996ee46035 --- /dev/null +++ b/alib2algo/src/automaton/transform/ReverseFSM.cpp @@ -0,0 +1,112 @@ +/* + * ReverseFSM.cpp + * + * Created on: 7. 11. 2014 + * Author: Tomas Pecka + */ + +#include "ReverseFSM.h" + +#include <automaton/FSM/MultiInitialStateNFA.h> +#include <exception/AlibException.h> + +#include <vector> +#include <algorithm> + +namespace automaton { + +namespace transform { + +automaton::Automaton ReverseFSM::convert(const Automaton& automaton) { + automaton::Automaton* out = NULL; + automaton.getData().Accept((void*) &out, ReverseFSM::REVERSE_FSM); + automaton::Automaton res(std::move(*out)); + delete out; + return std::move(res); +} + +automaton::MultiInitialStateNFA ReverseFSM::convert(const automaton::DFA& automaton) +{ + automaton::MultiInitialStateNFA res; + + res.setStates(automaton.getStates()); + res.addFinalState(automaton.getInitialState()); + for(const auto& q : automaton.getFinalStates()) + res.addInitialState(q); + res.setInputSymbols(automaton.getInputAlphabet()); + + for(const auto& t : automaton.getTransitions()) + res.addTransition(t.second, t.first.second, t.first.first); + + return res; +} + +automaton::MultiInitialStateNFA ReverseFSM::convert(const automaton::NFA& automaton) +{ + automaton::MultiInitialStateNFA res; + + res.setStates(automaton.getStates()); + res.addFinalState(automaton.getInitialState()); + for(const auto& q : automaton.getFinalStates()) + res.addInitialState(q); + res.setInputSymbols(automaton.getInputAlphabet()); + + for(const auto& t : automaton.getTransitions()) + for(const auto& q : t.second) + res.addTransition(q, t.first.second, t.first.first); + + return res; +} + +automaton::MultiInitialStateNFA ReverseFSM::convert(const automaton::MultiInitialStateNFA& automaton) +{ + automaton::MultiInitialStateNFA res; + + res.setStates(automaton.getStates()); + for(const auto& q : automaton.getInitialStates()) + res.addFinalState(q); + for(const auto& q : automaton.getFinalStates()) + res.addInitialState(q); + res.setInputSymbols(automaton.getInputAlphabet()); + + for(const auto& t : automaton.getTransitions()) + for(const auto& q : t.second) + res.addTransition(q, t.first.second, t.first.first); + + return res; +} + + +void ReverseFSM::Visit(void* data, const DFA& automaton) const { + automaton::Automaton*& out = *((automaton::Automaton**) data); + out = new automaton::Automaton(this->convert(automaton)); +} + +void ReverseFSM::Visit(void* data, const NFA& automaton) const { + automaton::Automaton*& out = *((automaton::Automaton**) data); + out = new automaton::Automaton(this->convert(automaton)); +} + +void ReverseFSM::Visit(void* data, const MultiInitialStateNFA& automaton) const { + automaton::Automaton*& out = *((automaton::Automaton**) data); + out = new automaton::Automaton(this->convert(automaton)); +} + +void ReverseFSM::Visit(void*, const ExtendedNFA&) const { + throw exception::AlibException("Unsupported automaton type ExtendedNFA"); +} + +void ReverseFSM::Visit(void*, const EpsilonNFA&) const { + throw exception::AlibException("Unsupported automaton type EpsilonNFA"); +} + +void ReverseFSM::Visit(void*, const CompactNFA&) const { + throw exception::AlibException("Unsupported automaton type CompactNFA"); +} + +const ReverseFSM ReverseFSM::REVERSE_FSM; + +} /* namespace transform */ + +} /* namespace automaton */ + diff --git a/alib2algo/src/automaton/transform/ReverseFSM.h b/alib2algo/src/automaton/transform/ReverseFSM.h new file mode 100644 index 0000000000000000000000000000000000000000..b5c3a7c0fdb88c24b3a21f99708e6ea54e0caa22 --- /dev/null +++ b/alib2algo/src/automaton/transform/ReverseFSM.h @@ -0,0 +1,45 @@ +/* + * ReverseFSM.h + * + * Created on: 7. 11. 2014 + * Author: Tomas Pecka + */ + +#ifndef REVERSE_FSM_H_ +#define REVERSE_FSM_H_ + +#include <automaton/Automaton.h> +#include <automaton/common/State.h> + +namespace automaton { + +namespace transform { + +class ReverseFSM : public VisitableConstFSMBase { +public: + static automaton::MultiInitialStateNFA convert(const automaton::DFA& automaton); + static automaton::MultiInitialStateNFA convert(const automaton::NFA& automaton); + static automaton::MultiInitialStateNFA convert(const automaton::MultiInitialStateNFA& automaton); + + /** + * Creates finite automaton accepting reverse language of given automaton + * @param automaton FSM + * @return Automaton reverse automaton + */ + static automaton::Automaton convert( const automaton::Automaton & automaton ); +private: + void Visit(void*, const DFA& automaton) const; + void Visit(void*, const NFA& automaton) const; + void Visit(void*, const MultiInitialStateNFA& automaton) const; + void Visit(void*, const ExtendedNFA& automaton) const; + void Visit(void*, const EpsilonNFA& automaton) const; + void Visit(void*, const CompactNFA& automaton) const; + + static const ReverseFSM REVERSE_FSM; +}; + +} /* namespace transform */ + +} /* namespace automaton */ + +#endif /* REVERSE_FSM_H_ */ diff --git a/areverse2/makefile b/areverse2/makefile new file mode 100644 index 0000000000000000000000000000000000000000..227a7c9fa3dfb8471f756087b4ceaa88c184ce3f --- /dev/null +++ b/areverse2/makefile @@ -0,0 +1,128 @@ +SHELL:=/bin/bash +EXECUTABLE:=areverse2 + +define NEW_LINE + + +endef + +export NEW_LINE + +LDFLAGS_DEBUG:=-L../alib2data/lib-debug -L../alib2algo/lib-debug -rdynamic -lxml2 -lalib2data -lalib2algo -Wl,-rpath,. + +LDFLAGS_RELEASE:=-L../alib2data/lib-release -L../alib2algo/lib-release -rdynamic -lxml2 -lalib2data -lalib2algo -Wl,-rpath,. + +OBJECTS_DEBUG:=$(patsubst src/%.cpp, obj-debug/%.o, $(shell find src/ -name *cpp)) + +OBJECTS_RELEASE:=$(patsubst src/%.cpp, obj-release/%.o, $(shell find src/ -name *cpp)) + +.PHONY: all build-debug clean-debug doc + +all: + @echo "What to do master?" + +obj%/makefile: makefile + mkdir -p $(dir $@) + echo "\ + SHELL:=/bin/bash$${NEW_LINE}\ + SRCDIR:=$${NEW_LINE}\ + DEPTH:=$${NEW_LINE}\ + OBJECTS_BASE_DIR:=$${NEW_LINE}\ + $${NEW_LINE}\ + define NEW_LINE$${NEW_LINE}\ + $${NEW_LINE}\ + $${NEW_LINE}\ + endef$${NEW_LINE}\ + $${NEW_LINE}\ + export NEW_LINE$${NEW_LINE}\ + $${NEW_LINE}\ + CXXFLAGS:= -std=c++11 \$$(CXX_OTHER_FLAGS) -c -Wall -pedantic -Wextra -fPIC -I/usr/include/libxml2/ -I../../\$$(DEPTH)alib2data/src/ -I../../\$$(DEPTH)alib2algo/src/$${NEW_LINE}\ + $${NEW_LINE}\ + SOURCES:= \$$(shell find ../\$$(DEPTH)\$$(SOURCES_BASE_DIR)/\$$(SRCDIR) -maxdepth 1 -type f -name \"*.cpp\")$${NEW_LINE}\ + DEPENDENCIES:= \$$(patsubst ../\$$(DEPTH)\$$(SOURCES_BASE_DIR)/\$$(SRCDIR)%.cpp, ../\$$(DEPTH)\$$(OBJECTS_BASE_DIR)/\$$(SRCDIR)%.d, \$$(SOURCES))$${NEW_LINE}\ + OBJECTS:= \$$(patsubst %.d, %.o, \$$(DEPENDENCIES))$${NEW_LINE}\ + SOURCES_DIRS:= \$$(shell find ../\$$(DEPTH)\$$(SOURCES_BASE_DIR)/\$$(SRCDIR) -maxdepth 1 -mindepth 1 -type d)$${NEW_LINE}\ + OBJECTS_DIRS:= \$$(patsubst ../\$$(DEPTH)\$$(SOURCES_BASE_DIR)/\$$(SRCDIR)%, %/, \$$(SOURCES_DIRS))$${NEW_LINE}\ + OBJECTS_DIRS_MAKEFILES:= \$$(patsubst %, %makefile, \$$(OBJECTS_DIRS))$${NEW_LINE}\ + $${NEW_LINE}\ + .PHONY: all$${NEW_LINE}\ + .PRECIOUS: \$$(DEPENDECIES) \$$(OBJECTS_DIRS_MAKEFILES)$${NEW_LINE}\ + $${NEW_LINE}\ + all: \$$(OBJECTS_DIRS) \$$(OBJECTS)$${NEW_LINE}\ + $${NEW_LINE}\ + %.d: makefile$${NEW_LINE}\ + @echo \"\\$${NEW_LINE}\ + \$$(shell sha1sum <<< \"\$$@\" | sed \"s/ -//g\") = \\$$\$$(shell (\\$$\$$(CXX) -MM \\$$\$$(CXXFLAGS) \$$(patsubst ../\$$(DEPTH)\$$(OBJECTS_BASE_DIR)/\$$(SRCDIR)%.d,../\$$(DEPTH)\$$(SOURCES_BASE_DIR)/\$$(SRCDIR)%.cpp, \$$@) 2>/dev/null || echo \\\"\$$(patsubst ../\$$(DEPTH)\$$(OBJECTS_BASE_DIR)/\$$(SRCDIR)%.d,../\$$(DEPTH)\$$(SOURCES_BASE_DIR)/\$$(SRCDIR)%.cpp, \$$@) FORCE\\\") | sed \\\"s/.*://g;s/\\\\\\\\\\\\\\\\//g\\\")\$$\$${NEW_LINE}\\$${NEW_LINE}\ + \$$(patsubst %.d,%.o, \$$@): \\$$\$$(\$$(shell sha1sum <<< \"\$$@\" | sed \"s/ -//g\")) makefile\$$\$${NEW_LINE}\\$${NEW_LINE}\ + \\$$\$$(CXX) \\$$\$$(CXXFLAGS) \\$$\$$< -o \$$(patsubst %.d,%.o, \$$@)\$$\$${NEW_LINE}\\$${NEW_LINE}\ + \" > \$$@$${NEW_LINE}\ + $${NEW_LINE}\ + %/makefile: makefile$${NEW_LINE}\ + mkdir -p \$$(dir \$$@)$${NEW_LINE}\ + cp makefile \$$@$${NEW_LINE}\ + $${NEW_LINE}\ + %/: FORCE | %/makefile$${NEW_LINE}\ + @accesstime=\`stat -c %Y \$$@\` && \\$${NEW_LINE}\ + \$$(MAKE) -C \$$@ SRCDIR=\$$(SRCDIR)\$$(notdir \$$(patsubst %/, %, \$$@))/ DEPTH=\$$(DEPTH)../ OBJECTS_BASE_DIR=\$$(OBJECTS_BASE_DIR) SOURCES_BASE_DIR=\$$(SOURCES_BASE_DIR) CXX_OTHER_FLAGS=\"\$$(CXX_OTHER_FLAGS)\" && \\$${NEW_LINE}\ + accesstime2=\`stat -c %Y \$$@\` && \\$${NEW_LINE}\ + if [ "\$$\$$accesstime" -ne "\$$\$$accesstime2" ]; then \\$${NEW_LINE}\ + touch .; \\$${NEW_LINE}\ + fi$${NEW_LINE}\ + $${NEW_LINE}\ + FORCE:$${NEW_LINE}\ + $${NEW_LINE}\ + -include \$$(DEPENDENCIES)" > $@ + +debug: build-debug + +release: build-release + +clean: clean-debug clean-release + $(RM) -r doc + + + +bin-debug/$(EXECUTABLE): obj-debug/ $(OBJECTS_DEBUG) + mkdir -p $(dir $@) + $(CXX) $(OBJECTS_DEBUG) -o $@ $(LDFLAGS_DEBUG) + +bin-release/$(EXECUTABLE): obj-release/ $(OBJECTS_RELEASE) + mkdir -p $(dir $@) + $(CXX) $(OBJECTS_RELEASE) -o $@ $(LDFLAGS_RELEASE) + + + +obj-debug/: FORCE | obj-debug/makefile + $(MAKE) -C $@ OBJECTS_BASE_DIR=obj-debug SOURCES_BASE_DIR=src CXX_OTHER_FLAGS="-g -O0" + +obj-release/: FORCE | obj-release/makefile + $(MAKE) -C $@ OBJECTS_BASE_DIR=obj-release SOURCES_BASE_DIR=src CXX_OTHER_FLAGS="-O3" + + + +$(OBJECTS_DEBUG): obj-debug/ + +$(OBJECTS_RELEASE): obj-release/ + + +build-debug: bin-debug/$(EXECUTABLE) + +build-release: bin-release/$(EXECUTABLE) + + + +clean-debug: + $(RM) -r *.o *.d bin-debug obj-debug + +clean-release: + $(RM) -r *.o *.d bin-release obj-release + + + +FORCE: + + + +doc: + doxygen + diff --git a/areverse2/src/areverse.cpp b/areverse2/src/areverse.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0db912965eb7597ea83374f7eb528024037f2c41 --- /dev/null +++ b/areverse2/src/areverse.cpp @@ -0,0 +1,48 @@ +/* + * areverse.cpp + * + * Created on: 7. 11. 2014 + * Author: Tomas Pecka + */ + +#include <iostream> +#include <tclap/CmdLine.h> + +#include "exception/AlibException.h" +#include "factory/DataFactory.hpp" +#include "automaton/transform/ReverseFSM.h" + +int main(int argc, char** argv) { + + try { + TCLAP::CmdLine cmd("Automaton FSM reverse binary", ' ', "0.01"); + + TCLAP::ValueArg<std::string> input( "a", "automaton", "Automaton to reverse", false, "-", "file"); + cmd.add( input ); + + cmd.parse(argc, argv); + + std::list<sax::Token> tokens; + if(input.isSet()) { + if(input.getValue() == "-") { + sax::SaxParseInterface::parseStdin(tokens); + } else { + sax::SaxParseInterface::parseFile(input.getValue(), tokens); + } + } else { + sax::SaxParseInterface::parseStdin(tokens); + } + + alib::DataFactory::toStdout(automaton::transform::ReverseFSM::convert(alib::DataFactory::fromTokens<automaton::Automaton>(tokens))); + return 0; + + } catch (const exception::AlibException& exception) { + alib::DataFactory::toStdout(exception); + return 1; + } catch(const TCLAP::ArgException& exception) { + std::cout << exception.error() << std::endl; + return 2; + } catch(...) { + return 127; + } +} diff --git a/makefile b/makefile index a86e54808405b297bc9a4d4cc3e9eed5c5de48a5..6d09c66776b40726ebcdd28298536d4a7e245cba 100644 --- a/makefile +++ b/makefile @@ -16,6 +16,7 @@ SUBDIRS_BINS = acat2 \ anormalize2 \ arand2 \ arename2 \ + areverse2 \ arun2 \ astat2 \ astringology2 \