root/trunk/examples/assignvars/fsm3.lgt

Revision 4601, 1.6 KB (checked in by pmoura, 7 weeks ago)

Added svn:mime-type property to source files (set to text/x-logtalk).

  • Property svn:mime-type set to text/x-logtalk
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2
3% fsm(Transitions, Initial, Final)
4%
5% fsm(-list, -nonvar, -list)
6
7fsm([red-0-red, red-1-green, red-2-red,     % a simple finite state machine example
8     yellow-0-red, yellow-1-green, yellow-2-red,
9     green-0-yellow, green-1-yellow, green-2-red],
10    red,
11    [red]).
12
13
14:- object(fsm(_Transitions, _Initial, _Final),
15    imports(private::assignvars)).
16
17    :- info([
18        version is 1.0,
19        author is 'Paulo Moura',
20        date is 2005/1/8,
21        comment is 'A simple implementation of finite-state machines using assignable variables and parametric objects. Adapted from a similar example by Nobukuni Kino.',
22        parnames is ['Transitions', 'Initial state', 'Final states']]).
23
24    :- public(recognise/1).
25    :- mode(recognise(+list), zero_or_more).
26    :- info(recognise/1,
27        [comment is 'Recognise a list of events.',
28         argnames is ['Events']]).
29
30    recognise(Events) :-
31        parameter(2, Initial),
32        ::assignable(Current, Initial),
33        recognise(Events, Current).
34
35    recognise([], State) :-
36        ::State => Current,
37        final_state(Current).
38    recognise([Event| Events], State) :-
39        ::State => Current,
40        transition(Event, Current, Next),
41        (write(Current-Event-Next), nl
42         ;
43         write('backtracking...'), nl, fail),
44        ::State <= Next,
45        recognise(Events, State).
46
47    transition(Event, Current, Next) :-
48        parameter(1, Transitions),
49        transition(Transitions, Event, Current, Next).
50
51    transition([Current-Event-Next| _], Event, Current, Next).
52    transition([_| Transitions], Event, Current, Next):-
53        transition(Transitions, Event, Current, Next).
54
55    final_state(State) :-
56        parameter(3, Final),
57        final_state(Final, State).
58
59    final_state([State| _], State).
60    final_state([_| States], State) :-
61        final_state(States, State).
62
63:- end_object.
Note: See TracBrowser for help on using the browser.