HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 p0 X4 V8 y* @* Z6 P; p6 v
; W- K: q5 x1 O
public Object buildActions () {3 t; n) |; v; v/ V; J
super.buildActions();
* Y+ W, N9 k/ A* L$ v " Q. X( Y p; J _$ J
// Create the list of simulation actions. We put these in
( `( d2 ?1 \1 `: d+ @: ] // an action group, because we want these actions to be
; o- i$ G4 A, X6 ~- ]* ?6 X, n% n // executed in a specific order, but these steps should
0 `$ ? l: S$ u0 I& N // take no (simulated) time. The M(foo) means "The message- s" M2 z, |/ k
// called <foo>". You can send a message To a particular2 A7 I% V" h- p+ K: i1 t+ o
// object, or ForEach object in a collection.6 h) u5 r$ w, Z" X
+ E. S1 D5 h, o2 D' | // Note we update the heatspace in two phases: first run
* a) u2 I6 m& M // diffusion, then run "updateWorld" to actually enact the
+ W) S5 B- H) L) z; D // changes the heatbugs have made. The ordering here is& w/ X/ P- `! e
// significant!3 z' S! A$ R9 D8 j, A5 D, E' l. [
. x7 T. k" Q9 m' R // Note also, that with the additional
3 e) V+ c) H7 ?$ z // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 J' {# l' R) @# D) j* ^. i" B. ` // randomize the order in which the bugs actually run
$ C! O2 a+ b. v! C/ o // their step rule. This has the effect of removing any
?# O! p# E: r) g1 h# g! u // systematic bias in the iteration throught the heatbug' u1 f. U" p! G+ ]
// list from timestep to timestep
t0 R/ C$ G% c( A% ] 9 P; K8 j4 b( t/ I- _5 Z z; z2 p
// By default, all `createActionForEach' modelActions have( o: F8 t V" n7 y6 h! l P% ~# e
// a default order of `Sequential', which means that the; o6 y6 O) _' [: H/ J6 N9 A \
// order of iteration through the `heatbugList' will be. V7 c( N0 e* L2 I. p0 m
// identical (assuming the list order is not changed0 s7 d' {8 J% e6 p% i
// indirectly by some other process).9 c( K3 v" M$ M
2 y: ?: a/ V0 @- {7 z6 n
modelActions = new ActionGroupImpl (getZone ());
1 s/ H- p6 q/ d; p
& M4 R$ p$ D6 `% K* V. L: W% b try {
9 {4 z$ B( J; ? modelActions.createActionTo$message# t, c: b* J5 H# D$ c( F' b7 ^" i5 f
(heat, new Selector (heat.getClass (), "stepRule", false));3 y7 g: V. C6 g8 z/ M* W
} catch (Exception e) {( Y8 }+ v" L& m, O' @* Y: s- c# C
System.err.println ("Exception stepRule: " + e.getMessage ());8 k) x5 Q" _* O
}
9 i: X% E2 q2 V9 y$ X0 N/ Q7 L# |4 {5 d2 w/ H( E
try {
0 C$ U$ H' s' e& }- f( F Heatbug proto = (Heatbug) heatbugList.get (0);
" D3 g j) y8 j0 V9 q4 l Selector sel = / {" N; @" L6 P6 [& L3 Z
new Selector (proto.getClass (), "heatbugStep", false);
4 _8 b' E. X% F1 ]* `3 g actionForEach =
2 F' X; ]4 t7 Q) L4 F6 ^8 U modelActions.createFActionForEachHomogeneous$call
5 E; A3 K4 D, O% d, u1 A' s1 R& @4 l (heatbugList,% X, Y6 r% r* T6 C0 _
new FCallImpl (this, proto, sel,
7 X9 n' B6 X% v3 p new FArgumentsImpl (this, sel)));5 l# d: Y- S+ y* }; l @' P% L
} catch (Exception e) {$ [$ p8 v, j* Q! w5 R0 Z' B
e.printStackTrace (System.err);
- j1 y3 G6 f8 l; J& u4 W }
0 a+ j0 V" i- G* ]% q . N! k9 J) t1 L# W
syncUpdateOrder ();
* X2 }, z) { D9 r' m2 `' |6 g- v# K5 s
try {
/ j* n1 R2 M0 @+ z- p' b modelActions.createActionTo$message 9 ^6 z3 `, ?+ P1 q. i, k" m
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ z' c- G$ K) |( E+ K } catch (Exception e) {
@$ Y! ]. q, D0 i& c0 X System.err.println("Exception updateLattice: " + e.getMessage ());2 c; e1 u4 q# u
}
" o- N& s6 D9 G: J
( d& ^1 g* h- f7 e/ `7 W; c/ P // Then we create a schedule that executes the
5 M! d; @9 g' p5 ? // modelActions. modelActions is an ActionGroup, by itself it
# R2 W1 ~* @$ N // has no notion of time. In order to have it executed in
2 r. @! Z/ g1 N, ? // time, we create a Schedule that says to use the# g. k. G% {% }6 T! ?7 E
// modelActions ActionGroup at particular times. This. F( c6 j# w$ m( r3 E2 S" }* D
// schedule has a repeat interval of 1, it will loop every) z2 t$ k. N, a1 m
// time step. The action is executed at time 0 relative to+ u( C. S0 |, n+ t/ l" A
// the beginning of the loop.+ a9 b0 n: w' Y0 x% o& O5 u j6 }
$ u. x2 n# {( A
// This is a simple schedule, with only one action that is
# w. ]- U! ]. x& K. g! F. L // just repeated every time. See jmousetrap for more" ?2 y8 S( a) ], ]8 U$ m
// complicated schedules.
0 U5 F1 g, f6 D* a) B ~8 W
! I1 s& ]1 j2 |7 E4 W modelSchedule = new ScheduleImpl (getZone (), 1);: ~! Q* o5 k8 n: S3 B, S5 c- ^
modelSchedule.at$createAction (0, modelActions);+ t- @$ @# x) \6 q1 l
6 d! @2 s9 X) Z2 z% X return this;
3 W& F# {2 q6 u+ G4 t! o3 `+ \5 o } |