HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 `4 l [* D; O i! w; _; p
% h# f! P* X8 B1 D0 Q public Object buildActions () {
3 u; o }2 F5 [; A) n, | super.buildActions();
( E, z/ I7 {3 U0 y; B1 n9 c % E6 j* J) P9 ?- X. Y( T
// Create the list of simulation actions. We put these in
7 A7 k; H& L4 p6 L' k( T // an action group, because we want these actions to be+ c) v. P% y# R# I: I, H& @
// executed in a specific order, but these steps should
* g6 j$ l8 T4 R# M$ X( n8 x // take no (simulated) time. The M(foo) means "The message
. w# p5 K; p& R // called <foo>". You can send a message To a particular0 p2 B8 s" T) i3 A# t2 F- k
// object, or ForEach object in a collection.' _% p7 T+ ~; K; I3 w1 u: k
$ d4 |- n$ R! F2 z3 A
// Note we update the heatspace in two phases: first run& J' Y* b' k0 C' H% M
// diffusion, then run "updateWorld" to actually enact the
8 P: f- }2 m2 z1 S8 z // changes the heatbugs have made. The ordering here is9 c7 `" O) G. V+ |& }1 X
// significant!
9 r. T v( Z) v4 P: K5 u( `$ T
6 `" ]. B- d- m. b/ y1 [ // Note also, that with the additional
0 H9 G7 p1 w. w, w) B5 G // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 {5 x; ~0 M0 W$ b0 P W5 q7 D: Q // randomize the order in which the bugs actually run
1 w7 \* M% {0 {4 e; C$ n& H // their step rule. This has the effect of removing any! a" S9 A4 `* o6 E
// systematic bias in the iteration throught the heatbug3 }2 J; z( X" K. O2 N1 L
// list from timestep to timestep
/ d u" x+ c% R / w8 N% I0 ]) h+ q
// By default, all `createActionForEach' modelActions have
. t- [% ?2 u: V7 T // a default order of `Sequential', which means that the
6 M3 h# i0 r2 @6 m- d- v // order of iteration through the `heatbugList' will be% V$ i8 n4 h2 p/ t7 W' W
// identical (assuming the list order is not changed
1 Q, `4 ]4 `% q* S; ] // indirectly by some other process).' D' R. _2 X6 V5 @/ o) {/ k
' a8 k% q1 `$ y7 U
modelActions = new ActionGroupImpl (getZone ());
: s3 T6 E; i$ C2 P; A$ `0 O |4 p& y) o. r4 H/ U1 O2 J5 E- z+ x0 N- \
try {' g" J0 ]/ {* b1 ?. A _! _3 A
modelActions.createActionTo$message
4 f% P5 B+ K6 U (heat, new Selector (heat.getClass (), "stepRule", false));5 i% M" }3 I9 F0 c4 v# k7 M- E
} catch (Exception e) {
/ ^3 Y$ \% C. E+ p/ Y0 k3 S System.err.println ("Exception stepRule: " + e.getMessage ());$ B% W# F8 E& a( g4 ^: j1 F2 c" U
}% a6 t1 d% ~' u& ?
8 w1 L& o3 D/ R1 D# c/ g( J try {
, y% c' N% c- R) j/ Y' p Heatbug proto = (Heatbug) heatbugList.get (0);
f3 C7 P& u( @) c Selector sel = ! c0 @% f. J7 d: l* o
new Selector (proto.getClass (), "heatbugStep", false);
0 R& ~; @! Z$ o8 A! ^) l1 M actionForEach =# J* k- \( g: i! ?" M M/ c
modelActions.createFActionForEachHomogeneous$call
5 x5 O g. h8 j3 w (heatbugList,
* H9 x8 Z( U' H/ l, q+ o new FCallImpl (this, proto, sel,8 |7 T! w9 s( k. w+ _' K: p3 s- x
new FArgumentsImpl (this, sel)));+ h0 C2 Y3 f9 T% v
} catch (Exception e) {
v) p8 `$ w; A- P7 L; W e.printStackTrace (System.err);& v" F7 h, w: }7 n, {4 h: n$ a9 ~
}
* g; N+ Q$ [' j D 3 r. n; k% n3 ? p6 R6 w
syncUpdateOrder ();: ~8 _/ w; s7 b) U: Y7 x$ d9 H
& S3 q7 J; F4 \! p! _3 \
try {. R2 }1 q6 B8 R
modelActions.createActionTo$message
4 y; W+ ~7 f" u2 k$ o# x (heat, new Selector (heat.getClass (), "updateLattice", false));
$ r: Z. I6 E5 l } catch (Exception e) {3 R, _" G! \/ H/ B, B2 ]! t1 H2 z7 ?
System.err.println("Exception updateLattice: " + e.getMessage ());. F* a2 l: J2 p" f) q, w. T
}) q8 d- R/ ~, W3 b. p8 M9 x
. ^ c2 M/ ^% G% w% g
// Then we create a schedule that executes the( x X8 I6 U& p$ l% H1 }- L
// modelActions. modelActions is an ActionGroup, by itself it U7 E/ }0 f+ X8 T" z1 g5 a
// has no notion of time. In order to have it executed in
F% Z+ M1 n/ J0 ]) i // time, we create a Schedule that says to use the* K* G/ N+ A6 I4 G- C3 |1 j* i
// modelActions ActionGroup at particular times. This( s& Y7 H: @. M8 H; | T
// schedule has a repeat interval of 1, it will loop every
: v! o$ E$ c$ q7 b // time step. The action is executed at time 0 relative to
% m* f2 ^+ H) h+ n // the beginning of the loop.
0 S" R/ {! {3 ^- X T: w, N& f3 l& ^4 o ^
// This is a simple schedule, with only one action that is7 D u' w, i3 m! ]9 F4 H5 x
// just repeated every time. See jmousetrap for more
3 p7 i7 f8 I, v! s // complicated schedules.' E P* B% l; m% k! C
9 ^, d1 t' b; v8 b7 Z0 f1 K) I modelSchedule = new ScheduleImpl (getZone (), 1);* O/ f9 [8 s+ } Y- D+ I
modelSchedule.at$createAction (0, modelActions);
& j! I8 T0 D: v' s
+ z9 [# m4 f" T5 W$ d8 j1 t) f" @& p8 l return this;
/ ?- W2 J1 }1 @ } |