HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& Q: L" O! @5 s: X( M; l# U( _
; @3 `2 I/ q% u3 b# p; t/ ^4 a
public Object buildActions () {/ ~0 n; F% k1 d4 r# X
super.buildActions();
! N5 a' D6 t7 ^2 a! z; B" X
, M7 R6 Y" V+ Y9 `, ` X2 C1 W1 a. C // Create the list of simulation actions. We put these in ^9 y3 c& y* |4 P
// an action group, because we want these actions to be1 D1 c* W( ^! G7 S8 C$ o4 G
// executed in a specific order, but these steps should# J3 i) s, o) z5 M0 S; [
// take no (simulated) time. The M(foo) means "The message. ]4 n1 @ m- _3 d3 \
// called <foo>". You can send a message To a particular% q4 ?, s, `' Q# Z, H% G" t+ ~& A
// object, or ForEach object in a collection.
5 s; g$ Q* d8 X! A. E) |4 H( Q, w7 W ) w1 T( E+ |$ x6 m- A
// Note we update the heatspace in two phases: first run! z) C. Q# e+ W# F' H7 V9 Z' N/ f; P
// diffusion, then run "updateWorld" to actually enact the) z. |0 j8 N$ g! M% W
// changes the heatbugs have made. The ordering here is
; }$ g- X0 H+ g! e( I, E" {8 ^ // significant!
: w9 X6 @" L3 i' l' ` * U1 N" R) x" V n4 K+ m7 m
// Note also, that with the additional( r, U& O" f2 N- Y. \* H
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 g" ~. S+ }( Q: y, `
// randomize the order in which the bugs actually run* B/ S! D6 v1 [1 L! z9 }$ y) U* G
// their step rule. This has the effect of removing any2 y( l) H( s8 x( h4 E% U
// systematic bias in the iteration throught the heatbug
" d. `. ]! C0 z% @: x // list from timestep to timestep
" n+ e3 t3 S7 I0 q' _2 Y u" q/ L
7 Z8 Q' j- q0 x // By default, all `createActionForEach' modelActions have S1 S% ~8 Y5 Z& C& S9 q
// a default order of `Sequential', which means that the4 g; h! |# }) l# |. e
// order of iteration through the `heatbugList' will be" n6 R) ]; L* t, L9 L
// identical (assuming the list order is not changed% J8 o/ y1 r$ R
// indirectly by some other process).
5 f1 h) `0 s( f9 `
! X1 G: V6 w3 S9 n% F3 s; |9 S modelActions = new ActionGroupImpl (getZone ());; b5 V, d3 h7 y, \6 `
: M L* e% y r) W. k4 n% ]- C
try {
0 ^- c- X( E7 e/ a modelActions.createActionTo$message T5 b" `+ T7 B% F: o! |
(heat, new Selector (heat.getClass (), "stepRule", false));
% @/ z$ `" |* ~) [( ?$ P5 |7 | } catch (Exception e) {
$ P' `6 ~: e* C1 z System.err.println ("Exception stepRule: " + e.getMessage ());
0 L3 R; l5 Q9 l0 S+ b8 g- o }. t& \7 } g- W. E; K. [2 J
% K5 V/ \$ c& a' t4 a5 T* ~( p try {4 a0 ^+ T' r9 x! a
Heatbug proto = (Heatbug) heatbugList.get (0);6 ^/ A% G0 s* T7 ?2 v/ x
Selector sel = 4 ^' X. i7 f" S2 h2 ?
new Selector (proto.getClass (), "heatbugStep", false);
6 q( _3 B, x, d S actionForEach =* O {3 M6 Z" R, f1 B
modelActions.createFActionForEachHomogeneous$call: H t0 s" k$ a5 T6 r% b% n
(heatbugList,4 v0 b5 I& a0 {' X/ D- I( ~! T
new FCallImpl (this, proto, sel,
3 `9 m+ ^7 v' }, q3 m- x( I new FArgumentsImpl (this, sel)));
f3 v. w9 z4 x; a7 n; n0 y7 ~$ s } catch (Exception e) {
5 y8 Q( a# w- f* V5 o: G5 G+ t e.printStackTrace (System.err);
- f4 Z. P5 |) n9 U }5 v! b+ P: q9 v9 J
. w; y* V' I9 x) L; {" G syncUpdateOrder ();
/ M) x$ E9 c& @* x1 z
' _8 K7 V; O: U3 W6 i0 ` try {
& n2 ~ y- G& y i; j& Q! y* L- j modelActions.createActionTo$message # n6 G7 K8 o2 X7 ~8 y7 |6 E8 t4 ]
(heat, new Selector (heat.getClass (), "updateLattice", false));( c7 f' u5 b! i( m3 A( ?4 K* `; C% N2 v
} catch (Exception e) {
+ O B+ F7 G5 ~6 d Y4 l* J System.err.println("Exception updateLattice: " + e.getMessage ());
8 c8 T0 P! r- H, ]7 V0 ^1 m' d8 R' z }# N3 V2 z2 f. k! F
7 d) R2 k; {* T% j9 z P, S // Then we create a schedule that executes the
, \) N1 e" U7 e; I // modelActions. modelActions is an ActionGroup, by itself it! W5 i; Y$ E: a6 L! K) w P. X5 T# P
// has no notion of time. In order to have it executed in
E% N/ r' z, @+ J // time, we create a Schedule that says to use the1 `8 A0 I& U/ p6 E1 ]
// modelActions ActionGroup at particular times. This
$ s$ ]5 K) r8 f! [( t% n( [ // schedule has a repeat interval of 1, it will loop every, e8 c- Y/ C2 k# Q. c- x
// time step. The action is executed at time 0 relative to; Z" Z) u1 d" q1 W+ L/ n$ L" N
// the beginning of the loop.' O$ z+ E1 Z! B
0 m, Z$ E$ l9 f: h2 O" T9 ^ // This is a simple schedule, with only one action that is7 \2 a. }& v, }& X
// just repeated every time. See jmousetrap for more
! B1 V h! z& L! r // complicated schedules.
/ O7 J, d& n' M. w2 o/ f g' C ! U5 o' o3 d& M" a: W
modelSchedule = new ScheduleImpl (getZone (), 1);& S3 ^& X1 s. i O) l7 G! X
modelSchedule.at$createAction (0, modelActions);
6 _( E: h7 s4 Q/ J 3 ]* U) ?% {" b" g& d
return this;
$ x3 _' }( p+ K0 y9 K. U) a } |