HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* W7 `& X, P' R: x# r
% {2 M' D3 a4 L0 {- _; g, J public Object buildActions () {
2 Z! Z# U3 @! T. S" m super.buildActions();
r: X$ c; [2 U* A! l
* D8 w2 a; ]4 T // Create the list of simulation actions. We put these in; Z- |( j3 |$ w4 W$ F5 b8 D, b
// an action group, because we want these actions to be
3 `5 c6 E" p o // executed in a specific order, but these steps should
2 A* M* ~+ ?6 w8 w; |7 { // take no (simulated) time. The M(foo) means "The message. l' e3 F8 I6 V+ V3 x
// called <foo>". You can send a message To a particular! @) C9 g- ^, l. g( {7 p
// object, or ForEach object in a collection.
& Y) ]4 U3 s4 v" ], K( D1 S ' s! V) _/ k& A
// Note we update the heatspace in two phases: first run+ W; M, R. c# g2 U
// diffusion, then run "updateWorld" to actually enact the3 N3 X; Q7 A7 K6 T. {5 Z, s
// changes the heatbugs have made. The ordering here is& R% z3 O: p" P6 @* x! e) E" v
// significant!
5 {2 m. B+ K+ m
1 ` d9 F) `3 o7 D/ V& t/ N6 D. ] // Note also, that with the additional( X) |& n+ e, }* N( c8 x/ g# M
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* X2 M1 |4 p' P6 ^) n // randomize the order in which the bugs actually run, Z5 B, y$ c; o- ]8 [- _& t
// their step rule. This has the effect of removing any
# A! j; d4 W# A3 b7 A" w& d3 O, t // systematic bias in the iteration throught the heatbug
& |# j, N2 A0 {5 A: E // list from timestep to timestep
. s* x+ s; d5 @- f $ N9 T9 U* {7 l- R. \, ?/ `
// By default, all `createActionForEach' modelActions have4 U3 k* }2 W3 S8 F
// a default order of `Sequential', which means that the# v0 m3 I! g+ d" W! t
// order of iteration through the `heatbugList' will be0 E" Y! c1 @# x" b
// identical (assuming the list order is not changed! M$ f: `* s! Y! c
// indirectly by some other process).
: Q% n, H! F" r! w. v8 S, `' i
7 u5 i0 w N' e5 d# I modelActions = new ActionGroupImpl (getZone ());
% X0 q8 f# w- ~" N3 k. N0 @
4 U9 r- }# J! @* _" T try {
) Z' Q) w6 ]. B/ o! e modelActions.createActionTo$message
; G2 c& C1 B) d- k" l/ Y (heat, new Selector (heat.getClass (), "stepRule", false));
! o7 S" b. N) m7 ? } catch (Exception e) {3 p' P& \* c. i; Z0 C0 ~+ Q
System.err.println ("Exception stepRule: " + e.getMessage ());* c. c- B/ q) h
}" A" I: C2 f' o5 c7 J/ s O
8 R2 w" [) E' Z4 B$ Q$ @
try {
% ]9 n6 Q5 ]" ? C b Heatbug proto = (Heatbug) heatbugList.get (0);/ Y0 B+ H$ }$ X- n$ @' U0 l" V
Selector sel =
) T6 ^. B1 n; {/ C6 Q: [& u% U \ new Selector (proto.getClass (), "heatbugStep", false);9 F- x2 n4 V0 ^( i
actionForEach =( ]5 ~$ M/ t2 ~9 u0 V5 }9 b6 [6 _
modelActions.createFActionForEachHomogeneous$call
3 r8 G) |4 S+ L+ H2 }3 M. G4 F (heatbugList,$ C) j$ D/ i9 `9 ?. T
new FCallImpl (this, proto, sel,! B" s! l, m. ^6 @& ^3 F4 i$ _3 u5 ~( m
new FArgumentsImpl (this, sel)));
( D$ h* k: G( C: u: Y# O } catch (Exception e) {. s |' w) a+ C \4 \9 P
e.printStackTrace (System.err);
5 p- ?2 Z1 p9 d0 b+ g }
" q: k* i5 D `$ i8 O0 M! _
# @ A' \4 }4 M; w syncUpdateOrder ();9 D. S" r8 Z, }5 _& R
; [5 \0 j& R a- \4 d
try {# @, f% f4 U3 P; H% E, x
modelActions.createActionTo$message
8 Z1 |- c+ Y2 I, p& E/ {& j (heat, new Selector (heat.getClass (), "updateLattice", false));5 W& C' I. i" d% X
} catch (Exception e) {
! l( O6 D4 F1 y1 a! S0 _' q System.err.println("Exception updateLattice: " + e.getMessage ());2 x% G' S7 l! e. q2 X/ [
}
; y3 S+ h9 x S( M( ?
0 L7 m7 S6 W( @8 l2 N* I8 m* ? // Then we create a schedule that executes the2 @) L) L& d7 o" K7 C9 n
// modelActions. modelActions is an ActionGroup, by itself it
& o5 x2 C$ d, r. g8 ~ // has no notion of time. In order to have it executed in& k; i( H5 V3 \' Q
// time, we create a Schedule that says to use the
- B% I9 M* ]9 Z( g- X1 D // modelActions ActionGroup at particular times. This, C% Z7 R5 [( e4 K, Z# d
// schedule has a repeat interval of 1, it will loop every
7 G; w7 a. K7 \0 ~8 Z // time step. The action is executed at time 0 relative to
* [- z$ K1 ?4 d1 Z; E // the beginning of the loop.
4 @( `7 b+ Z- ?0 y$ i: {: T7 u4 M* [+ q
// This is a simple schedule, with only one action that is8 S+ f4 |2 _3 H1 f% q9 o
// just repeated every time. See jmousetrap for more" |& ]8 Y4 E1 P6 [
// complicated schedules./ a6 L# j0 `( Q. i7 x! q! Y1 s; E
8 X9 o0 o$ k6 q% B/ r3 e) y2 t modelSchedule = new ScheduleImpl (getZone (), 1);7 r* R( @. J" V+ S% K7 [, K( Y% v2 S
modelSchedule.at$createAction (0, modelActions);
4 H; D7 X. U0 \) g5 G3 _
B9 h; z5 H' w1 g return this;
. v: i/ ?4 f: z8 ] } |