Browse Source

Added xpand template files for the creation process. The files are all plane code.

jos.itemis@gmail.com 14 years ago
parent
commit
9df5f037e3

+ 29 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CMakeLists.xpt

@@ -0,0 +1,29 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'CMakeLists.txt'»
+cmake_minimum_required(VERSION 2.8)
+
+set(SRC
+	Statemachine.cpp
+	MyStatemachine.cpp
+	main.cpp
+)
+
+add_definitions( -Wall -O0 -g)
+add_executable(MyStatemachineMain ${SRC})
+«ENDFILE»
+
+«ENDDEFINE»

+ 49 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CustomEventH.xpt

@@ -0,0 +1,49 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'MyEvents.h'»
+#ifndef MYEVENTS_H_
+#define MYEVENTS_H_
+
+#include "Event.h"
+
+enum EventID {
+	event1,
+	event2,
+	event3,
+	event4,
+	event5,
+	event_last
+};
+
+class Event1 : public _Event { public: Event1() : _Event((uint32_t)event1) {} };
+class Event2 : public _Event { public: Event2() : _Event((uint32_t)event2) {} };
+class Event3 : public _Event { public: Event3() : _Event((uint32_t)event3) {} };
+
+/* Event with internal variable */
+class Event4 : public _Event {
+ public:
+	Event4() : _Event((uint32_t)event4) {}
+
+	uint32_t eventVar1;
+};
+
+class Event5 : public _Event { public: Event5() : _Event((uint32_t)event5) {} };
+
+
+#endif /* MYEVENTS_H_ */
+«ENDFILE»
+
+«ENDDEFINE»

+ 52 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CustomRepositoryH.xpt

@@ -0,0 +1,52 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'MyDataRepository.h'»
+#ifndef MYDATAREPOSITORY_H_
+#define MYDATAREPOSITORY_H_
+
+#include "definition.h"
+
+class MyDataRepository {
+
+	uint32_t var1;      /* variable in the main region */
+	uint32_t counter;   /* counter in the main region */
+	float sub_counter;  /* counter in a sub region */
+
+	virtual void lock(){};
+	virtual void unlock(){};
+
+public:
+
+	MyDataRepository() {};
+	virtual ~MyDataRepository() {};
+
+	void init_data(uint32_t _var1, uint32_t _counter, float _sub_counter)
+	{ var1 = _var1; counter = _counter; sub_counter = _sub_counter; }
+
+	uint32_t get_var1() { uint32_t value; lock(); value=var1; unlock(); return(value); }
+	uint32_t get_counter() { uint32_t value; lock(); value=counter; unlock(); return(value); }
+	float get_sub_counter() { float value; lock(); value=sub_counter; unlock(); return(value); }
+
+	void set_var1(const uint32_t value) { lock(); var1 = value; unlock(); }
+	void set_counter(const uint32_t value) { lock(); counter = value; unlock(); }
+	void set_sub_counter(const float value) { lock(); sub_counter = value; unlock(); }
+
+};
+
+#endif /* MYDATAREPOSITORY_H_ */
+«ENDFILE»
+
+«ENDDEFINE»

+ 189 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CustomStatemachineCPP.xpt

