HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: `+ }( ?7 g) }6 w D9 |
4 @0 _; b2 c( u3 n2 Y3 M3 Y public Object buildActions () {
7 l! d0 D& G* [5 [0 I: u/ w super.buildActions();
2 |& L. m' e! F, }2 \ . e% P) u5 H$ Z2 B$ m8 a
// Create the list of simulation actions. We put these in
! i; a0 S7 n0 z // an action group, because we want these actions to be
$ W) ]( M2 }* Y+ h! v$ I# k // executed in a specific order, but these steps should
8 \7 P8 q# r& I) e) u0 N // take no (simulated) time. The M(foo) means "The message
2 @. d: e. t; g. V& T* b/ m // called <foo>". You can send a message To a particular( z7 v( l# n0 v8 o- F8 O D+ K
// object, or ForEach object in a collection.
- z( P5 V- P; L
{7 s/ e \: E4 Y5 l, v // Note we update the heatspace in two phases: first run
4 A* I P' w/ i& m' H! j/ ~' S // diffusion, then run "updateWorld" to actually enact the
% A% z, i! V8 v3 k* ~, l5 G // changes the heatbugs have made. The ordering here is7 W) T# _8 T+ ?6 o. l
// significant!. I' b) c: [) b5 Q7 }; M8 h- F
* q* X( {) E; t4 g // Note also, that with the additional
& ]" Y* l+ z3 D; d0 x // `randomizeHeatbugUpdateOrder' Boolean flag we can4 ^" ]# E3 X4 _1 F& n/ C
// randomize the order in which the bugs actually run
. t7 X/ R& y! q$ e: D! A // their step rule. This has the effect of removing any
8 ~, H& t' D! S, y9 e // systematic bias in the iteration throught the heatbug- g# B! w9 n0 g" H% g; q! @8 J( `
// list from timestep to timestep
. P( G5 @- ~* N/ l! V, H
8 U" ]/ H7 N. h9 S. D7 C$ z // By default, all `createActionForEach' modelActions have- U5 s, s3 v. @' f" z
// a default order of `Sequential', which means that the
; |* z4 k% p/ o+ o. t2 I5 A // order of iteration through the `heatbugList' will be1 m$ z/ l ^; K/ D
// identical (assuming the list order is not changed7 S! R/ g" | f0 T2 p
// indirectly by some other process).
7 [4 q7 D7 v7 l f7 `) e 9 z$ ` s4 b4 a7 @& W* v6 K" W) A/ m
modelActions = new ActionGroupImpl (getZone ());
( @" T3 D7 B. q* U
( `: ]) C) G# [; I# o, n try {: N' l: X: u2 V
modelActions.createActionTo$message
+ E: y; B& e8 r( |- N; H# e1 f (heat, new Selector (heat.getClass (), "stepRule", false));1 t; `% s. [9 A/ P/ D# M% K
} catch (Exception e) {. I7 R( b# Z& v1 ~( B3 K
System.err.println ("Exception stepRule: " + e.getMessage ());
8 d. k% @, m" V% I6 | }* y6 C7 I0 c8 J7 C: B; X
2 P' p7 w- I7 i2 h/ s% b try {
9 L. \, B1 S0 B, u7 l Heatbug proto = (Heatbug) heatbugList.get (0); |; |7 R; [% ]2 a; J; [) Y
Selector sel = ! D* l& v4 R" k" H! W4 V
new Selector (proto.getClass (), "heatbugStep", false);
! b8 P$ N, M) ?) f) U3 o actionForEach =
( I R! y" r7 n% m1 S" K modelActions.createFActionForEachHomogeneous$call# U; v; H7 F0 {3 P5 C
(heatbugList,
. l; Z5 o! q; r; x' \ new FCallImpl (this, proto, sel,
/ ~' g: ~0 M# T7 p; k new FArgumentsImpl (this, sel)));2 O6 M% @- A1 s, r8 G
} catch (Exception e) {( ^+ i2 h2 a- H- V# Q3 f w/ D
e.printStackTrace (System.err);
8 S& F0 W, |$ y }
' q- U- O3 N' O6 K4 c! G; Q) s : Q8 I' F- q. Z% E& f
syncUpdateOrder ();* ^) L* U, X' S' H* m6 O
' Z4 z- ^+ H4 a! x( S try {* G C' o3 I- U( @
modelActions.createActionTo$message
+ _- G* r) Z- C3 @" e (heat, new Selector (heat.getClass (), "updateLattice", false));
2 {8 t$ o2 E) N' n! J. D } catch (Exception e) {
+ [: L! t f) w3 a$ W' D7 \ System.err.println("Exception updateLattice: " + e.getMessage ());' U2 u4 ?3 E/ n- u5 d8 g: w
}& k, a( s' b* S
0 M3 S& [4 ~, r3 R/ n
// Then we create a schedule that executes the
2 ^+ ?( U; J9 ` // modelActions. modelActions is an ActionGroup, by itself it' | {- E7 ?6 x ?$ v, X0 R
// has no notion of time. In order to have it executed in
8 u! ]& L' a$ Q- a! e$ r% H5 I% g // time, we create a Schedule that says to use the; |! ^+ R2 h* v1 O
// modelActions ActionGroup at particular times. This$ }( P4 f+ L* O9 ]- [$ ?
// schedule has a repeat interval of 1, it will loop every0 w! H, X& n' b
// time step. The action is executed at time 0 relative to% w5 q8 g7 d2 t2 _- Y+ p9 A
// the beginning of the loop.' U2 X/ I8 }. a
- O/ R* f- l9 i! {& w q, E // This is a simple schedule, with only one action that is
( Q' x7 c0 w4 _8 f' A# K" S) u // just repeated every time. See jmousetrap for more
. ]0 Y) |$ L' U; Q9 }7 l3 O // complicated schedules.
4 C' r& h# j$ l+ p$ u. {: i+ N ) c5 X3 Y# ~% s) _# O" O8 @
modelSchedule = new ScheduleImpl (getZone (), 1);
& v% g& y$ r" f modelSchedule.at$createAction (0, modelActions);; L8 m4 R/ Y1 k) }/ p; t- X' |
8 Z2 x) c- v( @2 T, T return this;
w' x! @6 z0 Z } |