Browse Source

Added some features to support interfaces in Cpp statechart code generator

jos.itemis@gmail.com 13 years ago
parent
commit
cc1f8b3f66

+ 8 - 6
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/CMakeLists.xpt

@@ -16,7 +16,6 @@ Templates for the main statechart cpp file.
 «DEFINE file FOR ExecutionFlow»
 
 «FILE 'CMakeLists.txt'»
-cmake_minimum_required(VERSION 2.8)
 
 set ( SRC_BASE
 	DataRepository.cpp
@@ -25,23 +24,26 @@ set ( SRC_BASE
 	StatemachineBase.cpp
 )
 
+set ( SRC_IFACE
+«FOREACH this.scopes.typeSelect(InterfaceScope) AS interface -»
+	«interface.interfaceClassName()».cpp
+«ENDFOREACH»
+)	
+
 set ( SRC_EV
 	Statemachine_ev.cpp
 	Statemachine_ev.h
-	main_ev.cpp
 	«StName()»_ev.cpp
 )
 
 set ( SRC_CY
 	Statemachine_cy.cpp
 	Statemachine_cy.h
-	main_cy.cpp
 	«StName()»_cy.cpp
 )
 
-add_definitions( -Wall -O0 -g)
-add_executable(«StName()»_ev_main ${SRC_BASE} ${SRC_EV})
-add_executable(«StName()»_cy_main ${SRC_BASE} ${SRC_CY})
+add_library(«StName()»_ev ${SRC_BASE} ${SRC_IFACE} ${SRC_EV})
+add_library(«StName()»_cy ${SRC_BASE} ${SRC_IFACE} ${SRC_CY})
 
 «ENDFILE»
 

+ 34 - 2
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/Expression.ext

@@ -2,6 +2,7 @@ import stext;
 import sexec;
 import ecore;
 import sgraph;
+
 toCppCode(Void void) :
 	"/*toCppCode() called with NULL element. Polymorphic resolver could not match callable method!*/";
 String toCppCode(Void o, String statechartReference) : 
@@ -12,9 +13,12 @@ String toCppCode(Expression statement) :
  
 String toCppCode(Statement statement) : 
 	null; //polymorphic placeholder (abstract rule)
- 
+
+String toCppCode(Literal lit) :
+   lit.toString() ;
+
 String toCppCode(PrimitiveValueExpression primValue) :
-	primValue.value;
+	primValue.value.toCppCode();
 
 /* Assignment */
 String toCppCode(Assignment assignment) :
@@ -35,6 +39,21 @@ String toCppCode(Assignment assignment) :
 		
 /* EventRaising */
 
+String toCppCode(EventRaising eventRaising) :
+	((eventRaising.value == null)?"":
+	"((" + eventRaising.event.name + "*) handle->" + eventRaising.getInterfaceName() + "." + eventRaising.event.name + ")->value = " 
+	+ eventRaising.value.toCppCode() + ";") + 
+	" statemachine_cy_setEvent(&handle->base, handle->" + eventRaising.getInterfaceName() + "." + eventRaising.event.name + "); ";
+//  "{ _Event* ev = eventPool_createEvent(handle->base.eventPool, ev_"+eventRaising.event.name+"); if (ev) { " + eventRaising.addValue() + "statemachine_cy_setEvent(&handle->base, ev); } }";
+
+String getInterfaceName(EventRaising event) :
+	"interface" + (( ((InterfaceScope)event.event.eContainer).name == null)?"":((InterfaceScope)event.event.eContainer).name.toFirstUpper());
+
+String addValue(EventRaising event) :
+  ( (event.value == null)?"":("((" +event.event.name + "*)ev)->value = " + event.value.toCppCode() + "; ") );
+
+
+
 /* Logical Expressions */
 String toCppCode(LogicalOrExpression expression) :
   	expression.leftOperand.toCppCode() + " || " + expression.rightOperand.toCppCode();
@@ -102,4 +121,17 @@ String eventTypeToString(Type type) :
   case (Type::string)  : "strng"
   default : "unknownType"
   };
+  
+Set[EventDefinition] getInEvents(InterfaceScope interface) : 
+	interface.declarations.typeSelect(EventDefinition).select(e|e.direction == Direction::IN);
+
+Set[EventDefinition] getOutEvents(InterfaceScope interface) : 
+	interface.declarations.typeSelect(EventDefinition).select(e|e.direction == Direction::OUT);
+
+Set[EventDefinition] getLocalEvents(InterfaceScope interface) : 
+	interface.declarations.typeSelect(EventDefinition).select(e|e.direction == Direction::LOCAL);
+ 
+/* Timer handling */
+
+ 
 	

+ 155 - 0
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/Interfaces.xpt

@@ -0,0 +1,155 @@
+«REM»
+
+Templates for the main statechart cpp file.
+
+@auther joern seger
+
+«ENDREM»
+
+«IMPORT sexec»
+«IMPORT stext»
+«IMPORT sgraph»
+
+«EXTENSION org::yakindu::sct::generator::cpp::templates::Expression»
+«EXTENSION org::yakindu::sct::generator::cpp::templates::Naming»
+
+
+«DEFINE file FOR ExecutionFlow»
+«FOREACH this.scopes.typeSelect(InterfaceScope) AS interface»
+«FILE StName()+interface.interfaceClassName()+".h"»
+
+#ifndef «interface.interfaceClassName().toUpperCase()»_H_
+#define «interface.interfaceClassName().toUpperCase()»_H_
+
+#include "definition.h"
+#include "Event.h"
+#include "EventSet.h"
+#include "EventPool.h"
+#include "Statemachine.h"
+#include "«EvName()».h"
+#include <vector>
+
+namespace «StName()» {
+
+class AbstractInterface {
+
+};
+
+class «interfaceClassName(interface)» : public AbstractInterface {
+
+protected:
+	Statemachine& statemachine;
+    std::vector<_Event*> outEventList;
+
+«FOREACH interface.declarations.typeSelect(VariableDefinition) AS variable -»
+	«variable.type» «variable.name»;
+«ENDFOREACH» 
+	
+	bool isMyEvent(Event& event);
+	
+public:
+
+	«interfaceClassName(interface)»(Statemachine& statemachine);
+	
+«FOREACH interface.declarations.typeSelect(VariableDefinition) AS variable -»
+	«variable.type»& get_«variable.name»() const;
+	«IF variable.readonly == false -»
+	void set_«variable.name»(«variable.type»& value);
+	«ENDIF»
+«ENDFOREACH» 
+	
+	void resetEvent();
+	
+	«FOREACH interface.getOutEvents() AS event -»
+	boolean is_«event.name»_raised(«
+	IF event.type != Type::void -» «event.type.eventTypeToString()»* value«ENDIF» );
+	«ENDFOREACH»
+
+	«FOREACH interface.getInEvents() AS event -»
+	void raise_«event.name»(«
+	IF event.type != Type::void -» «event.type.eventTypeToString()» value «ENDIF»);
+	«ENDFOREACH» 
+	
+
+};
+
+}
+
+#endif /* INTERFACEBASE_H_ */
+
+
+«ENDFILE»
+
+«FILE StName()+interface.interfaceClassName()+".cpp"»
+#include "«StName()»«interface.interfaceClassName()».h"
+
+using namespace «StName()»;
+
+bool «interfaceClassName(interface)»::isMyEvent(Event& event)
+{
+
+}
+
+
+«interfaceClassName(interface)»::«interfaceClassName(interface)»(Statemachine& _statemachine)
+  : statemachine(_statemachine) 
+{}
+	
+«FOREACH interface.declarations.typeSelect(VariableDefinition) AS variable -»
+«variable.type»& «interfaceClassName(interface)»::get_«variable.name»() const
+{ return «variable.name»; }
+
+«IF variable.readonly == false -»
+void «interfaceClassName(interface)»::set_«variable.name»(«variable.type»& value)
+{ «variable.name» = value; }
+«ENDIF-»
+«ENDFOREACH» 
+	
+void «interfaceClassName(interface)»::resetEvent()
+{  outEventList.clear(); }
+	
+«FOREACH interface.getOutEvents() AS event -»
+boolean «interfaceClassName(interface)»::is_«event.name»_raised(«
+	IF event.type != Type::void -» «event.type.eventTypeToString()»* value«ENDIF» )
+{
+	std::vector<Event>::const_iterator cit(outEventList.begin());
+	for(; cit != outEventList.end(); ++cit)
+	  if (cit->getID() == (uint32_t)«event.getEventEnumName()») {
+	  	«IF event.type != Type::void -»
+	  	*value = static_cast<«event.name»*>(cit)->value;
+	  	«ENDIF»
+	  	return true;
+	  } 
+	return false;
+}
+«ENDFOREACH»
+
+
+«FOREACH interface.getInEvents() AS event -»
+void «interfaceClassName(interface)»::raise_«event.name»(«
+	IF event.type != Type::void -» «event.type.eventTypeToString()» value «ENDIF»)
+{
+	Event ev = new «event.name»;
+	statemachine.setEvent(ev);
+}
+«ENDFOREACH» 
+	
+bool «interface.interfaceName()»::is_my_event(uint32_t evid)
+{
+	bool ret = false;
+	switch (evid) {
+«FOREACH interface.getInEvents() AS event -»
+	case «event.getEventEnumName()»:
+«ENDFOREACH»
+		ret = true;
+		break;
+	default:
+		break;
+	}
+	return ret;
+}
+
+
+«ENDFILE»
+«ENDFOREACH»
+«ENDDEFINE»

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

@@ -47,6 +47,8 @@ Contais the root templates that call the different file templates.
 	
 	«REM» custom code «ENDREM»	
 
