Преглед изворни кода

Code templates for various fragments added

jos.itemis@gmail.com пре 14 година
родитељ
комит
d7cce405d3

+ 69 - 125
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CustomStatemachineCPP.xpt

@@ -10,148 +10,96 @@ Templates for the main statechart cpp file.
 «IMPORT stext»
 «IMPORT sgraph»
 
+«IMPORT org::yakindu::sct::generator::cpp::templates»
+«EXTENSION org::yakindu::sct::generator::cpp::templates::Expression»
 
-«DEFINE file FOR ExecutionFlow»
+«DEFINE ActionCode FOR Step»
+// ActionCode for Step not defined
+«ENDDEFINE»
 
-«FILE 'MyStatemachine.cpp'»
-#include <iostream>
-#include "MyStatemachine.h"
DEFINE ActionCode FOR Execution»
+// ActionCode for Execution not implemented yet
+«ENDDEFINE»
 
-MyStatemachine::MyStatemachine() : Statemachine(MAX_PARALLEL_STATES) {
-}
+«DEFINE ActionCode FOR Call»
+// ActionCode for Call not implemented yet
+«ENDDEFINE»
 
-MyStatemachine::~MyStatemachine() {
-}
+«DEFINE ActionCode FOR Sequence»
+  «EXPAND ActionCode FOREACH this.steps»
+«ENDDEFINE»
 
+«DEFINE ActionCode FOR Check»
+// ActionCode for Check not implemented yet
+«ENDDEFINE»
 
-bool MyStatemachine::condition_state1_id1(const _Event & event, const MyDataRepository & data)
-{
-	return (event.getID() == (uint32_t)event1);
-}
+«DEFINE ActionCode FOR Reaction»
+  «EXPAND ActionCode FOR this.effect» 
+«ENDDEFINE»
 
-bool MyStatemachine::condition_state3_id1(const _Event & event, const MyDataRepository & data)
-{
-	return (event.getID() == (uint32_t)event2);
+«DEFINE ActionCode FOR If»
+if («this.check.condition.toCppCode("bla")») {
+  «EXPAND ActionCode FOR this.thenStep»
 }
-
-bool MyStatemachine::condition_state3_id2(const _Event & event, const MyDataRepository & data)
-{
-	return (event.getID() == (uint32_t)event3);
+else {
+  «EXPAND ActionCode FOR this.elseStep»
 }
+«ENDDEFINE»
 
-bool MyStatemachine::condition_state4_id1(const _Event & event, const MyDataRepository & data)
-{
-	return (event.getID() == (uint32_t)event4);
-}
+«DEFINE ActionCode FOR EnterState»
+// dummy Enter
+// states = newstates
+«ENDDEFINE»
 
-bool MyStatemachine::condition_state4_id2(const _Event & event, const MyDataRepository & data)
-{
-	return (event.getID() == (uint32_t)event5);
-}
+«DEFINE ActionCode FOR ExitState»
+// dummy Exit
+// oldstates = states
+«ENDDEFINE»
 
-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;
+«DEFINE ConditionMethodsImplement FOR ExecutionState»
+  «FOREACH reactions AS r»
+bool MyStatemachine::condition_«this.simpleName»_«r.name»(const _Event& ev) {
+  return (	«r.check.condition.toCppCode("bla")» );
 }
+  «ENDFOREACH»
+«ENDDEFINE»
 
-void MyStatemachine::statement_state3_id1()
-{
-	std::cout << "statement_state3_id1 \n";
-	/* exit S3 */
-	/* exit S2 */
-	/* transition action event 2 */
-	/* entry S1 */
-	state[0] = State1;
+«DEFINE StatementMethodsImplement FOR ExecutionState»
+  «FOREACH reactions AS r»
+void MyStatemachine::actions_«this.simpleName»_«r.name»() {
+  «EXPAND ActionCode FOR r.effect»
 }
+  «ENDFOREACH»
+«ENDDEFINE»
 
-void MyStatemachine::statement_state4_id1()
+«DEFINE CycleMethodsImplement FOR ExecutionState»
+void MyStatemachine::cycle_«this.simpleName»(const _Event& ev)
 {
-	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;
-
+  «FOREACH reactions AS r -»
+	  if(condition_«this.simpleName»_«r.name»(ev)) {
+	    actions_«this.simpleName»_«r.name»();
+	  } 
+  «ENDFOREACH»
 }
+«ENDDEFINE»
 
-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;
+«DEFINE file FOR ExecutionFlow»
+«FILE 'MyStatemachine.cpp'»
+#include <iostream>
+#include "MyStatemachine.h"
 
+MyStatemachine::MyStatemachine() : Statemachine(«((ExecutionFlow)this.eRootContainer).stateVector.size») {
 }
 
-void MyStatemachine::cycle_state1(const _Event& ev)
-{
-	if (condition_state1_id1(ev, *this)) {
-		statement_state1_id1();
-		return;
-	}
-
+MyStatemachine::~MyStatemachine() {
 }
 
-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();
-	}
-}
+«EXPAND ConditionMethodsImplement FOREACH this.states»
 
-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;
-	}
-}
+«EXPAND StatementMethodsImplement FOREACH this.states»
 
