HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! _' [ T0 b0 o4 O, ?0 k9 T
0 n* D9 r# ^% X3 N& t- ]) h
public Object buildActions () {
" e2 i+ K( p8 D, q super.buildActions();
4 b. v/ n( f) D0 i* E! r a ) Y9 p3 O2 o) P2 u% v8 W
// Create the list of simulation actions. We put these in* ~7 N3 |1 ~! g2 r5 z6 ~
// an action group, because we want these actions to be2 f! @2 K/ ^$ e0 ^$ b' k
// executed in a specific order, but these steps should
I: n; u0 } R! z7 m( m // take no (simulated) time. The M(foo) means "The message
6 ?2 r( g+ ?) z% `& Q1 w // called <foo>". You can send a message To a particular1 U6 y& q5 Z$ s- S1 V7 p
// object, or ForEach object in a collection.
9 _, O! m( W! B( S/ o: `5 M # S- U* O& R+ W' [( {1 @. v: _
// Note we update the heatspace in two phases: first run
3 O) O% o' ~! ~' e' e) s // diffusion, then run "updateWorld" to actually enact the" ^6 G) L: Y& N3 h( o9 l. }
// changes the heatbugs have made. The ordering here is+ U( e o' a7 r$ y1 _
// significant!
& j8 i. {$ n" [7 A7 I8 N0 p
2 u9 O0 E G7 R- R // Note also, that with the additional f7 I) I+ D# y& O
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& n9 M' p0 ?' g; W6 n: l // randomize the order in which the bugs actually run9 z, c1 l- J1 q
// their step rule. This has the effect of removing any
/ w4 Q5 B" i1 k/ I // systematic bias in the iteration throught the heatbug! G5 {4 d4 I1 R9 h3 w: s: a ]- h" N
// list from timestep to timestep- T; G- L* r7 s. q
K! k1 C3 ~- } // By default, all `createActionForEach' modelActions have
, x% J2 N. R, @! J- J! R! q // a default order of `Sequential', which means that the
|. v7 D& U9 ^' N- y // order of iteration through the `heatbugList' will be6 E" q4 B0 c |* A% }1 V
// identical (assuming the list order is not changed- [% R/ [% c& Q1 V
// indirectly by some other process).
* X( c# Q. V8 A8 S& u + d2 v4 [4 ~ o; w# M
modelActions = new ActionGroupImpl (getZone ());) n0 i4 Q2 T6 d( p' ?
8 j5 e8 N% ^+ R try {2 G; x% e5 x6 K5 v! A1 G
modelActions.createActionTo$message8 w1 p" x2 j# o$ h a+ }
(heat, new Selector (heat.getClass (), "stepRule", false));
! e9 Y% m5 P, x+ \$ | } catch (Exception e) {7 Y$ X7 A0 v# K
System.err.println ("Exception stepRule: " + e.getMessage ());
' a! m m6 ?+ p0 R" E }
, K W9 w5 h# [
4 k& E5 U% S) H5 a try {# [8 t1 o! e" V# r
Heatbug proto = (Heatbug) heatbugList.get (0);
9 w# b( k+ N9 r |, B Selector sel = " E" ~% l* {, N5 U- {7 x' s
new Selector (proto.getClass (), "heatbugStep", false);( }# I* K5 _* }+ K
actionForEach =2 h2 v* ^5 L" O: G3 ]
modelActions.createFActionForEachHomogeneous$call
; @, d0 A8 `" p" h u1 ^9 z (heatbugList,
. m* U5 f4 n( l8 C( v new FCallImpl (this, proto, sel,2 y8 n6 q/ t5 S& l2 A; Q7 E. Y) d% n
new FArgumentsImpl (this, sel)));
! ]+ U I, t6 t1 o } catch (Exception e) {( k9 n! L* B; @- Z# o4 T
e.printStackTrace (System.err); ~ ?, l& \) l: H; E8 S+ S# @( ]' ?
}0 m m2 d5 Y& Y: ~* Y' N" n3 O
5 K5 V' |3 l& r" T syncUpdateOrder ();
+ `. a* Q3 Q% J2 G* d" F% q5 X4 C
4 ~! A! w$ Q8 R- f' i+ ^ try {0 h0 R3 D9 X, K
modelActions.createActionTo$message - b$ v% {0 I) O+ w: [
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ w; V7 }# m( [! v% \ } catch (Exception e) {
7 g$ i6 _- H5 ?6 z& f1 y5 Z- }$ f System.err.println("Exception updateLattice: " + e.getMessage ());
) h8 U+ Q, `$ B6 ~: f }
* e" ^" k. ^" {8 o9 G
# {0 g4 E& ]3 s9 y( y; q // Then we create a schedule that executes the
/ {0 O X4 U& f2 i( d7 o5 A // modelActions. modelActions is an ActionGroup, by itself it
0 I0 R; x2 @" u9 {9 u9 L* x! K' e // has no notion of time. In order to have it executed in2 O0 H! M: Y7 q( U" [
// time, we create a Schedule that says to use the
# D* Y0 a. d7 p# P, \ // modelActions ActionGroup at particular times. This
) C( U) F) k7 H& U. G% n j, I // schedule has a repeat interval of 1, it will loop every
7 d+ x3 y- P4 E) V' U# M // time step. The action is executed at time 0 relative to
6 z$ K8 ~ m3 m. o // the beginning of the loop. y- Y, B# M" Y7 \
P2 _4 {* @/ S3 Z( h% G( w. o // This is a simple schedule, with only one action that is# I2 f4 f2 {9 f8 R0 N
// just repeated every time. See jmousetrap for more" I0 E7 P- b. k
// complicated schedules.2 a) r! h- i }3 Z& v# `: `9 Q
. ^; z ` ~; z# R. U8 e modelSchedule = new ScheduleImpl (getZone (), 1);
6 M/ \; o$ ~- I# |# X9 \; ?$ K modelSchedule.at$createAction (0, modelActions);/ e' M# E3 K L4 [
1 ~3 \, W- Q' @/ s, J return this;
/ E. t. m$ F0 h/ \- X5 s7 w } |