@@ -0,0 +1,189 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'MyStatemachine.cpp'»
+#include <iostream>
+#include "MyStatemachine.h"
+
+MyStatemachine::MyStatemachine() : Statemachine(MAX_PARALLEL_STATES) {
+}
+
+MyStatemachine::~MyStatemachine() {
+}
+
+
+bool MyStatemachine::condition_state1_id1(const _Event & event, const MyDataRepository & data)
+{
+	return (event.getID() == (uint32_t)event1);
+}
+
+bool MyStatemachine::condition_state3_id1(const _Event & event, const MyDataRepository & data)
+{
+	return (event.getID() == (uint32_t)event2);
+}
+
+bool MyStatemachine::condition_state3_id2(const _Event & event, const MyDataRepository & data)
+{
+	return (event.getID() == (uint32_t)event3);
+}
+
+bool MyStatemachine::condition_state4_id1(const _Event & event, const MyDataRepository & data)
+{
+	return (event.getID() == (uint32_t)event4);
+}
+
+bool MyStatemachine::condition_state4_id2(const _Event & event, const MyDataRepository & data)
+{
+	return (event.getID() == (uint32_t)event5);
+}
+
+bool MyStatemachine::condition_state4_id3(const _Event & event, const MyDataRepository & data)
+{
+	return (event.getID() == (uint32_t)event2);
+}
+
+void MyStatemachine::statement_state1_id1()
+{
+	std::cout << "statement_state1_id1 \n";
+	/* exit  S1 */
+	/* transition action event1 */
+	/* entry S2 */
+	/* entry S3 */
+	state[0] = State3;
+}
+
+void MyStatemachine::statement_state3_id1()
+{
+	std::cout << "statement_state3_id1 \n";
+	/* exit S3 */
+	/* exit S2 */
+	/* transition action event 2 */
+	/* entry S1 */
+	state[0] = State1;
+}
+
+void MyStatemachine::statement_state4_id1()
+{
+	std::cout << "statement_state4_id1 \n";
+	/* exit S4 */
+	/* exit S2 */
+	/* transition action event 2 */
+	/* entry S1 */
+	state[0] = State3;
+
+}
+
+void MyStatemachine::statement_state4_id2()
+{
+	std::cout << "statement_state4_id2 \n";
+	/* exit S4 */
+	/* exit S2 */
+	/* transition action event 5 */
+	/* entry S1 */
+	state[0] = State1;
+
+}
+
+void MyStatemachine::statement_state4_id3()
+{
+	std::cout << "statement_state4_id3 \n";
+	/* exit S4 */
+	/* transition action event 3 */
+	/* entry S3 */
+	state[0] = State1;
+
+}
+
+void MyStatemachine::statement_state3_id2()
+{
+	std::cout << "statement_state3_id2 \n";
+	/* exit S3 */
+	/* transition action event 4 */
+	/* entry S4 */
+	state[0] = State4;
+
+}
+
+void MyStatemachine::cycle_state1(const _Event& ev)
+{
+	if (condition_state1_id1(ev, *this)) {
+		statement_state1_id1();
+		return;
+	}
+
+}
+
+void MyStatemachine::cycle_state3(const _Event& ev)
+{
+	if (condition_state3_id1(ev, *this)) {
+		statement_state3_id1();
+		return;
+	}
+	if (condition_state3_id2(ev, *this)) {
+		statement_state3_id2();
+	}
+}
+
+void MyStatemachine::cycle_state4(const _Event& ev)
+{
+	if (condition_state4_id1(ev, *this)) {
+		statement_state4_id1();
+		return;
+	}
+	if (condition_state4_id2(ev, *this)) {
+		statement_state4_id2();
+		return;
+	}
+	if (condition_state4_id3(ev, *this)) {
+		statement_state4_id3();
+		return;
+	}
+}
+
+
+void MyStatemachine::init()
+{
+	/* initialize enter states */
+	state[0] = State1;
+
+	/* uninitialized states:
+	 * state[1] = last_state; */
+}
+
+
+
+void MyStatemachine::_runCycle(Event ev)
+{
+	for (int i=0; i<MAX_PARALLEL_STATES; ++i) {
+		switch(state[i]) {
+		case State1:
+			cycle_state1(*ev);
+			break;
+		case State3:
+			cycle_state3(*ev);
+			break;
+		case State4:
+			cycle_state4(*ev);
+			break;
+		default:
+			/* no state found */
+			break;
+		}
+	}
+}
+
+«ENDFILE»
+
+«ENDDEFINE»

+ 79 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CustomStatemachineH.xpt

@@ -0,0 +1,79 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'MyStatemachine.h'»
+#ifndef MYSTATEMACHINE_H_
+#define MYSTATEMACHINE_H_
+
+#include "Statemachine.h"
+#include "MyDataRepository.h"
+#include "MyEvents.h"
+
+//namespace MyStatemachineNS {
+
+#define MAX_PARALLEL_STATES 1
+
+class MyStatemachine: public Statemachine, public MyDataRepository {
+
+public:
+	enum States {
+		State1,
+		State2,
+		State3,
+		State4,
+		last_state
+	};
+
+protected:
+	/* conditions */
+	bool condition_state1_id1(const _Event& event, const MyDataRepository& data);
+	bool condition_state3_id1(const _Event& event, const MyDataRepository& data);
+	bool condition_state3_id2(const _Event& event, const MyDataRepository& data);
+	bool condition_state4_id1(const _Event& event, const MyDataRepository& data);
+	bool condition_state4_id2(const _Event& event, const MyDataRepository& data);
+	bool condition_state4_id3(const _Event& event, const MyDataRepository& data);
+
+	/* statement */
+	void statement_state1_id1();
+	void statement_state3_id1();
+	void statement_state3_id2();
+	void statement_state4_id1();
+	void statement_state4_id2();
+	void statement_state4_id3();
+
+	/* cycles */
+	void cycle_state1(const _Event& ev);
+	void cycle_state3(const _Event& ev);
+	void cycle_state4(const _Event& ev);
+
+	virtual void _runCycle(Event ev);
+
+public:
+
+	MyStatemachine();
+	virtual ~MyStatemachine();
+
+	virtual void init();
+
+	//virtual void queueEvent(const Event& ev);
+
+};
+
+//}; // namespace
+#endif /* MYSTATEMACHINE_H_ */
+
+«ENDFILE»
+
+«ENDDEFINE»

