HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
j- o( f$ l2 z4 P3 F* i9 z* a. p o) k8 P2 }
public Object buildActions () {
1 Z" B' n/ P8 r3 t super.buildActions();- [' N( i5 N2 [
) r7 U( C9 f4 }
// Create the list of simulation actions. We put these in5 X, [; K- r. Z+ W* K! S8 J# l& |1 ], n
// an action group, because we want these actions to be
7 z6 |) w$ h3 _ // executed in a specific order, but these steps should& E: J# J* X' v0 u* F, O1 h
// take no (simulated) time. The M(foo) means "The message# {# L, s" ^, E6 b! e5 H
// called <foo>". You can send a message To a particular
! P. Y$ s$ K$ i- G // object, or ForEach object in a collection.5 M- U/ o: d% ?5 T: O
& y+ _: B+ [, S2 g1 R8 ~ // Note we update the heatspace in two phases: first run2 ^0 R; o6 {: S# @9 v. e: J$ ^
// diffusion, then run "updateWorld" to actually enact the8 k0 ?, T1 J& x W9 p. \0 Z' s
// changes the heatbugs have made. The ordering here is: n( i4 @. B8 x0 x) J# f' g$ v
// significant!$ w6 ~- F! H/ H2 u7 r
# e. T4 s7 o# ~; G4 I r- f- \ k // Note also, that with the additional
+ t8 w. n, G; o' ~. ^7 l // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ e$ I# U" a' R; V' `. }4 W' N1 H. | // randomize the order in which the bugs actually run4 r+ I6 L5 I% k2 M5 x. u
// their step rule. This has the effect of removing any
2 C- V) x- K) w9 Z$ \ c$ o: R2 `- w // systematic bias in the iteration throught the heatbug
( y; \6 L- d$ B; g! `0 G // list from timestep to timestep" V/ u3 P1 N. B* D5 {
* q" u$ c) W! C$ Z7 W
// By default, all `createActionForEach' modelActions have( g" f. k. N- n& |5 a7 {
// a default order of `Sequential', which means that the
6 H3 D* h, T' f. i // order of iteration through the `heatbugList' will be. Z$ N6 \$ p2 U# {) T c. u+ q
// identical (assuming the list order is not changed
) E) {. [6 C6 _3 t // indirectly by some other process).
+ A8 e) L3 @. p" _: c / N& X/ x4 u& Z3 n7 u& H4 K. I
modelActions = new ActionGroupImpl (getZone ());/ m$ z4 Y2 ?' F% N( S
3 O; Q& T2 W. i6 |/ F2 x& F0 w$ a, ?
try {
/ s) o2 Q6 v: b, y6 _/ [ modelActions.createActionTo$message
/ x2 q! ^! _. H1 x0 ^ (heat, new Selector (heat.getClass (), "stepRule", false));$ R: a$ [2 Q* W3 Z0 g+ l7 ~; k
} catch (Exception e) {# P' Q8 v' M8 Z, o& k
System.err.println ("Exception stepRule: " + e.getMessage ());
) y! ~8 T4 M; X }8 B& C- U% |7 \
2 L* ?/ V5 Q8 o* b try {
0 O3 P0 l" H9 d) y% L( m Heatbug proto = (Heatbug) heatbugList.get (0);+ y. o& Z* v# Z6 L4 \$ B% J
Selector sel =
f: O* B: f7 L" A0 K& X4 d6 g9 V new Selector (proto.getClass (), "heatbugStep", false);! B" T( @4 t3 r' }: ^
actionForEach =
! n( ]; i0 d$ N: g; ]9 g; s' X% O modelActions.createFActionForEachHomogeneous$call1 u* @& j8 g* L3 g' W; q' j
(heatbugList,6 ^ h6 P7 ^" j& B2 T( ~+ ]5 }
new FCallImpl (this, proto, sel,
: M2 M6 ?8 h; }" g$ {- x new FArgumentsImpl (this, sel)));
' L, ?; k' A( o t } catch (Exception e) {
* P3 [2 F$ p( S6 {/ Z2 u e.printStackTrace (System.err);
3 q# q8 W7 y; O }! {- Y- H8 P8 @
4 ?" G9 b/ |' Y) @9 l: c, v+ @ syncUpdateOrder ();# }- f- h- E! y
d" t: O' v" t" Q try {3 t' P7 ^3 {4 S
modelActions.createActionTo$message
& H+ _ ~9 _ y* z# j. h (heat, new Selector (heat.getClass (), "updateLattice", false));
( Z( `6 q8 v7 T H } catch (Exception e) {/ E/ d9 A6 [7 n8 C
System.err.println("Exception updateLattice: " + e.getMessage ());2 S! d* ~- v' c* `- J( J
}
' C- i" D2 s5 u5 h5 D K6 D ) }$ ?! N/ D4 n9 E0 \& B
// Then we create a schedule that executes the
8 `: o( f. f0 I7 _+ K6 k7 `0 _ // modelActions. modelActions is an ActionGroup, by itself it
% {. o- n) ]9 j! k // has no notion of time. In order to have it executed in! l% _3 V$ p8 i6 Z* I; k+ D
// time, we create a Schedule that says to use the
: x2 P# }) a: G3 f. p# k- e5 m // modelActions ActionGroup at particular times. This9 A P/ F3 ~5 w$ m! s
// schedule has a repeat interval of 1, it will loop every9 y' \" h3 o1 W4 o2 D( {- h
// time step. The action is executed at time 0 relative to
8 E+ B9 |' e. K1 u( d Z; Y* h // the beginning of the loop.: W: m% Y0 p) @: _. K3 f
# S: Q1 c( G; t5 f! Q% h! | // This is a simple schedule, with only one action that is
1 |; {8 C& M6 U( M- l // just repeated every time. See jmousetrap for more% n! U9 L9 F& N) I5 q0 Y
// complicated schedules.# i3 [# L9 r/ Z8 X( E0 B! T+ F
& }. m- l! ?0 x; D( [2 q
modelSchedule = new ScheduleImpl (getZone (), 1);) d% V- s, A, H1 l8 e0 L7 X
modelSchedule.at$createAction (0, modelActions);
; e% m! r" m. E8 o 3 N0 e) [" u# \
return this;
2 T. g5 _# W( C/ \, ?% A6 R( q } |