+«EXPAND CycleMethodsImplement FOREACH this.states»
 
 void MyStatemachine::init()
 {
@@ -166,17 +114,13 @@ void MyStatemachine::init()
 
 void MyStatemachine::_runCycle(Event ev)
 {
-	for (int i=0; i<MAX_PARALLEL_STATES; ++i) {
+	for (int i=0; i<«((ExecutionFlow)this.eRootContainer).stateVector.size»; ++i) {
 		switch(state[i]) {
-		case State1:
-			cycle_state1(*ev);
-			break;
-		case State3:
-			cycle_state3(*ev);
-			break;
-		case State4:
-			cycle_state4(*ev);
-			break;
+	«FOREACH this.states AS state»
+	    case «state.simpleName»:
+	        cycle_«state.simpleName»(*ev);
+	        break;
+	«ENDFOREACH»
 		default:
 			/* no state found */
 			break;

+ 40 - 22
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CustomStatemachineH.xpt

@@ -29,34 +29,33 @@ class MyStatemachine: public Statemachine, public MyDataRepository {
 
 public:
 	enum States {
-		State1,
-		State2,
-		State3,
-		State4,
+	    «FOREACH this.states AS exState»
+	      «exState.simpleName»,
+	    «ENDFOREACH»
 		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();
+	«FOREACH states AS conStates»
+	  «FOREACH conStates.reactions AS state»
+	void condition_«conStates.simpleName»_«state.name»(const _Event& event);
+	  «ENDFOREACH»
+	«ENDFOREACH»
+	
+	/* actions */
+	«FOREACH states AS conStates»
+	  «FOREACH conStates.reactions AS state»
+	void actions_«conStates.simpleName»_«state.name»();
+	  «ENDFOREACH»
+	«ENDFOREACH»
 
 	/* cycles */
-	void cycle_state1(const _Event& ev);
-	void cycle_state3(const _Event& ev);
-	void cycle_state4(const _Event& ev);
+	«FOREACH states AS conStates»
+	  «FOREACH conStates.reactions AS state»
+	void cycle_«conStates.simpleName»();
+	  «ENDFOREACH»
+	«ENDFOREACH»
 
 	virtual void _runCycle(Event ev);
 
@@ -76,4 +75,23 @@ public:
 
 «ENDFILE»
 
-«ENDDEFINE»
+«ENDDEFINE»
+
+
+«DEFINE codeMethodPrototype FOR If»
+
+  condition_STATENAME_TRANSITIONPRIO_HERE(const _Event& event, const MyDataRepository& data);
+«ENDDEFINE»
+
+«DEFINE codeMethodPrototype FOR Cycle»
+  //NONE run through cycles
+  «EXPAND codeMethodPrototype FOREACH this.steps»
+«ENDDEFINE»
+
+«DEFINE codeMethodPrototype FOR Step»
+  //NONE Step
+«ENDDEFINE»
+
+«DEFINE codeMethodPrototype FOR Sequence»
+  «EXPAND codeMethodPrototype FOREACH this.steps»
+«ENDDEFINE»

+ 10 - 1
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/Expression.ext

@@ -1,9 +1,15 @@
+import sgraph;
 import stext;
+import sexec;
+
 
 
 String toCppCode(Expression statement, String statechartReference) : 
 	null; //polymorphic placeholder (abstract rule)
-
+ 
+String toCppCode(Statement statement, String statechartReference) : 
+	null; //polymorphic placeholder (abstract rule)
+ 
 String toCppCode(PrimitiveValueExpression primValue, String statechartName) :
 	primValue.value;
 
@@ -69,4 +75,7 @@ String getOperator(MultiplicativeOperator operator) :
 String toCppCode(NumericalMultiplyDivideExpression expression, String statechartReference) :
   	statechartReference + "." + expression.leftOperand.toCppCode(statechartReference) + getOperator(expression.operator) + statechartReference + "." + expression.rightOperand.toCppCode(statechartReference);
 
+String toCppCode(ElementReferenceExpression ev, String statechartReference) :
+    " ( " + ev.value.name + " == ev.getID() ) ";
 
+String scName(Expression statement) : ((ExecutionFlow)statement.eRootContainer).name ;

+ 51 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/MyEvents.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 'MyEvent.h'»
+#ifndef MYEVENTS_H_
+#define MYEVENTS_H_
+
+#include "Event.h"
+
+enum EventID {
+	event1,
+	event2,
+	event3,
+	event4,
+	event5,
+	timerEvent1,
+	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) {} };
+class TimerEvent1 : public _Event { public: TimerEvent1() : _Event((uint32_t)timerEvent1) {} };
+
+#endif /* MYEVENTS_H_ */
+
+«ENDFILE»
+
+«ENDDEFINE»