HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# t+ `$ v) Y C {+ W+ _' _7 w6 [- ~- M) p* e7 t; Y9 Z/ \0 t7 m
public Object buildActions () {# q7 a5 q0 j0 x$ I
super.buildActions();
; B# r/ ]' y$ u% h3 a7 n0 U 8 ?, N; h0 Z* ^5 `9 I9 q
// Create the list of simulation actions. We put these in+ K+ Z) |6 ~3 g( u
// an action group, because we want these actions to be3 n$ G4 b: q V( t
// executed in a specific order, but these steps should
4 Z( Y' ]6 u: Q8 K2 L2 D. f // take no (simulated) time. The M(foo) means "The message
, V1 P- i/ s. e( d // called <foo>". You can send a message To a particular8 F) k. ] h9 `: R
// object, or ForEach object in a collection.
/ t8 n% |% v+ ~$ r) U - G$ s9 A$ v( ^" c# C
// Note we update the heatspace in two phases: first run
# F- Y6 \0 C0 g7 C // diffusion, then run "updateWorld" to actually enact the
}! q3 w/ R5 `/ T // changes the heatbugs have made. The ordering here is
4 E4 J6 c' \ v( z; P // significant!( Y; a' {9 V: Z3 a7 d8 F
+ F7 _9 p1 v8 h# c5 `% a" E
// Note also, that with the additional
, L( K, `. k( j // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ i2 M7 B& }4 H- G! D // randomize the order in which the bugs actually run- J$ I( B1 a' d4 D, N
// their step rule. This has the effect of removing any0 y# `+ G; t+ Z* ^7 _/ Z- J
// systematic bias in the iteration throught the heatbug5 u1 O" F& X7 Y
// list from timestep to timestep
% W1 Y. a+ x7 r7 a% c F ( s [6 F: t: ]) _- ]! Y) R; u
// By default, all `createActionForEach' modelActions have) ^0 k$ X( M: O9 P' u: \3 u7 H- V& O
// a default order of `Sequential', which means that the) `! u( O6 T( W! t
// order of iteration through the `heatbugList' will be8 R, a9 U$ s/ ?# J# B( b
// identical (assuming the list order is not changed0 |- \, u8 J P O! K" x% I* ~, U
// indirectly by some other process).
; ?; {" B+ t3 N a- t2 S: g
3 P) b, v8 Q' Q$ o1 B5 _ modelActions = new ActionGroupImpl (getZone ());: h8 x/ Q: T5 l( G: h/ k5 J
9 \, n8 a1 e% v$ Z( |- [ try {- G! [& J) a$ e6 Q5 |& ]) x. |
modelActions.createActionTo$message; c/ \4 K! R+ V0 O/ V% [
(heat, new Selector (heat.getClass (), "stepRule", false));
/ | ~; z6 A( ]- q. S9 t- [% R" F1 ~ } catch (Exception e) {9 _* p! m' V8 j; [9 ~
System.err.println ("Exception stepRule: " + e.getMessage ());- f! i8 C% T8 G
}
5 P, O0 G$ H6 M+ s: f3 |" u8 _- N# s) n7 d" ^* v
try {+ `0 Q; c0 S3 |5 ^! X
Heatbug proto = (Heatbug) heatbugList.get (0);: s. O' }! M* y' }, P
Selector sel =
/ M" l- y; b7 K, s# f new Selector (proto.getClass (), "heatbugStep", false);4 i: X @8 z5 P$ c. v J" g
actionForEach =
! Q. ^/ x4 M) [: V o- E modelActions.createFActionForEachHomogeneous$call
+ L( w/ j% A2 e8 E' J. p (heatbugList,9 P( ` ]# j( {- d5 E5 m" Q$ y& t
new FCallImpl (this, proto, sel,/ |5 ^6 F% @4 r) Y5 `, m
new FArgumentsImpl (this, sel)));0 N- n5 Z2 t$ F
} catch (Exception e) {: P# I) i) o6 M# ?4 E
e.printStackTrace (System.err);, i6 e& @4 n4 ~% q! {
}. O3 Q4 y; m) S7 ]9 W+ \ }4 S9 g
" a3 U0 G4 ?* Q& v) k$ q I @ y syncUpdateOrder ();
. x" k6 V+ a7 J9 e/ s) Q* y U) Y; e! q: o$ H
try {
7 u9 b9 i; r0 i2 C$ ^ modelActions.createActionTo$message - k/ I* a* O: T' x! E: k
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 ]" a) I4 W* l } catch (Exception e) {
# T; z# J% J- L System.err.println("Exception updateLattice: " + e.getMessage ());- G4 [( [7 C& n% C
}* a; q# x9 f6 T, c
8 H) ~- n0 H' O8 c7 f // Then we create a schedule that executes the
3 l+ }; v5 \ E // modelActions. modelActions is an ActionGroup, by itself it
! _2 D0 s! y0 _7 g, T5 X7 E // has no notion of time. In order to have it executed in" [2 n; P5 z _
// time, we create a Schedule that says to use the! R) X" O$ m; A+ o2 _0 w/ F
// modelActions ActionGroup at particular times. This
- G3 p5 |) b4 ^9 D& B // schedule has a repeat interval of 1, it will loop every3 r) |. [7 T. J S' ^5 B: G
// time step. The action is executed at time 0 relative to+ k9 I) G& I2 g8 }7 ?6 q
// the beginning of the loop.
8 z( j! W0 u- E2 a9 z( `9 s( L
: Z6 r2 Z* \' p9 W // This is a simple schedule, with only one action that is
& ~/ i7 W. h& v( u& h5 _ // just repeated every time. See jmousetrap for more
. y% K0 y) U2 K. @7 u; _ // complicated schedules.& r i7 t% }1 q) y" i2 `) s
9 ^* g9 d" i) f% g* A" E modelSchedule = new ScheduleImpl (getZone (), 1);
8 o1 L( T' O' z% Y3 M R modelSchedule.at$createAction (0, modelActions);
0 ~2 m5 A/ B# ?9 B" R
5 ?8 P* E. n( M return this;) l: S; `0 |. }/ C8 T; P) t$ z# H& G
} |