HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" x$ J/ W% Y& L& p K3 s/ r) M; ?& Z: {
public Object buildActions () {0 n* ?: b$ n' g* I3 {7 \
super.buildActions(); b0 c- D0 R3 N& R; Q) _, F
6 W8 {* p4 i0 M I6 p6 H) e9 _# t
// Create the list of simulation actions. We put these in
v. o9 G# p/ L // an action group, because we want these actions to be
^: S" U2 w9 n5 h6 n' f // executed in a specific order, but these steps should
* o! R5 q2 N f // take no (simulated) time. The M(foo) means "The message4 t, g6 a7 f! ^4 S; m
// called <foo>". You can send a message To a particular
% O) P" J. Q$ c // object, or ForEach object in a collection.2 N8 n/ Y% D: m8 `, O
% x" b6 k. O" n // Note we update the heatspace in two phases: first run2 c( o! s& R1 [
// diffusion, then run "updateWorld" to actually enact the+ h% l" k/ t' m6 A. I5 O
// changes the heatbugs have made. The ordering here is
" i3 y' ^* Y$ N- D, [ // significant!
% i4 L f: x. ]
1 ?; C4 O- ^+ N // Note also, that with the additional
* e& b) I/ F, b) \, M$ B$ A // `randomizeHeatbugUpdateOrder' Boolean flag we can
; v) t5 j7 a R% D) ] // randomize the order in which the bugs actually run7 V$ `- K: X, k2 P P2 T9 j
// their step rule. This has the effect of removing any* X+ b! y, Q; D
// systematic bias in the iteration throught the heatbug3 Z" s- X4 g1 j$ A
// list from timestep to timestep
' P/ u6 \3 w7 f, V% ` d2 t$ ]6 x: J
// By default, all `createActionForEach' modelActions have
& V7 U( b" O/ ~9 N$ l3 e // a default order of `Sequential', which means that the
3 l p% p6 x' l* K/ ^' L# | // order of iteration through the `heatbugList' will be
$ X$ y) B" C0 A. \# L' W // identical (assuming the list order is not changed! F. @& Z |9 D% s
// indirectly by some other process)./ N/ P4 d' h3 J
, P& T2 A* e/ U7 h" j/ Q: b modelActions = new ActionGroupImpl (getZone ());
# | `+ w$ R5 u+ I( U, I
5 G- G5 \$ t# f- L try {* d7 g; c( k5 }5 E( d: C
modelActions.createActionTo$message0 z& V# b0 H, \4 i" v2 W
(heat, new Selector (heat.getClass (), "stepRule", false));
' M' E1 ^- r9 C$ F( j } catch (Exception e) {
/ `" q! g1 d* q1 g2 L System.err.println ("Exception stepRule: " + e.getMessage ());6 O. k9 ~2 ~. t5 z$ P/ D. ^
}
' F+ U) I. \) k. H6 d4 K! K& S/ O3 W$ j3 l3 A w8 g, \. S0 F6 }5 f$ o
try {" @) W; x3 I8 x; l
Heatbug proto = (Heatbug) heatbugList.get (0);
$ L8 ?0 R6 C, f' n Selector sel =
1 A2 \& x' Z( b+ I new Selector (proto.getClass (), "heatbugStep", false);$ I% n ?4 v$ V. u3 S5 _8 z
actionForEach =
# c u& Q2 c# c, L. ~: y modelActions.createFActionForEachHomogeneous$call4 M$ O0 q; R) W0 ?1 S s( [
(heatbugList,
; j5 R1 Y& b$ k! D( g new FCallImpl (this, proto, sel, G7 m/ C5 t) y8 n; Y! X) |# C
new FArgumentsImpl (this, sel)));( V$ x) ~4 p l) Y. s
} catch (Exception e) {) D% B5 n# F6 h" G r: N0 E Y6 f
e.printStackTrace (System.err);% M# W& n3 C3 ]% ]$ ~
}
8 v5 J% u, r/ E) L3 {0 t+ ~
$ S# J4 q; e; C3 Q syncUpdateOrder ();
7 R6 N2 ]9 |, O! c) [! |% V% a
try {
- K* z, s& B6 k- m" g; c6 x modelActions.createActionTo$message 3 w! I# A! h" D( @9 P
(heat, new Selector (heat.getClass (), "updateLattice", false));+ `) P) i8 R/ l2 t2 I% j
} catch (Exception e) {
+ }0 N( A: [0 T8 }3 @- a System.err.println("Exception updateLattice: " + e.getMessage ());
. |8 `0 N- s3 D2 J3 B3 g }$ i) b- @* M ^% P8 B% p
$ ], |3 K0 a- P/ f, M
// Then we create a schedule that executes the9 q; l% o; _0 @& `" H' d
// modelActions. modelActions is an ActionGroup, by itself it- h4 M6 k3 w5 y* M4 l Z, a
// has no notion of time. In order to have it executed in
; H+ q$ |* a; y' F2 | // time, we create a Schedule that says to use the
5 J" R) J- y: X // modelActions ActionGroup at particular times. This
m8 n4 F( T" T* u! @$ c4 C // schedule has a repeat interval of 1, it will loop every
! J% A% Z3 t" J6 L // time step. The action is executed at time 0 relative to
! d0 P+ p% Z& d- B# k0 e // the beginning of the loop.
. @' e# x( m. y( K. d: d8 q) ]7 ~+ s
// This is a simple schedule, with only one action that is. E7 H! e4 `! J% g& |
// just repeated every time. See jmousetrap for more6 Q. q4 B' v9 E8 k
// complicated schedules./ E* k5 q V7 {# Q
( {! l5 x) ]; s( K3 u3 w modelSchedule = new ScheduleImpl (getZone (), 1);% }8 j! Y/ n. d) U( F
modelSchedule.at$createAction (0, modelActions);& g( d# R! E b' e; {- m
' W; z$ g" I/ }- Q' Q4 ^- ]5 k. h
return this;
; }' P4 _% S7 r/ {, j0 w } |