/* * Filename: STGModel.java * * Author: Raf Van de Plas * * Contact: * Snail-mail: K.U.Leuven * ESAT-SCD * Kasteelpark Arenberg 10 * B-3001 Leuven-Heverlee * Belgium * Tel: +32 16 327927 * Fax: +32 16 321970 * Gsm: +32 474 243046 * E-mail: raf.vandeplas@esat.kuleuven.ac.be * WWW: http://www.esat.kuleuven.ac.be/~dna/BioI/ * * * Created on: Dec 5, 2003 * * Changed on: Dec 5, 2003 * * Version: 0.1 * * Description: * none * */ import java.util.*; /** * @author rvdplas * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class STGModel { StateTransitionGraph graph = null; State currentState = null; public STGModel(StateTransitionGraph graph) { this.graph = graph; this.currentState = this.graph.getStartState(); this.currentState.getStateCompanion().executeOnEnteringState(); } public StateCompanion actOnControlMessage(String controlMessage) throws STGModelException { //RULES: 'don't care'(=null) has precedense over actual values. //RULES: a state message 'don't care' has precedense over a control message 'don't care' //RULES: if there are several transitions that fit the first one is taken. //Check whether the current state has transitions at all. ArrayList transitionsFromCurrentState = this.graph.getTransitionsWithSource(this.currentState); if(transitionsFromCurrentState.size() > 0) { //Check whether there are transitions that have both null controlMessages and stateMessages ArrayList transitionsWithNullControlMessageNullStateMessage = StateTransitionGraph.getTransitionsWithControlMessageAndStateMessage(transitionsFromCurrentState, null, null); if(transitionsWithNullControlMessageNullStateMessage.size() > 0) { //Use the first transition with NULL controlMessage and NULL stateMessage return this.performTransition((Transition) transitionsWithNullControlMessageNullStateMessage.get(0)); } else{ //Check whether there are transitions that have a null stateMessage and an exact controlMessage ArrayList transitionsWithExactControlMessageNullStateMessage = StateTransitionGraph.getTransitionsWithControlMessageAndStateMessage(transitionsFromCurrentState, controlMessage, null); if(transitionsWithExactControlMessageNullStateMessage.size() > 0) { //Use the first transition with EXACT controlMessage and NULL stateMessage return this.performTransition((Transition) transitionsWithExactControlMessageNullStateMessage.get(0)); } else{ //Check whether there are transitions that have an exact stateMessage and a null controlMessage ArrayList transitionsWithNullControlMessageExactStateMessage = StateTransitionGraph.getTransitionsWithControlMessageAndStateMessage(transitionsFromCurrentState, null, this.currentState.getStateMessage()); if(transitionsWithNullControlMessageExactStateMessage.size() > 0) { //Use the first transition with NULL controlMessage and EXACT stateMessage return this.performTransition((Transition) transitionsWithNullControlMessageExactStateMessage.get(0)); } else{ //Check whether there are transitions that have an exact stateMessage and an exact controlMessage ArrayList transitionsWithExactControlMessageExactStateMessage = StateTransitionGraph.getTransitionsWithControlMessageAndStateMessage(transitionsFromCurrentState, controlMessage, this.currentState.getStateMessage()); if(transitionsWithExactControlMessageExactStateMessage.size() > 0) { //Use the first transition with EXACT controlMessage and EXACT stateMessage return this.performTransition((Transition) transitionsWithExactControlMessageExactStateMessage.get(0)); } else{ //No useful transitions found. Do nothing. return null; } } } } } else{ //The current state doesn't have any transitions. //Check whether the current state is the end state. If not, throw an exception. if(this.currentState.isEndState()) { //The current state is the end state. return null; //do nothing } else{ //We're stuck in a state we can't get out of. throw new STGModelException("The current state '" + this.currentState.getName() + "' (Id=" + this.currentState.getId() + ") doesn't have any transitions leaving her."); } } } private StateCompanion performTransition(Transition trans) { this.currentState.getStateCompanion().executeOnLeavingState(); this.currentState = trans.getTargetState(); this.currentState.getStateCompanion().executeOnEnteringState(); return this.currentState.getStateCompanion(); } } class STGModelException extends Exception { //Just to get an exception specific to the STGModel class public STGModelException(String exceptionMessage) { super(exceptionMessage); } }