HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! D$ s( w7 ~1 D5 X& W) A' ]: d2 K) J" Z# ]5 s. a5 p8 `2 M% q
public Object buildActions () {
" c/ G" p1 |* l$ N super.buildActions();
5 L0 J6 t7 k, i$ T4 Y 6 {& M. |4 a0 h6 M3 p% {
// Create the list of simulation actions. We put these in- Z! V# W; A! K) K2 R" Y2 k& ?
// an action group, because we want these actions to be
( F7 O) _* T) W3 I // executed in a specific order, but these steps should
3 G7 w' M) [2 n: P // take no (simulated) time. The M(foo) means "The message" n6 D& h' x F/ C
// called <foo>". You can send a message To a particular
1 X/ q4 _- {& U: m% d6 L // object, or ForEach object in a collection.
' Z+ h' D( I9 m9 d; s9 y 6 d: c8 d6 J2 }. }: d4 c5 S
// Note we update the heatspace in two phases: first run) Q- V) Z: E4 `1 R1 Q1 ^5 Q2 N$ H
// diffusion, then run "updateWorld" to actually enact the
( t8 t' P) u' D) M1 b& d3 U // changes the heatbugs have made. The ordering here is
1 ]% m C) Q0 I, [( v; X1 r# ]/ I9 [ // significant!
* V; u$ s: t( ^2 e! f* @ 5 g. H$ A3 x, J
// Note also, that with the additional
) T; J0 \0 }& E& y5 q7 N // `randomizeHeatbugUpdateOrder' Boolean flag we can2 c" W" K! Q' H! I1 ?+ N1 x
// randomize the order in which the bugs actually run
$ u6 t0 ?1 V! E1 {# k5 _' A // their step rule. This has the effect of removing any9 ~/ D2 d% c$ `: a
// systematic bias in the iteration throught the heatbug
# C0 O3 x! d# p' P. t: a4 E // list from timestep to timestep3 L2 V8 e! ~# i/ S; A; L
; @4 ?- p L3 i) g) K9 g" W$ g // By default, all `createActionForEach' modelActions have
3 u1 s/ E2 n" E; h& X/ f! i8 E" H* t // a default order of `Sequential', which means that the
8 D2 v! F" ^. N0 a // order of iteration through the `heatbugList' will be
4 E1 p9 S" d" t6 p: x! U0 D // identical (assuming the list order is not changed
/ w0 S. \7 P3 \- b; y // indirectly by some other process).) Q: v5 x1 w3 O5 C% c) L' N
9 q9 W- P7 J) H. ^7 w2 l) J, x4 }
modelActions = new ActionGroupImpl (getZone ());$ I6 U* I) x5 [5 s9 O. u
8 J( k% o2 d# o0 M
try {
& t$ ]0 n4 n, V, R$ e: X% R modelActions.createActionTo$message
, t/ T, Q& w4 q/ v; J (heat, new Selector (heat.getClass (), "stepRule", false));
% C. ]* ^. `0 u* h } catch (Exception e) {
1 H4 ~* q" {0 f# H; q System.err.println ("Exception stepRule: " + e.getMessage ());. ~ Y1 x( ^5 k6 i$ N5 K& W
}! U" D2 c/ s5 |# K' W% J
- o* o3 h7 ^3 {. j
try {
, j5 H8 w0 F- l$ t: }5 h5 U1 _ Heatbug proto = (Heatbug) heatbugList.get (0);
2 U* {( X7 q- W' b Selector sel =
7 x% R4 Z7 }+ U: l \+ K+ }4 w new Selector (proto.getClass (), "heatbugStep", false);8 \' R% _* S, d
actionForEach =, l! Y. x. f5 d7 c
modelActions.createFActionForEachHomogeneous$call
/ a, w- Q+ E2 O6 B; u1 H+ m (heatbugList,4 k, z; z% _9 p2 k- x. S, @
new FCallImpl (this, proto, sel,
" y5 ?* S9 T3 ^+ v new FArgumentsImpl (this, sel)));
$ }/ ?: P) P0 O } catch (Exception e) {
0 I. f9 z& W G, `0 X+ k W# q( _ e.printStackTrace (System.err);
8 f3 ?+ y- _( S, V }
( y0 X2 g" s/ ?$ Z9 ^( Z, T 1 ], w0 v- ]% T- s
syncUpdateOrder ();
" d- Z0 u! V6 a( o2 H7 V- t; |1 g$ `( L# ^4 N5 L5 [# v3 O% q
try {
6 F( r7 G1 y2 f0 M. t modelActions.createActionTo$message 3 m1 \+ S8 D; P
(heat, new Selector (heat.getClass (), "updateLattice", false));
a$ N2 |$ F" x' \' e( h# i } catch (Exception e) {
; l1 u3 ]7 Q5 q! x System.err.println("Exception updateLattice: " + e.getMessage ());
1 ^+ K0 G, h5 R) Q1 x$ t$ E% T; }3 L }! B5 K% B! B# E# P+ |7 [
& e+ J1 C5 b% ~. {. ?! s$ r // Then we create a schedule that executes the! w/ }6 {' F9 R, N( W
// modelActions. modelActions is an ActionGroup, by itself it' t9 H _3 ^) q9 f" s5 H/ @7 G
// has no notion of time. In order to have it executed in
% G+ d* n: C( W+ n" n$ Y8 W // time, we create a Schedule that says to use the
" i- [! p% ~$ {! |( e5 p // modelActions ActionGroup at particular times. This
0 B6 B1 M+ f0 t( T( z. @ // schedule has a repeat interval of 1, it will loop every3 Z5 ~$ Y1 G# O, L) O
// time step. The action is executed at time 0 relative to
, h3 c2 |$ K0 Y8 P // the beginning of the loop.
/ j) j6 B' `& e/ N$ Y+ U: K {% D! \4 e# k
// This is a simple schedule, with only one action that is, ], h* B2 y1 k9 i
// just repeated every time. See jmousetrap for more
9 L: B; I$ S: x2 B" j3 z- a, j; O // complicated schedules.
% L+ F" X# O7 S6 i) X8 a# x # ]7 Z. x6 x% C9 p" v
modelSchedule = new ScheduleImpl (getZone (), 1); R: T: v& v& ^* z# ]
modelSchedule.at$createAction (0, modelActions);6 ~+ |, P- m m9 o
5 ~' {/ d, E, H. H return this;& a4 Y9 k; g0 y! ~9 B8 V2 z
} |