HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 t, O* U. e. t. x; s
1 x( k! m& s% y0 o( { public Object buildActions () {. a9 a. [ `* }8 r2 K) {
super.buildActions();+ |/ v9 v/ A! O5 @) r! E: O4 _+ {
9 i8 A$ o' H- Q8 }* y. k, C // Create the list of simulation actions. We put these in; L. O0 o, g' C! V: v/ S
// an action group, because we want these actions to be1 m' ]9 W$ x- M# p" ], |! h
// executed in a specific order, but these steps should0 b" `' _( U7 u/ z# k5 Y4 M6 e
// take no (simulated) time. The M(foo) means "The message6 P0 | N1 g, J, g- }
// called <foo>". You can send a message To a particular, @- U l3 [1 w+ {7 g. l& i' J
// object, or ForEach object in a collection.
. X+ H: }* v% K& Q9 ^4 E- h3 `" ~ ! C( u& @9 X# r! w6 Q
// Note we update the heatspace in two phases: first run
; q; a4 j: W/ g6 m" f) K0 ^; o // diffusion, then run "updateWorld" to actually enact the
, j2 D4 c& @* l" h. P$ U- ~ // changes the heatbugs have made. The ordering here is7 z- n( x0 W% ]: ]/ P
// significant!
+ O8 ?! [7 Q& M: h 1 G( U: S0 s- y* c% n
// Note also, that with the additional% \& N" Z- d6 E* k. A
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- c2 k G% H1 _ // randomize the order in which the bugs actually run
, x# A: a. X7 k2 Y // their step rule. This has the effect of removing any+ n6 A( i4 H! X' H5 |7 x7 h$ Z
// systematic bias in the iteration throught the heatbug7 I) V V$ i$ {
// list from timestep to timestep
7 G! _6 F, ]) [; [9 {# p # E% v! Z9 f/ H8 {% t2 M. Q' {3 x
// By default, all `createActionForEach' modelActions have/ {* ], r. I- y4 T& G
// a default order of `Sequential', which means that the9 M( o! Z' ^ n& ?) ?& \, g
// order of iteration through the `heatbugList' will be
1 Z+ o# v& R0 h8 H v0 K% z // identical (assuming the list order is not changed% K. O) e& F& \1 b1 G+ A4 D
// indirectly by some other process).
+ ]3 v& k/ N$ j: D" U$ |9 _ ) F s g; n; ? ]
modelActions = new ActionGroupImpl (getZone ());4 k* ~- U' V/ I5 Y9 I, l' ^% b# l
6 W" }, l. W( Y( \$ ?* |$ b try {
% W/ b9 S/ T/ ~# C3 k) i modelActions.createActionTo$message2 Z5 o% I! J! t m7 r1 x
(heat, new Selector (heat.getClass (), "stepRule", false));
/ K5 ^5 i J4 J6 T. A4 } } catch (Exception e) {
* D3 v2 T) J( @$ H1 r System.err.println ("Exception stepRule: " + e.getMessage ());% L9 J9 \, \: O
}
! L! `$ @! S0 ?
; ] B; q; R4 A9 m) v# @ try {" ~ y8 h; b! h2 R- H; C
Heatbug proto = (Heatbug) heatbugList.get (0);
- d9 I/ [& g- X$ D Selector sel =
q* a. X$ M$ N5 R9 V new Selector (proto.getClass (), "heatbugStep", false);
; [; r% \9 R* C8 @$ {) O2 s5 p actionForEach =
& s+ u. v" G* s0 l modelActions.createFActionForEachHomogeneous$call, R+ f, }, L' ]& S
(heatbugList,
* i" w; I6 Y, |, M! N) W6 \ new FCallImpl (this, proto, sel,
% \5 F: L( w& d- @$ [5 s new FArgumentsImpl (this, sel)));
+ b- P! c" z* ^: y0 F) f } catch (Exception e) {
- t0 L* B& B0 z% r7 @ e.printStackTrace (System.err);
% e) U' s6 d: h5 q5 ~! x }( K" ]$ e; Z) Z3 G9 h) t0 {
# @2 } w6 @4 i1 m: y2 p syncUpdateOrder ();
- C) Y. Y1 M& S" _, d5 b v. v0 E6 ^ p k$ e* y) D5 j0 @/ G
try {' y. @* U1 E/ D- C8 X5 _
modelActions.createActionTo$message
' I8 h& i) G5 V* q8 } (heat, new Selector (heat.getClass (), "updateLattice", false));
# }6 \( d# j2 j1 V4 u y. h$ Z } catch (Exception e) {
9 h" e2 H: ^, i" E1 D# Y j: Y/ Y1 B System.err.println("Exception updateLattice: " + e.getMessage ());" h& s9 V" x; Z3 w5 H5 a
}! Z$ B- r! v& f4 F& r; }: U0 A4 _
0 h# n& ?/ h4 L) w // Then we create a schedule that executes the
: e1 T3 V& _( ~) w0 N- e# u // modelActions. modelActions is an ActionGroup, by itself it
" u2 j5 U0 n1 h/ R" p8 G( P& @ // has no notion of time. In order to have it executed in
8 u) Z) Y3 X( O1 ]* h% ? // time, we create a Schedule that says to use the2 P) _- ~+ d4 t' x! j
// modelActions ActionGroup at particular times. This
4 j; A! a+ d, k# ?: G // schedule has a repeat interval of 1, it will loop every$ r C* ?. r& @$ w- W" k2 J5 Y9 h
// time step. The action is executed at time 0 relative to
7 t: N/ Q& L+ _ // the beginning of the loop.
4 r: n. @/ f$ Y0 u) u. |6 g* L+ j" p& y* |9 p
// This is a simple schedule, with only one action that is
& N! |+ J4 |' }; g // just repeated every time. See jmousetrap for more& {; M( f) ^( r: [9 `3 N# x% w+ \
// complicated schedules.
, o2 h6 o. j- c; G# i; ?
9 W7 |9 `3 T5 {* h/ D+ H modelSchedule = new ScheduleImpl (getZone (), 1);7 e9 u$ a9 L. H/ j
modelSchedule.at$createAction (0, modelActions);3 c- c$ i& I/ z8 S
: o- a' M1 I# _; P% t3 k- X; G
return this;5 y: r' R( k. W& ~, c0 O
} |