Browse Source

Added operation name escaping if java keyword is used.
Added generation of correct return types for operations.

markus.muehlbrandt@gmail.com 13 years ago
parent
commit
f0dba15647

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

@@ -27,7 +27,7 @@ String toCode(Expression expression) :
 	"/* Unknown expression: "+expression+" */"; 
 
 String toCode(OperationDefinition this) :
-	getContext() + "operationCallback." + name;
+	getContext() + "operationCallback." + identifier();
 
 String toCode(ElementReferenceExpression ref) :
 	if (ref.operationCall) then

+ 1 - 1
plugins/org.yakindu.sct.generator.java/src/org/yakindu/sct/generator/java/templates/Main.xpt

@@ -16,7 +16,7 @@ Contributors:
 «EXTENSION org::yakindu::sct::generator::java::templates::ExecutionModelExtensions»
 
 «DEFINE main(sgen::GeneratorEntry entry) FOR ExecutionFlow»
-	
+	 
 	«REM» base package files «ENDREM»
 	«EXPAND IStatemachine::file(entry) FOR this»
 	

+ 5 - 0
plugins/org.yakindu.sct.generator.java/src/org/yakindu/sct/generator/java/templates/Naming.ext

@@ -86,6 +86,11 @@ String getEventType(EventDefinition this) :
 	else
 		"ValuedEvent<Events, "+type.getJavaClassType()+">";
 
+String identifier(OperationDefinition this) :
+	if name.isJavaKeyword() then
+		name + "Operation"
+	else
+		name;
 	
 String getInitialValueAssignment(VariableDefinition this) : 
 	if initialValue != null then

+ 137 - 26
plugins/org.yakindu.sct.generator.java/src/org/yakindu/sct/generator/java/templates/RuntimeService.xpt

