HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' \& Y" o a3 F1 ] w- b0 W
7 l+ I- N: R6 g8 U$ A& t
public Object buildActions () {. H( S! t0 B$ x; y$ W
super.buildActions();
: \0 c# `# g# V$ |- M
3 K* `7 }/ b4 @1 E' x$ ~' J! ` // Create the list of simulation actions. We put these in. b, P* }. q0 p- ?0 r
// an action group, because we want these actions to be
- |6 ^4 A- @0 }, g+ G // executed in a specific order, but these steps should2 |- ?8 p, H8 ^. x7 [$ |4 o
// take no (simulated) time. The M(foo) means "The message
# Z! O! H# x) C // called <foo>". You can send a message To a particular
: l& F7 @ }8 `/ |& g; V2 \ // object, or ForEach object in a collection.
5 E1 `1 J, U q& C6 v$ M; q5 `0 W) a 8 a8 W% y& c4 W% d3 {1 O% y
// Note we update the heatspace in two phases: first run
+ m5 d) a( b6 @" O# O0 y# O- u // diffusion, then run "updateWorld" to actually enact the5 N3 f! C6 ]; C$ \" o
// changes the heatbugs have made. The ordering here is/ T$ n# l I+ y! j# e; H# k
// significant!
) p, Y) Y- Q- f2 w7 P
1 E p. o% h2 t8 L // Note also, that with the additional
/ Z/ b1 M7 Y* M/ y8 I2 u // `randomizeHeatbugUpdateOrder' Boolean flag we can1 [+ Q, t1 \$ [7 d7 r5 S: A
// randomize the order in which the bugs actually run
7 ]% T6 @( w0 M' `* X; j. B // their step rule. This has the effect of removing any
3 Q- H8 v9 j& C) d2 L1 V // systematic bias in the iteration throught the heatbug$ |( T5 G1 \$ P" D5 L4 a8 b
// list from timestep to timestep8 W' [* T0 ^( |! ~ o6 @. V
/ f! E6 a7 R4 J' N- u
// By default, all `createActionForEach' modelActions have s1 x2 Y0 d- k8 h8 F- z
// a default order of `Sequential', which means that the
: F# U5 A C. b7 t m# t // order of iteration through the `heatbugList' will be7 ~& J: k) F; i9 V2 i
// identical (assuming the list order is not changed1 j. H% Y( G* a. ` V1 X! k; g! e4 I
// indirectly by some other process).
0 S$ R) O) H# X3 i$ X
Y1 g0 `) e5 m+ I modelActions = new ActionGroupImpl (getZone ());
# \7 h; x# f( t% l& s3 X4 k" J1 f u, B! k+ O) t6 u/ ~2 \; m) s
try {
E$ F; w( X' E; Z7 u modelActions.createActionTo$message
3 d- H3 k, c% }* E (heat, new Selector (heat.getClass (), "stepRule", false));
! R1 h1 ^9 A( b J7 j j! y } catch (Exception e) {, v4 }' N/ V6 [% n! z+ N* O
System.err.println ("Exception stepRule: " + e.getMessage ());; u: d; W8 ^% [, J: w0 J0 e
}7 m+ L' Y3 Q6 \/ [
4 {9 }. @5 r& u$ F5 W try {$ ^: v2 f a; T4 L j7 u; `) @
Heatbug proto = (Heatbug) heatbugList.get (0);
- o3 |7 w& F/ o- K Selector sel = 2 l- i& z4 X9 q: ]# C0 ]
new Selector (proto.getClass (), "heatbugStep", false);. d9 X! W- C: L/ z# h" \
actionForEach =
{; T9 k6 J# t/ x2 A modelActions.createFActionForEachHomogeneous$call- _' L" v! C* i9 W! M
(heatbugList,
1 L& z, c1 I( w9 y new FCallImpl (this, proto, sel,2 n3 K0 |: @* o. t
new FArgumentsImpl (this, sel)));& @) n- U& V ^& y
} catch (Exception e) {
1 W, i7 d3 q9 s6 a x0 B0 { e.printStackTrace (System.err);
$ U! j( |, F1 |9 F }
6 A8 x F: C. q# N% q) X0 V
3 K) M4 z& @0 M0 _% a syncUpdateOrder ();- J$ {% E" W( H) |0 L' P1 |
% {0 _% ]) Z" x$ w: n try {7 J, `9 p; g- H& a7 B
modelActions.createActionTo$message " I4 a3 @# [' b! \
(heat, new Selector (heat.getClass (), "updateLattice", false));
( F. L. d7 x+ H% Q } catch (Exception e) {# D* Y4 g* W% Z* D# \# [% m' e
System.err.println("Exception updateLattice: " + e.getMessage ());
) ~- l# \& S3 l3 U( w6 S' Z }
) t* R& }* Z7 H3 Y8 R8 ?' [1 t9 A3 H 5 p! w5 W6 M) a. x
// Then we create a schedule that executes the0 n1 R+ h& Q6 f7 J) Y
// modelActions. modelActions is an ActionGroup, by itself it
1 A x7 O1 b5 w! d5 M // has no notion of time. In order to have it executed in; _4 E0 Q7 |+ H+ n
// time, we create a Schedule that says to use the$ H/ S. j$ ~3 ]: }- J) Y. w' q) y
// modelActions ActionGroup at particular times. This
( q- }* H( Z% A! N: l // schedule has a repeat interval of 1, it will loop every
- p5 ?2 D% j/ Q4 L& r( N" w7 A // time step. The action is executed at time 0 relative to7 g# N6 F0 J6 H# o: R& f
// the beginning of the loop.
" S0 o p0 B- m8 E' J
7 H3 |& I$ U6 V% G) ` // This is a simple schedule, with only one action that is
. F( c' C, v3 C4 N& U* \ // just repeated every time. See jmousetrap for more0 N6 j" z0 Y7 B% _/ O
// complicated schedules.
, K8 f" q9 T8 o" P! F/ V
3 G g" T# X' u6 N# X N modelSchedule = new ScheduleImpl (getZone (), 1);8 v6 p& J2 |7 ~* P, |
modelSchedule.at$createAction (0, modelActions);$ c) }$ u, [% V, h& \7 \
/ v: o; C" {4 P; ~3 U return this;
- @; J3 G9 X* r& Z/ D* I } |