HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" ]' H. S; h* U, y( @( Z* M
! j* K5 g( w3 X* P- M public Object buildActions () {
" q7 _0 i m, Q super.buildActions();( T. y, Y& p0 u2 Z5 \
. h4 _ b; J$ z // Create the list of simulation actions. We put these in5 E- {& p- o) W) y# ~4 }+ X
// an action group, because we want these actions to be) z. `' ?# K* u! X
// executed in a specific order, but these steps should
& n; d3 I! S+ [, S // take no (simulated) time. The M(foo) means "The message: |; A: ^8 u0 F( v4 | x9 a, |. s
// called <foo>". You can send a message To a particular
9 _4 C) E- _$ Y: q* M // object, or ForEach object in a collection.
7 F& m; m2 T# K7 k% r - t- g* \# X* H2 O0 G8 ?0 I/ ^
// Note we update the heatspace in two phases: first run4 y8 y* E2 i5 u L9 |+ l
// diffusion, then run "updateWorld" to actually enact the& B" j3 |" K9 D/ }- ?
// changes the heatbugs have made. The ordering here is% U0 C& B" p8 h! W
// significant!
) U8 Q6 b; x5 f/ V 2 ~; o3 L; H3 G* O2 w/ z% L6 B- y
// Note also, that with the additional
7 T: S" k y/ [) t0 A) z" @ // `randomizeHeatbugUpdateOrder' Boolean flag we can3 ]% c/ e; E& P
// randomize the order in which the bugs actually run
( q S4 i5 _0 h: x5 P: M) S0 m // their step rule. This has the effect of removing any4 ]$ K+ C" j$ e1 |
// systematic bias in the iteration throught the heatbug' y; C8 U% N; m3 Q
// list from timestep to timestep
; K% R! C& d4 C& t% D' i3 Z5 [ 2 l- R2 q- R$ U" O/ R& D: ~% N# o5 z
// By default, all `createActionForEach' modelActions have
! A2 j% Z& \6 K5 G q- n // a default order of `Sequential', which means that the
4 R/ N- J: m; q% O+ X% y4 r // order of iteration through the `heatbugList' will be* e% m# u! P2 f- L9 d
// identical (assuming the list order is not changed
, C5 u6 w% T) j" z6 T% U# h, j P // indirectly by some other process).
. ~% U, o" J$ Y/ L# S & \$ \) E2 P7 G9 {
modelActions = new ActionGroupImpl (getZone ());
7 T) a: {/ a" X5 S2 W7 U+ m. ?/ I7 x1 @& y4 h/ }
try {
4 p" q) A; P& D$ c# a. ~. _ modelActions.createActionTo$message
* p0 V/ G3 {5 N( | (heat, new Selector (heat.getClass (), "stepRule", false));
3 x) F8 c! i" D$ b } catch (Exception e) {
$ S5 t; s7 ^' `' ?1 K System.err.println ("Exception stepRule: " + e.getMessage ());
; U8 M( `; A7 R4 x: | }* A( |: g, ]2 U$ Z
B' ~# j/ b K try {
5 N9 m2 s% K3 V u5 [' J Heatbug proto = (Heatbug) heatbugList.get (0);
( j# |- W z# ] Selector sel =
m1 Q6 C2 R2 ]/ e6 c/ i new Selector (proto.getClass (), "heatbugStep", false);
% {& c* ^! E6 u actionForEach =
% G) N( \4 p$ o0 A- F modelActions.createFActionForEachHomogeneous$call
& n: ^, D- c. U" ?+ }2 C# I (heatbugList,
0 v, ?, T+ l: f/ H; S6 O new FCallImpl (this, proto, sel,
+ E- L! L; n, }+ ]4 n0 t" `8 { new FArgumentsImpl (this, sel)));
0 ^: A9 G8 g T1 m, Y } catch (Exception e) {6 m2 b! k' y# P; P g* a. i
e.printStackTrace (System.err);) F, }" p3 y) v
}" X5 t8 t0 d1 B, j& j! U. z& ~
; W9 f: @/ F- N3 o+ a& G/ V& V' r syncUpdateOrder ();
0 z) ^8 K. l# Q! G" O5 _2 `8 o# F" X6 D' X7 ?
try {
9 E: K! w. j& r. c6 p0 ]( m modelActions.createActionTo$message : w$ i/ z- n4 B: n C; V. h/ Y/ |
(heat, new Selector (heat.getClass (), "updateLattice", false));
% a* R; g" i9 A% t3 z } catch (Exception e) {
$ m2 i1 g/ v4 K9 t# B5 n System.err.println("Exception updateLattice: " + e.getMessage ());' b! }3 n) j* d4 ?1 s- {. S
}
7 Y+ h E- ^4 L
* r3 Z: m: y5 i) A // Then we create a schedule that executes the# `- I* E( |+ F/ H% l0 @
// modelActions. modelActions is an ActionGroup, by itself it4 E5 u, D6 t7 w
// has no notion of time. In order to have it executed in( d4 _/ \& {/ ^7 P
// time, we create a Schedule that says to use the; Z9 z$ q2 ^/ @4 ]! X5 H, c
// modelActions ActionGroup at particular times. This5 @+ g. f; Q* u4 c% o" c3 `
// schedule has a repeat interval of 1, it will loop every
, c4 a3 B7 {& j+ t6 r7 e // time step. The action is executed at time 0 relative to
- ?& p, Y: W2 _+ u) B5 A // the beginning of the loop.
8 E }. h7 t/ E: s8 T' `* P i: H4 z4 u) R+ V4 }6 {! A: j* \
// This is a simple schedule, with only one action that is8 v; [* D C9 A6 `" Z3 ?! C9 C5 m
// just repeated every time. See jmousetrap for more
7 m& _" e$ ^* F2 d // complicated schedules.
+ o/ V/ G" Z3 q7 m8 @- \
% A6 P/ _! J- j3 k/ \) ^ modelSchedule = new ScheduleImpl (getZone (), 1);- b9 T8 ?6 `5 n! y- A. @1 L8 ^
modelSchedule.at$createAction (0, modelActions);
4 z: _" a3 _* g- B5 w
- O2 M' j4 Z5 f return this;
& B* J) r( h# J1 b6 \ } |