From d0e70ecf685d0386611a9aff03278b8335784942 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 22 Jan 2015 14:58:27 +0100
Subject: [PATCH] Generate random strings

---
 .../string/generate/RandomStringFactory.cpp   | 55 +++++++++++++++++++
 .../src/string/generate/RandomStringFactory.h | 32 +++++++++++
 arand2/src/arand.cpp                          | 13 +++++
 3 files changed, 100 insertions(+)
 create mode 100644 alib2algo/src/string/generate/RandomStringFactory.cpp
 create mode 100644 alib2algo/src/string/generate/RandomStringFactory.h

diff --git a/alib2algo/src/string/generate/RandomStringFactory.cpp b/alib2algo/src/string/generate/RandomStringFactory.cpp
new file mode 100644
index 0000000000..bbbaf5e106
--- /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 0000000000..4c02c31098
--- /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 9e2f41959e..e94b2d3b4f 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.");
 		}
-- 
GitLab