HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 |. [) ]! D1 Q. N7 I9 D# R' f% d# L
public Object buildActions () {
6 V' v7 F* Z# \ super.buildActions();
, i# ~+ d: t4 R4 n$ O i& b# M8 z' m. q$ W1 p
// Create the list of simulation actions. We put these in+ W3 h5 O. f. e3 ]" S
// an action group, because we want these actions to be
# \7 m0 P9 ] s* h: `" d // executed in a specific order, but these steps should
L+ c! \+ L4 v$ \% b8 F* x0 D2 w // take no (simulated) time. The M(foo) means "The message
! M; m$ t( ^$ P- I4 F0 k0 _! F // called <foo>". You can send a message To a particular8 d( P Q+ B8 f. B" u
// object, or ForEach object in a collection.5 }0 s# V; q" l P3 g
1 V- |7 T# E% p* U+ k1 ]4 E C // Note we update the heatspace in two phases: first run/ g& M& H5 ~) N1 }) D
// diffusion, then run "updateWorld" to actually enact the
4 Y+ h; C( k+ r8 ^ // changes the heatbugs have made. The ordering here is. c" x/ Z8 [ T- q2 ?8 V
// significant!
: o9 T' h' }/ Y5 T% z7 B 8 G ~% I8 Y+ M% }, [
// Note also, that with the additional
) { W' @, b0 S // `randomizeHeatbugUpdateOrder' Boolean flag we can
K0 l2 c( J) b // randomize the order in which the bugs actually run
, b$ Z% l3 p% _- x$ `4 y // their step rule. This has the effect of removing any& d5 u! q i& O
// systematic bias in the iteration throught the heatbug! u0 ?) @* r% L& Z
// list from timestep to timestep: l% V* U9 P% V7 I% b* Z
+ D: i+ _2 l2 |+ h- z/ q6 C# ]8 k0 W
// By default, all `createActionForEach' modelActions have
& a1 }' B$ y7 w/ b // a default order of `Sequential', which means that the
* C/ a0 G( E: g // order of iteration through the `heatbugList' will be
' U+ G$ q6 L; a K! d1 F // identical (assuming the list order is not changed
: ~- Z: @6 l( Y2 ? // indirectly by some other process).
% K! z, K$ l& l7 ` 3 ]+ I; ?6 O. T+ ^ K0 Q8 I
modelActions = new ActionGroupImpl (getZone ());9 w) X5 B, t0 R s0 s3 A1 k
, _9 S- i4 C* j5 i. D$ i try {
! [! E% [" J3 Z7 \2 v/ k modelActions.createActionTo$message
' g. ]2 h) l9 V6 V2 j0 B6 [ (heat, new Selector (heat.getClass (), "stepRule", false));" w3 ?/ _- _. p E9 Z) L3 t) @
} catch (Exception e) {
- s8 x3 H i. O# E) e9 g System.err.println ("Exception stepRule: " + e.getMessage ());' E& j$ M4 U B) r# N r! a, a+ J
}
. Z- o" k. }: i0 _7 T% u: U
2 b# j/ Q$ F* s. W; w. ^ try {
' ~) w6 Z1 V$ `7 v+ j- D; e Heatbug proto = (Heatbug) heatbugList.get (0);
+ ^2 v2 d1 n! Z1 U Selector sel = - g0 G3 ?+ o1 T& L& S9 P# N( _& q
new Selector (proto.getClass (), "heatbugStep", false);
# X: H* V; g* I c& m9 F actionForEach =
+ K, L' _' _7 l, E( A' R modelActions.createFActionForEachHomogeneous$call6 E+ b; x' a J1 ~% w
(heatbugList,% g+ @: G0 s/ P- g
new FCallImpl (this, proto, sel,( f. q( S' h8 R
new FArgumentsImpl (this, sel)));7 ~1 x/ O$ @ A3 b# t( m
} catch (Exception e) {
. w' B5 i, P0 t( ]' A3 J% e e.printStackTrace (System.err);
: y2 s Z1 E/ n/ J5 a }( R7 \; H; k: @2 \& H8 F; F# C8 P
6 H+ J+ U2 D/ u! ^5 o
syncUpdateOrder ();
# }3 f' N: \" J6 F' ]* n( U) C) d% ] M3 U# p' x2 z
try {) j, g; L! O! s% C7 c) q2 b6 u9 z4 k
modelActions.createActionTo$message
) g4 s# |6 T' E* C; r5 m2 Q* W: N (heat, new Selector (heat.getClass (), "updateLattice", false));
' @, V9 k: z% d# c } catch (Exception e) {
$ C# ?; X* A! t2 V System.err.println("Exception updateLattice: " + e.getMessage ());
- X/ R' n' d- G$ q3 p9 c) U: o }
0 o/ f1 T1 O$ L$ p+ W
' @) M. T- ? O' r5 p+ G // Then we create a schedule that executes the
( h: p8 r5 F) _2 N8 m } // modelActions. modelActions is an ActionGroup, by itself it0 I4 E4 {% [; }) n/ ~+ O
// has no notion of time. In order to have it executed in) S/ K! I- `. L; w
// time, we create a Schedule that says to use the5 @- `* S- p7 X% A
// modelActions ActionGroup at particular times. This0 V5 A6 e5 O4 u/ @3 r
// schedule has a repeat interval of 1, it will loop every0 ]: B2 A- l1 t/ a
// time step. The action is executed at time 0 relative to
4 q f9 o7 u0 Z // the beginning of the loop.
3 c* o7 [! {* b: g3 j! m; x4 Y+ m$ q% k+ @8 J% ?2 l9 r" Z
// This is a simple schedule, with only one action that is4 K& M8 ^" x1 y I! F" o3 v
// just repeated every time. See jmousetrap for more% `- R; z0 ~2 l- h8 v
// complicated schedules.2 B+ |% L- w! K! Z
( i' _: }* a; v1 i modelSchedule = new ScheduleImpl (getZone (), 1);
# z, V; l E' { O* e modelSchedule.at$createAction (0, modelActions);
9 O9 ]1 B# N( {
1 ?; j! ?" ]" Q8 D# E return this;1 @1 n# ]; y+ i r( l
} |