HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 d' a- x! R& H: y7 E/ x' T' [ X
% C+ A; G0 U4 k* l. k0 E3 @4 _( h
public Object buildActions () {
) Q( {" s/ [3 Q8 G super.buildActions();0 Y2 Z/ C. r+ b7 o
! r) h$ l7 Y( S- r# D: G // Create the list of simulation actions. We put these in2 o- j! U# e# r" \+ j" y/ t
// an action group, because we want these actions to be
7 ^" b' G3 G& T# I7 I( }! V& ]- C // executed in a specific order, but these steps should _; X. o1 E/ |) h
// take no (simulated) time. The M(foo) means "The message
& [5 X" |% J9 m1 v; P/ ?/ P7 }! C // called <foo>". You can send a message To a particular9 @, }+ L. z0 B
// object, or ForEach object in a collection.
) f6 f/ O7 G9 ~5 j. p # R" I+ o; ~6 g
// Note we update the heatspace in two phases: first run, y8 O% C J; P5 p* ], s1 v
// diffusion, then run "updateWorld" to actually enact the
* r0 D/ D6 J7 g9 ? // changes the heatbugs have made. The ordering here is% X% I- ^, r5 ^' R) a* J/ b
// significant!8 l" e" o7 Z {3 Z6 ?
+ k2 B4 [4 e. K) A9 w // Note also, that with the additional0 ?3 D( g/ n; U' e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ S- x- i2 y* ]3 ]: z3 b3 z8 ] // randomize the order in which the bugs actually run
$ X8 q6 |+ v4 n8 M' z, u2 q' G; { // their step rule. This has the effect of removing any
7 f$ R7 i0 Q$ E // systematic bias in the iteration throught the heatbug
7 q4 w" h/ p1 H( c# J // list from timestep to timestep! E" n0 T: N, \/ [* q7 o1 ^$ X
2 O2 L$ V4 x9 z8 w
// By default, all `createActionForEach' modelActions have1 U. h5 v$ v* k, O* J1 T- C
// a default order of `Sequential', which means that the- ^; l0 s8 V$ [$ y3 p, B
// order of iteration through the `heatbugList' will be
' Z0 f; T0 K4 b/ ?1 S // identical (assuming the list order is not changed' f; Q5 P1 A3 ?/ q, h
// indirectly by some other process)., m! n$ _4 H* K
4 z1 ?+ _5 D1 m5 m8 s0 ` modelActions = new ActionGroupImpl (getZone ());% |" o8 K2 k/ b) n6 m
; n) q7 p j7 N0 h# ]
try {
( L% M3 z9 l) t, q modelActions.createActionTo$message. d ?7 A: L0 i5 A1 I8 i
(heat, new Selector (heat.getClass (), "stepRule", false));: }! x2 I) U% H, H& x$ V6 z
} catch (Exception e) {" s% `. F4 B* S+ c& i3 e
System.err.println ("Exception stepRule: " + e.getMessage ());' u' I! z/ Q. o8 E1 |$ d9 \
}; [/ U: Q# S* k- y' t/ g, q
9 q8 {( Y% `8 x4 w9 ]2 t
try {. g7 w5 B) C8 r$ p; B) @+ M6 a
Heatbug proto = (Heatbug) heatbugList.get (0);
( L! Z' Z+ I/ J6 h" ?8 a# q( _ Selector sel = 8 j. s" g: @1 I0 B' F# W1 l# A
new Selector (proto.getClass (), "heatbugStep", false);
$ T3 @5 @/ @8 c j actionForEach =
* {$ _6 A& @' Q# t, m" K( E( C: z' q modelActions.createFActionForEachHomogeneous$call G* q: R/ d9 M! p
(heatbugList,4 a7 z( W! [+ v; g
new FCallImpl (this, proto, sel,! h k9 t; A% N1 s4 x- ^, d
new FArgumentsImpl (this, sel)));& m$ r* k0 V6 k5 Y0 t$ i @
} catch (Exception e) {; D5 Z" {$ O# r
e.printStackTrace (System.err);/ u2 f6 z9 H5 Z) C' w
}- C; r, A! E& p2 `
. ^+ K6 n- l( _5 [1 ?8 H syncUpdateOrder ();8 n1 Y' O/ ~$ B9 f
b1 J) e. g& d$ v, y3 h try {; S" f7 Q1 T X" X/ }
modelActions.createActionTo$message
, J( n, s$ ]1 H (heat, new Selector (heat.getClass (), "updateLattice", false));
# r$ r8 i# D9 N Y! y } catch (Exception e) {& |+ L( g1 j$ _( O" Q
System.err.println("Exception updateLattice: " + e.getMessage ());
; _9 ?" }2 w2 X2 D2 s4 v4 ^5 k( | }
( b" }' C4 D( } * N/ ^ x ~, o
// Then we create a schedule that executes the# P% L g3 [' }: v. P/ U
// modelActions. modelActions is an ActionGroup, by itself it2 E' x# x" ^# Q# G ^* C/ C! m
// has no notion of time. In order to have it executed in3 [: ^; k s: r$ D8 q- U
// time, we create a Schedule that says to use the
( J% E* o- `+ K# a9 }/ T# t // modelActions ActionGroup at particular times. This
0 l# F+ U3 d( E4 d. w1 G, j // schedule has a repeat interval of 1, it will loop every
" }3 M5 b9 @1 t# g* o' u* a+ s // time step. The action is executed at time 0 relative to: H; z; t3 {6 T) E# Z) \3 N2 ?
// the beginning of the loop." F- |2 z4 j# F4 k* `* C# K
0 q4 ^3 s% G* Z' ^. b# F4 m" m
// This is a simple schedule, with only one action that is
# x6 w& Y3 u5 _+ Z // just repeated every time. See jmousetrap for more
* [) [1 w3 n6 f% y" G* C0 A" C" E3 O0 T // complicated schedules.
$ g; J9 L' l9 w; l6 } 5 M S% p1 y1 `
modelSchedule = new ScheduleImpl (getZone (), 1);3 H& T( U; J" q! B1 q' G
modelSchedule.at$createAction (0, modelActions);
6 m! T# v6 O: q1 G. x' u
1 f$ B# Y' A2 i$ V- p" ] O8 b( k) @ return this;
0 x6 t- j. v; Y6 j* r } |