HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) R4 x7 f0 [0 [
; B* B5 J% _* l2 Z. r1 B6 b# W public Object buildActions () {' e" \2 D i7 l) }+ u9 v% a
super.buildActions();7 j9 q) a" z/ O* o
7 Y" t* @4 N* k
// Create the list of simulation actions. We put these in& T1 R( n9 H+ \
// an action group, because we want these actions to be
0 d; ?) U8 W- U$ n( w // executed in a specific order, but these steps should
4 t1 f# @# }6 n9 N, [$ b: b // take no (simulated) time. The M(foo) means "The message
0 t# V r0 A5 f% t0 B& M; Z // called <foo>". You can send a message To a particular( x3 {. c( b2 I* q. |$ a/ Z
// object, or ForEach object in a collection." v2 V) R" t5 _
! B* ~( y; f- p+ C // Note we update the heatspace in two phases: first run4 U5 w* x. D( h4 j: A2 G
// diffusion, then run "updateWorld" to actually enact the; n9 h7 W7 A1 S7 k1 E
// changes the heatbugs have made. The ordering here is5 X9 G3 B2 M5 b2 l5 v K6 L% j7 |
// significant!$ z+ [( A2 }0 \; l6 g) ]* A
4 A( w9 p+ w: d& @
// Note also, that with the additional
3 v( W: t o5 w // `randomizeHeatbugUpdateOrder' Boolean flag we can1 m' ^$ Q" J4 a, O8 A* A" k
// randomize the order in which the bugs actually run
6 y! @, w% G! i/ ~. P! Q // their step rule. This has the effect of removing any6 ~% I' Z; @: y
// systematic bias in the iteration throught the heatbug. a( Q1 ~5 ?& w [
// list from timestep to timestep. H i4 L9 @5 }5 M! {: ^
! l+ }7 c& u# L4 i! N // By default, all `createActionForEach' modelActions have
, H$ Q. u. R4 E( H4 F // a default order of `Sequential', which means that the
* K* f. K( }* B5 p' q7 o+ \ // order of iteration through the `heatbugList' will be
. ^( e9 A- h/ c) s // identical (assuming the list order is not changed; t0 i. x2 a0 Y6 L5 L
// indirectly by some other process).
& j+ g* e T9 o* B. F8 i; q4 p
3 f7 Q( d; ]; K; P/ ^ modelActions = new ActionGroupImpl (getZone ());, b' j) X; [4 }+ h0 o; u5 ~
7 d+ P. I, R: J( Y& \
try {; Y# A/ F, [% D% I! l W( l$ ~
modelActions.createActionTo$message
+ p) T3 \+ k& N (heat, new Selector (heat.getClass (), "stepRule", false));
! ? W5 E" o$ t. w: ~' Z& J" W } catch (Exception e) {) `: u( K5 `) y' Q! e
System.err.println ("Exception stepRule: " + e.getMessage ());
6 l% u C+ y, [# Q }
4 q* f6 p* b& ]/ R5 D
! I# v6 r! s& g+ t try {; O) r" _* I7 E$ W9 Y
Heatbug proto = (Heatbug) heatbugList.get (0);
0 u6 t5 z. ?3 {4 M Selector sel =
5 W! g& r) ?* d) J. f# A5 k" I; a/ E new Selector (proto.getClass (), "heatbugStep", false);
% h$ Y/ O8 O1 U: J" U actionForEach =: z( u; b! b' k1 f5 R
modelActions.createFActionForEachHomogeneous$call
1 ^3 `' [& r6 C3 g: u. Q2 B (heatbugList,9 |- R# _( l$ q; {7 ?
new FCallImpl (this, proto, sel, H+ p6 F2 Q" q
new FArgumentsImpl (this, sel)));
3 a& b% T9 t3 M } catch (Exception e) {5 k* [ l8 Z [9 N% p
e.printStackTrace (System.err);4 O `6 h. ]8 y) Y% k
}' H0 P; p6 F% c0 z% n% [( c
* z- E& o/ F' |1 B3 J* P$ S% g syncUpdateOrder ();
6 c' x1 \+ \6 `# L' Z; `" ]
( s5 U( M3 T* e" y3 E2 h" m& t try {
% H' H0 c d9 z: S4 Z9 @$ o; b modelActions.createActionTo$message * Y" c0 }# J; n2 m% M& y
(heat, new Selector (heat.getClass (), "updateLattice", false));
* g0 K% o! `6 E) S+ {; P } catch (Exception e) {! x+ i- @/ @, s
System.err.println("Exception updateLattice: " + e.getMessage ());; i. a4 _9 ?2 v3 }9 y( ~' U! u7 [
}
9 F4 ?# M4 k1 p3 m, |: ?
) {1 u& p$ u0 I* N( A# b // Then we create a schedule that executes the' C, G2 s v/ o) u5 t e
// modelActions. modelActions is an ActionGroup, by itself it
, Q% {( P# L5 u! T$ T$ b( s // has no notion of time. In order to have it executed in
' H4 N" o& I$ F8 R // time, we create a Schedule that says to use the
: l6 c. U" Q, u) f( l // modelActions ActionGroup at particular times. This5 b) E% M5 A) q& w" B% @2 y+ ~- a
// schedule has a repeat interval of 1, it will loop every% b& t; Q( c: h! E! E5 l* L: ~( ^# K0 m
// time step. The action is executed at time 0 relative to% L. O4 ]* I7 ^4 c8 h
// the beginning of the loop.
' A$ N8 \; {6 G8 E: u+ n, Y, v) X
% T# K9 @2 }; R3 `+ A // This is a simple schedule, with only one action that is8 {5 A+ j, |- L S: H3 D4 L# `9 G
// just repeated every time. See jmousetrap for more
; A( p8 A: Y6 B+ X7 ~8 c0 Z, D // complicated schedules.
& \1 \6 o# u& y( |0 ?8 K+ ` , T$ ?" b+ o% x. B) i! ~) Q3 c, T& d
modelSchedule = new ScheduleImpl (getZone (), 1);4 J* b) W" F# V$ _: E+ p& `
modelSchedule.at$createAction (0, modelActions);
8 [7 r, k2 Y0 _& v# `5 k $ a ?- \! F; @+ L3 K; `
return this;
/ x- }& {. L# ^ } |