HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 l! w% Y9 P3 ?- }: J
; |8 e: s4 c! c+ q4 |' D" s public Object buildActions () {
+ e' B8 @* r- z4 n ? super.buildActions();3 Y4 O! n$ Q; G2 `& I; \3 M& ]
! O, f; F0 ?6 b( l // Create the list of simulation actions. We put these in4 d# c- Z7 x/ S* ]4 t: v: I" \
// an action group, because we want these actions to be
8 G0 m* [ Q" k+ V) }( d9 }: p$ P/ ? // executed in a specific order, but these steps should
" w3 O" A0 N( {* v // take no (simulated) time. The M(foo) means "The message
* C! r, r! q2 n2 O* ]0 v // called <foo>". You can send a message To a particular& r+ y5 t+ r4 _$ U
// object, or ForEach object in a collection.
6 c! L9 |. L" v, Y- D2 `) k4 B ) i) g+ W: q- e: V$ j1 R
// Note we update the heatspace in two phases: first run/ d9 g9 ]6 a6 B$ y- c" g3 P7 D3 s
// diffusion, then run "updateWorld" to actually enact the, v7 U% |. a! {4 j- r
// changes the heatbugs have made. The ordering here is% b e( r$ x* \
// significant!& I* T; ^/ y* c& ] W. h
' `+ W! c9 X1 H( E }5 u* q // Note also, that with the additional
$ C7 M4 T% s) h6 ^# d" v // `randomizeHeatbugUpdateOrder' Boolean flag we can4 A% m' F, U5 ~2 `, B
// randomize the order in which the bugs actually run8 @9 ]# L4 W4 v
// their step rule. This has the effect of removing any& v1 z2 ~& U) D! Y$ [ t
// systematic bias in the iteration throught the heatbug
: F* F8 |1 q/ V // list from timestep to timestep
. K6 z6 W( K. z7 b/ h+ ^ * t/ ?- ]0 I) y
// By default, all `createActionForEach' modelActions have! ~) ^7 S, c4 D4 X# b. h3 q
// a default order of `Sequential', which means that the
0 L* \8 p3 A( m; S1 g // order of iteration through the `heatbugList' will be9 N$ B) B) p8 D% Q
// identical (assuming the list order is not changed
6 S4 b0 L+ E1 a8 r% M; e0 o // indirectly by some other process).
- V( p0 S! E( V! M; K
& u8 ?* r0 o$ X" B; ]& i modelActions = new ActionGroupImpl (getZone ()); i9 J: q! N# j' `9 O" k) a: _3 T
: d1 G: y. P+ V
try {
1 V [: e' g# T( T modelActions.createActionTo$message
. r# E* I) x7 R$ o# L (heat, new Selector (heat.getClass (), "stepRule", false));
# T! N( P( i, W r* |$ E3 K! e } catch (Exception e) {
- O B+ v! H' I0 O- U System.err.println ("Exception stepRule: " + e.getMessage ());
% o& w0 v, Q! U% ~* a" T6 ]+ W }% T# U3 E( y1 z& S2 J2 H+ P& G
" V; a, b; f1 M0 p try {
5 O3 v) ]" }5 f, K. m Heatbug proto = (Heatbug) heatbugList.get (0);9 ~ L- t; h4 A
Selector sel =
$ O( Q0 N1 G& _, o! T new Selector (proto.getClass (), "heatbugStep", false);1 V* C+ U6 m; u0 w1 y2 ]) W3 K
actionForEach =1 s' ]( D( a% f
modelActions.createFActionForEachHomogeneous$call
3 y+ q. W8 S! H+ E( ?3 F& t/ ? (heatbugList,
6 \- H/ q6 M" \" {/ n new FCallImpl (this, proto, sel," {6 b- P4 V+ [) {2 V
new FArgumentsImpl (this, sel)));
8 a9 c+ r1 H' d0 b# M$ k0 f } catch (Exception e) {/ g6 ?6 F- z: R% w
e.printStackTrace (System.err);5 t, t6 ~1 [: f5 k
}
- R u/ i) C" O( \ " O; i6 G6 P% Q
syncUpdateOrder ();
& m; Z' x8 W7 \" X: L+ R, a5 Y! z6 K, c4 A+ _/ S( ?6 r
try {" ~$ B: ?5 Q' r. v3 E
modelActions.createActionTo$message
1 j: d2 h+ [$ V$ u0 R8 j" |) ^ (heat, new Selector (heat.getClass (), "updateLattice", false));+ C# F; K7 {5 f8 z
} catch (Exception e) {
0 }% J6 ~; V* u# F$ g& W/ q: Q System.err.println("Exception updateLattice: " + e.getMessage ());
, ~2 P' L2 x7 L n( E+ t }; A" n% b" S9 G' z9 C1 ? i9 z
) @8 y+ M8 y% _
// Then we create a schedule that executes the
: ~0 n' E/ P0 h, B. n4 d // modelActions. modelActions is an ActionGroup, by itself it. L$ @2 m4 w; _1 L
// has no notion of time. In order to have it executed in
. S* H) v& W6 w2 u // time, we create a Schedule that says to use the- Z' ^7 H0 F. V7 v0 B" l- R
// modelActions ActionGroup at particular times. This+ A& H% o% p9 i+ G) U) C; u
// schedule has a repeat interval of 1, it will loop every4 \. p3 L% m/ M, y# k) L
// time step. The action is executed at time 0 relative to
, f( s, e; ~+ a1 e% Y+ } // the beginning of the loop.7 I2 \3 P4 F+ I6 S3 x* S
5 }* `- C Z4 ~( a3 j5 ]3 B // This is a simple schedule, with only one action that is
! {3 M+ w" M: U4 X8 ` // just repeated every time. See jmousetrap for more. t8 \: g: E; q
// complicated schedules.
6 R; ^8 J7 q' ~( L$ k
/ N9 }. J; I: O) E modelSchedule = new ScheduleImpl (getZone (), 1);* I) A3 P% o8 J H1 [
modelSchedule.at$createAction (0, modelActions);9 h8 K/ j4 R4 t$ G
6 \# L7 a0 v, Q8 V" B return this;
: g2 j" d- V% Q8 v v* l } |