/* * Filename: StateTransitionGraph.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 3, 2003 * * Changed on: Dec 3, 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 StateTransitionGraph { private ArrayList states = null; private ArrayList transitions = null; private Random rn = new Random(); public StateTransitionGraph() { this.states = new ArrayList(); this.transitions = new ArrayList(); } public void addState(String stateName, StateCompanion stateComp) { //Get all the used IDs int[] usedIds = this.getIds(); //Sort the used IDs in preparation for a binary search Arrays.sort(usedIds); //DEBUG STATEMENTS System.out.println("Function Show to be removed!\ngetIds():"); StateTransitionGraph.show(usedIds); //DEBUG STATEMENTS //Try to find a random new ID that's not already in the list int newId; do{ //DEBUG STATEMENTS System.out.println("Trying a random int!"); //DEBUG STATEMENTS newId = rn.nextInt(); } while(Arrays.binarySearch(usedIds,newId) >= 0); //Create and add a new state this.states.add(new State(newId, stateName, stateComp)); } public void removeState(int id) { //Remove any transitions that are connected to this state this.removeTransitions(this.getTransitionsWithSource(this.getState(id))); this.removeTransitions(this.getTransitionsWithTarget(this.getState(id))); //Remove the state itself this.states.remove(this.getState(id)); } public State getStateAtIndex(int index) { return (State) this.states.get(index); } public State getState(int id) { //Get the IDs of the states int[] ids = this.getIds(); //Find the index of the wanted ID in the ID array int index = StateTransitionGraph.unsortedFind(ids,id); if(index < 0) { return null; } else{ return (State) this.states.get(index); } } public State getState(String stateName) { //Get the names of the states ArrayList names = this.getStateNames(); //Find the index of the wanted name in the name array int index = names.indexOf(stateName); if(index < 0) { return null; } else{ return (State) this.states.get(index); } } public void setStartState(int startStateId) { //To make sure there's only one start state, //first turn all the start state flags to off. int numberOfStates = this.states.size(); for(int i = 0; i < numberOfStates; i++) { ((State) this.states.get(i)).setStartFlag(false); } //Then put the start state flag to true for the one we want this.getState(startStateId).setStartFlag(true); } public void setStartState(String startStateName) { this.setStartState(this.getState(startStateName).getId()); } public int getStartStateId() { return this.getStartState().getId(); } public State getStartState() { //The first start state is returned (hopefully the only start state ;-) State stateAtI; int numberOfStates = this.states.size(); for(int i = 0; i < numberOfStates; i++) { stateAtI = (State) this.states.get(i); if(stateAtI.isStartState()) { return stateAtI; } } //If the for-loop doesn't break, no start state was found //and we'll set the first state in the states arraylist //as start state and return it. stateAtI = this.getStateAtIndex(0); stateAtI.setStartFlag(true); return stateAtI; } public void setEndState(int endStateId) { //To make sure there's only one end state, //first turn all the end state flags to off. int numberOfStates = this.states.size(); for(int i = 0; i < numberOfStates; i++) { ((State) this.states.get(i)).setEndFlag(false); } //Then put the end state flag to true for the one we want this.getState(endStateId).setEndFlag(true); } public void setEndState(String endStateName) { this.setEndState(this.getState(endStateName).getId()); } public int getEndStateId() { return this.getEndState().getId(); } public State getEndState() { //The first end state is returned (hopefully the only end state ;-) State stateAtI; int numberOfStates = this.states.size(); for(int i = 0; i < numberOfStates; i++) { stateAtI = (State) this.states.get(i); if(stateAtI.isEndState()) { return stateAtI; } } //If the for-loop doesn't break, no end state was found //and we'll set the first state in the states arraylist //as end state and return it. stateAtI = this.getStateAtIndex(0); stateAtI.setEndFlag(true); return stateAtI; } public int[] getIds() { //We prepare for as many IDs as there are states currently //(and we hope they are all unique :-) int numberOfStates = this.states.size(); int[] ids = new int[numberOfStates]; for(int i = 0; i < numberOfStates; i++) { ids[i] = ((State) this.states.get(i)).getId(); } //DEBUG STATEMENTS System.out.println("Function Show to be removed!\ngetIds():"); StateTransitionGraph.show(ids); //DEBUG STATEMENTS return ids; } public ArrayList getStateNames() { int numberOfStates = this.states.size(); ArrayList names = new ArrayList(numberOfStates); for(int i = 0; i < numberOfStates; i++) { names.add(this.getStateAtIndex(i).getName()); } //DEBUG STATEMENTS System.out.println("To be removed!\ngetStateNames():"); System.out.println("number of names: " + names.size()); //DEBUG STATEMENTS return names; } public static int unsortedFind(int[] data, int x) { for(int i = 0; i < data.length; i++) { if(data[i] == x) { return i; //found } } return -1; // not found } private static void show(int[] array) { System.out.print("\n{"); for (int index = 0; index < array.length; index++) { System.out.print(array[index] + ","); } System.out.println("\b}"); } public void addTransition(State sourceState, State targetState, String requiredStateMessage, String requiredControlMessage) throws StateTransitionGraphException { //Check whether the supplied states are also in this graph if(this.states.contains(sourceState)) { if(this.states.contains(targetState)) { //Both checks passed. Add the transition. this.transitions.add(new Transition(sourceState, targetState, requiredStateMessage, requiredControlMessage)); } else{ throw new StateTransitionGraphException("Supplied targetState not found in state list."); } } else{ throw new StateTransitionGraphException("Supplied sourceState not found in state list."); } } public void addTransition(int sourceStateId, int targetStateId, String requiredStateMessage, String requiredControlMessage) throws StateTransitionGraphException { //Check whether the supplied IDs are possible if(this.getState(sourceStateId) != null) { if(this.getState(targetStateId) != null) { //Both checks passed. Add the transition. this.transitions.add(new Transition(this.getState(sourceStateId), this.getState(targetStateId), requiredStateMessage, requiredControlMessage)); } else{ throw new StateTransitionGraphException("Supplied targetStateId not found in state list."); } } else{ throw new StateTransitionGraphException("Supplied sourceStateId not found in state list."); } } public void addTransition(String sourceStateName, String targetStateName, String requiredStateMessage, String requiredControlMessage) throws StateTransitionGraphException { //Check whether the supplied names are possible if(this.getState(sourceStateName) != null) { if(this.getState(targetStateName) != null) { //Both checks passed. Add the transition. this.transitions.add(new Transition(this.getState(sourceStateName), this.getState(targetStateName), requiredStateMessage, requiredControlMessage)); } else{ throw new StateTransitionGraphException("Supplied targetStateName not found in state list."); } } else{ throw new StateTransitionGraphException("Supplied sourceStateName not found in state list."); } } public void addTransitionViaIndex(int sourceStateIndex, int targetStateIndex, String requiredStateMessage, String requiredControlMessage) throws StateTransitionGraphException { //Check whether the supplied indices are possible if(this.states.get(sourceStateIndex) != null) { if(this.states.get(targetStateIndex) != null) { //Both checks passed. Add the transition. this.transitions.add(new Transition((State) this.states.get(sourceStateIndex), (State) this.states.get(targetStateIndex), requiredStateMessage, requiredControlMessage)); } else{ throw new StateTransitionGraphException("Supplied targetStateIndex not found in state list."); } } else{ throw new StateTransitionGraphException("Supplied sourceStateIndex not found in state list."); } } public void removeTransition(Transition transitionToRemove) { if(transitionToRemove != null) { this.transitions.remove(transitionToRemove); } } public void removeTransitions(ArrayList transitionsToRemove) { for(int i = 0; i < transitionsToRemove.size(); i++) { this.removeTransition((Transition) transitionsToRemove.get(i)); } } public Transition getTransitionAtIndex(int index) { return (Transition) this.transitions.get(index); } public Transition getTransition(int sourceId, int targetId) { //Returns the first transition that conforms to the requirements Transition transitionAtI; for(int i = 0; i < this.transitions.size(); i++) { transitionAtI = (Transition) this.transitions.get(i); if(transitionAtI.getSourceState().getId() == sourceId && transitionAtI.getTargetState().getId() == targetId) { return transitionAtI; } } return null; } public Transition getTransition(State sourceState, State targetState) { //Returns the first transition that conforms to the requirements Transition transitionAtI; for(int i = 0; i < this.transitions.size(); i++) { transitionAtI = (Transition) this.transitions.get(i); if(transitionAtI.getSourceState().equals(sourceState) && transitionAtI.getTargetState().equals(targetState)) { return transitionAtI; } } return null; } static public ArrayList getTransitionsWithSource(ArrayList transitionsToFilter, State sourceState) { ArrayList selectedTransitions = new ArrayList(); Transition transitionAtI; for(int i = 0; i < transitionsToFilter.size(); i++) { transitionAtI = (Transition) transitionsToFilter.get(i); if(transitionAtI.getSourceState().equals(sourceState)) { selectedTransitions.add(transitionAtI); } } return selectedTransitions; } public ArrayList getTransitionsWithSource(State sourceState) { return StateTransitionGraph.getTransitionsWithSource(this.transitions, sourceState); } static public ArrayList getTransitionsWithTarget(ArrayList transitionsToFilter, State targetState) { ArrayList selectedTransitions = new ArrayList(); Transition transitionAtI; for(int i = 0; i < transitionsToFilter.size(); i++) { transitionAtI = (Transition) transitionsToFilter.get(i); if(transitionAtI.getTargetState().equals(targetState)) { selectedTransitions.add(transitionAtI); } } return selectedTransitions; } public ArrayList getTransitionsWithTarget(State targetState) { return StateTransitionGraph.getTransitionsWithTarget(this.transitions, targetState); } static public ArrayList getTransitionsWithStateMessage(ArrayList transitionsToFilter, String stateMessage) { ArrayList selectedTransitions = new ArrayList(); Transition transitionAtI; String reqStateMessage; for(int i = 0; i < transitionsToFilter.size(); i++) { transitionAtI = (Transition) transitionsToFilter.get(i); reqStateMessage = transitionAtI.getRequiredStateMessage(); if(reqStateMessage != null) { //reqStateMessage is not null: if(reqStateMessage.equals(stateMessage)) { selectedTransitions.add(transitionAtI); } } else{ //reqStateMessage is null: if(stateMessage == null) { selectedTransitions.add(transitionAtI); } } } return selectedTransitions; } public ArrayList getTransitionsWithStateMessage(String stateMessage) { return StateTransitionGraph.getTransitionsWithStateMessage(this.transitions, stateMessage); } static public ArrayList getTransitionsWithControlMessage(ArrayList transitionsToFilter, String controlMessage) { ArrayList selectedTransitions = new ArrayList(); Transition transitionAtI; String reqControlMessage; for(int i = 0; i < transitionsToFilter.size(); i++) { transitionAtI = (Transition) transitionsToFilter.get(i); reqControlMessage = transitionAtI.getRequiredControlMessage(); if(reqControlMessage != null) { //reqControlMessage is not null: if(reqControlMessage.equals(controlMessage)) { selectedTransitions.add(transitionAtI); } } else{ //reqControlMessage is null: if(controlMessage == null) { selectedTransitions.add(transitionAtI); } } } return selectedTransitions; } public ArrayList getTransitionsWithControlMessage(String controlMessage) { return StateTransitionGraph.getTransitionsWithControlMessage(this.transitions, controlMessage); } static public ArrayList getTransitionsWithControlMessageOrNull(ArrayList transitionsToFilter, String controlMessage) { ArrayList selectedTransitions = new ArrayList(); Transition transitionAtI; String reqControlMessage; for(int i = 0; i < transitionsToFilter.size(); i++) { transitionAtI = (Transition) transitionsToFilter.get(i); reqControlMessage = transitionAtI.getRequiredControlMessage(); if(reqControlMessage != null) { //reqControlMessage is not null: if(reqControlMessage.equals(controlMessage)) { selectedTransitions.add(transitionAtI); } } else{ //reqControlMessage is null: //Null means 'don't care' so we have to pass it in all circumstances selectedTransitions.add(transitionAtI); } } return selectedTransitions; } public ArrayList getTransitionsWithControlMessageOrNull(String controlMessage) { return StateTransitionGraph.getTransitionsWithControlMessageOrNull(this.transitions, controlMessage); } static public ArrayList getTransitionsWithControlMessageAndStateMessage(ArrayList transitionsToFilter, String controlMessage, String stateMessage) { ArrayList selectedTransitions = new ArrayList(); Transition transitionAtI; String reqControlMessage; String reqStateMessage; for(int i = 0; i < transitionsToFilter.size(); i++) { transitionAtI = (Transition) transitionsToFilter.get(i); reqControlMessage = transitionAtI.getRequiredControlMessage(); reqStateMessage = transitionAtI.getRequiredStateMessage(); if(reqControlMessage != null) { //reqControlMessage is not null: if(reqControlMessage.equals(controlMessage)) { if(reqStateMessage != null) { //reqStateMessage is not null: if(reqStateMessage.equals(stateMessage)) { selectedTransitions.add(transitionAtI); } } else{ //reqStateMessage is null: if(stateMessage == null) { selectedTransitions.add(transitionAtI); } } } } else{ //reqControlMessage is null: if(controlMessage == null) { if(reqStateMessage != null) { //reqStateMessage is not null: if(reqStateMessage.equals(stateMessage)) { selectedTransitions.add(transitionAtI); } } else{ //reqStateMessage is null: if(stateMessage == null) { selectedTransitions.add(transitionAtI); } } } } } return selectedTransitions; } public ArrayList getTransitionsWithControlMessageAndStateMessage(String controlMessage, String stateMessage) { return StateTransitionGraph.getTransitionsWithControlMessageAndStateMessage(this.transitions, controlMessage, stateMessage); } } class StateTransitionGraphException extends Exception { //Just to get an exception specific to the StateTransitionGraph class public StateTransitionGraphException(String exceptionMessage) { super(exceptionMessage); } }