HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 r" V/ q/ D/ c: @7 r- g
1 u1 R u) H3 O3 v' h! j0 Q6 F public Object buildActions () {# b+ ?" R" E8 {- t; N3 W" T4 l
super.buildActions();. Y9 D+ G$ F$ Q5 K
8 ?8 @$ x0 g9 H // Create the list of simulation actions. We put these in
, ~0 x: @7 O! d0 o // an action group, because we want these actions to be
+ ?% o* s& ?2 N6 j6 p! K // executed in a specific order, but these steps should% S4 l1 w* d ^* k+ G: C( |
// take no (simulated) time. The M(foo) means "The message/ ?. \; P Q" m' A! ~% k
// called <foo>". You can send a message To a particular
9 I1 J& E ^1 T7 W // object, or ForEach object in a collection.
# a# c! h$ J5 L, D5 \; U h - O: D/ T6 H. u1 B
// Note we update the heatspace in two phases: first run" u6 w" c* e+ ?, B8 b9 h
// diffusion, then run "updateWorld" to actually enact the
6 _- i5 a( U" f$ ^7 D z, j( Z // changes the heatbugs have made. The ordering here is5 b5 a2 j# [3 l: ]8 N, \( Z) b
// significant!
; g5 V: A/ ^8 r1 P 2 ?/ {' m5 _/ X; O, W9 B
// Note also, that with the additional5 S" L" { I l6 U, H$ W( w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# e* P0 q9 X- ~1 ?( O0 e // randomize the order in which the bugs actually run8 K7 p# h5 S9 c Y% z+ G
// their step rule. This has the effect of removing any
* k+ Y% L: z6 f. M. ]+ d% M; _ // systematic bias in the iteration throught the heatbug
% O3 m2 X, T8 C" g8 E6 r // list from timestep to timestep. \$ }* T2 y, n7 @; A
# m2 R$ U# I" |2 e) H! b
// By default, all `createActionForEach' modelActions have: p) r U8 W; M- s
// a default order of `Sequential', which means that the& n! V! H$ {2 ^; L5 z/ ?
// order of iteration through the `heatbugList' will be8 O4 k6 @% c# E+ b# ~
// identical (assuming the list order is not changed% P; O* G; h+ U) r# P
// indirectly by some other process).! l6 a1 A" `8 P5 q, \
1 g+ r) y r' M" c2 z1 Z modelActions = new ActionGroupImpl (getZone ());
# L* [, C4 u1 e# v, [$ R. b" V1 E& Y* e5 s I
try {" W: t3 z8 D% g
modelActions.createActionTo$message
- p8 k, G {+ ` (heat, new Selector (heat.getClass (), "stepRule", false));
% W, x2 \4 _& e! R- V } catch (Exception e) {
6 _+ {, \$ [+ A0 M+ q8 o System.err.println ("Exception stepRule: " + e.getMessage ());
6 y% ]5 u [9 p$ Y# A% A }. o! W+ w% [& E' H
% h% ^: r% a: s try {' X( O9 z6 J4 `: v- m2 n
Heatbug proto = (Heatbug) heatbugList.get (0);, L% }; m0 d# \/ g$ |4 @
Selector sel = ^" O& X* B, V- C A
new Selector (proto.getClass (), "heatbugStep", false);
* e$ ^3 r2 g; Z! U actionForEach =4 I* b7 C' L4 i; L) G
modelActions.createFActionForEachHomogeneous$call% P% ~1 }) M* {3 Y. W5 ~
(heatbugList,# m* [6 s4 P: B2 ^* C! E
new FCallImpl (this, proto, sel,# L) ~; U' X& f4 u0 t! F
new FArgumentsImpl (this, sel)));
1 `+ l/ O+ Q' I2 R# G2 Q) u } catch (Exception e) {) |. _; J4 I% J% r
e.printStackTrace (System.err);
8 [. S+ ~2 i9 Y# l; a% x1 s* H }
6 e0 x( u9 T9 ]/ L( R% Z7 G0 o
5 J( L" v! |! A2 {* f1 c! r* | syncUpdateOrder ();7 h l# b- K3 J
: E; f2 u8 R( c# A4 L* ?8 u try {
- l( r6 s0 Y0 L7 H9 J/ c modelActions.createActionTo$message 7 u9 R. L+ r" _; A& C" ^
(heat, new Selector (heat.getClass (), "updateLattice", false));6 U" ?3 o6 @. \# k4 ?0 E; {
} catch (Exception e) {+ W6 W9 M6 D$ W( f8 k
System.err.println("Exception updateLattice: " + e.getMessage ());3 C2 ?% d# q" v* d. ]' b" E& d4 R) l
}
/ I2 G4 l5 H5 r
5 f N# \" p) I# x9 P5 p // Then we create a schedule that executes the- o9 _3 y! y/ T# y4 b. l
// modelActions. modelActions is an ActionGroup, by itself it5 B5 n+ _7 H4 ~7 B4 J
// has no notion of time. In order to have it executed in3 V: B# t0 z1 n; ]: s
// time, we create a Schedule that says to use the! m5 W+ s2 s* V, }
// modelActions ActionGroup at particular times. This1 K- \ F% s! r8 C9 [
// schedule has a repeat interval of 1, it will loop every' j3 W4 e. P# v# o0 E
// time step. The action is executed at time 0 relative to% L8 ?8 A' _# ^ K. U, X
// the beginning of the loop.
" Y' K* X/ C! p* {
2 E7 y; C7 c+ b9 ]3 } // This is a simple schedule, with only one action that is
" B( y/ `* z8 [" Z6 | // just repeated every time. See jmousetrap for more& a, q4 o3 I* s6 C% L, [0 a* ^) t
// complicated schedules.0 r4 L9 l( N: \. ?8 n
! M0 {5 Q$ E: v7 p- o. k modelSchedule = new ScheduleImpl (getZone (), 1);0 U! H& n: X3 M1 m. T; L
modelSchedule.at$createAction (0, modelActions);4 P. z0 D# I( L
7 f" |; _- p$ O' j; T5 i
return this;
& ]: o" @, N7 c$ g" g c+ T' r } |