HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 i5 p2 A, G( E# g& D! Q: f- R- o" I2 u; G/ U# f0 _7 R
public Object buildActions () {) f3 f7 H5 R. `2 [' @( j
super.buildActions();. ]0 F3 f+ {; k* n
5 Y4 p" t. D3 p( @$ x; W; F // Create the list of simulation actions. We put these in
# `) H& d2 k4 ?4 d' t. ` // an action group, because we want these actions to be
$ j9 k6 p! M# @ // executed in a specific order, but these steps should/ P$ U: b* |; n& Z, ~/ R; n1 z
// take no (simulated) time. The M(foo) means "The message
" V; c1 H3 J7 R; W+ Z // called <foo>". You can send a message To a particular
: t" e. d) ]/ c7 Y6 \# k' x" F // object, or ForEach object in a collection.5 U& S) F( k, i# T$ X) t% T
: \/ F% p# M; E! l. P
// Note we update the heatspace in two phases: first run
; Q/ A3 F7 i. ~$ R6 q0 ?8 v% y" S // diffusion, then run "updateWorld" to actually enact the- E; W2 b# d e# m l3 a9 Y
// changes the heatbugs have made. The ordering here is
% |0 H! g# C$ e( c: x4 T // significant!5 ?" V. f3 u- X6 s% V/ b1 T! k
2 V0 W. x, |) {# l // Note also, that with the additional
9 s; F, j3 V! `$ e2 h% e // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 _) v! A n; Z0 J+ B // randomize the order in which the bugs actually run& j j1 q) Z4 k: [
// their step rule. This has the effect of removing any
8 k$ ~% k9 v. k0 F; W& I! U // systematic bias in the iteration throught the heatbug& y; l* N0 M3 O+ h8 Y
// list from timestep to timestep
' t$ u: ^: S& a0 M+ N U * V8 b D5 n/ `8 C- r- Q$ @! p; C
// By default, all `createActionForEach' modelActions have# d! y4 Q: ]* {9 h6 a+ R
// a default order of `Sequential', which means that the
! M8 S9 g D# s/ } // order of iteration through the `heatbugList' will be
- _) _ ~3 x4 q1 o0 L# q+ B. Q$ I // identical (assuming the list order is not changed
* K* v1 Y: Y2 `! M // indirectly by some other process).
7 d( [/ F/ K8 |7 a+ e4 v
# u# U9 B' Q" `* O/ } modelActions = new ActionGroupImpl (getZone ());( z6 A0 C: M( v* x: D9 f) e- K. q
# `$ F, w& r- i3 s7 {2 ~0 @
try {! L; Z+ K% }$ J
modelActions.createActionTo$message
) q. D- h& s- u$ Y (heat, new Selector (heat.getClass (), "stepRule", false));
; y6 P( e' G5 s) b } catch (Exception e) {& o% i6 q6 X; g
System.err.println ("Exception stepRule: " + e.getMessage ());
' ?8 W$ v# o* s/ q; U- F3 M }
f) b2 @+ H" N! q7 P N9 u& W/ N5 h/ m
try { B% K |: n% x; c- ]: \4 X
Heatbug proto = (Heatbug) heatbugList.get (0);
: k+ u' D1 B& E: }' y- m% @/ Z Selector sel =
6 G+ Y V- {# z9 j9 ] new Selector (proto.getClass (), "heatbugStep", false);
3 K, m% ^6 ?8 g9 G P, a5 r/ E actionForEach =
! U3 o5 j ?) r9 O modelActions.createFActionForEachHomogeneous$call
* E0 e$ m8 T3 R; W) a# { (heatbugList,
& e2 {5 q6 S+ V- B5 o; v! R- T% @ new FCallImpl (this, proto, sel,. [. t; h, R, L1 n0 B1 E
new FArgumentsImpl (this, sel)));; p( R9 Q- ~9 F4 h! R- m$ X
} catch (Exception e) {
# P1 \; Z6 J2 \- P1 B e.printStackTrace (System.err);
" K! a8 F- q9 Z% t- V" Y }
% |/ p3 o6 y+ w% ?6 b+ ~) q
$ t4 O3 X4 C1 w9 f% H/ m syncUpdateOrder ();- s: Y( T& z! O7 a
( b7 O: l2 a }/ ?4 [6 T
try {1 }! N$ B: B4 X# C2 e
modelActions.createActionTo$message 7 E( t, B: g2 l& i: @+ a h
(heat, new Selector (heat.getClass (), "updateLattice", false));
% p6 z2 g9 J) V& G } catch (Exception e) {
( y; Q9 E; \$ h! N System.err.println("Exception updateLattice: " + e.getMessage ());
7 t; g3 p+ E+ _0 U! y0 s }
, z$ U/ f; t! _/ l/ C5 v5 l; ~' z0 A8 H
/ g$ a( N1 h6 g // Then we create a schedule that executes the+ N( n; T. r3 b# \/ f+ ^& ^& B. Q. T
// modelActions. modelActions is an ActionGroup, by itself it
3 G" s, D- n% J; c L% d // has no notion of time. In order to have it executed in8 `$ s5 P& o! |( p7 X
// time, we create a Schedule that says to use the5 q1 E. g; ?* h6 H2 A+ Z. [. D
// modelActions ActionGroup at particular times. This
" Z/ e+ R& x* `* U // schedule has a repeat interval of 1, it will loop every
- _ F# i! V' i) W$ T3 B3 D // time step. The action is executed at time 0 relative to
/ Y9 |: a3 U2 M/ w6 F // the beginning of the loop.
) J: M; t! i7 V2 b9 N/ w" s. Q
7 F0 R/ ^! c" v# y: V; t+ F, F // This is a simple schedule, with only one action that is# ?( v0 U4 W' r3 J* Q% l
// just repeated every time. See jmousetrap for more
# `/ m9 Z+ U5 Y d // complicated schedules.
* {# g/ u$ n" _, z4 }# [5 q# k ! n6 s" {0 M9 d8 f$ F
modelSchedule = new ScheduleImpl (getZone (), 1);" r6 h" K% Z6 {) L/ s/ D' r( M
modelSchedule.at$createAction (0, modelActions);- T* A d$ i9 w9 R: V9 M1 O
9 M7 V' n; a! Q
return this;. Z; d( T5 q6 |# t
} |