Solved

# Please teach me about how to implement state diagrams in C#

Posted on 2008-06-22
519 Views
This is not homework.

I am interested in scanning large amounts of textual content for many different search terms of interest.

I am told that using a "state diagram" is an efficient method to do this. I don't know if this is true and indeed it is likely that I will use a prebuilt product to do this. However, I would like to learn this as a matter of principle.

I would like it very much if you could show me a C# implementation of a state diagram. It would also help if you could comment the code and advise what state diagrams are and are not good for. Most helpful if you can teach me what you know.

thank you!

0
Question by:kennethfine
• 3
• 2

LVL 55

Expert Comment

ID: 21843477
well, indeed correct term is "state machines".
There are lots of good examples at codeproject.com, search for the 'state machine' keywords
0

LVL 6

Author Comment

ID: 21843737

Thanks. Can someone give an explanation? I read the Wikipedia articles on both "state diagrams" and "state machines" and found them unsatisfying.
0

LVL 23

Accepted Solution

Christopher Kile earned 500 total points
ID: 21847965
A "state" is simply the list of descriptions of your current situation.  Your clothes are dirty, your kitchen sink is filled with dishes, and you're running late.  An "event" occurs, such as "doing the dishes".  Your state is now "clothes are dirty, kitchen sink is empty, and you're running later than you were."

Don't complicate the idea of states too much.  Keep clear in your head that states are mundane lists of what is.  I made the mistake of overcomplicating on my first attempt at designing a state machine that determined what the remainder of dividing a binary number by 3 would be; my state machine "worked", but I had eleven states - of course, there are naturally only three states, those being remainder 0, remainder 1, and remainder 2.

You will have to describe every possible state for your system if you're building a state machine.  You also need to describe, for a given state, what state you will be in for each possible event that can change the given state to another state (some events, like "call your buddy to find out when the game is tonight", don't change the state at all; some events such as "burn the house down because you don't want to clean it" are not permitted events and require exception handling (such as police restraint and fire suppression)).

When you're processing text, sometimes simple state machines are not enough.  Compiling source code to something that can run on a computer is a typical example, as this frequently requires a "pushdown automaton" where one state machine is "pushed" onto a stack while another state machine is tested to see whether it can process the remainder of the text or not, with the idea of picking the state machine from an available list that process the greatest amount of the remaining text while not leaving text that cannot be processed by any available state machine.

Thus, simply learning about state machines may not solve your problem.  What sort of text processing are you trying to do?
0

LVL 6

Author Comment

ID: 21851454
Thanks very much for your time, cpkilekofp. I am first just interested in learning in these general concepts and your message is very helpful in that regard.

I am interested in matching large groups of short terms against a large collection of content. My best bet as a production thing will probably be to use a prebuilt indexing solution that exposes an API and work against that. However, it would be interesting to learn efficient ways of doing the same work "by hand" for instances where I don't want to work against an index or don't have an index.
0

LVL 23

Expert Comment

ID: 21856381
Hmmmmm....there is no general case for efficient ways of doing it "by hand."  The problem is sensitively dependent on the format of your data, and free-form text is the worst case.

Have you experimented with regular expressions?  Matching a regular expression to your input as you read it is a common way of generating an event.
0

LVL 6

Author Comment

ID: 21856445
Thanks. I was most interested in "theory" here and your post provided an excellent introduction. Much appreciated!
0

## Featured Post

### Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…