HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 l4 ` O5 G, y/ [7 J- L% }, @ Z8 O: P5 U3 B$ x2 A& b' h) C
public Object buildActions () {
: O: o% H- O+ ~2 { super.buildActions();$ e' j5 B" z. E4 o/ S. A
4 L3 B+ ^0 @2 O" f! @ // Create the list of simulation actions. We put these in, J( L" B4 X8 [7 H, @2 t5 m5 b& i
// an action group, because we want these actions to be
' P B4 g, {" X" \* P: h- K) |+ f // executed in a specific order, but these steps should
, x: N7 a+ h: J- P; B) _ // take no (simulated) time. The M(foo) means "The message6 _( A( x4 C0 O- Y7 Z9 H& N
// called <foo>". You can send a message To a particular: X5 P; f5 x6 f- j: l& G
// object, or ForEach object in a collection.
3 t& b+ ~2 Z) y" g ! J# |1 @1 N4 d9 C$ j ]
// Note we update the heatspace in two phases: first run7 j0 w: O1 y% f
// diffusion, then run "updateWorld" to actually enact the) d! y! F! k3 J$ n! H" h- ^& x
// changes the heatbugs have made. The ordering here is2 O5 Q; c2 m* X, T# Z/ \
// significant!
8 B0 {9 t$ V& P4 G) b& h, z6 @ 2 |$ j! |! ^, m9 P- m* k2 i
// Note also, that with the additional) I/ O* |' H" I* y
// `randomizeHeatbugUpdateOrder' Boolean flag we can* d- ^. ?% U9 q& g
// randomize the order in which the bugs actually run% \3 b- U% H, D9 d9 }+ i! Q! T
// their step rule. This has the effect of removing any" U# U; t# ?& Q
// systematic bias in the iteration throught the heatbug8 E& B: y" o) {, [* j- J
// list from timestep to timestep
- j/ s3 z8 c8 b2 X
8 c, s6 L7 s7 b7 p // By default, all `createActionForEach' modelActions have
4 U3 f; b5 E; Q0 ]/ b // a default order of `Sequential', which means that the) V" b9 t* s6 U% [! p7 q5 L
// order of iteration through the `heatbugList' will be
^% S. t9 n: \! {2 O$ _; C // identical (assuming the list order is not changed7 ~* M. N+ i1 q
// indirectly by some other process).
6 e5 o& d* |* i. P
3 a2 {# |8 R6 j; ~0 @# w3 O' @ modelActions = new ActionGroupImpl (getZone ());
8 m. [, P% | W$ W
3 w6 j1 {% U7 D, O( k$ k0 H try {
2 E% ^. w& g, }& P2 X' ]% O modelActions.createActionTo$message
, N4 Y6 K2 b1 l% Q4 O& H' h& F (heat, new Selector (heat.getClass (), "stepRule", false));' |9 ~2 }, u. s7 @
} catch (Exception e) {, s) L Q* B( _. g% p: D+ A
System.err.println ("Exception stepRule: " + e.getMessage ());
: [7 ^5 R) I0 u; Q }
8 J/ R: B; A# {% I8 n- w$ w5 G
* S/ E- e" Z6 x" u' m; S6 n try {
9 G( b3 Q6 z( c& } Heatbug proto = (Heatbug) heatbugList.get (0);' Q$ h0 D) [2 z0 ?3 J' d
Selector sel = : i. n/ e, I+ J1 @8 t) E9 M7 [" g
new Selector (proto.getClass (), "heatbugStep", false);8 X, k: p% f0 g+ P; V9 }3 m
actionForEach =7 ?2 H1 H6 H$ l0 H
modelActions.createFActionForEachHomogeneous$call
, R2 o/ K8 U$ V E. W* ]& B* c (heatbugList,
3 S9 \# s0 Q+ f, C9 L8 M( n% Z new FCallImpl (this, proto, sel,' R$ M0 @4 s+ T* L# h) d3 l4 t
new FArgumentsImpl (this, sel)));
2 L' D ^6 m# P2 \# ]2 F/ V } catch (Exception e) {9 ]! Q+ _& d: Q
e.printStackTrace (System.err);
3 `! I8 j. L/ X' w6 h2 h0 N } U; {9 d2 {7 t8 d2 g2 ^2 A
4 k- ?" a5 `$ J0 ^ syncUpdateOrder ();
: M' n/ P U ^5 X- a9 v8 z8 @
: ^5 t* j; ]! E. J+ B' h try {
( j& i3 H+ p: k! m' ?8 P modelActions.createActionTo$message 2 P/ G# N7 N- @
(heat, new Selector (heat.getClass (), "updateLattice", false));# j2 O4 B2 N' H+ B8 c# l7 Q
} catch (Exception e) {: ]. \3 o* w7 S+ Z m
System.err.println("Exception updateLattice: " + e.getMessage ());/ l5 L' Y, N* y
}# f0 _" c9 |7 [# @1 m: ?
* d$ A* E. @$ k9 {" s
// Then we create a schedule that executes the% h$ a; @; j0 S; t c
// modelActions. modelActions is an ActionGroup, by itself it
8 y% c2 @3 Q4 h# i# s4 W, Q // has no notion of time. In order to have it executed in% v+ ]# }" y& D- a/ C+ u
// time, we create a Schedule that says to use the5 @) s/ Y: A, R6 {8 f; _( f
// modelActions ActionGroup at particular times. This: u" G7 V4 g9 [
// schedule has a repeat interval of 1, it will loop every, \# T* X3 d/ O# F" S- B2 A/ [: {
// time step. The action is executed at time 0 relative to( ?" Y7 B+ q& V: j0 V5 f7 I
// the beginning of the loop.
0 n z8 n' j8 z# K' j y$ C
6 D) C `% b* w" E' z/ k // This is a simple schedule, with only one action that is; a2 G( P. ?* H1 H3 C* s% q
// just repeated every time. See jmousetrap for more. k( s6 O, d3 R O
// complicated schedules.
/ D! g! y5 z: q9 `4 ? b6 Q 3 I$ V* r( R' I6 f2 f) ] g& z
modelSchedule = new ScheduleImpl (getZone (), 1);
! f- X+ s9 H. N* G3 r modelSchedule.at$createAction (0, modelActions);
% d% Q1 B- p% `* ~% y
9 _1 P+ |6 g, ]) i2 N9 s }1 ` return this;
2 r% l8 \! m0 l7 o0 v6 C/ Q% b, Z, c } |