From 0be8b646e5528c188a90caca94d60705fe92cd79 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 30 Sep 2014 11:56:46 +0200
Subject: [PATCH] sample astat2 bin

---
 astat2/makefile             |  73 ++++++++++++++++++++++++++
 astat2/src/AutomataStat.cpp | 100 ++++++++++++++++++++++++++++++++++++
 astat2/src/AutomataStat.h   |  46 +++++++++++++++++
 astat2/src/Settings.h       |  19 +++++++
 astat2/src/astat.cpp        |  82 +++++++++++++++++++++++++++++
 makefile                    |   1 +
 6 files changed, 321 insertions(+)
 create mode 100644 astat2/makefile
 create mode 100644 astat2/src/AutomataStat.cpp
 create mode 100644 astat2/src/AutomataStat.h
 create mode 100644 astat2/src/Settings.h
 create mode 100644 astat2/src/astat.cpp

diff --git a/astat2/makefile b/astat2/makefile
new file mode 100644
index 0000000000..fb3e0af5b6
--- /dev/null
+++ b/astat2/makefile
@@ -0,0 +1,73 @@
+SHELL:=/bin/bash
+EXECUTABLE:=astat2
+
+LDFLAGS= -L../alib2data/lib -rdynamic -lxml2 -lalib2data -Wl,-rpath,.
+
+OBJECTS:=$(patsubst src/%.cpp, obj/%.o, $(shell find src/ -name *cpp))
+
+.PHONY: all build clean
+
+all: build
+
+
+
+bin/$(EXECUTABLE): obj/ $(OBJECTS)
+	mkdir -p bin
+	$(CXX) $(OBJECTS) -o $@ $(LDFLAGS)
+
+obj/makefile: makefile
+	mkdir -p $(dir $@)
+	echo "SHELL:=/bin/bash" > $@
+	echo "SRCDIR:=" >> $@
+	echo "DEPTH:=" >> $@
+	echo "" >> $@
+	echo "CXXFLAGS:= -std=c++11 -Og -g -c -Wall -pedantic -Wextra -I../../\$$(DEPTH)alib2data/src/ -I/usr/include/libxml2/" >> $@
+	echo "" >> $@
+	echo "SOURCES:= \$$(shell find ../\$$(DEPTH)src/\$$(SRCDIR) -maxdepth 1 -type f -name \"*.cpp\")" >> $@
+	echo "DEPENDENCIES:= \$$(patsubst ../\$$(DEPTH)src/\$$(SRCDIR)%.cpp, ../\$$(DEPTH)obj/\$$(SRCDIR)%.d, \$$(SOURCES))" >> $@
+	echo "OBJECTS:= \$$(patsubst %.d, %.o, \$$(DEPENDENCIES))" >> $@
+	echo "SOURCES_DIRS:= \$$(shell find ../\$$(DEPTH)src/\$$(SRCDIR) -maxdepth 1 -mindepth 1 -type d)" >> $@
+	echo "OBJECTS_DIRS:= \$$(patsubst ../\$$(DEPTH)src/\$$(SRCDIR)%, %/, \$$(SOURCES_DIRS))" >> $@
+	echo "OBJECTS_DIRS_MAKEFILES:= \$$(patsubst %, %makefile, \$$(OBJECTS_DIRS))" >> $@
+	echo "" >> $@
+	echo ".PHONY: all" >> $@
+	echo ".PRECIOUS: \$$(DEPENDECIES) \$$(OBJECTS_DIRS_MAKEFILES)" >> $@
+	echo "" >> $@
+	echo "all: \$$(OBJECTS_DIRS) \$$(OBJECTS)" >> $@
+	echo "" >> $@
+	echo "%.d:" >> $@
+	echo "	@echo \"\$$(shell sha1sum <<< \"\$$@\" | sed \"s/  -//g\") = \\$$\$$(shell (\\$$\$$(CXX) -MM \\$$\$$(CXXFLAGS) \$$(patsubst ../\$$(DEPTH)obj/\$$(SRCDIR)%.d,../\$$(DEPTH)src/\$$(SRCDIR)%.cpp, \$$@) 2>/dev/null || echo \\\"\$$(patsubst ../\$$(DEPTH)obj/\$$(SRCDIR)%.d,../\$$(DEPTH)src/\$$(SRCDIR)%.cpp, \$$@) FORCE\\\") | sed \\\"s/.*://g;s/\\\\\\\\\\\\\\\\//g\\\")\" > \$$@" >> $@
+	echo "	@echo \"\$$(patsubst %.d,%.o, \$$@): \\$$\$$(\$$(shell sha1sum <<< \"\$$@\" | sed \"s/  -//g\"))\" >> \$$@" >> $@
+	echo "	@echo \"	\\$$\$$(CXX) \\$$\$$(CXXFLAGS) \\$$\$$< -o \$$(patsubst %.d,%.o, \$$@)\" >> \$$@" >> $@
+	echo "" >> $@
+	echo "%/makefile:" >> $@
+	echo "	mkdir -p \$$(dir \$$@)" >> $@
+	echo "	cp makefile \$$@" >> $@
+	echo "" >> $@
+	echo "%/: FORCE | %/makefile" >> $@
+	echo "	@accesstime=\`stat -c %Y \$$@\` && \\" >> $@
+	echo "	\$$(MAKE) -C \$$@ SRCDIR=\$$(SRCDIR)\$$(notdir \$$(patsubst %/, %, \$$@))/ DEPTH=\$$(DEPTH)../ && \\" >> $@
+	echo "	accesstime2=\`stat -c %Y \$$@\` && \\" >> $@
+	echo "	if [ "\$$\$$accesstime" -ne "\$$\$$accesstime2" ]; then \\" >> $@
+	echo "		touch .; \\" >> $@
+	echo "	fi" >> $@
+	echo "" >> $@
+	echo "FORCE:" >> $@
+	echo "" >> $@
+	echo "-include \$$(DEPENDENCIES)" >> $@
+
+obj/: FORCE | obj/makefile
+	$(MAKE) -C $@
+
+$(OBJECTS): obj/
+
+
+build: bin/$(EXECUTABLE)
+
+
+
+clean:
+	$(RM) -r *.o *.d bin lib obj test-bin test-obj
+
+FORCE:
+
diff --git a/astat2/src/AutomataStat.cpp b/astat2/src/AutomataStat.cpp
new file mode 100644
index 0000000000..e023eb4501
--- /dev/null
+++ b/astat2/src/AutomataStat.cpp
@@ -0,0 +1,100 @@
+/*
+ * AutomataStat.cpp
+ *
+ *  Created on: 20. 9. 2014
+ *	  Author: Jan Travnicek
+ */
+
+#include "AutomataStat.h"
+
+#include <exception/AlibException.h>
+
+#include <automaton/FSM/NFA.h>
+#include <automaton/FSM/DFA.h>
+
+#include <factory/DataFactory.hpp>
+
+void AutomataStat::stat(const automaton::Automaton& automaton, const Settings& settings) {
+	automaton.getData().Accept((void*) &settings, AutomataStat::AUTOMATA_STAT);
+}
+
+void AutomataStat::stat(const automaton::NFA& automaton, const Settings& settings) {
+	switch(settings.states) {
+	case PrintOptions::PRINT:
+		alib::DataFactory::toStdout(automaton.getStates());
+		break;
+	case PrintOptions::QUANTITY:
+		std::cout << automaton.getStates().size() << std::endl;
+		break;
+	case PrintOptions::BOTH:
+		std::cout << automaton.getStates().size() << std::endl;
+		alib::DataFactory::toStdout(automaton.getStates());
+		break;
+	case PrintOptions::NOOP:
+		break;
+	}
+}
+
+void AutomataStat::stat(const automaton::DFA& automaton, const Settings& settings) {
+}
+
+void AutomataStat::Visit(void*, const automaton::UnknownAutomaton&) const {
+	throw exception::AlibException("Unsupported automaton type UnknownAutomaton");
+}
+
+void AutomataStat::Visit(void*, const automaton::EpsilonNFA&) const {
+	throw exception::AlibException("Unsupported automaton type EpsilonNFA");
+}
+
+void AutomataStat::Visit(void* data, const automaton::NFA& automaton) const {
+	const Settings& in = *((const Settings*) data);
+	this->stat(automaton, in);
+}
+
+void AutomataStat::Visit(void* data, const automaton::DFA& automaton) const {
+	const Settings& in = *((const Settings*) data);
+	this->stat(automaton, in);
+}
+
+void AutomataStat::Visit(void*, const automaton::ExtendedNFA&) const {
+	throw exception::AlibException("Unsupported automaton type ExtendedNFA");
+}
+
+void AutomataStat::Visit(void*, const automaton::CompactNFA&) const {
+	throw exception::AlibException("Unsupported automaton type CompactNFA");
+}
+
+void AutomataStat::Visit(void*, const automaton::DPDA&) const {
+	throw exception::AlibException("Unsupported automaton type UnknownAutomaton");
+}
+
+void AutomataStat::Visit(void*, const automaton::SinglePopDPDA&) const {
+	throw exception::AlibException("Unsupported automaton type SinglePopDPDA");
+}
+
+void AutomataStat::Visit(void*, const automaton::InputDrivenNPDA&) const {
+	throw exception::AlibException("Unsupported automaton type InputDrivenNPDA");
+}
+
+void AutomataStat::Visit(void*, const automaton::VisiblyPushdownNPDA&) const {
+	throw exception::AlibException("Unsupported automaton type VisiblyPushdownNPDA");
+}
+
+void AutomataStat::Visit(void*, const automaton::RealTimeHeightDeterministicNPDA&) const {
+	throw exception::AlibException("Unsupported automaton type RealTimeHeightDeterministicNPDA");
+}
+
+void AutomataStat::Visit(void*, const automaton::NPDA&) const {
+	throw exception::AlibException("Unsupported automaton type NPDA");
+}
+
+void AutomataStat::Visit(void*, const automaton::SinglePopNPDA&) const {
+	throw exception::AlibException("Unsupported automaton type SinglePopNPDA");
+}
+
+void AutomataStat::Visit(void*, const automaton::OneTapeDTM&) const {
+	throw exception::AlibException("Unsupported automaton type OneTapeDTM");
+}
+
+const AutomataStat AutomataStat::AUTOMATA_STAT;
+
diff --git a/astat2/src/AutomataStat.h b/astat2/src/AutomataStat.h
new file mode 100644
index 0000000000..a532281949
--- /dev/null
+++ b/astat2/src/AutomataStat.h
@@ -0,0 +1,46 @@
+/*
+ * AutomataStat.h
+ *
+ *  Created on: 20. 9. 2014
+ *	  Author: Jan Travnicek
+ */
+
+#ifndef AUTOMATA_STAT_H_
+#define AUTOMATA_STAT_H_
+
+#include <algorithm>
+#include <deque>
+#include <set>
+
+#include "automaton/Automaton.h"
+#include <automaton/common/State.h>
+
+#include "Settings.h"
+
+class AutomataStat : public automaton::VisitableAutomatonBase::const_visitor_type {
+public:
+	static void stat(const automaton::Automaton& automaton, const Settings& settings);
+
+	static void stat(const automaton::NFA& automaton, const Settings& settings);
+	static void stat(const automaton::DFA& automaton, const Settings& settings);
+
+private:
+	void Visit(void*, const automaton::UnknownAutomaton& automaton) const;
+	void Visit(void*, const automaton::EpsilonNFA& automaton) const;
+	void Visit(void*, const automaton::NFA& automaton) const;
+	void Visit(void*, const automaton::DFA& automaton) const;
+	void Visit(void*, const automaton::ExtendedNFA& automaton) const;
+	void Visit(void*, const automaton::CompactNFA& automaton) const;
+	void Visit(void*, const automaton::DPDA& automaton) const;
+	void Visit(void*, const automaton::SinglePopDPDA& automaton) const;
+	void Visit(void*, const automaton::InputDrivenNPDA& automaton) const;
+	void Visit(void*, const automaton::VisiblyPushdownNPDA& automaton) const;
+	void Visit(void*, const automaton::RealTimeHeightDeterministicNPDA& automaton) const;
+	void Visit(void*, const automaton::NPDA& automaton) const;
+	void Visit(void*, const automaton::SinglePopNPDA& automaton) const;
+	void Visit(void*, const automaton::OneTapeDTM& automaton) const;
+
+	static const AutomataStat AUTOMATA_STAT;
+};
+
+#endif /* AUTOMATA_STAT_H_ */
diff --git a/astat2/src/Settings.h b/astat2/src/Settings.h
new file mode 100644
index 0000000000..00b9c97f37
--- /dev/null
+++ b/astat2/src/Settings.h
@@ -0,0 +1,19 @@
+#ifndef __SETTINGS_H__
+#define __SETTINGS_H__
+
+enum class PrintOptions {
+	PRINT,
+	QUANTITY,
+	BOTH,
+	NOOP
+};
+
+struct Settings {
+	PrintOptions states;
+	PrintOptions finalStates;
+	PrintOptions initialStates;
+	PrintOptions alphabet;
+	PrintOptions transitions;
+};
+
+#endif /* __SETTINGS_H_ */
diff --git a/astat2/src/astat.cpp b/astat2/src/astat.cpp
new file mode 100644
index 0000000000..f866469739
--- /dev/null
+++ b/astat2/src/astat.cpp
@@ -0,0 +1,82 @@
+#include <tclap/CmdLine.h>
+#include <vector>
+
+#include <factory/DataFactory.hpp>
+#include <exception/AlibException.h>
+
+#include "Settings.h"
+#include "AutomataStat.h"
+
+int main(int argc, char* argv[]) {
+	try {
+
+		std::vector<std::string> allowed;
+		allowed.push_back("noop");
+		allowed.push_back("print");
+		allowed.push_back("quantity");
+		allowed.push_back("both");
+		TCLAP::ValuesConstraint<std::string> allowedVals( allowed );
+
+		TCLAP::CmdLine cmd("Prints usefull information about automata, grammars, regexps, ...", ' ', "0.01");
+		TCLAP::ValueArg<std::string> states(		"s",	"states",		"Print or display number of states",			false,	"noop",		&allowedVals);
+		cmd.add(states);
+		TCLAP::ValueArg<std::string> finalStates(	"f",	"finalStates",		"Print or display number of final states",		false,	"noop",		&allowedVals);
+		cmd.add(finalStates);
+		TCLAP::ValueArg<std::string> initialStates(	"i",	"initialStates",	"Print or display number of initial states",		false,	"noop",		&allowedVals);
+		cmd.add(initialStates);
+		TCLAP::ValueArg<std::string> alphabet(		"a",	"alphabet",		"Print or display number of symbols in alphabet",	false,	"noop",		&allowedVals);
+		cmd.add(alphabet);
+		TCLAP::ValueArg<std::string> transitions(	"t",	"transitions",		"Print or display number of transitions",		false,	"noop",		&allowedVals);
+		cmd.add(transitions);
+
+		TCLAP::UnlabeledValueArg<std::string>  file( "file", "Read from file", false, "-", "file");
+                cmd.add( file );
+
+		cmd.parse(argc,argv);
+
+		Settings settings = {PrintOptions::NOOP, PrintOptions::NOOP, PrintOptions::NOOP, PrintOptions::NOOP, PrintOptions::NOOP };
+
+		if(states.isSet()) {
+			if(states.getValue() == "noop") {
+				settings.states = PrintOptions::NOOP;
+			} else if(states.getValue() == "print") {
+				settings.states = PrintOptions::PRINT;
+			} else if(states.getValue() == "quantity") {
+				settings.states = PrintOptions::QUANTITY;
+			} else if(states.getValue() == "both") {
+				settings.states = PrintOptions::BOTH;
+			}
+		}
+
+		std::list<sax::Token> tokens;
+		if(file.isSet()) {
+			sax::SaxParseInterface::parseStdin(tokens);
+		} else {
+			sax::SaxParseInterface::parseFile(file.getValue(), tokens);
+		}
+
+		if( alib::FromXMLParsers::automatonParser.first(tokens)) {
+			automaton::Automaton automaton = alib::DataFactory::fromTokens<automaton::Automaton>(tokens);
+
+			AutomataStat::stat(automaton, settings);
+
+			return 0;
+		} else if( alib::FromXMLParsers::grammarParser.first(tokens)) {
+			grammar::Grammar grammar = alib::DataFactory::fromTokens<grammar::Grammar>(tokens);
+
+			return 0;
+		} else if( alib::FromXMLParsers::regexpParser.first(tokens)) {
+			regexp::RegExp regexp = alib::DataFactory::fromTokens<regexp::RegExp>(tokens);
+
+			return 0;
+		} else {
+			throw exception::AlibException( "Input not recognized." );
+			return 1;
+		}
+	} catch( const exception::AlibException & e ) {
+		alib::DataFactory::toStdout( e );
+		return 1;
+	} catch (...) {
+		return 127;
+	}
+}
diff --git a/makefile b/makefile
index e3f2807f19..e1b158ae2b 100644
--- a/makefile
+++ b/makefile
@@ -15,6 +15,7 @@ SUBDIRS_BINS = acat2 \
                aminimize2 \
                anormalize2 \
                arand2 \
+	       astat2 \
                atrim2 \
 
 SUBDIRS_WITH_MAKE = $(dir $(wildcard */makefile))
-- 
GitLab