📄 sect05.htm
字号:
<font color=#0000ff>def</font> __init__(self, name, value):
self.name = name
self.value = value
<font color=#0000ff>def</font> __str__(self): <font color=#0000ff>return</font> self.name
<font color=#0000ff>def</font> getValue(self): <font color=#0000ff>return</font> self.value
Money.quarter = Money(<font color=#004488>"Quarter"</font>, 25)
Money.dollar = Money(<font color=#004488>"Dollar"</font>, 100)
<font color=#0000ff>class</font> Quit:
<font color=#0000ff>def</font> __str__(self): <font color=#0000ff>return</font> <font color=#004488>"Quit"</font>
Quit.quit = Quit()
<font color=#0000ff>class</font> Digit:
<font color=#0000ff>def</font> __init__(self, name, value):
self.name = name
self.value = value
<font color=#0000ff>def</font> __str__(self): <font color=#0000ff>return</font> self.name
<font color=#0000ff>def</font> getValue(self): <font color=#0000ff>return</font> self.value
<font color=#0000ff>class</font> FirstDigit(Digit): <font color=#0000ff>pass</font>
FirstDigit.A = FirstDigit(<font color=#004488>"A"</font>, 0)
FirstDigit.B = FirstDigit(<font color=#004488>"B"</font>, 1)
FirstDigit.C = FirstDigit(<font color=#004488>"C"</font>, 2)
FirstDigit.D = FirstDigit(<font color=#004488>"D"</font>, 3)
<font color=#0000ff>class</font> SecondDigit(Digit): <font color=#0000ff>pass</font>
SecondDigit.one = SecondDigit(<font color=#004488>"one"</font>, 0)
SecondDigit.two = SecondDigit(<font color=#004488>"two"</font>, 1)
SecondDigit.three = SecondDigit(<font color=#004488>"three"</font>, 2)
SecondDigit.four = SecondDigit(<font color=#004488>"four"</font>, 3)
<font color=#0000ff>class</font> ItemSlot:
id = 0
<font color=#0000ff>def</font> __init__(self, price, quantity):
self.price = price
self.quantity = quantity
<font color=#0000ff>def</font> __str__(self): <font color=#0000ff>return</font> ´ItemSlot.id´
<font color=#0000ff>def</font> getPrice(self): <font color=#0000ff>return</font> self.price
<font color=#0000ff>def</font> getQuantity(self): <font color=#0000ff>return</font> self.quantity
<font color=#0000ff>def</font> decrQuantity(self): self.quantity -= 1
<font color=#0000ff>class</font> VendingMachine(StateMachine):
changeAvailable = ChangeAvailable()
amount = 0
FirstDigit first = null
ItemSlot[][] items = ItemSlot[4][4]
# Conditions:
<font color=#0000ff>def</font> notEnough(self, input):
i1 = first.getValue()
i2 = input.getValue()
<font color=#0000ff>return</font> items[i1][i2].getPrice() > amount
<font color=#0000ff>def</font> itemAvailable(self, input):
i1 = first.getValue()
i2 = input.getValue()
<font color=#0000ff>return</font> items[i1][i2].getQuantity() > 0
<font color=#0000ff>def</font> itemNotAvailable(self, input):
<font color=#0000ff>return</font> !itemAvailable.condition(input)
#i1 = first.getValue()
#i2 = input.getValue()
#<font color=#0000ff>return</font> items[i1][i2].getQuantity() == 0
# Transitions:
<font color=#0000ff>def</font> clearSelection(self, input):
i1 = first.getValue()
i2 = input.getValue()
ItemSlot <font color=#0000ff>is</font> = items[i1][i2]
<font color=#0000ff>print</font> (
<font color=#004488>"Clearing selection: item "</font> + <font color=#0000ff>is</font> +
<font color=#004488>" costs "</font> + <font color=#0000ff>is</font>.getPrice() +
<font color=#004488>" and has quantity "</font> + <font color=#0000ff>is</font>.getQuantity())
first = null
<font color=#0000ff>def</font> dispense(self, input):
i1 = first.getValue()
i2 = input.getValue()
ItemSlot <font color=#0000ff>is</font> = items[i1][i2]
<font color=#0000ff>print</font> (<font color=#004488>"Dispensing item "</font> +
<font color=#0000ff>is</font> + <font color=#004488>" costs "</font> + <font color=#0000ff>is</font>.getPrice() +
<font color=#004488>" and has quantity "</font> + <font color=#0000ff>is</font>.getQuantity())
items[i1][i2].decrQuantity()
<font color=#0000ff>print</font> (<font color=#004488>"Quantity "</font> +
<font color=#0000ff>is</font>.getQuantity())
amount -= <font color=#0000ff>is</font>.getPrice()
<font color=#0000ff>print</font>(<font color=#004488>"Amount remaining "</font> +
amount)
<font color=#0000ff>def</font> showTotal(self, input):
amount += ((Money)input).getValue()
<font color=#0000ff>print</font> <font color=#004488>"Total amount = "</font> + amount
<font color=#0000ff>def</font> returnChange(self, input):
<font color=#0000ff>print</font> <font color=#004488>"Returning "</font> + amount
amount = 0
<font color=#0000ff>def</font> showDigit(self, input):
first = (FirstDigit)input
<font color=#0000ff>print</font> <font color=#004488>"First Digit= "</font>+ first
<font color=#0000ff>def</font> __init__(self):
StateMachine.__init__(self, State.quiescent)
<font color=#0000ff>for</font>(int i = 0 i < items.length i++)
<font color=#0000ff>for</font>(int j = 0 j < items[i].length j++)
items[i][j] = ItemSlot((j+1)*25, 5)
items[3][0] = ItemSlot(25, 0)
buildTable(Object[][][]{
::State.quiescent, # Current state
# Input, test, transition, next state:
:Money.<font color=#0000ff>class</font>, null,
showTotal, State.collecting,
::State.collecting, # Current state
# Input, test, transition, next state:
:Quit.quit, null,
returnChange, State.quiescent,
:Money.<font color=#0000ff>class</font>, null,
showTotal, State.collecting,
:FirstDigit.<font color=#0000ff>class</font>, null,
showDigit, State.selecting,
::State.selecting, # Current state
# Input, test, transition, next state:
:Quit.quit, null,
returnChange, State.quiescent,
:SecondDigit.<font color=#0000ff>class</font>, notEnough,
clearSelection, State.collecting,
:SecondDigit.<font color=#0000ff>class</font>, itemNotAvailable,
clearSelection, State.unavailable,
:SecondDigit.<font color=#0000ff>class</font>, itemAvailable,
dispense, State.wantMore,
::State.unavailable, # Current state
# Input, test, transition, next state:
:Quit.quit, null,
returnChange, State.quiescent,
:FirstDigit.<font color=#0000ff>class</font>, null,
showDigit, State.selecting,
::State.wantMore, # Current state
# Input, test, transition, next state:
:Quit.quit, null,
returnChange, State.quiescent,
:FirstDigit.<font color=#0000ff>class</font>, null,
showDigit, State.selecting,
)
# :~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><A NAME="_Toc534420094"></A><BR></P></DIV>
<A NAME="Heading45"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H3 ALIGN="LEFT">
Testing the machine</H3></FONT>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE># c04:vendingmachine:VendingMachineTest.py
# Demonstrates use of StateMachine.py
vm = VendingMachine()
<font color=#0000ff>for</font> input <font color=#0000ff>in</font> [
Money.quarter,
Money.quarter,
Money.dollar,
FirstDigit.A,
SecondDigit.two,
FirstDigit.A,
SecondDigit.two,
FirstDigit.C,
SecondDigit.three,
FirstDigit.D,
SecondDigit.one,
Quit.quit]:
vm.nextState(input)
# :~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><A NAME="_Toc534420095"></A><BR></P></DIV>
<A NAME="Heading46"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
Tools</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Another approach, as your state machine
gets bigger, is to use an automation tool whereby you configure a table and let
the tool generate the state machine code for you. This can be created yourself
using a language like Python, but there are also free, open-source tools such as
<I>Libero</I>, at <A HREF="http://www.imatix.com">http://www.imatix.com</A>.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_189">Add Comment</A></FONT><A NAME="_Toc534420096"></A><BR></P></DIV>
<A NAME="Heading47"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
Exercises</H2></FONT>
<OL>
<LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create an example of the
“virtual proxy.”
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_190">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create
an example of the “Smart reference” proxy where you keep count of
the number of method calls to a particular object.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_191">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create
a program similar to certain DBMS systems that only allow a certain number of
connections at any time. To implement this, use a singleton-like system that
controls the number of “connection” objects that it creates. When a
user is finished with a connection, the system must be informed so that it can
check that connection back in to be reused. To guarantee this, provide a proxy
object instead of a reference to the actual connection, and design the proxy so
that it will cause the connection to be released back to the system.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_192">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Using
the <I>State</I>,<I> </I>make a class called <B>UnpredictablePerson</B> which
changes the kind of response to its <B>hello( )</B> method depending on
what kind of <B>Mood</B> it’s in. Add an additional kind of <B>Mood</B>
called <B>Prozac</B>.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_193">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create
a simple copy-on write implementation.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_194">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Apply
<B>TransitionTable.py</B> to the “Washer” problem.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_195">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create
a <I>StateMachine</I> system whereby the current state along with input
information determines the next state that the system will be in. To do this,
each state must store a reference back to the proxy object (the state
controller) so that it can request the state change. Use a <B>HashMap</B> to
create a table of states, where the key is a <B>String</B> naming the new state
and the value is the new state object. Inside each state subclass override a
method <B>nextState( )</B> that has its own state-transition table. The
input to <B>nextState( )</B> should be a single word that comes from a text
file containing one word per line.<B>
</B><A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_196">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Modify
the previous exercise so that the state machine can be configured by
creating/modifying a single multi-dimensional array.<B>
</B><A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_197">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Modify
the “mood” exercise from the previous session so that it becomes a
state machine using StateMachine.java
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_198">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create
an elevator state machine system using StateMachine.java
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_199">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Create
a heating/air-conditioning system using StateMachine.java<B>
</B><A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_200">Add Comment</A></FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">A
<I>generator</I> is an object that produces other objects, just like a factory,
except that the generator function doesn’t require any arguments. Create a
<B>MouseMoveGenerator</B> which produces correct <B>MouseMove</B> actions as
outputs each time the generator function is called (that is, the mouse must move
in the proper sequence, thus the possible moves are based on the previous move
– it’s another state machine). Add a method <B>iterator( )
</B>to produce an iterator, but this method should take an <B>int</B> argument
that specifies the number of moves to produce before <B>hasNext( )</B>
returns <B>false</B>.<B>
</B><A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_201">Add Comment</A></FONT></OL>
<HR><DIV ALIGN="LEFT"><P><A NAME="fn13" HREF="#fnB13">[13]</A><FONT FACE="Georgia"> No
mice were harmed in the creation of this example.</FONT><BR></P></DIV>
<DIV ALIGN="CENTER">
<FONT FACE="Verdana, Tahoma, Arial, Helvetica, Sans" size = "-1">
[ <a href="Sect04.htm">Previous Chapter</a> ]
[ <a href="javascript:window.location.href = 'Index.htm';">Table of Contents</a> ]
[ <a href="DocIdx.htm">Index</a> ]
[ <a href="Sect06.htm">Next Chapter</a> ]
</FONT>
<BR>
Last Update:12/31/2001</P></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -