HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! y; [% k- W" {! w1 {" G. m+ E1 e
5 F* k6 k- r5 C/ H# i0 m" [ public Object buildActions () {4 x$ {/ X0 `* V3 o( H/ m
super.buildActions();
; ?" ~3 o J. S% o' j " Z9 a2 D, z) ~1 q, R4 U
// Create the list of simulation actions. We put these in: }! {5 F! w1 E6 }( v! i; q) I
// an action group, because we want these actions to be! Z: k Z) Q) X8 Y/ T
// executed in a specific order, but these steps should( ]7 a8 @7 m, }! H
// take no (simulated) time. The M(foo) means "The message P9 Z* r" s3 k$ h" Q
// called <foo>". You can send a message To a particular ~( @' L2 x& S8 V% H: V
// object, or ForEach object in a collection.
" \$ u# ?. m/ l0 u4 ?" ~: ` + C8 y) W4 d7 ?* P# c& N' i
// Note we update the heatspace in two phases: first run: T, i# Q2 {- M
// diffusion, then run "updateWorld" to actually enact the- K# s" t5 _. Z4 G ~
// changes the heatbugs have made. The ordering here is& n `3 o' q5 U; C% h
// significant!
3 x, x! o# i6 z9 K2 T 1 q3 O6 u; U9 |% x
// Note also, that with the additional) p; A) l' \' j A$ S# i% `
// `randomizeHeatbugUpdateOrder' Boolean flag we can% Q( B% ~: @/ x( N9 M5 Z& I
// randomize the order in which the bugs actually run
6 [- }' K& k% f! d // their step rule. This has the effect of removing any
. Z& Z9 C# i7 C, @9 y // systematic bias in the iteration throught the heatbug
4 j% q" q, F& G) ]& G // list from timestep to timestep8 F1 |& e/ k }" l6 N
8 [) Y# Z$ ~+ d
// By default, all `createActionForEach' modelActions have
9 e8 A1 v6 S3 j0 I" { // a default order of `Sequential', which means that the
4 ^( i' n4 ?+ M6 V // order of iteration through the `heatbugList' will be
' Z! v8 d8 o+ C& |4 f5 f, ` // identical (assuming the list order is not changed6 G* }! K) V. D2 w _
// indirectly by some other process). \# Q5 F I7 I1 N6 G' c( a; a1 t
, A# R7 J( Y1 p
modelActions = new ActionGroupImpl (getZone ());* X* K' M5 _+ Z) f* I ~' s
" [! J4 F& O' o+ v3 o; P# {
try {
% G6 |9 ]! f- x) L& @! D8 ] modelActions.createActionTo$message! S' s' C6 h- x# T4 B* L0 S+ u& _5 N
(heat, new Selector (heat.getClass (), "stepRule", false));
/ ` j& F5 g7 I! \9 X# { } catch (Exception e) {
' [7 p/ V% o+ T System.err.println ("Exception stepRule: " + e.getMessage ());
- _* t9 B5 {+ q! R) t }! p/ Y2 }( x U7 s- E# f
+ N. T' a2 b; K3 Z0 M6 w9 ^
try {4 @! K- a2 X: p/ C0 H- d7 |
Heatbug proto = (Heatbug) heatbugList.get (0);- {, S0 W. j: U' G
Selector sel =
; K- \0 T" F5 Y. d6 C0 { new Selector (proto.getClass (), "heatbugStep", false);" v) J3 l+ I3 `3 p! L: t( v0 l9 Y
actionForEach =0 Y! q4 S( l' @* G& B. G
modelActions.createFActionForEachHomogeneous$call
9 V2 v2 I. C. X8 C (heatbugList,/ v5 {. I! A0 }2 _
new FCallImpl (this, proto, sel,
' N" W; P. G$ h% }3 s. j; C; W new FArgumentsImpl (this, sel)));
4 _7 ?+ @; U4 N/ H: Z) ]) v- p } catch (Exception e) {
) ^0 W4 l! m& W e.printStackTrace (System.err);5 _" m8 p- k& t) L* s
}8 n! D: Y0 X+ w: A8 R; H8 i& h3 T/ r! c
5 t3 _8 E9 k" b* p
syncUpdateOrder ();
( C' A |7 }9 r0 G+ |* A! S4 x! _& x8 O
try {
7 |$ D, g# j1 q' w! V7 x modelActions.createActionTo$message
: g& A2 ]: }' n- L# z7 U3 ? (heat, new Selector (heat.getClass (), "updateLattice", false));$ T' o$ ]+ P6 N) A/ C! l
} catch (Exception e) {
+ C+ W G. l/ p: N System.err.println("Exception updateLattice: " + e.getMessage ());4 E% `) m9 q( d# w7 r
}
: e/ }& e9 T7 l- v" D _
2 B' u% t$ F E) a; `4 | // Then we create a schedule that executes the
: l a. n0 G, N. `. G // modelActions. modelActions is an ActionGroup, by itself it
% T2 Q5 |8 e/ _, a7 A6 k. h // has no notion of time. In order to have it executed in4 L' M! Z: X0 A- U0 b
// time, we create a Schedule that says to use the
! i5 G3 T8 J2 I* Y7 `5 A8 Q // modelActions ActionGroup at particular times. This
; {. P% A& c0 m // schedule has a repeat interval of 1, it will loop every3 H3 e1 ]# O1 K( }7 ]: R
// time step. The action is executed at time 0 relative to
. {; W; ~& K' L // the beginning of the loop.( `( D5 t) |. L$ E1 K1 U
" G8 i& U% }: ]2 S7 u // This is a simple schedule, with only one action that is
* s! I3 |& D/ V/ J // just repeated every time. See jmousetrap for more+ A3 ? ?8 M4 _. ?. {: `
// complicated schedules.3 l! x8 ^1 C$ Z( B& ]
g3 p, X& e5 e) `9 w modelSchedule = new ScheduleImpl (getZone (), 1);
$ u6 V4 ~6 U- W6 A. i modelSchedule.at$createAction (0, modelActions);8 X% d* V& M% C
, d1 R' ]) ]* |6 f
return this;
9 z2 O" n7 i7 I3 r6 G0 b } |