HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( P+ p' v2 S5 X: M, I! o5 ]
4 Q, }: n6 r, J: B4 m0 c( F4 z
public Object buildActions () {
/ i) F5 U; U3 W* Y super.buildActions();8 e* O, M; Q9 k5 Y8 ~5 R
3 V% r/ ^/ h$ r+ \
// Create the list of simulation actions. We put these in! r; y2 E: m& j1 L# d
// an action group, because we want these actions to be0 s/ i( A4 N% N# \4 f
// executed in a specific order, but these steps should
0 S+ i3 M- t8 V( v! V! p8 W; D0 F // take no (simulated) time. The M(foo) means "The message- I( K# r, |, q% I
// called <foo>". You can send a message To a particular
3 u: }0 w. f7 ?; @8 p // object, or ForEach object in a collection.
( S8 }" m" s2 `0 k3 E$ O
) d5 v7 L0 o7 h$ l' x // Note we update the heatspace in two phases: first run
; s# K' U: s0 X5 Q E, x, x // diffusion, then run "updateWorld" to actually enact the
f- S" e M. [% u // changes the heatbugs have made. The ordering here is- J, k( b5 @, U% ~" I8 ~2 T) m
// significant! W4 H% W9 _5 [" i) K0 v
5 h7 d( ?& u8 g8 H
// Note also, that with the additional. m2 E0 y1 z6 a) h! Z% e6 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' n6 B+ u% ]( n- t // randomize the order in which the bugs actually run( d8 ~7 \1 M$ R" j6 I: ^
// their step rule. This has the effect of removing any) j) p' p" z: o. ]" A
// systematic bias in the iteration throught the heatbug9 B. i7 B3 j+ T+ n" o( ?) ~% L3 ~
// list from timestep to timestep1 ^8 J/ V1 J3 X1 a7 \- a
$ C% F4 b4 R# I& z
// By default, all `createActionForEach' modelActions have3 f6 h( C4 j: y. l ]
// a default order of `Sequential', which means that the
5 I# e) f" h2 _% n // order of iteration through the `heatbugList' will be7 [& A1 f& n2 F9 h5 k
// identical (assuming the list order is not changed
, d) G7 X/ j3 C2 X S // indirectly by some other process).
7 _9 q0 V) Z5 h6 K6 m: s' v , n/ I4 T! T- s1 p* I0 T$ Z
modelActions = new ActionGroupImpl (getZone ());
/ j Z- _& O- {* o. {6 V, n, R" m+ r
try {
* }6 P: @2 [( i, D modelActions.createActionTo$message
$ |2 L; u4 D3 i* k/ x- v (heat, new Selector (heat.getClass (), "stepRule", false));
! J9 o. G! C2 A } catch (Exception e) {* _3 _& Q( {4 U V3 @. j
System.err.println ("Exception stepRule: " + e.getMessage ());; @) ^$ \+ ^. r" L
}
4 g* A) M) g. \& s g$ m% ~7 [
try {% G) L; z- R1 ]
Heatbug proto = (Heatbug) heatbugList.get (0);1 I+ ?3 C/ [- r, h
Selector sel = # b$ q, w' x) M: [, L) m
new Selector (proto.getClass (), "heatbugStep", false);" z4 ^ d0 }- z; r+ o, ?
actionForEach =
2 K) G2 [/ f5 T; A! q/ ] modelActions.createFActionForEachHomogeneous$call- i" N7 R, e4 Z @* H7 {+ f
(heatbugList,/ i$ j0 b, e5 r) I+ M( g
new FCallImpl (this, proto, sel,
* c: b m" u* ]$ C, l new FArgumentsImpl (this, sel)));( T1 z: [4 [" n& n1 [# J
} catch (Exception e) {3 H: v/ M! O" L8 x& t
e.printStackTrace (System.err);" M3 I& C1 r+ |1 _0 B; G: M
}
* `1 B- x! c' p9 r3 d: Q+ w! o - h# _" ?9 {0 u( w
syncUpdateOrder ();5 D/ v2 _$ ]5 N3 j
+ ~' ?& O8 E2 q. M0 j
try {) M A) g8 h3 C# R( ]$ U
modelActions.createActionTo$message ! l }: W j' L& W
(heat, new Selector (heat.getClass (), "updateLattice", false));
" l% f1 S' P# I4 ]7 l } catch (Exception e) {
, |& d6 f- ^1 f! ~ System.err.println("Exception updateLattice: " + e.getMessage ());7 q4 B1 `8 l U1 Y/ {
}. M8 }2 X; _- O1 [- D5 ?. [" \
! L) P* W5 x) S. J" c
// Then we create a schedule that executes the) u# g) C9 d! z+ ?; j% o
// modelActions. modelActions is an ActionGroup, by itself it
( S: n% |& M K7 C# g // has no notion of time. In order to have it executed in5 T# y! _" T ^$ ?: P0 U! i
// time, we create a Schedule that says to use the
9 j M# K$ \/ L: y4 Q // modelActions ActionGroup at particular times. This- }1 R* W# l9 O/ Q. J1 K3 {
// schedule has a repeat interval of 1, it will loop every
! @3 ~' m$ L# R& i7 t& {3 F$ y* f // time step. The action is executed at time 0 relative to
9 E4 D3 a- `1 k, y) m3 d // the beginning of the loop., x& Q8 ?* x! T0 r1 o4 n& F* u
5 s6 H, Z2 h1 @ // This is a simple schedule, with only one action that is
3 f. H" t1 s% `( L' j+ Q( | // just repeated every time. See jmousetrap for more3 `5 P5 I. [8 x4 r0 }3 g* _1 \
// complicated schedules.
! ^/ }9 w& U- |" z5 o 7 |# L7 r" k8 `: L
modelSchedule = new ScheduleImpl (getZone (), 1);
- x `4 F" |0 w4 F modelSchedule.at$createAction (0, modelActions);+ X5 e. d3 A3 N
8 o! `8 X3 s* _7 S return this;/ \6 f A# S3 Q1 {; C: K! q
} |