HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; X9 b& H" U# P4 n/ _$ D' u7 U
0 J# G; O5 G3 L0 \+ N$ x
public Object buildActions () {# `; _& j3 F: J
super.buildActions();
# F0 ] y, @" `- [' A
4 c% o$ \4 @" d+ |7 J // Create the list of simulation actions. We put these in+ O# ~" e' B H4 d$ n9 V5 m
// an action group, because we want these actions to be
8 A5 i2 \7 T! _ // executed in a specific order, but these steps should" K6 q5 [7 K4 n$ R. F
// take no (simulated) time. The M(foo) means "The message
8 E: J+ h$ ?' w2 y8 Q' e& I // called <foo>". You can send a message To a particular
9 Z! p5 ]; O- @! y, |5 h: b2 o // object, or ForEach object in a collection.
" e# O+ |' W2 m r6 O% ` W 4 ?$ S9 j5 B2 G: g3 a
// Note we update the heatspace in two phases: first run
0 K% [( ]3 ~6 C: t* l* h9 L# A // diffusion, then run "updateWorld" to actually enact the
) v0 H: o" G6 F& T1 e // changes the heatbugs have made. The ordering here is; ^6 L, c- L) E. l7 Q% u
// significant!) R9 l5 K2 y& h3 I! q
( ]4 A3 d- d2 H9 t
// Note also, that with the additional
3 c$ d9 t5 F( a% u& | _8 u3 z // `randomizeHeatbugUpdateOrder' Boolean flag we can% C. d! U( ~: M
// randomize the order in which the bugs actually run
$ Y3 ^2 l) t. m4 ?" Q$ A // their step rule. This has the effect of removing any
/ t0 p( q$ O: {. ]" t // systematic bias in the iteration throught the heatbug# w7 D. {' d: ~8 K# A, ]
// list from timestep to timestep4 ]' w: F" P4 N
/ e( \0 V9 l# J; G4 @: ^8 ^ // By default, all `createActionForEach' modelActions have
8 I: p1 }( n. O: }: g: ? // a default order of `Sequential', which means that the
# O C5 r0 ]% T( a // order of iteration through the `heatbugList' will be
4 S5 {& H0 S- K. f. D- Q // identical (assuming the list order is not changed2 }2 m: E( d; p) U [3 c, i
// indirectly by some other process).3 h! I3 [2 z9 L x" P
1 M8 q; C0 {1 J. k& H. L
modelActions = new ActionGroupImpl (getZone ());7 q% I* d: @7 m, U
1 W+ f, a; C, r6 n) o) ]
try {0 L0 E4 e. {: b5 z' V
modelActions.createActionTo$message8 {, n# o8 |% {
(heat, new Selector (heat.getClass (), "stepRule", false));
; }- k( {1 j3 _% Z1 s } catch (Exception e) {
! N7 r: c/ D; z/ o System.err.println ("Exception stepRule: " + e.getMessage ());4 F) b- e& t: F% i
}
8 N4 N7 n( j; C! n4 J" ]$ K$ R+ T+ q) Q% w, b) v2 D" m0 B+ `+ D" a! a
try {
3 Z" x1 Y# V* b/ c" d Heatbug proto = (Heatbug) heatbugList.get (0);: H+ A* T0 u6 ?4 d
Selector sel =
% j' |; z8 {( K new Selector (proto.getClass (), "heatbugStep", false);
& b# ~) \3 q( d. v" z9 W actionForEach =' j/ w. N0 x- h# `0 D% h
modelActions.createFActionForEachHomogeneous$call
! k' e0 \/ [& x1 f9 n( e (heatbugList,
" T+ n5 ?, T7 u& X new FCallImpl (this, proto, sel,
! f# |1 ?6 ^( S2 ^, X new FArgumentsImpl (this, sel)));% D" p) Q- B* D& K* W7 p
} catch (Exception e) {' {, b" b( E+ J. L: @
e.printStackTrace (System.err);
' b% R L1 i/ p8 h3 m! H7 d1 F }/ [8 o. }, O% h3 T ~
6 B# @+ d; M" q' o0 W0 y syncUpdateOrder ();
8 L; X Z, D! G$ A% M5 N9 j1 c% g1 b
try {
. v9 d. s# s6 i modelActions.createActionTo$message / x9 u' G$ T" k5 }
(heat, new Selector (heat.getClass (), "updateLattice", false));
% o+ ^9 x7 n# T2 g' O' X } catch (Exception e) {
# G3 n+ R g$ Z% Z1 @3 [& ? System.err.println("Exception updateLattice: " + e.getMessage ());
1 R O; X0 |6 U8 i# b8 M7 X }
1 U8 Y& p4 [4 n" d; ?7 c ( y& u7 ]+ u" \, [) M
// Then we create a schedule that executes the" m8 x2 c, V- I; k
// modelActions. modelActions is an ActionGroup, by itself it
$ f6 j2 B, R; \/ _ // has no notion of time. In order to have it executed in* N: x! ^* S. o! x
// time, we create a Schedule that says to use the
$ b4 Q1 ?' F1 m // modelActions ActionGroup at particular times. This) A( W( h" o4 t) d2 B' \9 J: u
// schedule has a repeat interval of 1, it will loop every9 }* w+ ~4 r2 q- X* B$ j. h6 ?
// time step. The action is executed at time 0 relative to8 @2 D- E( x# d
// the beginning of the loop.9 l2 k) g8 b |/ x1 m
( i# l* D4 k2 J) f // This is a simple schedule, with only one action that is+ s& f# f4 b' C3 X- d
// just repeated every time. See jmousetrap for more
P( A( q( w g6 Y // complicated schedules.
! ^% `' F9 d9 P0 e
4 F- I0 g' P2 }+ K modelSchedule = new ScheduleImpl (getZone (), 1);
5 v4 Q# m! U0 z modelSchedule.at$createAction (0, modelActions);# y, L3 ~) t6 b/ R
" \% B7 r- U1 Q# E return this;
- n! T' S% a/ ~% i6 @( \7 } } |