HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 {- ~7 f6 v2 Z. {
, _& S. G) h7 ^: V# R public Object buildActions () {
9 Y# p" T1 {, q( s, t5 T super.buildActions();
0 w( V+ x4 A( }7 Q7 l% f" C4 Y0 L
& |% I- r: A9 Q. k" e // Create the list of simulation actions. We put these in i! C" L5 E3 w6 j8 b
// an action group, because we want these actions to be
) G; g9 M6 G" D // executed in a specific order, but these steps should
6 J8 I. _3 [2 @ G* e$ p // take no (simulated) time. The M(foo) means "The message4 z$ ?% ], S. a# {4 z g
// called <foo>". You can send a message To a particular+ D4 r8 k1 [' \5 C+ e
// object, or ForEach object in a collection., h4 G$ L# x7 \, Z
* E6 X% q1 r/ l! i0 ] // Note we update the heatspace in two phases: first run
7 H' \5 a, D; G- ?$ _3 ], E& @ // diffusion, then run "updateWorld" to actually enact the0 |" T) ~: O! H! D/ U5 s
// changes the heatbugs have made. The ordering here is
?8 x7 [8 M- | ~6 s8 F9 c* | // significant!* g- p5 |. W# g. Z
. @) H5 G2 Z) d // Note also, that with the additional
5 T" O% s7 z* Q1 x) J // `randomizeHeatbugUpdateOrder' Boolean flag we can) z' ~6 V+ h1 c8 b$ N$ t
// randomize the order in which the bugs actually run
$ N. l& ^/ p; ^- R+ U // their step rule. This has the effect of removing any
* M1 x! D u2 t; S3 G // systematic bias in the iteration throught the heatbug% F7 ~+ R: F& K( h' Z
// list from timestep to timestep
6 R+ Y! a( J }( F+ l& T- t : W n0 f7 A4 M# X V4 o' `1 A
// By default, all `createActionForEach' modelActions have
! V1 F8 S+ M1 _5 n // a default order of `Sequential', which means that the
- M) t, a+ D! z3 ^2 k% N7 G // order of iteration through the `heatbugList' will be! D1 A. l0 ?/ P* a' x5 b$ F) U
// identical (assuming the list order is not changed( k' [$ i2 D. h& A; E$ e
// indirectly by some other process).
! I. O9 q. U! Y! n, c( d, X 8 Q/ J9 p) ^0 g" b, I0 k. E. R
modelActions = new ActionGroupImpl (getZone ());
S( ~% X5 d* V- s! E; v7 c$ G
: s y/ \: ]8 U7 j# _6 S T try {7 L( T: \0 ]/ W5 A6 o: {- {
modelActions.createActionTo$message
: z- w( X" r: ]- \ (heat, new Selector (heat.getClass (), "stepRule", false));
, O3 Z; B2 U3 r, @; y } catch (Exception e) {
7 M0 |5 j: f( b# c3 P System.err.println ("Exception stepRule: " + e.getMessage ());
6 D1 T9 u* D& P6 {9 q1 J" G | }6 i$ s) y% a* m7 c& k6 ]
}, k: Q( k& L# N/ N4 Q+ V
try {
& y' D6 P0 V% R: h" `+ I. Z Heatbug proto = (Heatbug) heatbugList.get (0);8 u. N6 V) g6 ~
Selector sel = 2 M: A/ U& n }5 N* l3 L* u/ Z6 N
new Selector (proto.getClass (), "heatbugStep", false);9 @! y& M$ n9 S9 W A/ l9 t
actionForEach =" f" X$ J' Y ~- Z; `
modelActions.createFActionForEachHomogeneous$call1 }; @/ e$ V" ]0 ?4 l1 R0 }
(heatbugList,
, j5 i5 O: c i# e% J( X/ d5 E new FCallImpl (this, proto, sel,/ V7 C. G8 Q5 t* j. i% x
new FArgumentsImpl (this, sel)));
+ b6 H+ D4 _* i3 X! _, S } catch (Exception e) {
9 @& I( r% V2 A e.printStackTrace (System.err);1 D/ F5 j% z4 d' }+ ^& s
}4 q& V5 [& H8 R. ~6 U& ~- y7 T" P
8 I9 s/ t0 v& a* b" i( ~ syncUpdateOrder ();
8 M3 o3 G. H. l3 D' X2 B0 ]* \/ Z. `
; [+ R2 E8 w# V3 g! k, v0 A& K1 y try {
/ P" N! }( Y4 s B8 w modelActions.createActionTo$message , R$ L# v9 c; X8 a) g
(heat, new Selector (heat.getClass (), "updateLattice", false));
' R6 @% |0 j0 B& l! G& U2 W- N8 c } catch (Exception e) {
" Z/ I, x, E7 S# h* d4 | System.err.println("Exception updateLattice: " + e.getMessage ());
. E/ e9 ]8 j4 C! _: S4 S3 a4 S }
" N2 S2 _, S2 x6 _8 p& } 7 `- V( s( n, ~) [
// Then we create a schedule that executes the: F3 {) }% q) L1 v3 F
// modelActions. modelActions is an ActionGroup, by itself it
3 }! r$ N8 b. G3 Z/ C // has no notion of time. In order to have it executed in
5 b( X ~3 E! P0 X. P // time, we create a Schedule that says to use the9 [2 l5 r- q. ?3 F( o
// modelActions ActionGroup at particular times. This
- _' |! Z% t0 _, n& u E& D1 p // schedule has a repeat interval of 1, it will loop every/ J H) y: w0 D+ t) u5 t
// time step. The action is executed at time 0 relative to
5 l: L+ g& H) q8 z4 {: v6 I' z // the beginning of the loop.5 p+ P2 U) t' G0 |
* b3 D; _* k$ } // This is a simple schedule, with only one action that is
]1 Q f. w2 c8 u( ` // just repeated every time. See jmousetrap for more
^' r" [0 y' E- c# R // complicated schedules.
5 S1 R: i6 a' z
9 k: n; j+ ~- t2 d+ v: @% w modelSchedule = new ScheduleImpl (getZone (), 1);! D* r$ W1 o9 F5 j
modelSchedule.at$createAction (0, modelActions); E9 |$ K! J [: Y
$ q. G# H( Q, q- P* q1 h; L. R5 V K return this;/ S3 ^3 e6 s' W: p5 z" y9 X/ `
} |