HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 x$ C" v8 G6 |6 T/ }, f6 ]: i: }0 X" I, f% v0 U- ~
public Object buildActions () {! G$ b& |8 H" ]* Y
super.buildActions();
, p% F" f. q: P% a: J# ]
) V1 f* r2 E1 A9 E+ k& w // Create the list of simulation actions. We put these in9 i9 }9 z7 c8 \! ^' }$ u' L; d6 _: d U
// an action group, because we want these actions to be
* q- w1 q9 _# \2 y // executed in a specific order, but these steps should
4 U7 ^5 I6 S$ m: u8 T5 q" R v* r // take no (simulated) time. The M(foo) means "The message4 ]- p4 r/ q: ^) o7 @2 r
// called <foo>". You can send a message To a particular
: I, u* `7 g& Z // object, or ForEach object in a collection.
* O7 h& m3 c6 P4 K
8 G5 h% | J, L; Z2 p // Note we update the heatspace in two phases: first run
, v: c' Q4 p7 d+ G // diffusion, then run "updateWorld" to actually enact the
3 d) ]: L% }4 |1 O6 f# a; x // changes the heatbugs have made. The ordering here is& X+ Y( u# a- d8 p
// significant!
/ T7 v! d( j* a
2 `: A' K# d6 O/ O // Note also, that with the additional4 n) P0 i+ i) X+ } }
// `randomizeHeatbugUpdateOrder' Boolean flag we can) B) {" i9 K0 G
// randomize the order in which the bugs actually run
0 u7 @! P) z) ]7 a7 r5 H8 s/ f // their step rule. This has the effect of removing any
7 \+ v; b! l0 t2 `0 J! a: z // systematic bias in the iteration throught the heatbug/ S0 ~8 D+ i1 v
// list from timestep to timestep6 t5 x2 g# C1 Q/ s. ]- I+ K
$ R' Q U+ z( O6 @6 w" [' X; R: L
// By default, all `createActionForEach' modelActions have; D" Y) K9 \+ s7 r
// a default order of `Sequential', which means that the9 l7 G6 ]9 h1 x) |4 n2 |5 D
// order of iteration through the `heatbugList' will be
* d: J7 ], D% n M4 j // identical (assuming the list order is not changed9 L6 q& c* `" W* ~3 C3 T, x
// indirectly by some other process).
- p" U4 m# c5 X6 M 5 J5 ^1 ]8 C# l( {
modelActions = new ActionGroupImpl (getZone ());
8 }/ ~( `9 Y! X; P% G1 U- u
$ c' N, i) l# b) t$ X# ` try {: n( q9 R s8 n0 {) z
modelActions.createActionTo$message( m: Z/ Q/ y/ ]1 f7 A; N% i' R
(heat, new Selector (heat.getClass (), "stepRule", false));
9 g2 M8 {/ F7 A5 \+ V } catch (Exception e) {( i4 C# S5 z8 w& E# l0 {
System.err.println ("Exception stepRule: " + e.getMessage ());" c# v8 E* x" m2 P
}
. F' ]( U, M Y3 ] O. B. |2 G7 [, [1 f2 ~4 ^# v$ m
try {
5 b4 V; e( O) W# |: }6 c Heatbug proto = (Heatbug) heatbugList.get (0);( b/ [) C+ H0 c( Y1 @$ z+ o8 A
Selector sel =
6 r5 m# m; C" r) q; f new Selector (proto.getClass (), "heatbugStep", false);/ B: `5 q' u6 ^/ [. D- X
actionForEach =% T$ d9 G: {% |& @) X& f
modelActions.createFActionForEachHomogeneous$call
/ `- }5 y9 Z: @3 p3 a) y& C (heatbugList,
6 m |. E' E1 c1 J. [ new FCallImpl (this, proto, sel,
; K1 @+ d* z0 W7 Y3 p new FArgumentsImpl (this, sel)));
% j4 x1 R! H* n/ a8 g) D } catch (Exception e) {
" N5 K! g$ {- x# Y e.printStackTrace (System.err);
7 H2 F1 C2 M4 d0 P }9 `; E- {* n. T- g) {1 ?0 x
" i1 A# y1 n* q- @% O syncUpdateOrder ();
$ M& v5 M' ] K! h2 Q, B( E; |1 Z, |; ~
try { G5 @ J) S) c( [* n& e
modelActions.createActionTo$message " j! p! n% f# b. Z/ J* h& V" w5 Q
(heat, new Selector (heat.getClass (), "updateLattice", false));
- |# `3 o7 F+ R( z$ X7 J+ a7 i } catch (Exception e) {. d9 ^- j) s- l* @" z1 `% X
System.err.println("Exception updateLattice: " + e.getMessage ());
6 m. C" W$ S9 m& \5 H }* B2 {# p0 t. F* c
3 e$ M/ c8 n4 _, h
// Then we create a schedule that executes the
+ `2 ^6 h4 `6 X1 @! t/ O- F6 I! g // modelActions. modelActions is an ActionGroup, by itself it5 D& X1 P; Z1 @) @
// has no notion of time. In order to have it executed in+ {- i& ~/ x# D. q3 z
// time, we create a Schedule that says to use the. Z, z1 @% \3 U# {! ]# G. W
// modelActions ActionGroup at particular times. This6 n* y) k8 ^ c7 i3 ?, I ^
// schedule has a repeat interval of 1, it will loop every6 x6 Q. F) N3 u2 b% l9 j7 u
// time step. The action is executed at time 0 relative to
3 v5 _, |' v2 W- X# ^# _0 ^ // the beginning of the loop.6 |. G- z+ C0 ~$ ^! M S
) Y1 S1 ~. Z- O' } // This is a simple schedule, with only one action that is3 N* S/ P& g# o5 X* e6 D3 }
// just repeated every time. See jmousetrap for more
, Q- } W" x! Z // complicated schedules.
3 q4 Y; C) c: h7 [. N0 R
; W1 p' c$ G& J' s3 D) m modelSchedule = new ScheduleImpl (getZone (), 1);1 b' _ o/ g9 a' G' l8 f5 h( o( D
modelSchedule.at$createAction (0, modelActions);
0 A9 N3 a p4 x6 L% S3 Z! ?' w2 T 9 ^0 v/ U. l# N/ |
return this;
' n& l5 a x" M+ l% { } |