/* * Filename: WizardModel.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 WizardModel { StateTransitionGraph graph = null; State currentState = null; public WizardModel(StateTransitionGraph graph) { this.graph = graph; this.currentState = this.graph.getStartState(); } public ValidatingInterface actOnControllerMessage(String controllerMessage) throws WizardModelException { //RULES: 'don't care'(=null) has precedense over actual values. //RULES: a fulfillment 'don't care' has precedense over a controller '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 controllerMessages and fulfillmentMessages ArrayList transitionsWithNullControllerMessageNullFulfillmentMessage = StateTransitionGraph.getTransitionsWithControllerMessageAndFulfillmentMessage(transitionsFromCurrentState, null, null); if(transitionsWithNullControllerMessageNullFulfillmentMessage.size() > 0) { //Use the first transition with NULL controllerMessage and NULL fulfillmentMessage return this.performTransition((Transition) transitionsWithNullControllerMessageNullFulfillmentMessage.get(0)); } else{ //Check whether there are transitions that have a null fulfillmentMessage and an exact controllerMessage ArrayList transitionsWithExactControllerMessageNullFulfillmentMessage = StateTransitionGraph.getTransitionsWithControllerMessageAndFulfillmentMessage(transitionsFromCurrentState, controllerMessage, null); if(transitionsWithExactControllerMessageNullFulfillmentMessage.size() > 0) { //Use the first transition with EXACT controllerMessage and NULL fulfillmentMessage return this.performTransition((Transition) transitionsWithExactControllerMessageNullFulfillmentMessage.get(0)); } else{ //Check whether there are transitions that have an exact fulfillmentMessage and a null controllerMessage ArrayList transitionsWithNullControllerMessageExactFulfillmentMessage = StateTransitionGraph.getTransitionsWithControllerMessageAndFulfillmentMessage(transitionsFromCurrentState, null, this.currentState.getFulfillment()); if(transitionsWithNullControllerMessageExactFulfillmentMessage.size() > 0) { //Use the first transition with NULL controllerMessage and EXACT fulfillmentMessage return this.performTransition((Transition) transitionsWithNullControllerMessageExactFulfillmentMessage.get(0)); } else{ //Check whether there are transitions that have an exact fulfillmentMessage and an exact controllerMessage ArrayList transitionsWithExactControllerMessageExactFulfillmentMessage = StateTransitionGraph.getTransitionsWithControllerMessageAndFulfillmentMessage(transitionsFromCurrentState, controllerMessage, this.currentState.getFulfillment()); if(transitionsWithExactControllerMessageExactFulfillmentMessage.size() > 0) { //Use the first transition with EXACT controllerMessage and EXACT fulfillmentMessage return this.performTransition((Transition) transitionsWithExactControllerMessageExactFulfillmentMessage.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 WizardModelException("The current state '" + this.currentState.getName() + "' (Id=" + this.currentState.getId() + ") doesn't have any transitions leaving her."); } } } private ValidatingInterface performTransition(Transition trans) { this.currentState = trans.getTargetState(); return this.currentState.getValidatingObject(); } } class WizardModelException extends Exception { //Just to get an exception specific to the WizardModel class public WizardModelException(String exceptionMessage) { super(exceptionMessage); } }