0

How to write a program that will read the set of states of FSM. The input data will be from text file with the format (state input next-state) and the last line is final state. for example :

    s0   a   s1
    s1   a   s2 
    s2   a   s2 
    s1 

The program output will be:

a) The list of string generated by the FSM.

b) The program can determine whether the FSM is DFA or NDFA and print the result

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
akmal elias
  • 1
  • 1
  • 1

2 Answers2

2

I'm not going to give you a coded solution, but are some directions of thought.

First of all, FSMs need start states and end states, so you're missing important info.

If you're generating a list of strings, you're probably dealing with a very limited subset of FSMs that accept a finite number of strings. Therefore, it's reasonable to try every possibility; follow every path through the graph, and print whenever you hit an end state.

Think about what differentiates a DFSM from a NDFSM. It's non-deterministic if there are multiple ways to go with some input. So, when you're building your graph, if you ever have a node with two identical transitions to different states, that's non-deterministic. Since any non-determinism makes the entire system non-deterministic, determinism is just the complete absence of non-determinism.

So, you're probably going to want to start with actually creating a representation. Two easy ways come to mind. More visually, you can create a graph. The simplest way to do this is to create a node class, then an object for each node, containing pairs of transitions and destinations.

A way I prefer to represent FSMs is with a hash map/dictionary. Use the node and transition as a key with the destination as the value. That makes navigation fairly easy.

Good luck!

EDIT: In determining non-determinism, don't forget to think about epsilon transitions (like I just did for a second. :) )

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61
  • THANKS for your directions. I have make the programme can browse for a input file then retrieve the data and store all the states and inputs in arrays. But after that, i have no idea where to start and cant understand how the algorithm will be. As u know, the string can be different as long as the path reach the final state. Thats mean the string can be more than one. How to make the programme can compute it? I dont ask for full coding, atleast please give some pictures of the algorithm that can help me to understand it. – akmal elias Jun 16 '12 at 13:48
  • It's probably going to be easiest with recursion. So, write a method the travels through the graph. Start the method at whatever you're start state is. Then have it find all the possible transitions from there. Recursively call the method you're in, taking each possible transition to a new state. You may see how this is branching out with lots of recursive calls to take all possible paths through the FSM (aka, taking every possible string it accepts). So now your base case: that's when you reach an end state. Then you can just print the path it took to get there. – Cannoliopsida Jun 16 '12 at 19:46
  • As you go through the graph, if you ever have two identical transitions (or an epsilon transition to an identical transition), you've determined that it's non-deterministic. – Cannoliopsida Jun 16 '12 at 19:47
0

Well this is an old question but for the people who stumbles on here, while nobody mentioned here, I will advice to check two existing frameworks before you implement you own State Machines with Java.

One is Spring State Machine, which you can model your State and Events as Java DSL or better use tools like Eclipse Papyrus. Most of you are familiar with Spring framework, which allow us to use several features of Spring like dependency injection and everything else that Spring can offer.

It is really great for modelling the lifecycle of an Apparat, with states like INITIALIZING, STARTED, ERROR, RECOVERING, SHUTTINGDOWN, etc.. but I see lots of people are trying to model a Shopping Chart, a Reservation System with it, the memory footprint a Spring State Machine is relatively big to model millions of Shopping Charts or Reservations.

One other disadvantage, with Spring State Machine, while has a capability to persist itself for long running processes but it does not have any mechanism to adapt to changes in these processes, if you persist a process and you have to recover it lets say 10 days later with a changed happened in the business process because of a new software release / requirements, you have no built in means to deal with it.

I have several blogs, blog1 blog2, demonstrating how you can program Spring State Machine, specially model driven way if you want to check it.

But mainly because the disadvantages I mentioned before, I advice you to look another framework first, Akka FSM (Finite State Machine) which is more fitting with its low memory footprint to have millions and millions of instances and has a capability to adapt changing long running processes.

Now you can develop with Akka framework with Java but believe me because of some missing language elements, you don't want to read the code, Scala is a much more fitting language to develop with Akka with its Case Classes (which will like lots of similar to Java Enumeration) and powerful pattern matching capabilities with Switch statements in Scala.

Now I hear you saying Scala is too complex, I can't convince my project leads to develop with Scala, to convince you all this is an option, I developed a Proof of Concept application using a Java/Scala hybrid with all Scala Akka Finite State Machine code generated from an UML model, if you want to check it out here the links to the blogs, blog3 blog4.

I hope this information would help you.

posthumecaver
  • 1,584
  • 4
  • 16
  • 29