HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
^0 s' n% [% _3 f/ `% O2 w6 @- g
4 p, f }2 V0 X |" V public Object buildActions () {! ^% J( G, Z9 R5 m: E8 p& q8 P' k
super.buildActions();" U% H: `% |5 u9 _4 l* d y3 b; h
& Y) u+ S2 J& d7 j
// Create the list of simulation actions. We put these in9 v6 d& X# w; H
// an action group, because we want these actions to be' u- ^: C6 {/ w7 G5 m' w
// executed in a specific order, but these steps should8 Z9 D. n% ?; T" [
// take no (simulated) time. The M(foo) means "The message
* Q6 f. ]2 G+ w7 O/ t7 {; F // called <foo>". You can send a message To a particular
6 V! ` j4 r! e // object, or ForEach object in a collection.' F2 [7 X0 }: V1 ^
% v$ M, ~3 o8 m" \1 @ // Note we update the heatspace in two phases: first run; w! I4 s. g! g: E6 }1 f+ Q
// diffusion, then run "updateWorld" to actually enact the v9 N; C, s& R/ |0 f/ L( o, L0 O
// changes the heatbugs have made. The ordering here is. B( g# T0 K: i" C
// significant!8 G1 L2 ~ g u$ {3 n+ H" [+ h6 T2 ]
. Z5 |8 A: I5 Y! D/ P9 b+ s3 V // Note also, that with the additional
7 f5 J) n' |0 p) m // `randomizeHeatbugUpdateOrder' Boolean flag we can m5 G( q. Q& [3 J
// randomize the order in which the bugs actually run
& R$ X# M. w" e3 m // their step rule. This has the effect of removing any
9 c" c* h, z0 t b" M // systematic bias in the iteration throught the heatbug- J/ P. L1 [* A
// list from timestep to timestep, q, C4 _ v) q- `" t0 z* v( q3 t
5 ^: F8 k& N3 f/ n! z0 T' l+ D L // By default, all `createActionForEach' modelActions have. p I( T$ s# a" P
// a default order of `Sequential', which means that the
* ~8 u% P. t% Q3 S // order of iteration through the `heatbugList' will be
- u- c; I* G$ D4 P // identical (assuming the list order is not changed" i1 w, E) B$ K: I/ Z g5 ~0 \
// indirectly by some other process).
' z7 h; p8 P3 ?1 Y. _
7 X8 h1 w. }' ?; Z5 V modelActions = new ActionGroupImpl (getZone ());4 t! _2 \6 Y6 ]% ?# U; t
6 f/ I+ o5 a' @! \& l
try {; X" N, C( t e) Y, {
modelActions.createActionTo$message3 \+ A5 v) ] V3 P; K
(heat, new Selector (heat.getClass (), "stepRule", false));/ u3 g5 H- N! f& W1 C9 Z' G
} catch (Exception e) {
P% v) V$ }0 @! ~" O5 d System.err.println ("Exception stepRule: " + e.getMessage ());
$ w* s s* X) b/ R& u }
' a$ Q" R; m" ?
+ H# ?( }, O: p! R7 h try {0 ~6 }4 G& p5 L9 K9 u
Heatbug proto = (Heatbug) heatbugList.get (0);; i' D. Q% L/ H: G L% x7 y3 @3 L
Selector sel = + B1 F5 b8 r8 P, P5 V8 N% Z+ s" ~
new Selector (proto.getClass (), "heatbugStep", false);
M4 t/ J+ W+ Q+ m' @- ~$ J actionForEach = `0 `. J. y9 N+ l" D2 @* U
modelActions.createFActionForEachHomogeneous$call2 t3 a3 ?/ _" Z2 h, E' F
(heatbugList,
% x; f/ t8 x; Z/ @& W2 K* q new FCallImpl (this, proto, sel,
) j7 D* t* v+ S2 [: a new FArgumentsImpl (this, sel)));
+ Z4 ]7 z2 e1 W6 g" G6 \ } catch (Exception e) {0 Q, O9 v ?; r% f/ e# w
e.printStackTrace (System.err);; K. n7 ^8 I" W0 y
}
, d2 K% n9 x8 v5 h2 L5 t! k 9 t0 |* e( a( e" Z+ M/ u
syncUpdateOrder ();
( v5 J( u& B* V- W
; ]: s: Y) [7 L* W8 J try {3 e" @4 T$ Z' B7 ]
modelActions.createActionTo$message 6 ]9 Q& c# j: i' I
(heat, new Selector (heat.getClass (), "updateLattice", false));- ?. p9 W% J6 \! C( _7 e
} catch (Exception e) {, c4 }' X, a7 i
System.err.println("Exception updateLattice: " + e.getMessage ());
, m9 A: s. i# S# p6 w0 N7 e2 _ }" x9 B( P9 d. J1 M4 C( l
: [" X6 [; }) e! [) q // Then we create a schedule that executes the
4 o/ C3 _+ B+ \0 N0 }1 Q // modelActions. modelActions is an ActionGroup, by itself it
& X4 _! H9 z0 @: x0 e) p/ M // has no notion of time. In order to have it executed in
' R1 ?/ u) [& `' z0 V // time, we create a Schedule that says to use the
* w/ _+ s0 @9 ?2 u // modelActions ActionGroup at particular times. This L% O/ g, y1 t& v, ?
// schedule has a repeat interval of 1, it will loop every
# ]' G) U3 V) X6 t // time step. The action is executed at time 0 relative to
. T0 w u- o6 D5 |. H // the beginning of the loop.. j# r8 m7 p$ R0 c7 Y
: r. O+ R5 P, G# H8 L. J$ g% s // This is a simple schedule, with only one action that is
$ h6 S8 W3 h0 ~2 c$ U1 u // just repeated every time. See jmousetrap for more: r( u3 G6 Z9 f3 \/ {/ h$ z
// complicated schedules.! A* m. h6 D. q+ b7 [
1 e2 Q5 B( f! B* J" L m+ V. I modelSchedule = new ScheduleImpl (getZone (), 1);9 l: q$ C* _4 d1 E6 e( g
modelSchedule.at$createAction (0, modelActions);
: @ u: O) s; e2 ~& T; M$ }3 } 7 C3 O. B9 ?) n% U- l
return this;
- O/ o/ X, I5 G ^3 V" p/ [9 P } |