+	«EXPAND Interfaces::file FOR this»
+
 	«EXPAND CustomStatemachine_cyH::file FOR this»
 	«EXPAND CustomStatemachine_cyCPP::file FOR this»
 

+ 25 - 9
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/templates/Naming.ext

@@ -3,18 +3,19 @@ import sexec;
 import ecore;
 import sgraph;
 
+ExecutionFlow getExecutionFlow(InterfaceScope this) : ((ExecutionFlow)this.eContainer);
 
 String scName(Expression statement) : ((ExecutionFlow)statement.eRootContainer).name ;
 
-List declaredEvents(ExecutionFlow flow) : flow.scopes.declarations.typeSelect(EventDefinition);
-
-List declaredVariables(ExecutionFlow flow) : flow.scopes.declarations.typeSelect(VariableDefinition);
+List[EventDefinition] declaredEvents(ExecutionFlow flow) : flow.scopes.declarations.typeSelect(EventDefinition);
+List[TimeEvent] declaredTimerEvents(ExecutionFlow flow) : flow.scopes.declarations.typeSelect(TimeEvent);
+List[VariableDefinition] declaredVariables(ExecutionFlow flow) : flow.scopes.declarations.typeSelect(VariableDefinition);
 
 String test(NamedElement obj) :  "";
-String StName(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper() + "Statemachine";
+String StName(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper();// + "Statemachine";
 String STNAME(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toUpperCase() + "STATEMACHINE";
-String StNameEv(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper() + "Statemachine_ev";
-String StNameCy(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper() + "Statemachine_cy";
+String StNameEv(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper();// + "Statemachine_ev";
+String StNameCy(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper();// + "Statemachine_cy";
 
 String DRName(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toFirstUpper() + "DataRepository";
 String DRNAME(Object this) : ((ExecutionFlow)((EObject)this).eRootContainer).name.toUpperCase() + "DATAREPOSITORY";
@@ -26,22 +27,37 @@ cached String functionName(Step step) :
 	(step.isEffect()) ? step.actionFunctionName() : (
 	(step.isReactionCheck()) ? step.checkFunctionName() : (
 	(step.isEntryAction()) ? step.entryActionFunctionName() : ( 
-	(step.isExitAction()) ? step.exitActionFunctionName() : 
-	" !! unknown function type !!" )));
+	(step.isExitAction()) ? step.exitActionFunctionName() : (
+	(step.isEnterSequence()) ? step.enterSequenceFunctionName() : ( 
+	(step.isExitSequence()) ? step.exitSequenceFunctionName() : 	
+	" !! unknown function type !!" )))));
 
 
 String actionFunctionName(Step this) : "actions_" + this.reaction().state().simpleName + "_" + this.reaction().name; 
 String checkFunctionName(Step this) : "condition_" + this.reaction().state().simpleName + "_" + this.reaction().name; 
 String entryActionFunctionName(Step this) : "entryActions_" + this.state().simpleName; 
 String exitActionFunctionName(Step this) : "exitActions_" + this.state().simpleName; 
+String enterSequenceFunctionName(Step this) : "_enterSequence_" + this.state().simpleName; 
+String exitSequenceFunctionName(Step this) : "_exitSequence_" + this.state().simpleName; 
 
 boolean isEffect(Step step) : (! Check.isInstance(step)) && Reaction.isInstance(step.eContainer) ;
 boolean isReactionCheck(Step step) : Reaction.isInstance(step.eContainer) && Check.isInstance(step);
 boolean isEntryAction(Step step) : ExecutionState.isInstance(step.eContainer) && step.state().entryAction == step;
 boolean isExitAction(Step step) : ExecutionState.isInstance(step.eContainer) && step.state().exitAction == step;
-
+boolean isEnterSequence(Step step) : ExecutionState.isInstance(step.eContainer) && step.state().enterSequence == step;
+boolean isExitSequence(Step step) : ExecutionState.isInstance(step.eContainer) && step.state().exitSequence == step;
+  
 Reaction reaction(Step this) : (Reaction) this.eContainer ;
 ExecutionState state(Reaction this) : (ExecutionState) eContainer;
 ExecutionState state(Step this) : (ExecutionState) eContainer;
 
+String interfaceName(InterfaceScope this) : ((this.name == null) ? "_if" : "_if_" + this.name);
+String interfaceClassName(InterfaceScope this) : "If" + ((this.name == null)?"":this.name.toFirstUpper());
+String interfaceObjectName(InterfaceScope this) : "interface" + ((this.name == null)?"":this.name.toFirstUpper());
+
+String getEventEnumName(EventDefinition this) : "ev_" + this.name.toFirstLower();
+String getEventEnumName(TimeEvent this) : "ev_" + this.name.toFirstLower();
+
+String getBoolTrue() : "bool_true";
+String getBoolFalse() : "bool_false";