HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. o+ @6 w6 n5 C, L2 i8 s" x9 P7 V) G. u5 l& h/ t
public Object buildActions () {
i. v- c) x V super.buildActions();
* p: m7 l: e+ \; d
, p1 v. R* x3 j9 @# c // Create the list of simulation actions. We put these in/ R+ h0 f3 f! a, b! H
// an action group, because we want these actions to be9 v( {+ l0 |% h$ ^! V
// executed in a specific order, but these steps should
+ C# E; b3 x8 E" h l0 i // take no (simulated) time. The M(foo) means "The message
( H. n6 J; q! K6 U& p" a2 n // called <foo>". You can send a message To a particular
) |/ t9 |: T+ F6 K# H6 G I // object, or ForEach object in a collection.* z, Q3 D6 P3 k+ ]8 B: i& }
% S" u* P" v7 A' V5 `5 ^
// Note we update the heatspace in two phases: first run$ h0 ?7 R) [* {+ u: h5 z- u5 v
// diffusion, then run "updateWorld" to actually enact the7 z( k# f" Q( I+ o1 U
// changes the heatbugs have made. The ordering here is- f1 K! f; ~, v V0 n$ q! m' N
// significant!; B+ Y1 O) } ~
! b7 h$ e9 a t8 b3 }" `! K( x
// Note also, that with the additional
/ M$ K7 v* Z% T. \2 v/ j4 f // `randomizeHeatbugUpdateOrder' Boolean flag we can. A7 Y4 `, d1 V4 z2 W4 q# C( F
// randomize the order in which the bugs actually run
$ \5 ]- z4 F5 Q1 A3 i' E9 P5 y$ t& z // their step rule. This has the effect of removing any3 f( w: v1 u2 I: ~0 S- O
// systematic bias in the iteration throught the heatbug5 n& X# V3 b9 h' u9 j6 L4 {2 ~
// list from timestep to timestep' l& `) t, M5 l# |
( P6 \2 y2 m7 @+ S3 `# I! t Q
// By default, all `createActionForEach' modelActions have. u" e* t; l! q5 ~" N7 l
// a default order of `Sequential', which means that the1 M* e- M% ^% e; Q
// order of iteration through the `heatbugList' will be
" L( t6 k1 ^7 t5 n% y; [+ _ // identical (assuming the list order is not changed
8 V& ^. O. l, U1 D4 @% p$ f // indirectly by some other process).9 D a1 `, G! p% X1 n' q3 f6 S
/ ?0 {3 z' T( i5 G modelActions = new ActionGroupImpl (getZone ());4 O! n/ P6 E" J, z
C" l( d2 m5 ~3 N' q2 v0 m! z( m& O: _
try {
$ I+ g' N) d" b! \! F modelActions.createActionTo$message. H' J% X5 k- a4 C
(heat, new Selector (heat.getClass (), "stepRule", false));
7 U; C+ \* B) z: _/ n- W0 L } catch (Exception e) {, s4 B* w4 i4 f; q+ J' [
System.err.println ("Exception stepRule: " + e.getMessage ());
" f, e- b! b+ k/ s }
. U7 s* A- x! } w
1 U3 k! z0 N& h( S- b: v. Q, [2 k, T try {
; W* _, B1 f0 c# [4 X; z7 Y Heatbug proto = (Heatbug) heatbugList.get (0);
]) E3 t1 F' Z( m5 ~3 H( V Selector sel = 6 E8 j% _8 t; U+ k2 }- @
new Selector (proto.getClass (), "heatbugStep", false);
2 s; n' ^) r, K2 S actionForEach =
+ Q, }) X M1 n4 @9 L0 I7 e( v modelActions.createFActionForEachHomogeneous$call i, W- D$ h& \/ Y2 m
(heatbugList,
: R) Z. {% u) ]4 m) B5 o+ \ new FCallImpl (this, proto, sel,0 ?3 i9 e" c6 O# o) F* F7 t
new FArgumentsImpl (this, sel)));
6 { M9 N" d( P9 g \+ [2 t2 p } catch (Exception e) {* K- `% v8 \ D$ @( m
e.printStackTrace (System.err);7 q1 a( l. o. U4 j% t1 s3 G% v
}
5 I# u6 t9 P) c0 w0 b c' \ / F3 ^( ?5 Y* `3 r
syncUpdateOrder ();- x' U. I3 ]) e1 w: P! |
, D1 ]$ z T* u
try {) i4 K% h, Q. X0 o& b$ v$ e
modelActions.createActionTo$message
9 d# A. G8 {; M9 t$ l& x (heat, new Selector (heat.getClass (), "updateLattice", false));4 z' z! ?( o# U- J- h
} catch (Exception e) {
) O3 b0 a* @6 s b6 x* M System.err.println("Exception updateLattice: " + e.getMessage ());
9 k6 @7 ]7 i( s2 |# a }
4 F" k* ]" `5 c$ W3 m# u+ A; ]. ? 8 T6 Q: v$ L; d
// Then we create a schedule that executes the
. m1 i, F' _# h8 d* ^ // modelActions. modelActions is an ActionGroup, by itself it8 W% E/ @ L4 M- q
// has no notion of time. In order to have it executed in
{. W' b0 b' M5 P- w& g5 w/ [ // time, we create a Schedule that says to use the
+ [9 m! J9 l5 I // modelActions ActionGroup at particular times. This
- [* E8 F) Y; e6 S // schedule has a repeat interval of 1, it will loop every" C" d- P( z- w% E2 a- v4 X/ o
// time step. The action is executed at time 0 relative to' A8 ^1 R b4 [) ^4 |' y0 ]
// the beginning of the loop.
\5 ?7 N5 t5 L: Y: f/ ^- a
' r4 M1 n, l5 f9 D // This is a simple schedule, with only one action that is, i# j9 F+ b- G$ K: U! ]+ `2 Z
// just repeated every time. See jmousetrap for more
" P/ F4 }& a5 I! u2 A+ y- |( E9 d // complicated schedules.
+ Q* P! @5 }7 W ! w- k& Q$ M6 T9 Y
modelSchedule = new ScheduleImpl (getZone (), 1);$ R9 e( V9 a6 R, {2 O, D# w+ f
modelSchedule.at$createAction (0, modelActions);
& O V5 ~) ~2 H! p7 r3 u
3 l% d- M) t2 ~* }, N; o return this;. ]0 x9 ^8 d/ T2 a( O
} |