HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- n* Y V4 n/ `1 \, Z
* Q. V- I# N% e
public Object buildActions () {
, L: ?0 l" J( v$ v w super.buildActions();
- E: W/ Y8 v6 N+ v * \9 Y2 U/ Z1 c2 K6 R% ], d
// Create the list of simulation actions. We put these in
v3 O6 j; x5 l6 q& j; \5 L% r // an action group, because we want these actions to be
S: K/ q: l! J7 K- x // executed in a specific order, but these steps should' b( e. p- X l* ~" G# b, a
// take no (simulated) time. The M(foo) means "The message2 i. B! e1 Z- F& Y1 T; @% e
// called <foo>". You can send a message To a particular$ K2 F+ k, g; C6 k8 e
// object, or ForEach object in a collection.3 s3 ~9 e, a, j$ a* ?
! L) v# v# c; ~5 {
// Note we update the heatspace in two phases: first run9 \# h8 o; q0 {
// diffusion, then run "updateWorld" to actually enact the
5 b( Z" z/ X3 T; q7 e // changes the heatbugs have made. The ordering here is
& d4 B# h2 x$ m5 x! @- a# u // significant!
' G I' {! D/ e
6 x- d$ I3 x/ J! k8 d+ o: f // Note also, that with the additional
; [# n# `) }3 d5 ?& O( R0 G // `randomizeHeatbugUpdateOrder' Boolean flag we can# x* [* _! @7 C$ f3 v+ s" S) L
// randomize the order in which the bugs actually run
8 {/ S% c: n/ V3 n1 p: w+ W // their step rule. This has the effect of removing any- R! a+ h0 \/ j/ n3 Y5 x9 r6 s" e
// systematic bias in the iteration throught the heatbug
* F) y( @4 T- K // list from timestep to timestep
- P/ n+ {; K) l + s1 H0 r z2 z: l0 B4 W4 n
// By default, all `createActionForEach' modelActions have7 b+ o$ l+ o# O5 j* \# Z0 w# T' R4 }
// a default order of `Sequential', which means that the: E& M0 u! V1 f+ l2 h% R
// order of iteration through the `heatbugList' will be
5 o) w3 z+ C" o) w! s, M // identical (assuming the list order is not changed8 D K9 J# v( s. X/ L5 K
// indirectly by some other process).
. L+ a' R; N1 s3 F Q F . g8 b* l1 N! U% f, Q1 I
modelActions = new ActionGroupImpl (getZone ());
v# w3 _( b" z$ H" T$ \4 w3 Y. V7 x$ j) g R1 |! f1 y
try {: T; p* K3 c* ?4 d+ ^6 Y
modelActions.createActionTo$message
0 S3 s. x: B0 L+ q5 R! H0 f (heat, new Selector (heat.getClass (), "stepRule", false));
* c0 y3 z( z; k1 V, N7 R } catch (Exception e) {1 j: f3 s: I& l8 w2 Y# B
System.err.println ("Exception stepRule: " + e.getMessage ());; Y& q' R) Y' \; I6 r8 }7 e5 d* Z
}
. ]! w1 B; ?1 C5 |, r4 c( A" I0 W' s2 s
try {
- H8 O' F' H$ C8 Z/ r$ C" u* l Heatbug proto = (Heatbug) heatbugList.get (0);: I- K& v. L0 {6 K" b
Selector sel =
" P J. `4 l- G/ | new Selector (proto.getClass (), "heatbugStep", false);
8 e- s( [; l) `# {3 O2 c% o- } actionForEach =
& Y& k5 o3 M3 w- ? modelActions.createFActionForEachHomogeneous$call
4 a; x2 m L& T (heatbugList,
5 V( G! @& h; v1 L1 F new FCallImpl (this, proto, sel,
( m' k3 `5 o" J+ s new FArgumentsImpl (this, sel)));! u1 O+ p/ y& J) v) T# ?
} catch (Exception e) {3 \) z9 Y. ^: Q' F/ M7 e* t
e.printStackTrace (System.err);
. e' L* g5 X1 Z) p" v }
% o6 R( Z# \- H5 { 3 Z Q9 E; f9 |+ r1 w; {+ k" f
syncUpdateOrder ();
0 {( J6 ^4 w. k: x0 j2 a; [) W& ^/ L$ Q* K) L7 E* s
try {
! n" _+ {: Q+ T0 A2 M, N modelActions.createActionTo$message , m' _+ b7 e) Z; O
(heat, new Selector (heat.getClass (), "updateLattice", false));1 x+ {; c5 b1 ^1 g, ]
} catch (Exception e) {# t1 W. ]: d0 w
System.err.println("Exception updateLattice: " + e.getMessage ());
0 T! g) G$ V8 L8 u }! u* m6 N+ s. c8 d- x* D
8 p# h/ D( G( a! }$ x# Z$ B3 W // Then we create a schedule that executes the; }" L/ w- ^# L" ^( r) ~
// modelActions. modelActions is an ActionGroup, by itself it7 z/ y# B; l! k4 @7 L# d( U2 k
// has no notion of time. In order to have it executed in
0 S" A6 s5 J+ ^2 k- z // time, we create a Schedule that says to use the
2 m" |/ I' c1 `( \$ Z) r8 T // modelActions ActionGroup at particular times. This Z# j% t. m! d' @
// schedule has a repeat interval of 1, it will loop every5 o& v& T- G9 w o
// time step. The action is executed at time 0 relative to
# ]) L' y; T2 x9 j. K0 J4 \% T; ? // the beginning of the loop.
! y! T1 `/ k( s7 ~* h
6 G* ?' V( x" P5 w // This is a simple schedule, with only one action that is8 f- n* b' W& }) p( Q% q2 [
// just repeated every time. See jmousetrap for more. l! |9 }3 ]; A1 l5 j( M; m0 J
// complicated schedules.
! l5 {' I8 y' G
B3 v- c3 r( O9 G | modelSchedule = new ScheduleImpl (getZone (), 1);$ v8 q* |( g6 j/ T
modelSchedule.at$createAction (0, modelActions);
* `$ O7 q" i7 r$ U. { . c9 W7 n! n4 K4 o3 B! b( V V
return this;
2 I5 \: d+ v6 m0 l5 p! v J+ B } |