+ 45 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/EventH.xpt

@@ -0,0 +1,45 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'Event.h'»
+#ifndef EVENT_H_
+#define EVENT_H_
+
+#include "definition.h"
+
+class _Event {
+
+protected:
+	uint32_t ID;
+
+public:
+	_Event() : ID(-1) {}
+	_Event(int _ID) : ID(_ID) {}
+	virtual ~_Event() {}
+
+	uint32_t getID() const { return(ID); }
+	void setID(uint32_t id) { ID = id; }
+
+};
+
+typedef _Event* Event;
+
+template <class A>
+Event createEvent() { return new A; }
+
+#endif /* EVENT_H_ */
+«ENDFILE»
+
+«ENDDEFINE»

+ 16 - 2
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/Main.xpt

@@ -15,9 +15,23 @@ Contais the root templates that call the different file templates.
 
 «DEFINE main FOR ExecutionFlow»
 
-	«EXPAND StatechartH::file FOR this»
-	«EXPAND StatechartCPP::file FOR this»
+	«REM» basis files «ENDREM»
+	«EXPAND EventH::file FOR this»
+	«EXPAND StatemachineH::file FOR this»
+	«EXPAND StatemachineExceptionH::file FOR this»
+	«EXPAND definitionH::file FOR this»
 	
+	«EXPAND StatemachineCPP::file FOR this»
+
+	«EXPAND CustomStatemachineH::file FOR this»
+	«EXPAND CustomStatemachineCPP::file FOR this»
+	«EXPAND CustomRepositoryH::file FOR this»
+	«EXPAND CustomEventH::file FOR this»
+	
+	«EXPAND CMakeLists::file FOR this»
+	«EXPAND TestMain::file FOR this»
+
+
 	«EXPAND Dump::file FOR this»
 	
 «ENDDEFINE»

+ 57 - 2
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/StatechartH.xpt

@@ -13,8 +13,63 @@ Templates for the main statechart cpp file.
 
 «DEFINE file FOR ExecutionFlow»
 
-«FILE 'statechart.h'»
-// generated by YAKINDU C++ statechart generator
+«FILE 'MyStatemachine.h'»
+#ifndef MYSTATEMACHINE_H_
+#define MYSTATEMACHINE_H_
+
+#include "Statemachine.h"
+#include "MyDataRepository.h"
+#include "MyEvents.h"
+
+//namespace MyStatemachineNS {
+
+#define MAX_PARALLEL_STATES 1
+
+class MyStatemachine: public Statemachine, public MyDataRepository {
+
+public:
+	enum States {
+		State1,
+		State2,
+		State3,
+		State4,
+		last_state
+	};
+
+protected:
+	/* conditions */
+	bool condition_state1_id1(const _Event& event, const MyDataRepository& data);
+	bool condition_state3_id1(const _Event& event, const MyDataRepository& data);
+	bool condition_state3_id2(const _Event& event, const MyDataRepository& data);
+	bool condition_state4_id1(const _Event& event, const MyDataRepository& data);
+	bool condition_state4_id2(const _Event& event, const MyDataRepository& data);
+	bool condition_state4_id3(const _Event& event, const MyDataRepository& data);
+
+	/* statement */
+	void statement_state1_id1();
+	void statement_state3_id1();
+	void statement_state3_id2();
+	void statement_state4_id1();
+	void statement_state4_id2();
+	void statement_state4_id3();
+
+	/* cycles */
+	void cycle_state1(const _Event& ev);
+	void cycle_state3(const _Event& ev);
+	void cycle_state4(const _Event& ev);
+
+public:
+
+	MyStatemachine();
+	virtual ~MyStatemachine();
+
+	virtual void init();
+	virtual void runCycle();
+
+};
+
+//}; // namespace
+#endif /* MYSTATEMACHINE_H_ */
 
 «ENDFILE»
 

+ 57 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/StatemachineCPP.xpt

