HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& P/ Y2 r" f) M
% Z6 i) {' R1 ~; x; h7 |
public Object buildActions () {" m4 W$ i. V! b9 U! e* c' i- F
super.buildActions();( e, T. h6 I# d0 l
. j5 S! E6 H: s% Y // Create the list of simulation actions. We put these in0 `, n- u" ?. s/ [" c0 ~) f
// an action group, because we want these actions to be
. C: m7 }5 P" b z // executed in a specific order, but these steps should
# l* |: y& \9 _& R // take no (simulated) time. The M(foo) means "The message/ P, s. [% t0 }4 n5 }/ k$ f z
// called <foo>". You can send a message To a particular
4 S& B u! |9 @* |( c, H0 ^! L2 y // object, or ForEach object in a collection.; A$ V' ]3 r l4 y# ~9 x/ C
$ `/ }- L! { _2 x5 Z
// Note we update the heatspace in two phases: first run4 W' J" E$ [5 W3 R
// diffusion, then run "updateWorld" to actually enact the9 f9 v: L4 r3 W! G7 \( c* w
// changes the heatbugs have made. The ordering here is
: g+ v! E8 u$ p3 P! S // significant!) D" c/ D$ ~- a* r, F7 C
" L# |8 _+ U: o, [0 `- j- _$ r8 O
// Note also, that with the additional& f& E3 h( B( o
// `randomizeHeatbugUpdateOrder' Boolean flag we can( M/ l3 u0 l! E
// randomize the order in which the bugs actually run
. l! _; B. s; A/ [ // their step rule. This has the effect of removing any0 S2 L* V6 T5 K, A5 T$ ~
// systematic bias in the iteration throught the heatbug
|4 ?( @! L+ i. {: S7 r // list from timestep to timestep
' V& ?* H. z. I9 ~, e% _: l6 Z # x1 F* X3 u' \! Q' X
// By default, all `createActionForEach' modelActions have T. z& }* T+ |" p' O
// a default order of `Sequential', which means that the
% Q; r& t+ R _4 r // order of iteration through the `heatbugList' will be% X; I, C8 H7 i; B8 K$ k8 I: i. f; k
// identical (assuming the list order is not changed
( u' U' A- @" m6 V, L8 h: W // indirectly by some other process).3 K7 p9 F8 E( h; H' G
! z# J. k4 y# ^! y+ j
modelActions = new ActionGroupImpl (getZone ());
! ]8 U9 K! N: y4 J; K* ^" N1 D
2 B6 Y1 i2 z: N& u5 h try {; i. n" O+ o( w4 X7 @
modelActions.createActionTo$message" W3 \" g* M; Y
(heat, new Selector (heat.getClass (), "stepRule", false));2 A$ w% h* [1 Z& a P0 Q
} catch (Exception e) {3 q% B4 L9 k" W( J1 @( |" {( |0 h' n
System.err.println ("Exception stepRule: " + e.getMessage ());
) \+ |* y; C0 x- N& P }
# w/ b( r+ a! J- O: J9 s. E
6 `! ~+ a# }$ m4 `! L( P try {
( J( `9 f# Z4 z: @4 Z; L Heatbug proto = (Heatbug) heatbugList.get (0);
; G" A6 W5 Z. I3 ^8 \4 h- G/ U Selector sel =
- `, L& C: s7 @8 [ new Selector (proto.getClass (), "heatbugStep", false);% ~( Q3 x2 |7 b) s
actionForEach =
0 J/ D' {1 k; p( ?5 H/ _* j: x modelActions.createFActionForEachHomogeneous$call
$ Z7 [8 d7 z0 g (heatbugList,3 [6 i) }9 x( a
new FCallImpl (this, proto, sel,1 ], x4 d* a$ A- y2 o
new FArgumentsImpl (this, sel)));: R9 \- O) x) m# ^) p
} catch (Exception e) {" `' Y% i8 y# X' e
e.printStackTrace (System.err);
& B( c9 w+ m" L- e }
7 j. v3 A: N8 j$ V
9 e; M, v* i. e# e syncUpdateOrder ();0 r( l/ e3 x: ^8 G7 B/ j
" d5 q/ f: l0 R1 `+ f5 \ try {
( c3 m% K% M/ z6 m6 h w; X2 V modelActions.createActionTo$message
, w: w W& f3 D- x) S0 @& P (heat, new Selector (heat.getClass (), "updateLattice", false));% y3 f3 u z7 i4 b3 |
} catch (Exception e) {8 @( R6 @) p4 R, _+ b) N* J' g
System.err.println("Exception updateLattice: " + e.getMessage ());, M( h3 E# _7 g& S( d6 U" y
}8 M: l4 D/ j# Q3 U1 l* n( c5 b
. c- l& y2 f7 K: u* D2 Y' U, _9 @# |
// Then we create a schedule that executes the b. Y* z) B( J
// modelActions. modelActions is an ActionGroup, by itself it4 Q) I& w' i6 H+ A% H9 E
// has no notion of time. In order to have it executed in
4 T0 U3 y9 V+ G/ r0 b // time, we create a Schedule that says to use the
+ w7 ?0 y1 Y, N7 r // modelActions ActionGroup at particular times. This
, a* W- Y$ h/ m, A/ y. T // schedule has a repeat interval of 1, it will loop every# E8 x' @* g' i% b8 r" {: n7 f
// time step. The action is executed at time 0 relative to
) v0 W; l8 U1 Z2 o // the beginning of the loop.
5 c7 Q" }" g* e
+ r- z3 j! m4 {* }# N // This is a simple schedule, with only one action that is
+ y7 i% F4 B0 o# s! s4 X // just repeated every time. See jmousetrap for more( K0 R1 @$ p2 z( Y
// complicated schedules.! v+ r5 ~. l6 K0 m# h# r3 [, k7 {
! ]: d0 U7 B0 M2 g& r# m; i
modelSchedule = new ScheduleImpl (getZone (), 1);' Z1 @; m+ l& S) C$ V U8 [
modelSchedule.at$createAction (0, modelActions);
5 A. u i( ?5 Q
( }' b3 w$ B ^. o% U) I5 ^ return this;0 V8 Y; i) g+ h/ {
} |