HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% R: Y0 L n5 C7 z
# E- a2 L5 T, Q/ i2 X9 V public Object buildActions () {0 b8 R+ J& `$ x1 K
super.buildActions();- h' D+ w$ b' S- Z' E
& C1 K& n0 Q- I( S f
// Create the list of simulation actions. We put these in% Z3 E2 ?6 x, Y' X* z% O
// an action group, because we want these actions to be% d( {$ A* q: ^. C' t8 `$ R- p
// executed in a specific order, but these steps should8 X4 a6 c8 I P' w6 Z
// take no (simulated) time. The M(foo) means "The message+ G {6 U3 z3 ~% F6 [
// called <foo>". You can send a message To a particular" V; s, l1 U5 V; v# I! v3 R3 f0 h7 S
// object, or ForEach object in a collection." _: \6 j( {# ? S3 m
2 P1 f& B) M- L! I$ d; E$ B // Note we update the heatspace in two phases: first run
" ]/ j) Y* b* y |4 n // diffusion, then run "updateWorld" to actually enact the/ ?3 d2 \: j- T1 n3 @: Z
// changes the heatbugs have made. The ordering here is! ~0 i# q9 e1 f
// significant!' j1 B! b& P% r% n
+ i! u- o, P1 b: {3 x+ h' h2 ^ // Note also, that with the additional
% R- I% A; I8 d4 c // `randomizeHeatbugUpdateOrder' Boolean flag we can; h$ \4 ]. F% }5 D- Q. d1 `% O
// randomize the order in which the bugs actually run+ t! b1 w* P6 G; W) P7 M a& x; O5 i
// their step rule. This has the effect of removing any% X& F6 O" d& i% h' X
// systematic bias in the iteration throught the heatbug
/ x5 e0 o6 z* @9 e5 N4 | // list from timestep to timestep
7 L' @5 t! w) z5 u: G# J* ^ 1 J; S( D: T' G% |3 z# f
// By default, all `createActionForEach' modelActions have
% w2 Y! O( T3 R6 u; T! u // a default order of `Sequential', which means that the
" @) E, @: o7 I0 \- B+ w* r0 v // order of iteration through the `heatbugList' will be
+ q3 p6 W% K. ~# D* S // identical (assuming the list order is not changed5 [& w4 N* y' w1 V7 A
// indirectly by some other process).
( f* ^# W0 A- Y& v" I3 T8 ^1 Y+ z
0 S3 E$ ~+ L4 s. g modelActions = new ActionGroupImpl (getZone ());) I: [$ ?* {) Z; S4 F: \
8 a' h7 d# D. D7 I* }
try {
x# w. C7 p g9 f+ e& A0 b modelActions.createActionTo$message
/ f# @7 a; X5 H7 i (heat, new Selector (heat.getClass (), "stepRule", false));3 p$ o P$ d& y9 f9 A
} catch (Exception e) {* A" B6 }) s, e! M2 K/ o$ c
System.err.println ("Exception stepRule: " + e.getMessage ());% {, K |+ r8 U' u7 y6 m
}$ ~5 v- {* d- N; O) o
; K: a3 X! r6 b. i$ z9 {1 t$ w9 G
try {! R0 `; M9 Z$ x. r
Heatbug proto = (Heatbug) heatbugList.get (0);% w" U% c2 z5 R+ C5 J5 @
Selector sel = - A e+ U' I$ M% [" H4 Y7 |
new Selector (proto.getClass (), "heatbugStep", false);
4 ?0 V5 D4 i3 w6 Y1 C, H8 Y% y actionForEach =4 T9 J+ s+ v6 Q E5 m
modelActions.createFActionForEachHomogeneous$call+ _# i" X, e) t- t
(heatbugList,% ^) _! Q g, C7 v$ v
new FCallImpl (this, proto, sel,
. p/ ?2 ^8 }: O: n new FArgumentsImpl (this, sel)));6 I% J% D6 q& g ]. R) w
} catch (Exception e) {! j& p4 B/ N h1 [( k$ {
e.printStackTrace (System.err);
' L: C" T8 x/ W* J& V& W }
5 o: F: l8 t- w( l) X$ r& ? 9 j* o% y4 n: q8 ]: h) G
syncUpdateOrder ();
: X5 p. k5 ^) n0 ~. V
& x% P6 w3 x+ V try {
- {& j5 B% m2 Y/ n modelActions.createActionTo$message : E- K5 A3 d7 `# y( S
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ I- k: \) u+ \! |! ] n- U) X- b2 j } catch (Exception e) {
' C d5 F3 W6 B System.err.println("Exception updateLattice: " + e.getMessage ());- ~' h0 p, s1 j- q% T H" q/ t( V% u7 t
}9 W- F( \" p' g
" U# Q2 b! n2 |! s/ \ // Then we create a schedule that executes the
7 h7 j4 J! V9 ~5 R' Y4 A // modelActions. modelActions is an ActionGroup, by itself it% X N6 ^2 R/ t6 k
// has no notion of time. In order to have it executed in
3 J/ M% ?( U& H // time, we create a Schedule that says to use the
O- L' v6 }) c* d/ u // modelActions ActionGroup at particular times. This
) G* S- c9 c% G5 f // schedule has a repeat interval of 1, it will loop every' B. T3 ~9 }2 c
// time step. The action is executed at time 0 relative to
* g; L2 E8 o8 Y3 y. {0 h // the beginning of the loop.
; w2 R7 ~+ z$ W+ I1 ~3 B" y
( C X& n% O+ \ f // This is a simple schedule, with only one action that is# W H& v) Z: I( U
// just repeated every time. See jmousetrap for more* V( o' F1 E$ V2 o2 K) Z& n# o
// complicated schedules.
& c2 m. W5 q% G N
6 p# v3 C8 V& d" }, j' _ modelSchedule = new ScheduleImpl (getZone (), 1);4 w% `, y. E& C* M. p4 |/ k
modelSchedule.at$createAction (0, modelActions);/ C! j# p* l: R0 f+ V4 E
: b, g% w' X/ Z% }6 d5 I
return this;
- p+ s& }3 U5 {+ O } |