PHP Classes
elePHPant
Icontem

FSM Chat: Automated dialog robot using finite state machines

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2006-01-11 (10 years ago) RSS 2.0 feedNot enough user ratingsTotal: 1,655 This week: 1All time: 2,295 This week: 1,047Up
Version License Categories
fsmchat 1.0Freely DistributableNetworking, Chat
Description Author

This class can be used to implement an automated dialog robot using finite state machines.

The class can be configured to define input text strings under each state, response strings and the new states to assume when the specified input texts appear.

It can act as an automated chat robot that can be hooked to a remote console via telnet or other networking protocol.

Other possible applications: text file parser, command line interpreter etc.

Recommendations

What is the best PHP telnet class?
Automated access to server via telnet

Innovation Award
PHP Programming Innovation award nominee
November 2005
Number 3


Prize: One downloadable copy of Roadsend PHP Compiler
Building robots to access remote servers and execute complex operations is often difficult task. Depending on the responses of the servers, the robot may need to respond differently to execute the intended operations.

This class provides a solution that simplifies the construction of software robots. It uses finite state machines to help the robot know where it is on each operation step and what it should do next depending on the server response.

Manuel Lemos
Picture of Dmitry A. Kirilin
Name: Dmitry A. Kirilin <contact>
Classes: 5 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 1x

Details
FSM Chat
========

What is it?
~~~~~~~~~~~
A PHP class for creating dialog robots, line-oriented file parsers and command line interpreters using Finite State Machine.

How to use?
~~~~~~~~~~~
First, include the class file.
Then you must define input patterns (as regular expressions), actions
to take when each of them are found, and optional machine states: the default target state (can be changed inside action; can be NULL if no change) and required state (NULL if no state check is needed).

Example: E1loadd - Dialog robot for interacting with Cisco AS53xx access server in order to continuously retrieve active calls information.

It is also possible to load FSM definition from file or another source usingLoadFSM() and LoadFSMFile() methods.

FSM definition syntax.
~~~~~~~~~~~~~~~~~~~~~~
It is just like Makefile syntax:

Line beginning with "#" is a comment.
Empty line is an end of state definiton (it is required even at EOF).
Line starting with an alphanumeric symbol is a state definition:
<Required state or *> <regular expression> [Default state]
Any following line starting with tab character is an action string.


Method description.
~~~~~~~~~~~~~~~~~~~

void FSM( string Expect, string Do [, string Target [, string Require] ] )
 Add a state definition.
  Expect: regex to match input string or packer.
  Do: PHP code to execute on match.
  Target: state to take if "Do" code did not specify it explicitly.
  Require: search for "Expect" only if macine is in this state.

 "Do" code may return:
   string: state to take.
   array("STOP"=> stop ,"NEWSTATE"=> state, "CONTINUE"=> cont, "REPLY"=> reply):
    if "stop" is nonzero, the FSM will stop with FSMSTOP_STOP return code.
    if "state" is nonempty string, the machine will take this state.
    if "cont" is nonzero, the FSM will ignore current match and continues match search.
    reply is passed directly to a stream.

void LoadFSM( string data )
 Load state definitions from string in syntax described above.

void LoadFSMFile( string filename )
 Load state definitions from specified file (wrapper for LoadFSM() ).

int RunString( resource Stream, string StartState )
 Runs a string-oriented FSM on specified stream, assuming StartState.
 Return codes:
  FSMSTOP_OK		Stopped at data end.
  FSMSTOP_STOP		Stopped by action handler.
  FSMSTOP_UNHANDLED	No matches found.

FSM Parser
==========

What is it?
~~~~~~~~~~~
A PHP class for creating parsers and preprocessors using Finite State Machine.

How to use?
~~~~~~~~~~~
First, include the class file.
Then you must define language constructs (as regular expressions), actions
to take when each of them are found, and optional machine states: the default
target state (can be changed inside action; can be NULL if no change) and
required state (NULL if no state check is needed).

Example: xmlparser.php - a very simple XML parser.

It is also possible to load FSM definition from file or another source using
LoadFSM() and LoadFSMFile() methods.

Example: xmlparser_loadfsm.php - the same XML parser loaded from .fsm file.

FSM definition syntax.
~~~~~~~~~~~~~~~~~~~~~~
It is just like Makefile syntax:

Line beginning with "#" is a comment.
Empty line is an end of state definiton (it is required even at EOF).
Line starting with an alphanumeric symbol is a state definition:
<Required state or *> <regular expression> [Default state]
Any following line starting with tab character is an action string.


Method description.
~~~~~~~~~~~~~~~~~~~

void FSM( string Expect, string Do [, string Target [, string Require] ] )
 Add a state definition.
  Expect: regex to match closest to current position.
  Do: PHP code to execute on best match. Inside this code, $STRING is an input string or packet that matches "Expect", $STATE is current state.
  Target: state to take if "Do" code did not specify it explicitly.
  Require: search for "Expect" only if macine is in this state.

 "Do" code may return:
   string: state to take.
   array("STOP"=> stop ,"NEWSTATE"=> state):
    if "stop" is nonzero, the FSM will stop with FSMSTOP_STOP return code.
    if "state" is nonempty string, the machine will take this state.

void LoadFSM( string data )
 Load state definitions from string in syntax described above.

void LoadFSMFile( string filename )
 Load state definitions from specified file (wrapper for LoadFSM() ).

int RunString( resource Stream, string StartState )
 Runs a string-oriented FSM algorithm on specified stream.
 Return codes:
  FSMSTOP_OK		Stopped at data end.
  FSMSTOP_STOP		Stopped by action handler.
  FSMSTOP_UNHANDLED	No matches found.

int RunPacket( resource Stream, string StartState )
 Runs a packet-oriented FSM algorithm on specified stream.
 Return codes:
  FSMSTOP_OK		Stopped at data end.
  FSMSTOP_STOP		Stopped by action handler.
  FSMSTOP_UNHANDLED	No matches found.

  Files folder image Files  
File Role Description
Accessible without login Plain text file E1loadd Example Cisco chatter
Plain text file fsmchatclass.inc.php Class Class source
Accessible without login Plain text file README Doc. Some documentation

 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,655
This week:1
All time:2,295
This week:1,047Up
User Comments (1)
Excellent
7 years ago (kishore kumar)
80%StarStarStarStarStar