HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# L6 V$ l3 o5 O$ z1 c" n+ q( o" c9 R2 ?3 ^
public Object buildActions () {6 X6 x. d; Q1 |+ Q3 o( {: \
super.buildActions();
" s% y. X) }( r& x1 @* }; | 2 A9 F" @3 k; F9 D3 b+ w
// Create the list of simulation actions. We put these in
& ]- q5 {% Z( Y- y' j3 K // an action group, because we want these actions to be5 C! n* l/ Z: f- f3 a8 ?; W
// executed in a specific order, but these steps should
$ {. a+ v @6 k, G5 G. @ // take no (simulated) time. The M(foo) means "The message" m0 @1 ~* M& A4 A9 B' @3 ~2 @3 O* j
// called <foo>". You can send a message To a particular9 A% J2 B8 v; n" c! m0 o9 N1 O4 a' P6 y
// object, or ForEach object in a collection.2 ?8 a+ T% {; K4 T' I
) F5 M; ?9 }% h, c4 C; J
// Note we update the heatspace in two phases: first run
2 ^ E4 S- M3 W# ^1 n // diffusion, then run "updateWorld" to actually enact the7 s( a4 U' T' F$ F3 j# D. y
// changes the heatbugs have made. The ordering here is1 h% N: M. V6 i# c3 d" a
// significant!
M: M% {3 y I7 h$ z5 C
4 f# g* y& \* ]' q5 J q // Note also, that with the additional- y8 L5 S3 s5 M( Z2 i, K: G
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 z6 q4 P9 h: C8 U {% D ^" s // randomize the order in which the bugs actually run
; b" I# X7 A) b4 S | // their step rule. This has the effect of removing any
; M5 Y$ a/ z3 g0 a' P* f // systematic bias in the iteration throught the heatbug) n- U- @' I$ S+ Q! p2 t9 x
// list from timestep to timestep
$ B4 P& \- f6 l2 F7 k0 x6 I + y$ a$ s( ]+ b/ M# k O4 A
// By default, all `createActionForEach' modelActions have: @8 W3 r6 \; I; F1 W
// a default order of `Sequential', which means that the
+ M9 \6 B+ ?& P- o R8 g // order of iteration through the `heatbugList' will be
2 a/ x1 x" a/ C // identical (assuming the list order is not changed& w5 @! W3 D4 @$ P1 G
// indirectly by some other process).
* S$ o3 F3 |/ s. N7 D
# M4 [6 ^8 O' D modelActions = new ActionGroupImpl (getZone ());
" ^ J0 `1 W' @. r7 {: S! P+ z# S0 u4 k8 J# D/ k
try {5 V) k6 ]& n0 ?2 z. e
modelActions.createActionTo$message
* y1 c$ `& X& u* z9 C (heat, new Selector (heat.getClass (), "stepRule", false));
( Q1 D4 C2 D$ d } catch (Exception e) {
' S" {/ a5 c: V3 B2 P System.err.println ("Exception stepRule: " + e.getMessage ());
! F" l. Z. w# l% J* J+ v }5 o0 I d1 w- @
! N1 Z6 j( E8 a+ u1 N y% [) ?
try {+ N ~( A8 h3 W. L0 P" C
Heatbug proto = (Heatbug) heatbugList.get (0);+ J- A1 i' D& j4 u8 h) L
Selector sel = 4 P4 Q. G4 g" e$ v6 r; K5 b* U
new Selector (proto.getClass (), "heatbugStep", false);5 \+ l; D7 w" Y0 K& u5 C) r
actionForEach =# Q _) t, n# F. b h
modelActions.createFActionForEachHomogeneous$call( o/ J/ T7 S$ X1 ]2 `* P. h3 T
(heatbugList,/ L) ]; \( r& w* P. B- T
new FCallImpl (this, proto, sel,
- ]9 e' F& P( u4 [' u; X new FArgumentsImpl (this, sel)));2 i, }8 V# o7 w& g" c
} catch (Exception e) {
6 u+ [8 F& _# Q9 v+ B' D e.printStackTrace (System.err);
# L; j9 t+ j$ {& t6 k4 Z }, W3 ?3 Q0 }, V6 L9 n
$ V# [# r/ ?4 T- b2 p syncUpdateOrder ();5 h t/ |; q+ o7 r* G6 G# t
# C: k# `+ p; L( J
try {' l5 `! {4 U& ^& [) ]
modelActions.createActionTo$message 3 m2 O& H' Q1 r
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 d* ~2 y, q" ~# { } catch (Exception e) {2 G1 M9 R% t3 P) q
System.err.println("Exception updateLattice: " + e.getMessage ());& _5 \1 S( x/ D; h
}7 B% U- c9 ]1 E: n, U( d
* `4 V! T7 r1 V! v
// Then we create a schedule that executes the- @- B# O0 \9 W, h+ ]( E+ k
// modelActions. modelActions is an ActionGroup, by itself it* P8 Z- ]+ k1 \9 U) S2 y2 `1 Q$ T0 X7 \
// has no notion of time. In order to have it executed in9 q) n% H. T7 o+ K( X0 M
// time, we create a Schedule that says to use the
: c9 g* y4 z1 f1 v& S // modelActions ActionGroup at particular times. This
4 H0 ?4 `; f6 a( m7 D- f // schedule has a repeat interval of 1, it will loop every
# W, m0 H! v( ~$ O) y // time step. The action is executed at time 0 relative to1 i2 } y- L% r# a2 N- |
// the beginning of the loop.
- @7 B# v/ I( k* w# n2 |' L0 i2 B
// This is a simple schedule, with only one action that is' B+ L; R8 _: E, P) K
// just repeated every time. See jmousetrap for more: c7 T( ~0 \. r* E! j- O5 V
// complicated schedules.
* a% j! o) T7 D3 L
6 |) O" J$ Y {. F1 q modelSchedule = new ScheduleImpl (getZone (), 1);
! |0 Y# ] s2 ?9 U! r modelSchedule.at$createAction (0, modelActions);6 m3 a7 {3 g4 A
( C1 \; |1 O1 Q! m return this;
; }; W$ g+ N2 D9 K0 c, K8 c } |