HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 o# j1 H1 @$ J; N- ~' D; r
E$ V: d7 y) \- J4 C public Object buildActions () {
J5 Q, g8 E, y/ q super.buildActions();4 r/ d. C8 ^0 i1 r0 }
j0 |+ G! U7 ~2 M. x+ o' k% @& f
// Create the list of simulation actions. We put these in$ v# k3 d3 O6 f. M$ h4 \' o$ N* i
// an action group, because we want these actions to be
# } D0 u0 _6 N9 |( X# K8 ]2 `, t' Z // executed in a specific order, but these steps should
9 f Z. b( y$ H* i4 i$ U1 S // take no (simulated) time. The M(foo) means "The message
& K5 C3 v* t+ d# X+ h' b; X3 n. u# t5 l // called <foo>". You can send a message To a particular
" @! p F. v. I2 @ h( r/ F // object, or ForEach object in a collection.
' \: H5 F' i$ d6 @& h : ?7 R* J5 I% o5 [) P* \
// Note we update the heatspace in two phases: first run& k4 f. |# D+ w s0 ` N7 B
// diffusion, then run "updateWorld" to actually enact the
o2 U7 i, ^7 F+ G+ r. O# | // changes the heatbugs have made. The ordering here is
2 d+ B e% Y4 F4 y0 j+ D! w // significant!
! ]" ~/ r) {6 o6 n3 ]" R& \ 9 d& F8 P+ N J3 a8 D
// Note also, that with the additional
- d* d) ~4 o, ^& S0 F0 G // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 O+ k' i7 l9 h) O: u# f6 s // randomize the order in which the bugs actually run
' g1 c# ~) |2 R n+ l* W; H // their step rule. This has the effect of removing any
8 w- d" t' B! J9 A. | // systematic bias in the iteration throught the heatbug
: E! B y$ |; l) C9 n7 g // list from timestep to timestep: u2 ^& k% F* g4 Y6 B$ I
) X2 t( S: k. a8 y, O // By default, all `createActionForEach' modelActions have& r2 r' w8 ?' _8 M: |2 r A
// a default order of `Sequential', which means that the) Y% y3 k: a$ R7 I+ ^
// order of iteration through the `heatbugList' will be7 l* |# p7 M; \1 a$ O9 v
// identical (assuming the list order is not changed- ], s7 h1 J: W" {: B+ C k
// indirectly by some other process).
( p* J6 \/ L4 V3 s
! }* b3 m+ A; e2 K& c. \ modelActions = new ActionGroupImpl (getZone ());# {& d; h3 ^0 L
( i( b' ] I, G6 }+ [( C* \ j3 S
try {' }; F3 Y& I5 I+ M+ N
modelActions.createActionTo$message
0 @0 z3 q- P- `, P& q/ g) _ (heat, new Selector (heat.getClass (), "stepRule", false));
+ i0 c$ I0 u& J7 D8 f9 v8 K } catch (Exception e) {
! y0 n! F7 ?) q# { System.err.println ("Exception stepRule: " + e.getMessage ());
+ _" r' _$ G* H7 d }' ~, } G. m: l( L7 g
0 b% s# z5 s( o# u: k' T4 b( j
try {8 Z; z5 c2 {9 z: e0 I2 m2 P
Heatbug proto = (Heatbug) heatbugList.get (0);& _5 `" }$ a( M }
Selector sel =
0 S& o& B: P6 P7 V new Selector (proto.getClass (), "heatbugStep", false);
& Z ]2 O& E$ G. L" m, u+ n actionForEach =* J6 t# c: u' B3 y0 u, `
modelActions.createFActionForEachHomogeneous$call
# _; @, U5 V0 z) A) l (heatbugList,
- s' X( i. T. E0 m new FCallImpl (this, proto, sel, i3 T7 _" \3 Y+ w n9 P
new FArgumentsImpl (this, sel)));
1 ^; A/ x9 o( v* v9 F( R } catch (Exception e) {* ~8 c$ ?, |; G3 q! K- P
e.printStackTrace (System.err);
/ x% A* f( S- Y; C& f5 w+ N% a" l }
m7 J! Z8 n2 U" L. G) e) K , k. K9 ]; O; ?1 t x
syncUpdateOrder ();$ U1 u1 l7 q+ I8 @% D
. t' I# `3 P; a
try {
/ w3 F3 [8 |6 W: f modelActions.createActionTo$message , B) Q/ ~+ o' ?6 ~. l
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 u3 @7 A9 G8 w% p$ U } catch (Exception e) {
# V* {2 E. r5 L# {! `7 _ System.err.println("Exception updateLattice: " + e.getMessage ());
! g. |7 y) H. _ B+ F' U; P }. V5 y) w$ `, p8 p: O; h' T
H% L! h3 n' m, _) N // Then we create a schedule that executes the0 H0 K: Y3 m* M S# K3 s$ c
// modelActions. modelActions is an ActionGroup, by itself it
6 v3 d S% s; Z! A5 A // has no notion of time. In order to have it executed in
& E( g1 t+ Q7 b# J% a; B; {& J // time, we create a Schedule that says to use the
, F9 O8 _, I3 i // modelActions ActionGroup at particular times. This
0 X( w4 v" Z2 |# X$ [ // schedule has a repeat interval of 1, it will loop every( R6 g; d _; |) [
// time step. The action is executed at time 0 relative to- B+ t4 }) I7 h# [8 o% w
// the beginning of the loop.
% V5 j) I2 e u
3 `. c9 p( y: b L // This is a simple schedule, with only one action that is# e# i( T0 G8 P) i! X4 V( Z6 B" ^
// just repeated every time. See jmousetrap for more" S' ^9 h3 R) s( N& B
// complicated schedules.
* l0 h# m* |" J' `; F3 b" e. d - R% b. ?% N1 Z* C
modelSchedule = new ScheduleImpl (getZone (), 1);
z* {! k* Z. J modelSchedule.at$createAction (0, modelActions);
, ?" b) M5 W. o% g4 C9 i
# i1 F! d4 z0 I5 s6 _$ | return this;% M+ r R; l+ f! y! D
} |