HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 V& A* |# m* g; q
7 z+ {6 X! B; L# u/ ^4 k$ w
public Object buildActions () {
& V7 a% n N. D! Y5 W2 ?: N super.buildActions();3 L* K4 r/ r4 Q
0 ?/ C, F) W" `( [: T
// Create the list of simulation actions. We put these in
. k# {1 L; u+ z+ o6 K; z6 z // an action group, because we want these actions to be* z0 W3 F4 x- F0 h6 c0 P3 H+ d5 g
// executed in a specific order, but these steps should
/ {2 C7 I) H7 H- ]- _ // take no (simulated) time. The M(foo) means "The message" L% J# {3 m8 p, l9 y7 |# D
// called <foo>". You can send a message To a particular
$ R$ k9 |" }% } R' O // object, or ForEach object in a collection.( W/ Z [5 D% W5 Z/ W3 k3 m% o$ x4 N
6 ~# g& `( C1 @; s$ A0 E0 D // Note we update the heatspace in two phases: first run
# I, g; e* _! K) @$ R2 Z // diffusion, then run "updateWorld" to actually enact the* h" C5 e }/ Q% V8 N
// changes the heatbugs have made. The ordering here is
# o& d4 k2 s2 _$ X3 q2 p // significant!
; X/ `5 R: S4 V( R$ W
; c$ `5 U& k0 h% k$ n8 @ // Note also, that with the additional
* c* `. {) r/ k+ N // `randomizeHeatbugUpdateOrder' Boolean flag we can
' c1 o* D2 f8 t2 D" ] // randomize the order in which the bugs actually run! R/ r1 M5 z' x: ]9 ~7 V# G( }- A& k
// their step rule. This has the effect of removing any
; P" t* g" E# ~! p$ j // systematic bias in the iteration throught the heatbug) J* O% e {9 A$ d" e1 K
// list from timestep to timestep
9 ^ e0 w% z$ @: M$ a" V; g- s7 ` 9 d, A1 [# o" p, E5 s3 [
// By default, all `createActionForEach' modelActions have
( D. r, f# y4 B$ n // a default order of `Sequential', which means that the
9 y+ m) ?0 ?) g& j+ \4 J7 n2 W8 Y // order of iteration through the `heatbugList' will be. E0 w3 g! ?% Q- ~/ a0 a
// identical (assuming the list order is not changed
, m+ C2 b' z% E% \6 H0 s% Z // indirectly by some other process).
/ g* l+ M5 Z4 U" m/ w3 ]
4 h5 a3 P+ Y. a9 F0 ~% s modelActions = new ActionGroupImpl (getZone ());' @2 `6 F% z# K
# T" R. F' M: a: y
try {+ M/ p( t. {, |% ?
modelActions.createActionTo$message% E& @, X) V9 G# S, y
(heat, new Selector (heat.getClass (), "stepRule", false));" O* n6 l. P! o, m& l) `7 w
} catch (Exception e) {
( Q- l* s4 [" a7 I, y1 G System.err.println ("Exception stepRule: " + e.getMessage ());
$ r* ]: k0 |" B2 W3 u8 Z }1 R, F! e* P" r" H/ R- P7 F
$ a/ G0 \$ W& \. Y" Z try {
) w4 c- Y |' C7 R: k7 o Heatbug proto = (Heatbug) heatbugList.get (0);
9 V7 ?1 U& R0 \4 L Selector sel =
h- ?/ K- ]$ ]* b8 n" d new Selector (proto.getClass (), "heatbugStep", false);8 z! p# v9 q1 s' ~
actionForEach =
$ [5 F- J; e0 F7 q modelActions.createFActionForEachHomogeneous$call
0 Q! n. p2 c* c! o4 S$ v( ?6 Y (heatbugList,
+ b! i# s+ l) ]% x new FCallImpl (this, proto, sel,
4 j7 ?* y; A; j9 c+ u9 g new FArgumentsImpl (this, sel)));% R! P+ n' t5 R) q8 q
} catch (Exception e) {- O% q' |/ q( M a0 d5 g3 \
e.printStackTrace (System.err);
- F( }+ ~4 }2 ^2 t# Q) q }
- |* V" K4 |2 @) Q! O' {: j , O: F% n& N O# ]
syncUpdateOrder ();5 @% Y/ U& r# F+ w. s
( m# Y; m% y/ ]* z. P, Q# c try {8 n2 x5 C7 ^. \( a" G- s
modelActions.createActionTo$message ; A9 Q" I8 K. a# \ e5 v' p$ n2 h& E
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 l. k. W/ [1 ?5 v8 S' m& ? } catch (Exception e) {
% f( M7 k( i q9 z+ f System.err.println("Exception updateLattice: " + e.getMessage ());5 s% i. S( J+ w9 r0 a" a
}
# G1 t2 K, d2 i! a: \ 5 w2 x1 p, E* x8 A5 Z
// Then we create a schedule that executes the
1 [4 ]3 W9 y/ F. @: |' `" K S* _ // modelActions. modelActions is an ActionGroup, by itself it
1 f0 V: n8 o* P) s$ U, t // has no notion of time. In order to have it executed in
: f" ~2 o+ S, a9 a7 \- `4 n // time, we create a Schedule that says to use the
, W" r- d5 h0 _/ ^+ v // modelActions ActionGroup at particular times. This
, }) D+ y' f* c6 }3 _" |# j) [ // schedule has a repeat interval of 1, it will loop every
8 j6 u: `" }9 J M6 O8 e // time step. The action is executed at time 0 relative to
7 v4 y7 a0 E2 m; P$ C3 | // the beginning of the loop.
9 ?$ m% ~/ I% @4 L& |9 \
. @' Q, C$ Y) ?* { // This is a simple schedule, with only one action that is
2 u6 v# o, L# K, w9 D6 t* t // just repeated every time. See jmousetrap for more. N- ]0 k$ _. H% C- x1 {
// complicated schedules.
7 T7 t+ A8 C. {: U" W0 k
* i7 e `* W, G7 L; R modelSchedule = new ScheduleImpl (getZone (), 1);
3 @1 k& N% x0 B6 L2 q modelSchedule.at$createAction (0, modelActions);
0 h# f R: m, n
# Y- e# h/ a! s5 U' Q return this;, T& \) ^: S6 x) h0 |% a
} |