HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& ?1 I( T/ K. A. P
4 ?6 K% x' h& C2 b& \& P$ w! W public Object buildActions () {$ f0 K# t3 M, a9 S |
super.buildActions();
O+ F3 {# f9 c! l; T, P
2 L! _! D2 p6 U* A" ?2 P // Create the list of simulation actions. We put these in
1 K4 Y7 T9 y& A) k. `8 J9 @ // an action group, because we want these actions to be
% S0 |3 d" r Q, q& ~ // executed in a specific order, but these steps should) a. o( h1 S5 W' M; |5 |2 `+ z
// take no (simulated) time. The M(foo) means "The message& ^( q. E2 F4 P
// called <foo>". You can send a message To a particular: k0 S9 ~) R5 p
// object, or ForEach object in a collection.
5 s1 K' S" X% h4 B + i$ x; u7 v0 N$ j" w. D
// Note we update the heatspace in two phases: first run
7 j& Q, M p q3 X // diffusion, then run "updateWorld" to actually enact the
6 o/ q1 x9 T+ Y/ @6 ~ // changes the heatbugs have made. The ordering here is
0 o. K, I5 _* Z/ J& r+ @8 Y2 N( I // significant!
, H2 [$ [$ [4 y2 S
3 n% z7 k) N. g, T" o5 |, x // Note also, that with the additional
: i6 A. @; B$ _& p, p. S7 B // `randomizeHeatbugUpdateOrder' Boolean flag we can$ h- `2 m1 f m8 h$ _% Y
// randomize the order in which the bugs actually run \$ Z' ^# n7 k7 [6 R- A8 n
// their step rule. This has the effect of removing any+ k+ F% |" B6 X3 p. P# g `
// systematic bias in the iteration throught the heatbug- t& g1 g' `5 l; G! r* t& Z6 z
// list from timestep to timestep
8 l5 i; c' I; ` 3 S$ [) `+ Z& _' s; Q/ B
// By default, all `createActionForEach' modelActions have
# g/ r# _8 n/ j( q // a default order of `Sequential', which means that the
. h7 ~/ w0 \+ z: y5 f! c' i // order of iteration through the `heatbugList' will be4 ^/ N6 g6 P* ~+ \1 C; O
// identical (assuming the list order is not changed
: [/ R7 t) A* ]8 x! ] // indirectly by some other process).
& B$ @# m' ^/ U0 K# d" T& o9 M0 L7 J* Z 0 j. @4 z3 P0 V: q
modelActions = new ActionGroupImpl (getZone ());
2 k' K: z4 U& [7 j" m, Z9 [; P# w# [
try {3 l. a# K' q% r1 c6 z
modelActions.createActionTo$message& H/ E# O: F) f
(heat, new Selector (heat.getClass (), "stepRule", false));; Q, T6 ~1 y$ P e1 e
} catch (Exception e) {
' X% j4 k) O7 q; R& z. K System.err.println ("Exception stepRule: " + e.getMessage ());# H9 y; [# A( h0 ^6 {
}: o, a k8 A! A2 i2 l2 A4 a }" x5 I
! r3 j+ _! }% I% c E
try {8 r; ~4 V6 ?, ^) {, F5 w9 a& k
Heatbug proto = (Heatbug) heatbugList.get (0);$ x( a! B" M% k" l( ~8 v) k: D
Selector sel =
( e) _ M; x- S new Selector (proto.getClass (), "heatbugStep", false);
2 _5 t u2 X: ]3 f9 x1 s actionForEach =& b0 e" i# G, K" ]# y1 ]& b* {, r
modelActions.createFActionForEachHomogeneous$call
/ g+ c. c. ~1 Z8 N3 D; o# w% C (heatbugList,
2 t) L9 O7 t- G) } new FCallImpl (this, proto, sel,/ h* y. z" H' d2 _
new FArgumentsImpl (this, sel)));$ k: ~, t* i* z: z$ ~
} catch (Exception e) {
, D8 t! z9 v2 Z$ O7 T, X5 O e.printStackTrace (System.err);
- ^% C2 B: ?% C+ [. D; L k# H+ [: G }
3 F& ]1 M, ]# E! K W1 r% o4 @
3 p- N, I6 @( L6 H8 | syncUpdateOrder ();
- h# R4 f. b2 n A/ R D* s H/ c9 z, A
try {/ d+ F, N) \; c# R( B0 N/ ?6 G
modelActions.createActionTo$message
7 [: z4 ~' c4 M: I- U (heat, new Selector (heat.getClass (), "updateLattice", false));+ Q, {9 \, V+ W% b# q. c
} catch (Exception e) {
% t& z) p. A; ~ System.err.println("Exception updateLattice: " + e.getMessage ());
# i* o% \- t& p* U' O$ Y7 F5 ? }
( I1 J! ~$ \* d9 d" N1 d 4 J" J M% ?- ?3 `' V) j% v s
// Then we create a schedule that executes the
, j& O0 P5 N: V6 b* ]9 g // modelActions. modelActions is an ActionGroup, by itself it) `, }% }' d6 D2 A& h0 _0 i1 k) f
// has no notion of time. In order to have it executed in# k* x, i5 `1 S3 ~, p8 L# ?5 c
// time, we create a Schedule that says to use the
8 z( W' C+ N5 Y! F# B/ ] r // modelActions ActionGroup at particular times. This
' j: _' N6 }- i& @* {2 _ // schedule has a repeat interval of 1, it will loop every J X. ?5 o/ L2 `
// time step. The action is executed at time 0 relative to
/ ~) Y. L+ k2 P4 V5 t // the beginning of the loop.
' ?5 w2 _1 U" [ m, @( f0 s$ o' O2 i5 F. ~# x
// This is a simple schedule, with only one action that is
, b6 W! }# x" p) [; w# ~ {/ q) f% F // just repeated every time. See jmousetrap for more
3 [4 w3 B" Q* t- P9 t2 J5 V( B" F8 p // complicated schedules.
4 ]1 p% s- x/ ?* f+ k4 P" Q. J6 h
6 |4 z" {0 i" R# q modelSchedule = new ScheduleImpl (getZone (), 1);( T2 y1 a) P% \( ~
modelSchedule.at$createAction (0, modelActions);( Y2 M8 y! C) P- _
; v/ M* c- V& {' W. c9 {3 B" x; S
return this;
: P& p8 B% p" I+ T2 f } |