HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! U7 s, b& v+ f' _, _
- o8 m2 B1 g* e! [ |+ v; s8 q6 O: \ public Object buildActions () {
2 ?: I' L2 c c3 a* G super.buildActions();
0 y8 y# B& P" k/ {/ ?4 U0 c ) X/ N' r$ v$ M+ w& j2 j7 |
// Create the list of simulation actions. We put these in, g2 P$ x4 C' W6 u
// an action group, because we want these actions to be* J. I2 f7 ^& \. V8 y- G
// executed in a specific order, but these steps should7 z9 A* ]4 \$ J, ~* o4 b/ u
// take no (simulated) time. The M(foo) means "The message* U3 D; U4 \/ T0 W' y9 f1 q
// called <foo>". You can send a message To a particular
/ g2 f+ g- J. B3 j' T // object, or ForEach object in a collection.
1 q/ p: s! _& K6 H$ P+ V: R
, R" H0 Z0 L# ~7 J; p J( R, S // Note we update the heatspace in two phases: first run* g8 E% U5 o# g& x
// diffusion, then run "updateWorld" to actually enact the
5 c4 a, [( H' D, ^ // changes the heatbugs have made. The ordering here is
# d7 n' _" p. Y6 H& { // significant!
( y0 S" l# I/ l* r8 O
% v5 ^2 v- F) X/ J; h7 F/ } // Note also, that with the additional
4 r% u0 K5 v% ^5 R+ A // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 r2 s2 ~+ F- ~4 j! U% C/ U // randomize the order in which the bugs actually run
6 }1 U( }0 c9 p( z // their step rule. This has the effect of removing any: T; z$ [' b7 L
// systematic bias in the iteration throught the heatbug
/ p) ]: v+ |4 h5 X2 T0 ]7 w // list from timestep to timestep+ N, i" ?. ~( p3 S+ O9 h9 z
! y' W# k/ v* n E h9 f
// By default, all `createActionForEach' modelActions have# H/ X4 R# q9 m: a
// a default order of `Sequential', which means that the
0 \; W( O& T7 a' Z, G* q. w // order of iteration through the `heatbugList' will be
9 V5 F/ Y# z3 f // identical (assuming the list order is not changed: M, h4 c, E9 @( `$ P
// indirectly by some other process).( n' a5 r l! f7 }$ G8 M
6 U, u0 _3 H3 h1 N' W X( r) K
modelActions = new ActionGroupImpl (getZone ());
0 X% r( [$ T7 `2 u2 Y& q* `5 I: q. s8 ^& y
try {1 K+ l7 J$ b( W0 A- M6 V
modelActions.createActionTo$message
& Z8 @0 O2 R# J; Z (heat, new Selector (heat.getClass (), "stepRule", false));8 V1 d2 U1 r' D5 O2 w' T& L
} catch (Exception e) {$ K$ l. U* L* {3 f: m7 b& u
System.err.println ("Exception stepRule: " + e.getMessage ());
: n9 l% A8 V; s0 @* L+ I }( {4 Q$ u# Y& ^* U, B/ L- B
0 a1 k6 _* O) U
try {
, d. {, K- }& {+ A3 h; x" X1 F. h Heatbug proto = (Heatbug) heatbugList.get (0);4 q/ S! {7 y! R2 K$ M" Z
Selector sel = ; x# ]; V4 P$ K* s+ I) D4 A$ n
new Selector (proto.getClass (), "heatbugStep", false);% y" J; n5 F9 j' R# F- r5 F
actionForEach =
1 R, w) J1 N8 h, c7 G( G9 \ ` modelActions.createFActionForEachHomogeneous$call4 u! b7 ]! x( L
(heatbugList,) @4 W, s2 ^3 a' u( V
new FCallImpl (this, proto, sel,
# }, T& d! d2 { new FArgumentsImpl (this, sel)));3 r! w& W2 M) t" w6 J
} catch (Exception e) {
/ W% \/ K9 _2 d1 q0 t e.printStackTrace (System.err);
$ P. M1 x6 G% a7 {4 b3 q0 F }
0 g6 a2 S$ q; V) l. Z
1 S, x( h/ v8 J" q6 h: G0 m! T syncUpdateOrder (); }& U1 B2 w1 X$ I
6 l1 h I1 j" m5 L( C try {
; m! W- y1 {; } a4 W5 v- u modelActions.createActionTo$message
6 }% p' x* c- Y! D (heat, new Selector (heat.getClass (), "updateLattice", false));. c4 E. w/ v& Z; U, _
} catch (Exception e) {
3 o6 Q4 \* U9 f) D1 P System.err.println("Exception updateLattice: " + e.getMessage ());
. d, h) M c5 p: |9 X+ c5 A- P- z* s }- Q1 }3 k& M% @2 V* {* j, x; a6 M
" A0 T* N6 O, @; A2 a
// Then we create a schedule that executes the
: |( H) W. X, U // modelActions. modelActions is an ActionGroup, by itself it
( g2 j# A5 N$ H // has no notion of time. In order to have it executed in
; O" F" U0 r, c' _9 p4 I7 h/ } // time, we create a Schedule that says to use the
E) d' O8 @$ A& O9 b" o3 ] // modelActions ActionGroup at particular times. This
" S- ^2 V3 y2 h/ s* B5 G // schedule has a repeat interval of 1, it will loop every
/ |& B* Y3 V9 ? y, w% n( X0 X; } // time step. The action is executed at time 0 relative to) g A! V4 Q5 R2 O
// the beginning of the loop.- y: U, i) i8 h5 B) f
* _8 u. c1 j7 m; D; ?( L8 F6 q6 \0 N
// This is a simple schedule, with only one action that is
, t/ |1 W% q- |* I5 D // just repeated every time. See jmousetrap for more
4 x3 u' i. p' o1 G% n // complicated schedules./ p& h; F) K% c* J) w$ \- m8 E
; H" K: |. `3 a. ?1 u* }1 F modelSchedule = new ScheduleImpl (getZone (), 1);$ }8 a$ T5 d: q" m. _
modelSchedule.at$createAction (0, modelActions);
& A" J& H: N8 Z6 k( t
) l+ I8 Z! @4 c9 K* ]. p- c return this;
) G6 P/ L% p$ B! C' ~. K/ T } |