HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 e+ p/ |: ?( `3 E& L% S; k
6 s% v+ x& L8 ~ public Object buildActions () {. K- K2 z0 z$ T# ]5 V" k
super.buildActions();
; N, J1 s7 A' f / g# \* L: Y/ i% F
// Create the list of simulation actions. We put these in8 |6 @' C) G5 R7 m4 _% B
// an action group, because we want these actions to be
) Z* g: X' N. m! z. W: [ // executed in a specific order, but these steps should) l7 T j# y+ F; p1 \
// take no (simulated) time. The M(foo) means "The message
% h" N( j. ?8 F% ^, o // called <foo>". You can send a message To a particular! Q' B* y L- z8 q% M* R
// object, or ForEach object in a collection.
) l; e1 E$ R, c- N
/ c6 o/ A/ D$ j. A // Note we update the heatspace in two phases: first run
7 \# I. |4 E; s% a6 b/ a // diffusion, then run "updateWorld" to actually enact the) m* P! I' A" t
// changes the heatbugs have made. The ordering here is
6 t+ z8 Y+ y2 z5 |/ B+ h4 U // significant!7 R' g, R4 _" x: E
& C9 K( \' N/ \$ W4 _9 t // Note also, that with the additional
2 ]7 Y% B- \+ d2 H1 E% O# P, h // `randomizeHeatbugUpdateOrder' Boolean flag we can
& \8 T9 W' g! Q9 E$ g' |3 ]' r // randomize the order in which the bugs actually run
$ j$ i* m, U* a6 `& l // their step rule. This has the effect of removing any' Y6 x; h! E! O
// systematic bias in the iteration throught the heatbug
1 P% a. m& A4 d8 o // list from timestep to timestep$ S3 Y; D4 s; ?
s' }. ~: V2 {. G/ n // By default, all `createActionForEach' modelActions have
$ f c( ^) A1 x& t# D& Q // a default order of `Sequential', which means that the; m ~5 q, u; x' w' {
// order of iteration through the `heatbugList' will be
, H+ M5 ^1 @2 M. ~ m' q1 l1 E // identical (assuming the list order is not changed g. x. K# H7 v/ ?9 I7 q. i
// indirectly by some other process).
+ B$ y$ X# i5 v+ c& G7 a- x , v0 V1 T0 R$ Y/ r; Y+ }
modelActions = new ActionGroupImpl (getZone ());# o5 Y. F% A! x( o) C3 n4 T
( {1 o* }3 F1 D/ [ try {5 S3 w5 z/ s0 ~- w* _& U0 o
modelActions.createActionTo$message2 ~( L7 _# H( ]
(heat, new Selector (heat.getClass (), "stepRule", false)); J2 Q" W( A" [- i/ U* G; [5 h
} catch (Exception e) {5 C) X: s2 [0 h. a- P; X
System.err.println ("Exception stepRule: " + e.getMessage ());
. f* c; c% x# s( S' N3 H3 }5 u7 [ }( q. U2 d7 I/ g2 x- _$ J
3 h& f# z+ w \
try {
0 x) e7 i4 o8 j Heatbug proto = (Heatbug) heatbugList.get (0);
R/ Y5 M: W$ K: _, G3 Q1 A* M Selector sel = + V5 W# Q( j' d7 B9 x0 A% z$ p
new Selector (proto.getClass (), "heatbugStep", false);
1 A3 f. @7 M5 n* ~, G actionForEach =
$ d1 x- e$ s% f7 j" w& p, U modelActions.createFActionForEachHomogeneous$call
- l* p0 r. o) n, a4 D' R (heatbugList,
) A. w/ b* I3 _- c! U4 K( h new FCallImpl (this, proto, sel,# H# s0 A1 I/ M* `. H
new FArgumentsImpl (this, sel)));
' W( P, @( I' I( l$ W } catch (Exception e) {. Y1 H+ d+ f- |3 ^0 l
e.printStackTrace (System.err);
v. l0 @3 F* l; x# w9 Y0 ? }' d! `4 K5 k) K9 i6 C
: K! }1 S* z7 n# i. G7 {$ S' T, f5 @ syncUpdateOrder ();' v9 @ _7 h& i* D! B D c
4 `2 `" |6 ~' W5 E
try {
; i+ c+ G/ Z- a+ Y/ c- i: c modelActions.createActionTo$message
4 H: i; P( ~5 b: q* v' K% O0 [5 u3 L (heat, new Selector (heat.getClass (), "updateLattice", false));; T: b# W* P& q! u5 a1 ]
} catch (Exception e) {! ?2 t# [& V& m4 o* }8 g; f% \
System.err.println("Exception updateLattice: " + e.getMessage ());
' S& |5 R4 x( \$ F }
( v- n, Q# K) ^: G. a0 ?5 z6 C, g8 Z
! J0 `: v, x5 o% M; ~: r // Then we create a schedule that executes the! O; S$ J+ l& g' c% v+ x6 `5 t
// modelActions. modelActions is an ActionGroup, by itself it0 Z- j% ^. U. u- a) t- E- v
// has no notion of time. In order to have it executed in
" S, z8 ~; m5 s# T! F! R // time, we create a Schedule that says to use the3 D) b! h+ t- N* Z O' k R
// modelActions ActionGroup at particular times. This n# P& h) T/ m1 Z4 j# {* G
// schedule has a repeat interval of 1, it will loop every
- y! D. @1 W2 a8 m // time step. The action is executed at time 0 relative to) w* }& J, w+ |* O' S- x
// the beginning of the loop.* E. q8 c# x* H- @8 q. v
, V, |# d7 U0 R$ }/ i
// This is a simple schedule, with only one action that is
4 \" B n9 b+ W; N // just repeated every time. See jmousetrap for more
$ s8 M+ ^3 c+ V. ~ // complicated schedules.8 Z) P" Q$ ~; ]3 @0 j+ y/ E
3 R1 O* w6 e C2 @ modelSchedule = new ScheduleImpl (getZone (), 1);
$ n( u4 }. n+ z e modelSchedule.at$createAction (0, modelActions);
4 x+ b$ L7 L. F+ Z+ Y7 X ' V& D4 o+ k. v
return this;
# E5 v- X& z1 E( K1 K7 R4 j7 e } |