HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; [9 a: m8 K8 y- p y! Y; o; e; l: V
# Y" N; g V6 M" W public Object buildActions () {% F _) e/ h5 @
super.buildActions();
3 X8 T, z8 O) y2 M1 W K: {9 i$ p1 Q( g H, y
// Create the list of simulation actions. We put these in
3 U# H9 d4 B$ \, u7 O, C! I: J" K% x // an action group, because we want these actions to be
, [3 h5 r& p. I* m& L- q' v' @1 t // executed in a specific order, but these steps should9 l7 @5 h; }7 J' s P' B
// take no (simulated) time. The M(foo) means "The message8 r6 j8 H; B& L& c) v& x0 y
// called <foo>". You can send a message To a particular: s: g+ r" A6 x7 s' S8 N/ Q1 [' c# [
// object, or ForEach object in a collection.
! [ v) F% N/ E& h 8 f. J: H! Z8 f- D
// Note we update the heatspace in two phases: first run2 Y, y7 a. D# z' K) ?' V' u
// diffusion, then run "updateWorld" to actually enact the
$ a7 g# u2 d1 m: q; k- M( v // changes the heatbugs have made. The ordering here is4 a2 a) L2 i/ E( a
// significant!! l# V6 m0 {; [+ I4 F- H
1 j! x7 k* y: h! c" c
// Note also, that with the additional
' C: J* e3 x! \! \6 T- a) `3 K: [+ m // `randomizeHeatbugUpdateOrder' Boolean flag we can
( m/ ^5 g0 \& `% F // randomize the order in which the bugs actually run
" J# ]* H& S, A0 B, Z, k0 K. P // their step rule. This has the effect of removing any, U) i! t- k1 w. U B% j
// systematic bias in the iteration throught the heatbug0 a9 k+ A4 x$ z( h0 s8 ]9 X
// list from timestep to timestep
$ }/ U- ?. L% L" C1 r " F; a& c6 [1 j+ i- L7 `/ ]
// By default, all `createActionForEach' modelActions have% p8 v0 I ~) K4 l; o
// a default order of `Sequential', which means that the! d7 f( t# |# P+ ]6 O& ^* C
// order of iteration through the `heatbugList' will be
@7 g8 {) Y( B: |) G# `% ] // identical (assuming the list order is not changed! C( Z' `* X D& O
// indirectly by some other process).: }- m+ \$ ~5 j# T
- C- ]2 v! \6 H( Q
modelActions = new ActionGroupImpl (getZone ());6 G2 [3 P9 l) v0 k5 F6 k; Y' c
5 ~5 \! j, a; p, _
try {
% O8 Z g5 m. C0 ~/ `# \8 _ modelActions.createActionTo$message
6 ~0 J4 B5 I2 j% T8 W1 k (heat, new Selector (heat.getClass (), "stepRule", false));, v% H8 m" ^5 _4 I
} catch (Exception e) {/ b3 C# O1 r3 a
System.err.println ("Exception stepRule: " + e.getMessage ());9 }! n; @, I4 Q# [5 {% {- r
}9 p/ p4 l" ^! E
5 m- @2 n" R9 B% A; \ try {
, p P2 W0 C+ [ q Heatbug proto = (Heatbug) heatbugList.get (0);
. o, _ B" M8 f b Selector sel =
$ h. Z6 o8 e) L- u7 |, N V" m new Selector (proto.getClass (), "heatbugStep", false);# ?) e3 z! o2 o$ D
actionForEach =
# m, X7 d3 J) A' k* Q5 s) { modelActions.createFActionForEachHomogeneous$call; L% T8 D1 V/ I4 ?/ h. p7 I+ c
(heatbugList,7 R5 f% e1 k# ^: O8 {
new FCallImpl (this, proto, sel,% x/ ~1 E+ O! V8 z2 [+ d4 J
new FArgumentsImpl (this, sel)));" ~( F0 ?, i6 L; ^* X
} catch (Exception e) {0 P; A, x( _( A. L4 v9 L2 m. A, k
e.printStackTrace (System.err);9 p& [* J8 |; J# t. ?" P9 Q2 c
}0 C9 `* M! U; m G; p K, Y1 O* w* l; d) U
1 w) V3 b/ r7 \, A7 B
syncUpdateOrder ();8 {% J E- E `; c
6 v( H5 q* E; Y8 Y try {0 I7 L7 {; q5 m. o; m9 ]3 E
modelActions.createActionTo$message
$ D( }. f$ A' S. O* N (heat, new Selector (heat.getClass (), "updateLattice", false));
8 x" `% k9 ^6 b# [; a8 B } catch (Exception e) {
( A+ ~. ?- S8 k7 A5 N& G System.err.println("Exception updateLattice: " + e.getMessage ());
/ G* \. ?0 V7 h0 O6 q }
. M1 o) D# p/ F/ e # x/ n0 N4 i+ a; i/ { Y4 W
// Then we create a schedule that executes the9 a0 |2 j: D$ l' p, j! G: {/ h
// modelActions. modelActions is an ActionGroup, by itself it1 C4 a c5 B n4 Q9 n; V
// has no notion of time. In order to have it executed in: @/ Z3 W: d- X4 O" n
// time, we create a Schedule that says to use the
4 P1 t6 m9 z: _9 I& K // modelActions ActionGroup at particular times. This2 Z( d' o) l2 d8 N: c0 i
// schedule has a repeat interval of 1, it will loop every% u& u9 L3 P! u" w+ J
// time step. The action is executed at time 0 relative to: B# L9 R4 b1 W8 C. e0 @ G
// the beginning of the loop.
- K8 _6 P+ n: c' P8 i! Z" k; b: [" f8 b5 }% Z# i/ q) K) Y0 {
// This is a simple schedule, with only one action that is
' h/ P3 d# l9 B& b% z // just repeated every time. See jmousetrap for more6 j: f) k6 V* D' h! ~
// complicated schedules.
4 r2 G" c/ z/ | n: b1 n
8 F# N; X5 W( P- J* E6 I9 A. x modelSchedule = new ScheduleImpl (getZone (), 1);; B" g1 a: U3 w/ S4 _2 ]
modelSchedule.at$createAction (0, modelActions);
* @7 |- [8 b* j8 G; M
# _" L+ c3 D7 _; ^! V* J+ x) h6 | return this;& G Q$ a7 ~# [# G# j3 X. h1 ]
} |