HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* v* P% o8 z' U( _; Z% }& `
( T% m- E# R C( r
public Object buildActions () {
# D( n5 J! K8 A6 J super.buildActions();; a3 r" K+ j( i5 y6 x. e) l& E* E
# |# J+ Y# z8 M6 T6 b$ N3 [
// Create the list of simulation actions. We put these in4 `* c3 K" M( z. {# R
// an action group, because we want these actions to be( y$ k. a/ x* h/ k
// executed in a specific order, but these steps should' N. R2 Y% `9 P. g5 V( F# W
// take no (simulated) time. The M(foo) means "The message
3 V _5 L" R5 h7 i C( G- @2 U" D // called <foo>". You can send a message To a particular0 {) F$ t8 m$ a4 o
// object, or ForEach object in a collection.
& p' K7 r o4 T% }2 Z * Y4 D" q! P% d. h/ b2 g& W
// Note we update the heatspace in two phases: first run
' F) S$ ]7 M# Q5 I6 P // diffusion, then run "updateWorld" to actually enact the3 E% y! z# D8 G* V- r0 g
// changes the heatbugs have made. The ordering here is
8 ?0 [& j C0 @1 m7 { // significant!# Q" _! c9 ]0 G& Q' [) M, u+ A
. }+ F3 l5 F6 }; F" x+ @. O
// Note also, that with the additional
/ @ S! T# ^7 i6 U // `randomizeHeatbugUpdateOrder' Boolean flag we can8 O3 A: v: I0 K9 M, L+ w! a3 \
// randomize the order in which the bugs actually run
- \# A8 x( K5 U# G // their step rule. This has the effect of removing any
( Q! k" ]. e0 O% H& Q. u // systematic bias in the iteration throught the heatbug
7 N3 ~# y h( T' t' x. x7 G // list from timestep to timestep
! V6 c4 V! H- w; Y2 R; ~ i( y9 M' }" B, b1 Z
// By default, all `createActionForEach' modelActions have" r: z) o' c% G9 U8 q% B U
// a default order of `Sequential', which means that the# v& r/ ~. P4 _3 z- v
// order of iteration through the `heatbugList' will be
- ]' g0 Z! I8 J+ N) K' H // identical (assuming the list order is not changed
, f/ N! B; B' |3 Z2 } // indirectly by some other process).
+ `% s+ x. J! r% G8 M1 e% v; ~ 1 `* k- b. k0 z+ ~+ r
modelActions = new ActionGroupImpl (getZone ());
: s1 Z7 U+ V0 Z+ o% j; `6 [' G4 `- h: D" ~' o' M1 F5 d
try {& b" ?" m; N3 B( T* ?$ x j, ?
modelActions.createActionTo$message
1 S* Y; g# q4 l5 `5 S. B( C, B2 ] (heat, new Selector (heat.getClass (), "stepRule", false));9 G- H s7 h: e+ }/ `4 E
} catch (Exception e) {
9 n: u! M0 }9 I. v ^; ]) t System.err.println ("Exception stepRule: " + e.getMessage ());" E( i) o- u" a$ `$ [
}/ ?3 |: r, m) f8 b; s% e3 b, Q
4 n; G2 B, Z2 p: I' a% R$ y6 K2 s
try {
7 K( C' |" F' F5 q# U Heatbug proto = (Heatbug) heatbugList.get (0);& {' J4 ?4 P S \7 J8 X M- v
Selector sel =
7 w8 R2 H, x( [0 U new Selector (proto.getClass (), "heatbugStep", false);0 G9 o e, p3 q# K
actionForEach =6 n4 U# Z d+ w+ a4 t8 ~( u9 ~
modelActions.createFActionForEachHomogeneous$call
. ?0 \ N( m/ z1 C- Z (heatbugList,
2 O/ U+ L: O: A* i" W; E% a- m: j new FCallImpl (this, proto, sel,( G8 P: `4 J5 P1 M ]0 G- x; ~
new FArgumentsImpl (this, sel)));9 Z4 U. [5 s, T! O
} catch (Exception e) {
- O0 c! p0 V4 J7 v/ a e.printStackTrace (System.err);$ x2 _. q; D" v5 K. k: P. R5 C
}
+ P' z6 @3 I0 n9 Q# n
" K& |) n2 t8 q3 q- V9 `1 s syncUpdateOrder (); n) a: z( m2 W$ i0 Z% f
/ B' q# ?1 v7 F% I. P+ {; N( K2 \( u try {" Z" b& Z- x# N* s) H
modelActions.createActionTo$message
! ^) u6 o: @( x (heat, new Selector (heat.getClass (), "updateLattice", false));
- @& f# I) R. _6 ?: m# Q" i } catch (Exception e) {
; h/ F# j# N* x8 }+ } ? H System.err.println("Exception updateLattice: " + e.getMessage ());; \' |9 \* w; _4 }1 ^& p" S9 E
}/ Q7 z- i8 i* Z4 m9 q5 F1 ~2 G, y% ~& I
% M% ~0 O" s" P" s3 T8 g0 P
// Then we create a schedule that executes the
- e2 v5 O) U0 v // modelActions. modelActions is an ActionGroup, by itself it3 o/ l6 |9 x7 G8 e
// has no notion of time. In order to have it executed in2 `& o* S1 k" d- k. v+ V4 z
// time, we create a Schedule that says to use the9 ]9 z# @% w% H0 W2 j3 R
// modelActions ActionGroup at particular times. This h$ f. x3 }& J
// schedule has a repeat interval of 1, it will loop every
: s! T0 m* z0 y* L: Q9 J // time step. The action is executed at time 0 relative to; H9 e, Q5 l1 S; G- a4 J8 @ F/ n
// the beginning of the loop.
; ^ b3 |% L& W! Q' m7 d( A: g+ {; v8 F( H+ W: X0 R
// This is a simple schedule, with only one action that is s6 e) R7 x, q, x6 A6 w, g9 r; u0 h
// just repeated every time. See jmousetrap for more, Z- Q5 P0 r$ j, o8 P
// complicated schedules.
( I0 l% e2 M! t& C. S; v& R- k9 h , T# y J t1 t( o
modelSchedule = new ScheduleImpl (getZone (), 1);- Q5 n0 G1 O+ V( C& t- e
modelSchedule.at$createAction (0, modelActions);
A) N4 F7 J$ g8 ~+ ?
% i$ [8 ~. x- _ ?* ~0 E return this;
1 ~! M9 A ]2 }2 J4 O, n } |