HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: Q/ H1 l' U2 R2 u0 b3 S$ n6 ]! y' K ^: \; D, H$ S
public Object buildActions () {
. s2 A& P/ n7 t; Z9 M" z super.buildActions();
! `( C6 D7 b, U0 o) N! ^ K; X 4 w! w1 j2 S7 H2 N6 p: `
// Create the list of simulation actions. We put these in" D, c- P' S, q% C( N. Z
// an action group, because we want these actions to be
6 O2 f* U$ R) C8 N# ~' w8 m // executed in a specific order, but these steps should
% H6 [8 k% N( e3 b) l // take no (simulated) time. The M(foo) means "The message
* L! u6 y- _" t // called <foo>". You can send a message To a particular
6 e7 p4 h3 x, w: k, {" G6 A // object, or ForEach object in a collection.
$ U' \( g. W; G5 b1 g 5 ]7 v/ Q( k7 X2 Y
// Note we update the heatspace in two phases: first run2 e: O5 ^/ V6 k
// diffusion, then run "updateWorld" to actually enact the" E4 s1 W9 i8 m9 L0 o3 ]
// changes the heatbugs have made. The ordering here is
/ Q J: H0 ^" p$ g/ | // significant!1 \1 x" `3 w9 D
3 k5 e: p3 N$ f
// Note also, that with the additional4 t( ?0 ^" Z v: I
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 ~( F: s; B3 H/ C5 x8 m8 e
// randomize the order in which the bugs actually run
' d3 X/ |: u7 I o // their step rule. This has the effect of removing any
6 f/ Y6 X8 h- \6 z7 ^ // systematic bias in the iteration throught the heatbug: p7 R5 v- F' `6 K+ B; h& }
// list from timestep to timestep
- ~2 W# V7 {9 _+ m* Q# p
" X" h/ F3 r9 \& J // By default, all `createActionForEach' modelActions have: c a D' j! l6 h; X
// a default order of `Sequential', which means that the8 ?7 e8 n- ^5 n1 F. z3 [
// order of iteration through the `heatbugList' will be; f0 ]8 {( p$ W ~. K2 r; k; {
// identical (assuming the list order is not changed6 G8 c9 ^- i5 z, l, o/ ~2 w
// indirectly by some other process).2 S3 n' m9 y, U) x o
& o' O1 [& o( ]: p8 L modelActions = new ActionGroupImpl (getZone ());' N; s( g8 C/ _* y9 t) V7 V
1 X+ K9 n6 b4 O' x* k' T M& d try {/ U% a: e; o- l
modelActions.createActionTo$message0 V4 y1 e! x* m7 e( @( h$ {7 U* `% `
(heat, new Selector (heat.getClass (), "stepRule", false));6 \" e/ N( o* o$ E: t& l. }
} catch (Exception e) {
) s% V2 ]5 Y% W* Z8 m System.err.println ("Exception stepRule: " + e.getMessage ());5 `5 O# ^: l: t! k" x0 _
}" M4 | v' O ^; V ~! F! l+ `
9 ~0 j- s1 ?) L- f/ n, j try {
@! } Z- n! W2 ]! e4 O% Z Heatbug proto = (Heatbug) heatbugList.get (0);! `0 ~9 G+ c6 ~1 [9 J, H5 a
Selector sel = ) u6 X! `& z% Y6 z
new Selector (proto.getClass (), "heatbugStep", false);2 x! R9 K) k2 z0 j, M
actionForEach =) s% R# N( O$ p% o7 A( i* d
modelActions.createFActionForEachHomogeneous$call
: Z0 P3 |: A2 f3 D3 R ^# E+ d (heatbugList,
" K, N- s: {* w) }6 K. Q# c new FCallImpl (this, proto, sel," G! R7 j- e2 i+ L
new FArgumentsImpl (this, sel)));
5 x5 O1 C; D4 J1 P# Z5 M } catch (Exception e) {
' \1 T/ ^7 J- F: | e.printStackTrace (System.err);8 R: F# T5 Z9 j# Z) ~. \% v
}9 T0 g. T T! Z) r! z F
. c4 s" }9 ]0 E- `7 N- h
syncUpdateOrder ();
5 ?+ q) L* V+ k5 o( Q# C/ o0 d" {: t7 l: M: L& X) T- j5 s+ x
try {
, E: o' `: B% U0 L% w3 M. ` modelActions.createActionTo$message ' A0 Z' _2 i1 [' a
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 x( d4 L- }' U% r& @4 ^ } catch (Exception e) {1 A3 ]7 g$ n# Y/ N
System.err.println("Exception updateLattice: " + e.getMessage ());8 u% q* q# C% m% L* D
} e1 b! V0 a3 D
! \" x7 e7 Y* t% q4 w& K i; G2 ?5 H* p
// Then we create a schedule that executes the6 n: b- C' \4 n: f" t% j; D
// modelActions. modelActions is an ActionGroup, by itself it" @; @ M% T$ z2 f
// has no notion of time. In order to have it executed in$ ^1 D$ x; R1 ^) K1 T4 k
// time, we create a Schedule that says to use the f3 d1 E& P0 {# h. G
// modelActions ActionGroup at particular times. This& w% v* B7 o4 ]* R# G R# g: Z2 {
// schedule has a repeat interval of 1, it will loop every
+ r2 m" ]2 y/ l, q // time step. The action is executed at time 0 relative to( J5 s Q7 z: S8 l
// the beginning of the loop.
, s# Y6 o9 X- H% U( l, r# a% {6 D; S) x7 p- M/ K
// This is a simple schedule, with only one action that is; s3 @) ?% l0 m2 o1 f
// just repeated every time. See jmousetrap for more
# I6 X4 W% @0 s& }- J3 x // complicated schedules.
. n9 P. p0 C) w( m1 F# H5 g( ~ : _( s6 q4 g. ]( n2 N* b
modelSchedule = new ScheduleImpl (getZone (), 1);
( p6 V5 p/ U) g9 T" Q0 W modelSchedule.at$createAction (0, modelActions);
: m% S2 z, Y9 S 2 \3 C/ M& s% f! R
return this;
; P" }/ [9 {! V r1 W } |