@//////////////////////////////////////////////////////////////////////////////
@// su4sml --- a SML repository for managing (Secure)UML/OCL models
@//             http://projects.brucker.ch/su4sml/
@//                                                                            
@// Java_SM.tpl --- 
@// This file is part of su4sml.
@//
@// Copyright (c) 2005-2007, ETH Zurich, Switzerland
@//
@// All rights reserved.
@//
@// Redistribution and use in source and binary forms, with or without
@// modification, are permitted provided that the following conditions are
@// met:
@//
@//     * Redistributions of source code must retain the above copyright
@//       notice, this list of conditions and the following disclaimer.
@//
@//     * Redistributions in binary form must reproduce the above
@//       copyright notice, this list of conditions and the following
@//       disclaimer in the documentation and/or other materials provided
@//       with the distribution.
@//
@//     * Neither the name of the copyright holders nor the names of its
@//       contributors may be used to endorse or promote products derived
@//       from this software without specific prior written permission.
@//
@// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
@// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
@// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
@// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
@// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
@// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
@// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@///////////////////////////////////////////////////////////////////////////////
@// $Id$

@openfile generated/java/J_StateMachine.java
// generated by su4sml GSMCG - Generic State Machine Code Generator
@foreach classifier_list
@if hasAG
@nl
@nl import java.util.Hashtable;
@nl import java.util.Vector;
@nl
@nl abstract class EvalClass
@nl {
@nl @tab	public abstract boolean eval();
@nl }
@nl
@nl class Transition
@nl {
@nl @tab public State target;
@nl @tab public EvalClass[] guards;
@nl @tab public EvalClass[] restrictions;
@nl @tab public EvalClass[] effects;
@nl
@nl @tab public Transition(State t, EvalClass[] guardArray, EvalClass[] restrictionArra)
@nl @tab {
@nl @tab@tab target = t;
@nl @tab@tab guards = guardArray;
@nl @tab@tab restrictions = restrictionArray;
@nl @tab }
@nl
@nl
@nl class State
@nl {
@nl @tab public String name;
@nl @tab public Hashtable transitions;
@nl @tab State(String n) {
@nl @tab@tab name = n;
@nl @tab@tab transitions = new Hashtable();
@nl }
@nl
@nl
@nl public class StateMachine
@nl {
@nl @tab private State CUR_STATE;
@nl @tab State START;
@nl @tab State END;
@nl @tab
@nl
	@foreach event_list
		@nl @tab@tab EvalClass $trigger_name$ = new EvalClass();
	@end
@nl
@nl @tab private String[] EVENTS =
@nl @tab {
@nl @tab@tab "ALWAYS",
	@foreach event_list
		@nl @tab@tab "$event_name$",
	@end
@nl @tab@tab "NUM_EV"
@nl @tab };
@nl
@nl @tab private void init_states()
@nl @tab {
	@//@nl @tab@tab START = new State("Initial");
	@foreach state_list
		@nl @tab@tab $state_name$ = new State("$state_ident$");
	@end
	@nl @tab@tab END = new State("Final");
@nl @tab }
@nl
@nl @tab public StateMachine()
@nl @tab {
@nl @tab@tab init_states();
@nl @tab@tab CUR_STATE = START;
@nl @tab }
@nl
@nl @tab public StateMachine()
@nl @tab {
@nl @tab init_states();
@nl
	@foreach state_list
		@foreach events_of_state
			@nl
			@nl @tab@tab $state_name$.transitions.put("$cur_event_id$",new Transition[]{
				@foreach transition_list
					@nl @tab@tab@tab
					new Transition($transition_target$,
					@nl @tab@tab@tab@tab new EvalClass[]{
						@foreach guard_list
							$guard_ident$
							if isLastGuard
								} @//closing the guard array
							else
								,
							@end
						@end @//of guard_list
						,
					@nl @tab@tab@tab@tab new EvalClass[]{
						@foreach restriction_list
							$restriction_ident$,
						@end
						})
						@if isLastTrans
							}); @//end of transition array for this event
						@else 
							, @//separate transitions
						@end
				@end @//transition_list
		@end @//events_of_state
		@nl
		@nl
		@if isStart
			@nl @tab@tab START = $state_name$;
		@end
	@end @//state_list
@nl @tab CUR_STATE = START;
@nl @tab END = $final_state_name$;
@nl }
@nl
@nl public boolean alwaysG(){return true;}
@nl public boolean elseG(){return true;}
@nl public boolean noneR(){return true;}
@nl
@nl boolean checkState(String EVENT_ID)
@nl {
@nl @tab return (CUR_STATE.transistion.containsKey(EVENT_ID));
@nl }
@nl
	@foreach event_list
		@nl @tab class $trigger_name$ implements EvalClass
		@nl @tab {
		@nl @tab public void eval()
		@nl @tab {
		@nl @tab@tab String E_ID = $event_name$;
		@nl @tab@tab if(checkState(E_ID))
		@nl @tab@tab {
		@nl @tab@tab@tab Transition[] trans = (Transition[]) CUR_STATE.transitions.get(E_ID);
		@nl @tab@tab@tab for(int i = 0; i < trans.length; i++)
		@nl @tab@tab@tab {
		@nl @tab@tab@tab@tab for(int j = 0; j < trans[i].guards.length; j++)
		@nl @tab@tab@tab@tab {
		@nl @tab@tab@tab@tab@tab if(trans[i].guards[j].eval())
		@nl @tab@tab@tab@tab@tab {
		@nl @tab@tab@tab@tab@tab CUR_STATE = trans[i].target;
		@nl @tab@tab@tab@tab@tab return;
		@nl @tab@tab@tab@tab@tab } else {
		@nl @tab@tab@tab@tab@tab break;
		@nl @tab@tab@tab@tab@tab }
		@nl @tab@tab@tab@tab }
		@nl @tab@tab@tab }
		@nl @tab@tab } else {
		@nl @tab@tab@tab System.out.println("Current state does not accept this event!\n");
		@nl @tab@tab }
		@nl @tab }
		@nl @tab } //end of class $trigger_name$
	@end
@nl }
@end @//foreach classifier_list
@end @if...