@@ -17,8 +17,10 @@ Contributors:
 «getLicenseHeader(entry)»
 package «entry.getBasePackageName()»;
 
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -30,66 +32,175 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
  */
 public class RuntimeService {
 
+	private static RuntimeService runtimeService;
+
 	private Timer timer = new Timer();
 
-	private List<IStatemachine> statemachineSet = new LinkedList<IStatemachine>();
+	private Map<Long, StatemachineTimerTask> timerTasks = new HashMap<Long, StatemachineTimerTask>();
+
+	private class StatemachineTimerTask extends TimerTask {
+
+		private List<IStatemachine> statemachineList = new LinkedList<IStatemachine>();
 
-	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+		private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
 
-	private TimerTask timerTask = new TimerTask() {
+		private boolean isPaused = false;
 
 		@Override
 		public void run() {
 			lock.readLock().lock();
-			for (IStatemachine statemachine : statemachineSet) {
-				statemachine.runCycle();
+			if (!isPaused) {
+				for (IStatemachine statemachine : statemachineList) {
+					statemachine.runCycle();
+				}
 			}
 			lock.readLock().unlock();
 		}
-	};
+
+		/**
+		 * Adds the given state machine to the TimerTask.
+		 * 
+		 * @param statemachine
+		 * @return {@code true} if state machine is added properly.
+		 */
+		public boolean addStatemachine(IStatemachine statemachine) {
+			lock.writeLock().lock();
+			boolean ret = statemachineList.add(statemachine);
+			lock.writeLock().unlock();
+			return ret;
+		}
+
+		/**
+		 * Removes the given state machine from the TimerTask.
+		 * 
+		 * @param statemachine
+		 * @return {@code true} if state machine is removed properly.
+		 */
+		public boolean removeStatemachine(IStatemachine statemachine) {
+			lock.writeLock().lock();
+			boolean ret = statemachineList.remove(statemachine);
+			lock.writeLock().unlock();
+			return ret;
+		}
+
+		public void pause() {
+			isPaused = true;
+		}
+
+		public void resume() {
+			isPaused = false;
+		}
+	}
+
+	private RuntimeService() {
+		// Not intended to be instantiated.
+	}
 
 	/**
-	 * Create runtime service.
+	 * Returns the {@code RuntimeService} instance as singleton.
 	 * 
-	 * @param cyclePeriod
-	 *            : The period with which the registered state machines run
-	 *            cycle method is called.
+	 * @return The singleton {@code RuntimeService} instance
 	 */
-	public RuntimeService(long cyclePeriod) {
-		timer.scheduleAtFixedRate(timerTask, 0, cyclePeriod);
+	public static RuntimeService getInstance() {
+		if (runtimeService == null) {
+			runtimeService = new RuntimeService();
+		}
+		return runtimeService;
 	}
 
 	/**
-	 * Adds the given state machine to runtime service.
+	 * Registers an {@link IStatemachine} for scheduled fixed rate execution
 	 * 
 	 * @param statemachine
+	 *            - The statemachine to execute
+	 * @param cyclePeriod
+	 *            - the fixed rate cycle period for scheduling
 	 * @return {@code true} if state machine is added properly.
 	 */
-	public boolean addStatemachine(IStatemachine statemachine) {
-		lock.writeLock().lock();
-		boolean ret = statemachineSet.add(statemachine);
-		lock.writeLock().unlock();
-		return ret;
+	public boolean registerStatemachine(IStatemachine statemachine,
+			long cyclePeriod) {
+
+		if (timerTasks.containsKey(cyclePeriod)) {
+			// TimerTask for cycle time already existing -> add statemachine
+			return timerTasks.get(cyclePeriod).addStatemachine(statemachine);
+		} else {
+			// Create new TimerTask for cycle period and add statemachine
+			StatemachineTimerTask timerTask = new StatemachineTimerTask();
+			timerTasks.put(cyclePeriod, timerTask);
+			boolean ret = timerTask.addStatemachine(statemachine);
+			timer.scheduleAtFixedRate(timerTask, 0, cyclePeriod);
+			return ret;
+		}
 	}
 
 	/**
 	 * Removes the given state machine from runtime service.
 	 * 
 	 * @param statemachine
+	 *            - the statemachine which should be removed
+	 * @param cyclePeriod
+	 *            - the scheduling cycle period of the statemachine
 	 * @return {@code true} if state machine is removed properly.
 	 */
-	public boolean removeStatemachine(IStatemachine statemachine) {
-		lock.writeLock().lock();
-		boolean ret = statemachineSet.remove(statemachine);
-		lock.writeLock().unlock();
-		return ret;
+	public boolean unregisterStatemachine(IStatemachine statemachine,
+			long cyclePeriod) {
+		if (timerTasks.containsKey(cyclePeriod)) {
+			boolean ret = timerTasks.get(cyclePeriod).removeStatemachine(
+					statemachine);
+
+			return ret;
+		}
+		return false;
+	}
+
+	/**
+	 * Cancels the execution of statemachines for the given cycle period. This
+	 * stops the execution of statemachines which are registered for the given
+	 * cycle period and cancels the executing timer task.
+	 * 
+	 * @return {@code true} if poperly cancelled
+	 */
+	public boolean cancelAll(long cyclePeriod) {
+		if (timerTasks.containsKey(cyclePeriod)) {
+			TimerTask task = timerTasks.get(cyclePeriod);
+			task.cancel();
+			timer.purge();
+			timerTasks.remove(cyclePeriod);
+			return true;
+		}
+		return false;
+	}
+
+	/**
+	 * Pauses the execution of all statemachine which are registered for the
+	 * given cyclePeriod.
+	 * 
+	 * @param cyclePeriod
+	 * @return {@code true} if poperly paused
+	 * 
+	 */
+	public boolean pauseAll(long cyclePeriod) {
+		if (timerTasks.containsKey(cyclePeriod)) {
+			timerTasks.get(cyclePeriod).pause();
+			return true;
+		}
+		return false;
 	}
 
 	/**
-	 * Ends the runtime service.
+	 * Resumes the execution of all statemachine which are registered for the
+	 * given cyclePeriod.
+	 * 
+	 * @param cyclePeriod
+	 * @return {@code true} if poperly resumed
+	 * 
 	 */
-	public void cancel() {
-		timer.cancel();
+	public boolean resumeAll(long cyclePeriod) {
+		if (timerTasks.containsKey(cyclePeriod)) {
+			timerTasks.get(cyclePeriod).resume();
+			return true;
+		}
+		return false;
 	}
 }
 «ENDFILE-»

+ 12 - 6
plugins/org.yakindu.sct.generator.java/src/org/yakindu/sct/generator/java/templates/StatemachineInterface.xpt

@@ -19,12 +19,18 @@ Contributors:
 «EXTENSION org::yakindu::sct::generator::java::templates::Naming»
 
 «DEFINE operationSignature FOR OperationDefinition»
-	public void «name»(«FOREACH parameters AS parameter ITERATOR iter-»
-							«IF iter.counter0 > 0-»
-								,
-							«ENDIF-»
-								«parameter.type.getJavaType()» «parameter.name»
-						«ENDFOREACH-»);
+	public «type.getJavaType()» «identifier()»(
+		«FOREACH parameters AS parameter ITERATOR iter-»
+			«IF iter.counter0 > 0-»
+				,
+			«ENDIF-»
+			«parameter.type.getJavaType()» 
+			«IF parameter.name.isJavaKeyword()-»
+				«parameter.name + "Arg"»
+			«ELSE-»
+				«parameter.name»
+			«ENDIF-»
+		«ENDFOREACH-»);
 «ENDDEFINE»
 
 «DEFINE file(sgen::GeneratorEntry entry) FOR ExecutionFlow-»

+ 1 - 0
plugins/org.yakindu.sct.generator.java/src/org/yakindu/sct/generator/java/templates/TimerService.xpt

@@ -71,6 +71,7 @@ public class TimerService implements ITimerService {
 	 */
 	public void cancel() {
 		timer.cancel();
+		timer.purge();
 	}
 }
 «ENDFILE-»

+ 1 - 0
plugins/org.yakindu.sct.generator.java/src/org/yakindu/sct/generator/java/templates/TypeModelExtensions.ext

@@ -18,6 +18,7 @@ String getJavaType(Type this) :
 		case "integer" : "int"
 		case "boolean" : "boolean"
 		case "string" : "String"
+		case "void" : "void"
 		default : "//"+this
 		};