HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- M, b; z+ b' j8 s3 }6 ?" D" t
1 j' a K0 ?5 U# v3 V7 o
public Object buildActions () {
: r4 Z( u5 k+ d9 S super.buildActions();; q0 O, \, D$ ^6 z3 C9 G7 \& O
- R$ l& q7 h5 u1 d( M // Create the list of simulation actions. We put these in/ V3 }3 e4 Y. M. | {9 s
// an action group, because we want these actions to be
4 d# A, h9 A2 W- }7 W) A: e1 b# d // executed in a specific order, but these steps should
" r. _/ C- k" X: g // take no (simulated) time. The M(foo) means "The message
) V; ]& g6 E8 b& n4 ? // called <foo>". You can send a message To a particular( c* S2 h: N- h/ F
// object, or ForEach object in a collection.
! ^, D! z& N" Q# T6 C- G
S4 g3 F- S/ x // Note we update the heatspace in two phases: first run
2 T. S& F, A1 f- @& _ // diffusion, then run "updateWorld" to actually enact the
+ T9 g; U, s' Z$ W: U // changes the heatbugs have made. The ordering here is
0 E% ~, c+ V h0 v3 n // significant!
/ j* Q4 Z! k0 i% `# R
' k; i6 v4 _' h9 N- ^ // Note also, that with the additional1 n9 K( v# ?+ [. P5 H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 e, q1 u0 ]$ J6 n' o- X; P // randomize the order in which the bugs actually run0 g' G* ]% h% l
// their step rule. This has the effect of removing any
3 B# `$ w3 L) Q3 q+ Q0 q // systematic bias in the iteration throught the heatbug/ g# @4 m. U: w, h
// list from timestep to timestep$ p$ X6 ~) U2 r7 G
* ?4 G# ?, q: x, h% g& w* F // By default, all `createActionForEach' modelActions have3 {* e+ l' N# \! G# A
// a default order of `Sequential', which means that the
) P1 e+ W: t+ W! X // order of iteration through the `heatbugList' will be6 f4 S1 r5 o: o% Y3 E! F8 I9 ~
// identical (assuming the list order is not changed
) h/ V, Y, i! u: c4 } // indirectly by some other process).3 `# c L5 i ~7 }3 Z
+ J$ }+ z7 t0 @4 h$ a7 L modelActions = new ActionGroupImpl (getZone ());/ y+ J3 `! b, J% D
$ q" F; }) }2 R5 w4 ] try {8 f" v" M& G. [ W9 D q! _2 ]
modelActions.createActionTo$message
9 N3 _5 C8 _, M (heat, new Selector (heat.getClass (), "stepRule", false));3 H9 ?( }& I+ c& l! X
} catch (Exception e) {- |7 y. N; |8 y
System.err.println ("Exception stepRule: " + e.getMessage ());
* e9 s3 `, j) Y! ?, } }9 ^! W' b4 f+ {6 H
& ~: C6 T9 \ L6 Y* n& h" | try {# t, C, f1 i4 ]" P
Heatbug proto = (Heatbug) heatbugList.get (0);
& e( `. `3 X/ ^3 _" r% a( Y Selector sel = " Z) G" P+ a! ~4 H8 a
new Selector (proto.getClass (), "heatbugStep", false);
5 P1 B& a% F) ], t/ I6 J# {: c actionForEach =0 S2 T. O9 u4 h+ `' ~9 Z6 b
modelActions.createFActionForEachHomogeneous$call, N) w3 v, l1 u* }, c7 o/ m! M! r
(heatbugList,2 o+ T" M/ K! Y. v: a- m
new FCallImpl (this, proto, sel,
$ X! b* q* }6 g new FArgumentsImpl (this, sel)));
3 P# R% @) y: O& O! ] } catch (Exception e) {% a9 H- L; O; b( T/ Q+ X4 y5 T; d
e.printStackTrace (System.err);
# Z' M" m) Y4 a }, Z! ^' |4 v+ z* e9 Z+ E4 W
/ d7 Q% ^% f9 l& U' F+ [
syncUpdateOrder ();
( i, K6 s n0 R
( L. e: @% @# j3 _ o1 o% Z9 y try {
2 q8 h) ?. \; `% S1 ^* @/ ~ modelActions.createActionTo$message
: f- Z0 o6 ^3 Z0 W# x (heat, new Selector (heat.getClass (), "updateLattice", false));
" G. v, C% A" y* Y+ r( S0 K4 C/ W: T3 p" L } catch (Exception e) {
/ p1 r8 B& o1 m3 s( A& o0 b9 j System.err.println("Exception updateLattice: " + e.getMessage ());6 E2 B4 {2 L8 X$ o/ T7 J
}8 b& f! n; h1 H' l% r
6 [: P E( { H9 C, K- q, l5 V // Then we create a schedule that executes the
0 N2 F* ?- ^& w- Q# } // modelActions. modelActions is an ActionGroup, by itself it
% Y! o+ E+ ^) s( a0 @2 X1 Z" ]8 f // has no notion of time. In order to have it executed in, D% \! z9 a- H" w- K& b9 j+ \4 t+ |
// time, we create a Schedule that says to use the
0 ?: z, i4 M' B8 r! i+ A) V! l4 S8 f // modelActions ActionGroup at particular times. This# h: b0 e3 P2 s6 ^
// schedule has a repeat interval of 1, it will loop every6 a0 Y( R$ y4 `4 Y; I
// time step. The action is executed at time 0 relative to8 E( J$ Z( X/ R" @5 }3 \9 `
// the beginning of the loop.% `8 l% ?" _6 F3 K" M
; @+ [! i% C1 }6 b; b& i
// This is a simple schedule, with only one action that is/ x$ l' T8 A& e) C1 I
// just repeated every time. See jmousetrap for more
/ N! ?# i: `9 V+ d( C: M // complicated schedules.
1 c; h3 {- L1 {3 m+ K6 Q* E + A9 l; i9 G% X0 B+ {
modelSchedule = new ScheduleImpl (getZone (), 1);
2 ~5 Y7 a9 r$ l modelSchedule.at$createAction (0, modelActions);
' V/ D! k) H& m/ y 6 q# C! d$ R! r2 x) W7 Q' t
return this;. z" q- _, z( P, R
} |