HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 p, L% @) G5 ~+ w9 L
+ i! e% u2 R. s& ?
public Object buildActions () {
: V7 S, }7 i |8 X: O0 t0 N0 V super.buildActions();
4 @: t& _ }* e* v5 I `; m
2 t" Q+ V! N* R& s // Create the list of simulation actions. We put these in8 C T L' Q. _# h0 d5 w
// an action group, because we want these actions to be
2 K% ?6 T: ?, V/ y // executed in a specific order, but these steps should! L1 X5 H7 B4 G
// take no (simulated) time. The M(foo) means "The message
6 ?' e& w0 J) o // called <foo>". You can send a message To a particular
' P, ~ i. m; c( @ Z // object, or ForEach object in a collection.1 f {% F+ R- f
9 n! E! v9 f+ h8 G* P# | // Note we update the heatspace in two phases: first run
" Z) I. i8 N" y8 Y$ g0 t* K // diffusion, then run "updateWorld" to actually enact the
! u' ~6 P+ d, W4 y // changes the heatbugs have made. The ordering here is
6 j% X. o u' O) B // significant!
% {* m7 g, A4 ^! ^8 V: f$ w 5 e5 A% U/ d6 H: u9 o0 |; G) y
// Note also, that with the additional
* R) z- Y+ d7 H @; Y; c // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 d F! T" W+ C, h0 D // randomize the order in which the bugs actually run
6 {$ J# \* K3 Z0 @+ J+ p // their step rule. This has the effect of removing any
4 R2 f- Y. K3 U7 s // systematic bias in the iteration throught the heatbug$ p5 N2 V/ w& w% U( G" R
// list from timestep to timestep
9 c r) ^- d4 Q5 h" G8 \ ' v) E; J, r) J, Q
// By default, all `createActionForEach' modelActions have
! }1 Z0 Q; Q: W0 B // a default order of `Sequential', which means that the
, _5 S) W0 L9 D // order of iteration through the `heatbugList' will be9 a8 B3 r: `6 z1 G; \
// identical (assuming the list order is not changed7 {9 v0 n% B. R& M4 I. |# W
// indirectly by some other process).
) y; x$ L) n |+ f
' j8 J0 W, R5 D modelActions = new ActionGroupImpl (getZone ());7 u' l& g5 {* U4 v
( t. Y& ^0 h& q6 Q4 Q: W try {4 y9 P$ @/ F) r8 X- h
modelActions.createActionTo$message* h* r: g# S' d2 u
(heat, new Selector (heat.getClass (), "stepRule", false));: u4 @* g* X( i# U
} catch (Exception e) {" M! V' Y3 f9 E2 q- D
System.err.println ("Exception stepRule: " + e.getMessage ());! [/ D3 v3 l% d
}; l' I8 L7 I% A* t0 O! T
! k9 l1 \! _% B0 Q- \% m9 J- w try {0 p: V( G) ?. [7 Y
Heatbug proto = (Heatbug) heatbugList.get (0);4 U' c1 e3 E2 O4 e- ~; F, x p6 u
Selector sel = . V3 f: o/ M4 N. q
new Selector (proto.getClass (), "heatbugStep", false);
: }6 J5 I/ ~6 _ actionForEach =
9 p9 B8 m0 t/ `3 P: |5 l9 q modelActions.createFActionForEachHomogeneous$call
9 F( q [- ^! L6 R& U (heatbugList,
1 Z% x0 c" r6 e" | new FCallImpl (this, proto, sel,
, D3 j& e8 L* P4 ^6 u) s7 E2 R new FArgumentsImpl (this, sel)));6 t% L6 N. }3 t* a) C* K
} catch (Exception e) {+ d" @* _7 E0 N$ g( Q6 }0 F7 n
e.printStackTrace (System.err);
" N, [; Q) D8 h% S+ r/ Q. K }
* L% D# p7 ^0 N$ b# N# b ) j9 k/ S: n: O/ \/ \, Y
syncUpdateOrder ();
8 f3 |2 r/ V& y% ?6 e0 p
. ~+ q# v2 E8 O% M' ` try {6 l$ p, s& a" v5 L- K9 N
modelActions.createActionTo$message
2 N8 C% R7 B+ c1 g0 g8 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
5 P, r! \ G1 T: Z9 i" U$ y+ r } catch (Exception e) {, N" k2 S' g( a' i6 t
System.err.println("Exception updateLattice: " + e.getMessage ());: K0 y& V/ J# G+ t p& d
}
, R. j9 t( [2 s
- P& h& ]+ O$ y // Then we create a schedule that executes the0 h) u6 A' ?3 E; H8 f
// modelActions. modelActions is an ActionGroup, by itself it5 a8 G" X& |( D$ L. Q
// has no notion of time. In order to have it executed in; L, Z1 \. }2 _2 o
// time, we create a Schedule that says to use the
- Q' K7 T5 Z* U* _9 \. X, q: \3 F // modelActions ActionGroup at particular times. This* C- U( ?: y* X) j, N" e! s
// schedule has a repeat interval of 1, it will loop every
8 O' }* `: J- N // time step. The action is executed at time 0 relative to
+ Y7 W2 R6 Y7 J: t // the beginning of the loop.
" J8 ?0 F6 h6 v. J9 f3 X0 a& c4 t
8 O) O" A) H# d7 K( t7 N, |1 r* F4 O2 H // This is a simple schedule, with only one action that is% V+ R3 L! H/ _; k- ]) r6 m( t
// just repeated every time. See jmousetrap for more6 d1 Q# L1 }9 f/ U
// complicated schedules.: W# C* R' z% i: Y5 T: f
3 z3 p& `6 ?0 I modelSchedule = new ScheduleImpl (getZone (), 1);, N0 t1 e, H/ _, A4 {
modelSchedule.at$createAction (0, modelActions);
# Q# d) M- C( @
" z N) @( H2 G return this;
9 v q4 z; T2 D$ x6 m& W( d R! @ } |