HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# q6 a3 l$ s6 t2 I1 `, F& T; y9 N# P- k0 f
public Object buildActions () {
6 Q$ x* n1 f# G2 s6 L4 U0 u super.buildActions();( ~. {2 x G; m# R- {
& i. B( B5 W9 j4 n
// Create the list of simulation actions. We put these in
6 @4 O- F, u% T' j7 U% C% o // an action group, because we want these actions to be
- q6 |! V+ l/ Z7 u7 l- }9 G // executed in a specific order, but these steps should$ c1 S+ q g4 |* t* e; Q6 j
// take no (simulated) time. The M(foo) means "The message
1 G# P, u/ Q2 t1 t // called <foo>". You can send a message To a particular
; d4 g! a; s& ^' [% j // object, or ForEach object in a collection.
* ?" [, F0 w8 v2 O 7 [* ]& D- w% |
// Note we update the heatspace in two phases: first run5 ?% f/ N& F7 i$ `/ j3 h. w
// diffusion, then run "updateWorld" to actually enact the
! r: X6 V) I/ G3 @$ d+ {: F' { // changes the heatbugs have made. The ordering here is. m- }5 C/ p, M9 [9 n6 ?9 k
// significant!
, U2 l- ~! Q/ r# w& t; A; b 4 K; G7 f* |& }5 x* N/ F
// Note also, that with the additional1 r3 R0 S; p y6 Q) i( A
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: S* |2 `- A3 {5 E' { // randomize the order in which the bugs actually run: ^- S. p5 O) a6 }
// their step rule. This has the effect of removing any
" O2 v: b0 C, t8 h3 O$ ^ m // systematic bias in the iteration throught the heatbug
4 D& A5 C- K7 x/ l+ Q1 y- `: ] // list from timestep to timestep6 O& z1 Y0 C* o
% H7 L. S. W& r( F S& s9 k1 o // By default, all `createActionForEach' modelActions have5 [# N! r5 @- H3 I
// a default order of `Sequential', which means that the
! x' K( Z; Q& D0 ?/ M) H) X // order of iteration through the `heatbugList' will be
& L0 V- z! p. B3 x9 d1 W2 Z // identical (assuming the list order is not changed+ H$ ~; m" ?* i X2 F; y8 u
// indirectly by some other process).) C7 l# R: [2 ]) b
& V5 ~5 f9 z% w' ~+ d2 f" D
modelActions = new ActionGroupImpl (getZone ());
: `' M. z) A2 h% j* ]7 Z2 w1 d: h1 |% d5 {
try {
5 L2 P4 C+ |" S8 Q modelActions.createActionTo$message
7 y, n( @2 ^- A% h. D: q" R3 E (heat, new Selector (heat.getClass (), "stepRule", false));" a( q; O0 S' Y4 h0 Q; ^
} catch (Exception e) {. `. ?" H( e! ^% I* M5 k% R
System.err.println ("Exception stepRule: " + e.getMessage ());
/ {' t. W- [ [) `. A9 G }0 ~% y! C' k- `( z- ?3 Q
5 k0 H: V8 T- ]6 ~7 n
try {3 d' v' z/ e. Z: @ u$ T: c
Heatbug proto = (Heatbug) heatbugList.get (0);
" [4 D3 a$ w; I) d- J2 F j Selector sel =
. G4 {+ s) C x9 `7 o/ B- Z new Selector (proto.getClass (), "heatbugStep", false);
. d+ q+ E2 P0 q: S6 [' k* I% e+ \ actionForEach =
x( Q; ]/ I Q1 S4 m/ K modelActions.createFActionForEachHomogeneous$call6 j/ ^2 O5 D/ ?5 \
(heatbugList,
3 | h4 `3 j. r$ _ new FCallImpl (this, proto, sel,& C: o, }+ f- |% m5 ~; Q6 J/ j
new FArgumentsImpl (this, sel)));( L% S) ?4 x# @1 l8 {6 ]& ^7 P
} catch (Exception e) {
1 O4 s" C3 @& U" `3 U e.printStackTrace (System.err);* V, h+ c4 S7 Z+ s
}
0 q8 X) D" T- {" b9 M
0 c2 K. [' U4 X syncUpdateOrder ();& ~/ F' P. l' J# ^& L
$ Y0 P6 k) x( E. f4 |, U try {
4 v% `( E& F8 f7 f0 Y modelActions.createActionTo$message
! H/ T6 e4 i6 S6 v- O (heat, new Selector (heat.getClass (), "updateLattice", false));
6 X" W2 \1 Y* `6 b( }8 a6 z } catch (Exception e) {. G/ b) b% e2 g( ]: z6 ~9 o" t
System.err.println("Exception updateLattice: " + e.getMessage ());
7 A. ~5 N4 A( w% w5 r) U }
9 B6 `( u: _# S B 4 C8 R& o& k% s( \
// Then we create a schedule that executes the
1 K2 b2 n* m3 E7 T; O* w // modelActions. modelActions is an ActionGroup, by itself it2 R4 i& m4 ?+ ?* D6 d- `2 H
// has no notion of time. In order to have it executed in
. Q1 d0 M$ c: S7 c1 x8 ?8 @ // time, we create a Schedule that says to use the1 w" O/ W/ p' n6 ?, f
// modelActions ActionGroup at particular times. This
& E* u; Y+ z: s, x: s$ P q7 F // schedule has a repeat interval of 1, it will loop every& y# A) k# C6 | p8 R
// time step. The action is executed at time 0 relative to
) m* G8 u5 ]( e // the beginning of the loop.
1 k: [) A* R8 |9 e. I r* S) {7 h* r. Q7 s1 z. f: R0 Q9 f
// This is a simple schedule, with only one action that is
' ^- Q: }0 y/ A, _( b // just repeated every time. See jmousetrap for more+ m$ L1 Q( B' r7 U+ L, y
// complicated schedules.
/ G! I& X, \' c/ A
. @/ P: `4 |5 x; j- w modelSchedule = new ScheduleImpl (getZone (), 1);" u& e! M, T6 n4 V" i8 O. [
modelSchedule.at$createAction (0, modelActions);" G0 l& e& z, Z. v
3 g0 M. X9 y4 S' Z' _" F2 s. G return this;1 h! o& ]4 y0 j: g
} |