HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, p0 z5 k* E( Y& b
: T! f; B4 T3 d9 r8 b5 D2 l/ B) e public Object buildActions () {2 i9 w8 a( Q! B y( L0 b! w
super.buildActions();- I* D5 ~" w$ h+ v
& q5 t- G1 a3 X // Create the list of simulation actions. We put these in
5 s' k b% B6 M- d: i/ m // an action group, because we want these actions to be
* O1 p5 N- g B // executed in a specific order, but these steps should( c& F0 |' s2 ?+ i. \' _% z
// take no (simulated) time. The M(foo) means "The message1 `0 \( x& u! f; q2 d2 e# r
// called <foo>". You can send a message To a particular
( D C7 v: `: u1 f // object, or ForEach object in a collection.4 `9 e3 ]9 i: L: _' E% {
( ~" N/ R! P" m) n1 T
// Note we update the heatspace in two phases: first run2 B. O7 }0 K, V8 N- p
// diffusion, then run "updateWorld" to actually enact the
# P! T( c% O. [ // changes the heatbugs have made. The ordering here is- z' c6 i8 V3 O+ Q0 S
// significant!
! c5 n- L5 n$ P' H+ z8 R3 ^
- [0 q6 _6 L2 I$ G" w // Note also, that with the additional. e9 A4 [2 h: r! }" X5 `
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 c5 u" n+ t) ]) k7 D( h3 ]* L
// randomize the order in which the bugs actually run
4 i! c4 @( t8 T5 V3 H& T( M // their step rule. This has the effect of removing any6 z) t2 a3 N7 B, ]6 ^ C" _3 F
// systematic bias in the iteration throught the heatbug: } g. H* `* G: r) K9 W( O& a" E
// list from timestep to timestep
; i/ z& J" y; X0 f* ~
) P. R" B6 I' j // By default, all `createActionForEach' modelActions have ?, y" z. }* I+ V! a# U
// a default order of `Sequential', which means that the
5 r; E9 d& z7 K6 p3 Z // order of iteration through the `heatbugList' will be
: M: V, @' g8 P" ` // identical (assuming the list order is not changed' \8 r+ J% H O8 I1 f5 A( m: e
// indirectly by some other process).
, v( S3 ?& }# v* Q
6 x* C. ^& m2 e$ c9 v7 T modelActions = new ActionGroupImpl (getZone ());5 u" ]7 V P# U, g4 r1 P% M
, N2 ~* l g8 v) Z% r
try {
! m6 |5 X5 j8 R, i modelActions.createActionTo$message& Z% I: X" G+ A5 W1 _4 M
(heat, new Selector (heat.getClass (), "stepRule", false));
# h& N) V7 i2 @! {1 q" v } catch (Exception e) {
7 b+ f( } W- T- V; @ System.err.println ("Exception stepRule: " + e.getMessage ());
# r" @2 D$ V d5 M }) ]* P# }: w5 }. ~
4 M4 K7 d1 U5 y* m+ T4 q4 |
try {
$ N8 M3 N& ]# w8 b- H Heatbug proto = (Heatbug) heatbugList.get (0);2 E# Y8 Z* B$ l# v% B& ]
Selector sel =
+ T& F7 ?% f+ d* [ new Selector (proto.getClass (), "heatbugStep", false);
1 f: T) z) R0 Y0 B; E4 D, h actionForEach =
3 W7 Q% m: I6 Q- Q- a modelActions.createFActionForEachHomogeneous$call* N& R8 _2 a1 _# r5 y& Z
(heatbugList,0 l2 w" e& Y: I* e _
new FCallImpl (this, proto, sel,* F& b2 Y; b7 J7 z
new FArgumentsImpl (this, sel)));$ ?& N& f L, W4 B
} catch (Exception e) {$ r* n5 Z- C1 n
e.printStackTrace (System.err);
R* y+ J% ?/ E# O+ o8 x }
. Z; @' T! {! _! k. m% I% n 9 X1 b) s3 h: _
syncUpdateOrder ();/ B% `0 d: L! J9 W# m a; t: ~
4 [" U( y: X- _ try {
: s% |9 f. T5 o0 E' \ modelActions.createActionTo$message
% d' N# z5 k e: d3 |* T (heat, new Selector (heat.getClass (), "updateLattice", false));
$ [; E. t9 k+ l% _8 L5 { } catch (Exception e) {5 ~9 f* Y% v3 W' M6 p3 w7 P
System.err.println("Exception updateLattice: " + e.getMessage ());
& Z5 \( i- P' v9 j }
/ P6 K; _# |2 \4 T9 P0 q
% Y3 N+ Z( r/ y4 f. r // Then we create a schedule that executes the
, U" ~8 a* ]- n" Q5 \- x // modelActions. modelActions is an ActionGroup, by itself it
& O' q6 w+ G+ i }0 j3 b" L // has no notion of time. In order to have it executed in
' |) k; n6 E: F8 n* l% N" Q // time, we create a Schedule that says to use the
1 _/ C* q+ U+ f6 _) g! h* C9 z+ u" X // modelActions ActionGroup at particular times. This F/ a1 T- q- F6 ?6 O
// schedule has a repeat interval of 1, it will loop every6 ]+ A+ I& o' O' N
// time step. The action is executed at time 0 relative to
! d/ c1 M; E. Q: W // the beginning of the loop.! Y" U; L3 Q$ r5 k
9 a$ M! \/ m% P0 c: L1 S
// This is a simple schedule, with only one action that is
* Y9 t( z* K" |6 ^ E // just repeated every time. See jmousetrap for more' G9 ~- t5 S, H' d, {, l
// complicated schedules.
' S) r) t4 a1 m# a3 r
# U" v5 n1 i5 l0 m3 x X* x6 Y+ h! @ modelSchedule = new ScheduleImpl (getZone (), 1);
6 a* W) |" b6 m' P. d, | modelSchedule.at$createAction (0, modelActions);9 W$ O" D4 L- R* p$ {
! @( U' U* m" R3 I
return this;' K/ s. O/ x. ]4 L2 d( U0 J9 p
} |