HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# S, ~6 {4 i6 J8 o) {8 V
4 Q2 v: C. B$ `$ o0 ` public Object buildActions () {0 Z( o, h( `/ J5 q& d* }* i. g
super.buildActions();
9 s5 n: d+ N5 H D% r ' R' V6 W, B8 p$ K$ ~6 \( S
// Create the list of simulation actions. We put these in
% p2 ~4 b9 z1 Y$ l // an action group, because we want these actions to be
9 @* U# ]2 ^: o3 P5 f! H0 w // executed in a specific order, but these steps should- v9 ]1 S6 d- R. J+ A
// take no (simulated) time. The M(foo) means "The message& ~3 B" o. m" d6 c& C* I
// called <foo>". You can send a message To a particular3 Z2 r# k$ E$ o8 A
// object, or ForEach object in a collection.
Q! `% i0 F; ^% Q0 o) |
6 q% C; [- t9 v6 f3 P! ` // Note we update the heatspace in two phases: first run
/ A, L" ^3 \1 ?0 ]& ~; M/ L // diffusion, then run "updateWorld" to actually enact the
* \' R0 D$ f$ V // changes the heatbugs have made. The ordering here is: G! y- F0 O2 W" Z
// significant!
1 u. z9 C' @5 E! F0 h
) R/ J/ h, I# J6 ~% R // Note also, that with the additional3 O: H" ?& g. k U! R
// `randomizeHeatbugUpdateOrder' Boolean flag we can E H+ H5 W. L- Y9 W" w" d
// randomize the order in which the bugs actually run6 \. @/ s. Q" m0 y; v& Q
// their step rule. This has the effect of removing any
/ q5 E) T1 o% U8 a, Q // systematic bias in the iteration throught the heatbug. Q+ }0 v$ l: o2 f, T) A
// list from timestep to timestep
}! p2 [1 k7 H' O
# ?0 F6 W) ^8 P' P8 R7 { // By default, all `createActionForEach' modelActions have
9 v9 } ]% E, J2 B, x; @ // a default order of `Sequential', which means that the2 v3 l5 W) B' J4 x$ j4 z# a* |
// order of iteration through the `heatbugList' will be
% g0 c5 g" @: v // identical (assuming the list order is not changed
+ C' \4 E( t/ w; x& I% h // indirectly by some other process).
' G$ R- O, M! H$ F, S
" }. |) \& `4 x2 J/ u0 }2 D) x& j1 }5 c modelActions = new ActionGroupImpl (getZone ());
4 e# u# V1 x# N( N t; b3 @/ {+ l1 [0 v5 y/ F
try {: A1 \6 ?- U3 x5 Z3 Z8 C
modelActions.createActionTo$message" d# i, [/ \' }6 @+ H x
(heat, new Selector (heat.getClass (), "stepRule", false));
. v* h# V, H# L" w: V: R9 X } catch (Exception e) {8 c' x6 x4 {: S% o9 @4 w
System.err.println ("Exception stepRule: " + e.getMessage ());
1 S. \. _9 q" @. f1 I( S6 o } W! ?! Q: @# l" j8 z e
: r- X' F# i1 j7 O9 `7 G try {
/ @. K+ A( M$ J' o0 O- c Heatbug proto = (Heatbug) heatbugList.get (0);
6 M* F$ I. g0 j Selector sel = # `+ u2 ^8 F# I0 a- R" [) m
new Selector (proto.getClass (), "heatbugStep", false);8 r4 d- R$ F* ~ E+ h" k+ G$ d
actionForEach =
7 ^" Y. {2 h- H4 G modelActions.createFActionForEachHomogeneous$call
$ V( W0 [2 m( x (heatbugList,! ]& V( G7 E1 G E, r3 X
new FCallImpl (this, proto, sel,
0 r) z0 N7 o. z! K. _0 w new FArgumentsImpl (this, sel)));
; R, A8 f, h! g) n$ y! V$ U } catch (Exception e) {
+ U7 N& o* H; P! h- J7 |5 X e.printStackTrace (System.err);) @2 Q9 o5 l. W3 H5 g3 \
}
* ]6 Q1 d3 o" Q
7 a R" I5 C9 Y# G L9 D syncUpdateOrder ();, f/ S4 e1 J# m+ l' r$ ~
3 V% H/ ]9 X! ^5 P
try {
5 j1 D$ A1 o m! F2 `8 g modelActions.createActionTo$message 5 ?" i! V, r1 e, H4 M/ K
(heat, new Selector (heat.getClass (), "updateLattice", false));; P7 `% D8 c; q
} catch (Exception e) {
- J( _4 W8 l9 Q& P! q" s6 V" S System.err.println("Exception updateLattice: " + e.getMessage ());$ m5 M1 f. T W5 F" b
}
5 F$ f- a' Y0 P3 ? $ y0 M! R+ S# @6 x4 e5 c7 D3 a
// Then we create a schedule that executes the
0 p* E# ?5 J# i2 Z // modelActions. modelActions is an ActionGroup, by itself it
& Q3 g' R' u& M9 D% Q- o // has no notion of time. In order to have it executed in0 s5 P( e! v3 G; D9 f4 A# A
// time, we create a Schedule that says to use the
1 H0 _! C8 j3 O. C* m // modelActions ActionGroup at particular times. This
% z0 L9 y3 H# a- n4 D& V/ n0 \ // schedule has a repeat interval of 1, it will loop every
2 `8 N4 T/ x0 _ // time step. The action is executed at time 0 relative to
+ E P) x* {4 w; A // the beginning of the loop.
- X9 H! @' G; p4 @" I. z6 Y
, `" ~/ T1 N7 D( X6 D& u/ d // This is a simple schedule, with only one action that is8 F% |7 r- [7 Q9 P8 G0 p/ T
// just repeated every time. See jmousetrap for more6 d) l$ z- t+ l1 v$ n" E0 r
// complicated schedules.
+ C8 W3 s# r# V7 I2 m " I7 }, J) c- k& p1 e5 ^
modelSchedule = new ScheduleImpl (getZone (), 1);
! Q1 S" g; e$ t% ^: r modelSchedule.at$createAction (0, modelActions);( A6 o9 `" I V" Z2 @; a! p* G2 t; I
7 b0 S- A) Y7 Q
return this;$ o& R' ?! e+ y L+ y
} |