HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 t0 E; r/ T$ W2 g
1 g. |3 ?& s @% a- a3 q* e
public Object buildActions () {! w# n0 y- m" o1 u$ W
super.buildActions();
: S4 u% q( t0 Z' k9 K2 G9 e0 a* ?3 K % D, k* {) n2 w6 i! K
// Create the list of simulation actions. We put these in
' `' V3 H f/ W9 H( ~0 e // an action group, because we want these actions to be T: R3 h4 h5 Y/ Z) @
// executed in a specific order, but these steps should; D U0 Y) H' j- K
// take no (simulated) time. The M(foo) means "The message* H' j# j( A/ R3 g, K
// called <foo>". You can send a message To a particular
( L) M/ N6 [$ q& h // object, or ForEach object in a collection.
3 X: e% H5 t: Z0 M% D3 W: W
( t" g/ r( A9 |& v // Note we update the heatspace in two phases: first run
+ j, J4 u, F1 @ // diffusion, then run "updateWorld" to actually enact the. z+ H/ a7 |, O9 U
// changes the heatbugs have made. The ordering here is) A5 f7 I3 g* S; @' c) N
// significant!
b S# j. D/ L % A) C* i d: }& q! A
// Note also, that with the additional
$ u) l4 X1 B+ @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
( Y, C& _& ]1 Y$ B$ {) w // randomize the order in which the bugs actually run
, w$ W- M+ P8 w( H1 ]; W // their step rule. This has the effect of removing any5 w1 Y" f& }3 K2 y+ O# T
// systematic bias in the iteration throught the heatbug
6 p2 b3 F3 N3 f- @: h `9 M" ^ // list from timestep to timestep; @6 t! X0 _8 p; {$ m. p- V& Z' G
5 v: i; Z/ o0 e4 w
// By default, all `createActionForEach' modelActions have
9 j! |: s7 B+ c8 g1 K* F& o7 e) F // a default order of `Sequential', which means that the) E3 _# T( |' D8 X" }, z& w; ^
// order of iteration through the `heatbugList' will be
% Q9 z) J* L1 a( X5 P // identical (assuming the list order is not changed8 |6 d; y7 O7 q$ T& E' K
// indirectly by some other process)./ n- e5 _3 X, O3 C
+ s/ D3 ]: D4 `+ j modelActions = new ActionGroupImpl (getZone ());8 a& J' t6 N& j6 G4 x1 l/ d; x0 R0 @
% d& Q* M. F9 f* U try {
) O3 g. R& z# G7 I& P& R# o! w modelActions.createActionTo$message: v/ ^% I2 @ L/ T7 ?
(heat, new Selector (heat.getClass (), "stepRule", false));
0 S0 C4 P" G n } catch (Exception e) {0 y% S# g+ P; P1 k. l& S! U
System.err.println ("Exception stepRule: " + e.getMessage ());
) b. q P7 Q8 e- ] }
* m" f% U: |# E z$ u1 k3 c V. y" E; y0 Y5 S- ~9 [! m0 E4 b
try {
$ F' s5 V& z9 W Heatbug proto = (Heatbug) heatbugList.get (0);! b. g% Q" v9 K0 z
Selector sel = 7 D) c; V1 x$ w
new Selector (proto.getClass (), "heatbugStep", false);9 B. }5 I6 O {' ?" ^' T0 [
actionForEach =" Y9 O. X' C# T0 K; K
modelActions.createFActionForEachHomogeneous$call
; ~3 x+ h4 U6 i. [6 u8 q' _ (heatbugList,8 J" J! P" J" Z& L! c! J
new FCallImpl (this, proto, sel,
# O4 v( \9 N5 ?( M5 t4 w. C; k9 g new FArgumentsImpl (this, sel)));# E. p0 z4 r6 J7 G E/ k5 r" N# e# a# u
} catch (Exception e) {& y- e8 g) f* l+ a
e.printStackTrace (System.err); {8 y( t9 J& z
}! Q: \5 a4 n1 I5 E
' a f& N- E) M9 _" a
syncUpdateOrder ();3 S' K, h8 R4 O4 |7 n) N
: k' @+ ~+ J$ ?" I/ d
try {1 G. | t m% x
modelActions.createActionTo$message
( X9 ^- t' R- }5 ~6 a' a (heat, new Selector (heat.getClass (), "updateLattice", false));; j! y2 f) M- w- r: g& U- L
} catch (Exception e) {* ~% o" R$ ^: L9 A
System.err.println("Exception updateLattice: " + e.getMessage ());, K* c7 ~ U. D7 d( Y. ?; b7 X
}
: k, z. K+ L! n0 m! p! \2 e 4 @ R' @% M! P9 W7 }) E
// Then we create a schedule that executes the
) @$ ^% `. [! o6 @ // modelActions. modelActions is an ActionGroup, by itself it
6 K3 u8 P. c0 [0 e' Z' q% u // has no notion of time. In order to have it executed in
: i1 C9 S& q4 z* n // time, we create a Schedule that says to use the
/ W0 K. U& I; Z# q8 l1 p // modelActions ActionGroup at particular times. This
7 a. T* k% E% f6 |% ~8 ~ // schedule has a repeat interval of 1, it will loop every
8 l( n( f. D7 Q // time step. The action is executed at time 0 relative to5 i: e; L) H- |. C) ^9 d7 t! Q4 P
// the beginning of the loop.
, ]. B/ W& o! f6 O/ ~4 i/ ^( M! j: o) S' y
// This is a simple schedule, with only one action that is2 h2 P4 K2 d+ j ?( n# h
// just repeated every time. See jmousetrap for more2 X ~, Y( h' W1 }% t
// complicated schedules.+ ?9 r8 v8 T* Y- }5 N$ r# K
+ d% D; I" p) i0 B2 @8 F3 e, ~! I$ i modelSchedule = new ScheduleImpl (getZone (), 1);6 e4 ?4 u }7 r" j* t
modelSchedule.at$createAction (0, modelActions);
0 ?$ D4 r3 S9 i - k& O$ H2 Q |4 |( f P8 ^4 x
return this;! j. ]: s: f: a
} |