diff --git a/alib2algo/src/string/generate/RandomStringFactory.cpp b/alib2algo/src/string/generate/RandomStringFactory.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbbaf5e10627b3e634bbf04e9478a0f8c1c5ad8b --- /dev/null +++ b/alib2algo/src/string/generate/RandomStringFactory.cpp @@ -0,0 +1,55 @@ +/* + * RandomStringFactory.cpp + * + * Created on: 27. 3. 2014 + * Author: Jan Travnicek + */ + +#include "RandomStringFactory.h" + +#include <algorithm> + +namespace string { + +namespace generate { + +string::LinearString RandomStringFactory::generateLinearString( size_t size, size_t alphabetSize ) { + srand( time( NULL ) ); + + if(alphabetSize > 26) + throw exception::AlibException("Too big alphabet."); + + if( alphabetSize <= 0 ) + throw exception::AlibException( "Alphabet size must be greater than 0." ); + + std::set<alphabet::Symbol> alphabet; + while( alphabet.size( ) < alphabetSize ) { + std::string s( 1, rand() % 26 + 'a' ); + alphabet::Symbol symbol = alphabet::symbolFrom (s); + alphabet.insert( symbol ); + } + + return RandomStringFactory::generateLinearString( size, alphabet ); +} + +string::LinearString RandomStringFactory::generateLinearString( size_t size, std::set<alphabet::Symbol> alphabet) { + + if( alphabet.size() > 26) + throw exception::AlibException("Too big alphabet."); + + if( alphabet.size() <= 0 ) + throw exception::AlibException( "Alphabet size must be greater than 0." ); + + std::vector<alphabet::Symbol> alphabetList(alphabet.begin(), alphabet.end()); + std::vector<alphabet::Symbol> elems; + + for(size_t i = 0; i < size; i++) { + elems.push_back(alphabetList[rand() % alphabetList.size()]); + } + + return string::LinearString(elems); +} + +} /* namespace generate */ + +} /* namespace string */ diff --git a/alib2algo/src/string/generate/RandomStringFactory.h b/alib2algo/src/string/generate/RandomStringFactory.h new file mode 100644 index 0000000000000000000000000000000000000000..4c02c3109823f724dd2be2e6edbd9fe8f7472bea --- /dev/null +++ b/alib2algo/src/string/generate/RandomStringFactory.h @@ -0,0 +1,32 @@ +/* + * RandomAutomatonFactory.h + * + * Created on: 27. 3. 2014 + * Author: Jan Travnicek + */ + +#ifndef RANDOM_STRING_FACTORY_H_ +#define RANDOM_STRING_FACTORY_H_ + +#include <set> +#include <vector> + +#include <exception/AlibException.h> +#include <string/LinearString.h> + +namespace string { + +namespace generate { + +class RandomStringFactory { +public: + static string::LinearString generateLinearString( size_t size, size_t alphabetSize ); + static string::LinearString generateLinearString( size_t size, std::set<alphabet::Symbol> alphabet); + +}; + +} /* namespace generate */ + +} /* namespace string */ + +#endif /* RANDOM_STRING_FACTORY_H_ */ diff --git a/arand2/src/arand.cpp b/arand2/src/arand.cpp index 9e2f41959e4f39e10fff7703be722fb6468f877a..e94b2d3b4f7a44a5cc725af70f33123f87abff92 100644 --- a/arand2/src/arand.cpp +++ b/arand2/src/arand.cpp @@ -12,6 +12,7 @@ #include <factory/XmlDataFactory.hpp> #include "automaton/generate/RandomAutomatonFactory.h" #include "regexp/generate/RandomRegExpFactory.h" +#include "string/generate/RandomStringFactory.h" int main(int argc, char* argv[]) { try { @@ -20,6 +21,7 @@ int main(int argc, char* argv[]) { std::vector<std::string> allowed; allowed.push_back("FSM"); allowed.push_back("RE"); + allowed.push_back("ST"); TCLAP::ValuesConstraint<std::string> allowedVals( allowed ); TCLAP::ValueArg<std::string> type( "t", "type", "Type of generated structure", true, "FSM", &allowedVals); @@ -40,6 +42,9 @@ int main(int argc, char* argv[]) { TCLAP::ValueArg<int> depth( "e", "depth", "Depth of the regexp", false, 5, "integer"); cmd.add( depth ); + TCLAP::ValueArg<int> size( "s", "size", "Size of the string", false, 5, "integer"); + cmd.add( size ); + cmd.parse(argc,argv); if(!type.isSet()) throw exception::AlibException("Type is not defined."); @@ -67,6 +72,14 @@ int main(int argc, char* argv[]) { regexp::UnboundedRegExp res = regexp::generate::RandomRegExpFactory::generateUnboundedRegExp(leafNodes.getValue(), depth.getValue(), alphabetSize.getValue() ); alib::XmlDataFactory::toStdout(res); + } else if( type.getValue() == "ST" ) { + + if(!size.isSet()) throw exception::AlibException("Size is not defined."); + + if(!alphabetSize.isSet()) throw exception::AlibException("Alphabet size is not defined."); + + string::LinearString res = string::generate::RandomStringFactory::generateLinearString(size.getValue(), alphabetSize.getValue() ); + alib::XmlDataFactory::toStdout(res); } else { throw exception::AlibException("Invalid type."); }