HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# T, `1 k7 g8 o9 a- C( E+ l
8 N; J( z7 |7 K5 Y
public Object buildActions () {- `- }* B( M& G# c( m
super.buildActions();
3 B* }4 j* ?5 t1 G' w& {
) K/ z- c {! `3 g1 l, w // Create the list of simulation actions. We put these in- V- m1 Z0 y* w( m* e, M0 v
// an action group, because we want these actions to be
, J: b4 n4 _+ S: \, u // executed in a specific order, but these steps should
; G N& V4 h7 Q: W4 W // take no (simulated) time. The M(foo) means "The message
$ g# k/ O% y- T% m, ` // called <foo>". You can send a message To a particular5 i! t1 o2 A+ N1 B% D9 Z, f
// object, or ForEach object in a collection.
2 u% b; D$ R% p
3 k# V! ^: M8 W { U // Note we update the heatspace in two phases: first run% e3 i( F- K) m8 @
// diffusion, then run "updateWorld" to actually enact the
* I% q: Q' T' t% ? // changes the heatbugs have made. The ordering here is' E' k& N: k3 v7 y z$ g8 A
// significant!, `& X" F4 e+ k
! J3 w& x* f0 V0 {# W
// Note also, that with the additional
; y! f% A5 V' Q/ |5 H w // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 R4 o8 L; o% L( ?4 V, k6 V* V3 m5 k // randomize the order in which the bugs actually run4 f, d$ x2 o8 v b5 v) |% M3 t5 p
// their step rule. This has the effect of removing any
2 q' _' J5 m, W, _7 ~' u // systematic bias in the iteration throught the heatbug" t4 W7 h$ n- ~7 e& F% }
// list from timestep to timestep
7 p" b* p4 x6 s. T. a4 Q" E
( D7 Y3 @7 s8 E+ S // By default, all `createActionForEach' modelActions have
( J1 Z. B3 A$ t, F, X, Y // a default order of `Sequential', which means that the. \' x& k. t9 i% i K: }$ H
// order of iteration through the `heatbugList' will be, q9 F; d% J/ Y! A$ ~5 }
// identical (assuming the list order is not changed9 _6 Q6 j- v* v. a+ L6 g
// indirectly by some other process).
5 }1 ~1 U$ K- |3 G. ~
6 }: q. o9 y+ h+ Y. t modelActions = new ActionGroupImpl (getZone ());
; h% q8 T1 e5 M* ~0 ?$ D1 R8 y9 O0 ^. W
try {
; `8 l( J2 Z+ |& b modelActions.createActionTo$message
! p$ e8 W& w3 l) W6 j" y (heat, new Selector (heat.getClass (), "stepRule", false));2 \! ^" y0 |4 g+ l4 Z
} catch (Exception e) {
. }- Q' E8 `. I' { y/ J System.err.println ("Exception stepRule: " + e.getMessage ());
e/ C3 Z) x% T' \! A }
( A" ~- P/ a9 @( j; I y L; u' @* L/ Y
try { |0 x+ { T! [9 [/ ]1 S. \
Heatbug proto = (Heatbug) heatbugList.get (0);2 ^8 h ]4 J( \* C" W( ]$ r( i; L
Selector sel =
5 }. |: \) l2 \+ c, z# L6 p0 @- I new Selector (proto.getClass (), "heatbugStep", false);
0 i- {. I1 u. w" ^' n+ {% F actionForEach =
" \2 \/ z$ Z3 H% G: Y modelActions.createFActionForEachHomogeneous$call1 z, ?! J, l- c! X0 d9 i4 U
(heatbugList,
5 D+ E# |/ D1 C! u6 @* H4 o4 t( s; } new FCallImpl (this, proto, sel,& i! k% Y/ S) r y
new FArgumentsImpl (this, sel)));9 x+ n' \' X9 \& \- i [: P
} catch (Exception e) {
! }+ e. e: M! v e.printStackTrace (System.err);
& b9 V9 s9 H: {3 ^3 |0 m, p }: m" Q: l( J5 j2 Y9 h
( x A4 J' X- Y7 [9 {0 @6 ` syncUpdateOrder ();
. s% r; `$ I, |: q& H7 |; s; \* t" d: W
try {" [( g6 @/ q$ ~+ P9 x+ u
modelActions.createActionTo$message . f+ |2 p+ m V* t1 ?* i, F
(heat, new Selector (heat.getClass (), "updateLattice", false));# R4 U4 C+ c; i. e3 C
} catch (Exception e) {) i, L7 a- r }) W4 `
System.err.println("Exception updateLattice: " + e.getMessage ());
% B# L/ ]- }3 Z8 J( R }& n( N7 q! U' K, H, i
1 S3 K+ \) y+ U( X9 P% |
// Then we create a schedule that executes the# f! {, O9 S E" I7 M5 d+ e* p
// modelActions. modelActions is an ActionGroup, by itself it
# z k, f. {, F( i& j7 z& b // has no notion of time. In order to have it executed in& \" B7 b {( d0 N$ }8 I
// time, we create a Schedule that says to use the$ P4 h3 ]# |; c' Q0 H% Y; g1 V5 K
// modelActions ActionGroup at particular times. This5 Y) d# C: P! X; |0 J
// schedule has a repeat interval of 1, it will loop every
# z7 U& v# b* @ // time step. The action is executed at time 0 relative to
% X* C' @5 i. ~* |& l // the beginning of the loop.
- M7 C- |; }) J: n+ x, b5 x7 D# b7 W$ Y; Y5 D7 i7 X. E+ K
// This is a simple schedule, with only one action that is
! K/ r8 x: Q6 j' R4 B% m0 { // just repeated every time. See jmousetrap for more
. L4 ?1 R% \" Y* @3 ]' d X& t // complicated schedules.
0 r1 v \9 O- D8 R& m 2 l' R( k, `# \+ ^+ i8 j
modelSchedule = new ScheduleImpl (getZone (), 1);
5 H4 z/ W# A. v, ]- P modelSchedule.at$createAction (0, modelActions);2 H( d# v2 U4 o- ^+ M
$ e Q# y8 M4 h! O
return this;
+ v7 z t5 E) B: s5 J8 r* s } |