@@ -0,0 +1,57 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'Statemachine.cpp'»
+#include "Statemachine.h"
+#include "StatemachineException.h"
+
+Statemachine::Statemachine(uint16_t maxStates) {
+	state = new uint32_t[maxStates]; /* memory allocation for all states */
+	statePtr = 0; /* actual state referencer */
+}
+
+Statemachine::~Statemachine() {
+	delete[] state;
+}
+
+void Statemachine::queueEvent(Event ev) {
+	/* creates a copy */
+	eventList.push_back(ev);
+}
+
+Event Statemachine::getEvent() {
+	if (eventList.empty())
+		throw(StatechartException("There is no event available"));
+	Event retEvent = eventList.front();
+	eventList.pop_front();
+	return (retEvent);
+}
+
+bool Statemachine::eventWaiting() const {
+	return (!eventList.empty());
+}
+
+void Statemachine::runCycle() {
+	if (!eventWaiting())
+		return;
+
+	Event ev = getEvent();
+	_runCycle(ev);
+
+	delete ev;
+}
+«ENDFILE»
+
+«ENDDEFINE»

+ 31 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/StatemachineExceptionH.xpt

@@ -0,0 +1,31 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'StatemachineException.h'»
+#ifndef STATEMACHINEEXCEPTION_H_
+#define STATEMACHINEEXCEPTION_H_
+
+#include <stdexcept>
+
+class StatechartException : public std::runtime_error {
+public:
+	StatechartException(const std::string & msg)
+      : std::runtime_error(msg) {}
+};
+
+#endif /* STATEMACHINEEXCEPTION_H_ */
+«ENDFILE»
+
+«ENDDEFINE»

+ 51 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/StatemachineH.xpt

@@ -0,0 +1,51 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'Statemachine.h'»
+#ifndef STATEMACHINE_H_
+#define STATEMACHINE_H_
+
+#include <deque>
+#include "Event.h"
+
+class Statemachine {
+
+protected:
+	uint32_t* state;
+	uint16_t statePtr;
+
+	std::deque<Event> eventList;
+
+	Event getEvent();
+
+	virtual void _runCycle(Event ev) = 0;
+
+public:
+	Statemachine(uint16_t maxStates);
+	virtual ~Statemachine();
+
+	virtual void init() = 0;
+	void runCycle();
+	void queueEvent(Event ev);
+	bool eventWaiting() const;
+
+	uint32_t getState(uint16_t pos) { return(state[pos]); }
+
+};
+
+#endif /* STATEMACHINE_H_ */
+«ENDFILE»
+
+«ENDDEFINE»

+ 99 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/TestMain.xpt

@@ -0,0 +1,99 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'main.cpp'»
+#include <cstdio>
+#include <iostream>
+#include "MyStatemachine.h"
+#include "MyDataRepository.h"
+#include "MyEvents.h"
+
+int main()
+{
+
+	char* stateName[6] = {"State1", "State2", "State3", "State4", "State5", "State6"};
+	MyStatemachine machine;
+
+	/* initialize data repository */
+	machine.init_data(1,0,0.0);
+
+	/* initialize the initial states */
+	machine.init();
+
+	/* create one event and enqueue it */
+	Event e = createEvent<Event1>();
+	machine.queueEvent(e);
+
+	/* run the cycle */
+	machine.runCycle();
+
+	/* check the state */
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State3") == 0);
+
+	e = createEvent<Event2>();
+	machine.queueEvent(e);
+
+	machine.runCycle();
+
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State1") == 0);
+
+	/* does event queuing work? */
+	e = createEvent<Event1>();
+	machine.queueEvent(e);
+	e = createEvent<Event3>();
+	machine.queueEvent(e);
+
+	machine.runCycle();
+
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State3") == 0);
+
+	/* run second cycle */
+	machine.runCycle();
+
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State4") == 0);
+
+	e = createEvent<Event4>();
+	machine.queueEvent(e);
+
+	machine.runCycle();
+
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State3") == 0);
+
+	e = createEvent<Event3>();
+	machine.queueEvent(e);
+
+	machine.runCycle();
+
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State4") == 0);
+
+	e = createEvent<Event5>();
+	machine.queueEvent(e);
+
+	machine.runCycle();
+
+	std::cout << stateName[machine.getState(0)] << "\n";
+	assert( strcmp(stateName[machine.getState(0)], "State1") == 0);
+
+	return(0);
+}
+«ENDFILE»
+
+«ENDDEFINE»

+ 29 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/definitionH.xpt

@@ -0,0 +1,29 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+
+«DEFINE file FOR ExecutionFlow»
+
+«FILE 'definition.h'»
+#ifndef DEFINITION_H_
+#define DEFINITION_H_
+
+/* platform/compiler specific additions (#includes) */
+/* temporarly set to fix values */ 
+
+typedef unsigned int uint32_t;
+typedef unsigned short uint16_t;
+
+#endif /* DEFINITION_H_ */
+«ENDFILE»
+
+«ENDDEFINE»