HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ H8 A% E+ d, m! C; V
. q7 Z' @1 M( e3 u6 o
public Object buildActions () {
; s' Q% Y y4 @ super.buildActions();% Q. W3 b8 n4 M) m/ `4 k" o4 L3 f
- K4 M' b" L5 G/ [+ ~7 p // Create the list of simulation actions. We put these in
; L2 ~5 Q* t/ w# h0 S, V // an action group, because we want these actions to be9 X' x2 b2 G) }! ^
// executed in a specific order, but these steps should' J- u u% ~' @! C6 L# w: K
// take no (simulated) time. The M(foo) means "The message8 O" P: S6 e6 `; P9 Q" V
// called <foo>". You can send a message To a particular
, Z- A7 C$ a& Z; P% `7 c // object, or ForEach object in a collection.
9 p+ \: o, L# j
4 d# q \1 `- ~ // Note we update the heatspace in two phases: first run
1 ^ G. B, f2 _" I // diffusion, then run "updateWorld" to actually enact the
. P }! O \/ M // changes the heatbugs have made. The ordering here is& Y! U6 {+ ~ _& D- ~
// significant!$ _! v9 \% }' T
7 ?7 s5 x1 w; j- m$ \9 ?: P9 W
// Note also, that with the additional; B P+ O+ f6 t) {4 a6 n) x
// `randomizeHeatbugUpdateOrder' Boolean flag we can! M( J6 F# u* Z$ g/ i. o/ v0 q
// randomize the order in which the bugs actually run9 Q8 M: P6 [2 S
// their step rule. This has the effect of removing any. g, {2 v+ L8 m% _6 g: x
// systematic bias in the iteration throught the heatbug* ]! }3 v. i' I, G) j; G
// list from timestep to timestep! K) ^( Y3 k3 W+ l; x
( C3 t. \9 u i. k1 M9 w) n; t; Q. t) `
// By default, all `createActionForEach' modelActions have
( y( b0 u1 q5 H0 i1 u // a default order of `Sequential', which means that the; \ h4 u: u. Y9 i
// order of iteration through the `heatbugList' will be; n, K- T& _: l, ^, U
// identical (assuming the list order is not changed
) }. W# Q" R3 J9 a // indirectly by some other process).
, v; x8 @9 c8 T4 ?7 s0 f+ [' l
0 m: O7 d. O' m modelActions = new ActionGroupImpl (getZone ());1 U/ v/ N- O! O: K
: G. R% R7 Y+ j/ }- t try {% s% F- x0 v- s8 ?
modelActions.createActionTo$message; D; O3 a8 s: E6 Y7 q+ o2 u
(heat, new Selector (heat.getClass (), "stepRule", false));
6 Z) ]: \2 s) i! G) h2 w } catch (Exception e) {$ b7 J% N7 u4 C
System.err.println ("Exception stepRule: " + e.getMessage ());
, \+ P6 ]) a a+ Z6 v; R- D+ j( E }" j( T* m$ }- E6 K. e4 T. L
7 w5 @* E2 D4 N1 g* [$ u try {
4 h! r5 l9 i$ L2 I+ k" d Heatbug proto = (Heatbug) heatbugList.get (0);
3 e, j# N* V% _ Selector sel =
+ ]; `! b' J* p' ^( Q* x" } new Selector (proto.getClass (), "heatbugStep", false);9 k) m$ m" R7 g& ^! I
actionForEach =
" |" U) T3 _2 p- s modelActions.createFActionForEachHomogeneous$call
7 }( d5 J0 d" \) ~ (heatbugList,* y) Q8 i' e; k. X& y
new FCallImpl (this, proto, sel,3 C( u" k4 u0 w8 j/ E) l$ b
new FArgumentsImpl (this, sel)));/ Q6 K1 L% H+ d6 w0 j; d* `" l
} catch (Exception e) {# l; i5 C1 Q" n8 F* O
e.printStackTrace (System.err);1 V$ [- A3 R2 b* B1 x8 g5 o
}
. F5 E$ R3 D: h( o1 @; n * `5 l8 k( n! @6 B" _
syncUpdateOrder ();
8 x8 ^' u5 P$ e8 n$ }, i4 @" n. q0 u4 c0 m. D
try {
4 |9 W2 @* P5 d( t4 X5 _, q modelActions.createActionTo$message
S0 V8 r2 y- |! m. \+ Q+ v9 ] (heat, new Selector (heat.getClass (), "updateLattice", false));
) w% o. L% k. x! N } catch (Exception e) {
! t5 L* P; ~/ c1 [: @ System.err.println("Exception updateLattice: " + e.getMessage ());
0 c- t4 b7 B. d/ t( c }
% d. l& X! B) q: {, D# ] # R# D, W# h5 Y- }
// Then we create a schedule that executes the
: q" t* k" ^% O // modelActions. modelActions is an ActionGroup, by itself it P' i \* d1 e$ m/ e6 i* r: T
// has no notion of time. In order to have it executed in C( P& e- S* w9 o$ G( ]
// time, we create a Schedule that says to use the S1 f( r r: e2 W
// modelActions ActionGroup at particular times. This% v2 a7 |/ R' ~- }0 k- K1 C* O
// schedule has a repeat interval of 1, it will loop every
6 h& T1 u6 j( ?7 A" Y0 v+ g // time step. The action is executed at time 0 relative to
# l1 @, W7 T/ o1 ` d% t8 a4 D // the beginning of the loop.- G3 U' H% V* a, p" J# D) s
/ I R: [( V9 b3 ]6 P, h" {* J/ k
// This is a simple schedule, with only one action that is
! J( W( b# E; q9 R // just repeated every time. See jmousetrap for more
. J6 \/ v m" T- N // complicated schedules.7 X' i) H: `* q" W2 [
# c& A2 `7 \0 m: M0 }5 B
modelSchedule = new ScheduleImpl (getZone (), 1);
, i2 q) ~' {! m. r8 ? modelSchedule.at$createAction (0, modelActions);
9 D8 a5 J, Y) r& o j" ]
6 r" v+ W- w4 w m: H5 A return this;5 ]/ H# S) e: `" I; A* ]+ Q" m" Q
} |