Finite state machine

Thursday, 14 April 2011 19:34

A finite-state machine (FSM) or finite-state automaton (plural: automata), or simply a state machine, is a mathematical abstraction sometimes used to design digital logic or computer programs.

It is possible to implement an elegant finite state machine in java using enumerations. Find below an example implementing a reparation workflow process.

Finite state machine example in Java


/**
 * Repair Status
 * 
 * @author sursini
 *
 */
public enum RepairStatus {

    ORDER_CREATION        ("ORDER_FILLED"),
    
    
    QUOTATION_IN_PROGRESS ("USER_QUOTE_ACCEPTANCE_WAITING"),
    USER_QUOTE_ACCEPTANCE_WAITING ("QUOTE_ACCEPTED","QUOTE_REFUSED"),
    QUOTE_ACCEPTED         ("WORK_IN_PROGRESS","READY_TO_TRANSFER"),
    QUOTE_REFUSED         ("READY_GATHER"),
    
    ORDER_FILLED        ("WORK_IN_PROGRESS","QUOTE_REQUESTED"),
    QUOTE_REQUESTED     ("QUOTATION_IN_PROGRESS"),
    DIAGNOSIS             ("WORK_IN_PROGRESS"),
    WORK_IN_PROGRESS     ("WORK_COMPLETED","NOT_FIXABLE","READY_TO_TRANSFER","SOFT_UPGRADE_REQUESTED"),
    WORK_COMPLETED         ("VERIFICATION_OK","VERIFICATION_KO"),
    SOFT_UPGRADE_REQUESTED ("SOFT_UPGRADE_DONE"),
    SOFT_UPGRADE_DONE    ("WORK_COMPLETED"),
    NOT_FIXABLE            ("READY_GATHER","DESTRUCTED_ON_REQUEST"),
    //DEVIS
    //Attente de pice
    

    /** Transfer **/
    READY_TO_TRANSFER     ("TRANSFERED"),
    TRANSFERED             ("TRANSFER_RETURNED","DESTRUCTED_ON_REQUEST"),
    
    TRANSFER_RETURNED         ("VERIFICATION_OK","VERIFICATION_KO","NOT_FIXABLE"),            //RestituŽ
    DESTRUCTED_ON_REQUEST    ("CLOSED"),

    /** Verification **/
    VERIFICATION_KO            ("READY_TO_TRANSFER","WORK_IN_PROGRESS"),
    VERIFICATION_OK            ("READY_GATHER"),
    
    READY_GATHER             ("GATHERED","CUSTOMER_NOTIFIED"),
    CUSTOMER_NOTIFIED       ("GATHERED"),
    GATHERED                 ("CLOSED"), 
    CLOSED,
    DELETED
    ;
    
    private String[] nexts;
    
    private RepairStatus(String... nextStatuses){
        this.nexts = nextStatuses;
    }
    
    public List getNexts(){
        List result = new ArrayList();
        for (String status : nexts) {
            result.add(RepairStatus.valueOf(status));
        }
        return result;
    }
    
    public String getKey(){
        return getClass().getSimpleName().toLowerCase() + "." + name().toLowerCase();
    }
    
    static public Set listCollected(){
        Set result = new HashSet();
        result.add(GATHERED);
        result.add(CLOSED);
        result.add(DELETED);
        return result;
    }

    static public Set listNotCollected(){
        Set result = new HashSet();
        Collections.addAll(result, values());
        result.removeAll(listCollected());
        return result;
    }
}

The parameter provided in the constructor are used to define the next possible states. According to the enumeration state you are you can ask using the getNexts() method the possible following states. 

              Tags:
              
               machine
              
              ,
              
               state
              
              ,
              
               finite
              
              ,
              
               finite-state
              
              ,
              
               repair
              
              ,
              
               status
              
              ,
              
               repairstatus
              
              ,
              
               enum
              
              ,
              
               states
              

Add comment


Security code
Refresh