HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 E2 \0 e1 R- l0 x0 [
2 l2 B$ a4 u6 w+ d9 U- b9 d
public Object buildActions () {' F/ Z2 I" V, O6 ]/ D& p7 s$ o; E
super.buildActions();9 f% y; Z' T4 W% X6 e2 R- ^' ]3 U+ v
; y* u+ D8 I# _: F X // Create the list of simulation actions. We put these in
. l3 h" L. \. @ // an action group, because we want these actions to be5 g/ a! n: g: E4 Q T# X D" z
// executed in a specific order, but these steps should2 z( A N4 w" t9 [+ K! |
// take no (simulated) time. The M(foo) means "The message
( X; a/ Q3 q8 d, R& l+ _ // called <foo>". You can send a message To a particular
0 z( O5 J0 E; p" {3 F1 T // object, or ForEach object in a collection.
" q0 ~! L L" C: e C- a
- F# o# q! ]8 G- V // Note we update the heatspace in two phases: first run
6 @6 Q- j1 F& v5 U5 a+ r( S // diffusion, then run "updateWorld" to actually enact the+ Q Q: {$ [$ G4 D( q w% ^, ~3 c
// changes the heatbugs have made. The ordering here is
0 n/ N7 t; k9 S+ \, U9 m! z$ Y6 M // significant!
. \7 N' h3 h( B5 H0 X- J 1 N5 j" q. }" g7 S0 v
// Note also, that with the additional
) B+ p8 k3 F% s // `randomizeHeatbugUpdateOrder' Boolean flag we can( n6 R) T# x; ~5 _
// randomize the order in which the bugs actually run
' ~& f: b) F$ F // their step rule. This has the effect of removing any
$ z# b7 b& N5 f. r0 Z$ f // systematic bias in the iteration throught the heatbug
* \# p- y- }: n0 J) {/ a& p // list from timestep to timestep/ |, y* W4 q( o/ c
( G6 z7 {1 |4 u4 ]8 X$ J8 b [" E) A // By default, all `createActionForEach' modelActions have
' _* |, p; q9 @7 \ // a default order of `Sequential', which means that the! z; `) k$ I4 M9 G+ p
// order of iteration through the `heatbugList' will be
: Q5 [! E3 W- O& E* a // identical (assuming the list order is not changed
$ k1 O: D) c9 D0 `0 w. \ N // indirectly by some other process).) B6 o8 Q0 p: I, y+ V4 R- [
5 _9 \$ ^2 z. d modelActions = new ActionGroupImpl (getZone ());
- s# x5 B- y/ |1 t2 c6 t( L [; Q
9 J4 e4 V4 y9 B9 y' t8 I try {. S, j( T; [, G$ W& R; ~
modelActions.createActionTo$message
7 K, C( ~ s! j$ x y* C (heat, new Selector (heat.getClass (), "stepRule", false));
$ z; D/ [) |2 A7 x$ l1 s. A: r } catch (Exception e) {! Q5 M o5 h5 t- }! ?& m
System.err.println ("Exception stepRule: " + e.getMessage ());
/ z& J, t1 q. r" Y5 e- Y }
& \' f+ G3 N8 @" [1 T- S0 w/ L) I8 }% D6 p
try {4 \$ B: X: ^1 |! R Z* u& J. y
Heatbug proto = (Heatbug) heatbugList.get (0);9 U! M b; {" Q( c6 v9 ?& i( K
Selector sel =
S% x6 d/ m9 j7 g! b; s2 j( O( c new Selector (proto.getClass (), "heatbugStep", false);! {0 b: w7 N3 L. g, C2 F! D
actionForEach =
5 _, o# ] g# A2 r9 n( H! _ modelActions.createFActionForEachHomogeneous$call
B$ R; ]5 n/ C8 v0 h6 I8 u (heatbugList,
' I# ~* x; L; P, D5 [1 i6 y new FCallImpl (this, proto, sel,9 W# z% P+ i; A0 H; u9 t0 D; L8 H
new FArgumentsImpl (this, sel)));
/ P2 r1 k1 }: k7 F } catch (Exception e) {
% @- u" R: p6 F& J- p e.printStackTrace (System.err);6 ?2 A. n6 {) m' ~6 V
}! P" R8 Q5 U" `4 x' r. c7 O+ h
; e* Q: ?2 A! X4 Q* i4 ?
syncUpdateOrder ();0 d0 y2 w6 T! `# |8 l0 q
( P$ ^# `6 }2 s1 Z) }( Z. b try {
1 l5 x2 ?: g( [3 Q, e modelActions.createActionTo$message $ [5 G& M& [) N- V7 d9 s) @( R( N& g
(heat, new Selector (heat.getClass (), "updateLattice", false));+ H- k" w- r* Y" _0 q, I
} catch (Exception e) {( x7 r. _2 E* ]$ b* N# G+ E
System.err.println("Exception updateLattice: " + e.getMessage ());! X$ F- I0 X. h3 D0 z
}
/ q3 h0 b6 O) z- I0 o- X& q
7 ^% ~' y* i/ a [ // Then we create a schedule that executes the
* h a$ W9 [% y8 f7 S, N0 c3 e$ x // modelActions. modelActions is an ActionGroup, by itself it
$ y" @# r: z h7 d# { // has no notion of time. In order to have it executed in! w! F' J; p' B$ L% p4 g/ Q' z
// time, we create a Schedule that says to use the
! w, e$ G1 t- U1 O, A // modelActions ActionGroup at particular times. This
* q) i L7 N, Q" N1 ^" H( N // schedule has a repeat interval of 1, it will loop every
1 Q% }; i5 g" K0 z a // time step. The action is executed at time 0 relative to& E$ {! B; O' {5 T5 x! m8 g
// the beginning of the loop.$ U6 L2 i. d( X3 n* s5 s7 o' G
4 T e# S" }3 v5 s' C/ i: t; q
// This is a simple schedule, with only one action that is
5 R U4 U& N" o; T2 i' _ // just repeated every time. See jmousetrap for more3 Q& i w6 w: Y8 Y- ?. @$ |. G7 q
// complicated schedules.: |* t8 O! j' a/ @! ~ j
/ _ {- A3 x0 _* r4 c" Y/ g modelSchedule = new ScheduleImpl (getZone (), 1);8 o$ h4 s4 Z6 n- v
modelSchedule.at$createAction (0, modelActions);4 _% K+ d* \) ~
0 S1 h; B0 _0 v5 L) h/ U
return this;& ]' L7 C8 F- [! l( M& D% f: u8 l3 `
} |