HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 w' y. v% A2 S
) [: [: y: F/ y) @ ^
public Object buildActions () {8 h I3 R) a: ^( c
super.buildActions();, A3 ]* D: [. G( D
; n2 Q Q+ j& P5 D$ t6 z4 G2 U
// Create the list of simulation actions. We put these in& }5 w/ c+ t6 ~
// an action group, because we want these actions to be; Y5 q/ R$ g$ L2 |" y: ^
// executed in a specific order, but these steps should
4 e8 }8 h' D, S; ~ // take no (simulated) time. The M(foo) means "The message6 K4 _4 c1 W& f6 P% i
// called <foo>". You can send a message To a particular
0 G+ D: w( t. H' Y' V8 [' g. t // object, or ForEach object in a collection.+ e1 y# b1 s, r) v
9 }% p+ Q" H: [9 K
// Note we update the heatspace in two phases: first run
. f d* L5 Y* [7 r! K t4 s // diffusion, then run "updateWorld" to actually enact the7 T9 ]; u3 I4 P
// changes the heatbugs have made. The ordering here is. h, J% h- o0 _& u% W/ c
// significant!
R! |6 F$ m8 L" t: P) T: V# ~- t4 P
% z( V/ |: Q1 F* W' \% y9 t // Note also, that with the additional; B# B0 N' W1 d' j4 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can- y) s n- x/ B' {2 E8 d8 W- z' p
// randomize the order in which the bugs actually run! D! z/ U% `' M! G P
// their step rule. This has the effect of removing any
* g7 J* t; e" r1 `0 Z8 R$ J3 H // systematic bias in the iteration throught the heatbug
( A7 ?& ^ h6 i$ I // list from timestep to timestep
+ T, k; R4 {9 d4 i* q3 x
) y5 ^4 L- `' q! {/ C // By default, all `createActionForEach' modelActions have
- g5 ?- d: B/ Z- K3 r% K // a default order of `Sequential', which means that the
9 @& M: ]3 O) V6 R // order of iteration through the `heatbugList' will be+ O: r) d5 ?1 ?( t
// identical (assuming the list order is not changed2 ]; y+ r5 O3 k: C8 K0 \' Q
// indirectly by some other process).
& }2 g4 f) l# Y8 l i : H" v! p0 b' c! H% m
modelActions = new ActionGroupImpl (getZone ());
# R" z$ I1 y @3 }0 g; r! }- _3 X1 P$ t
try {
. B7 x0 {7 }+ ^ modelActions.createActionTo$message" `) }/ I$ v7 W/ p ^8 G! k( g
(heat, new Selector (heat.getClass (), "stepRule", false));
, N1 ]! F, ^7 C } catch (Exception e) {
# B0 S/ k2 a/ k# F9 S, H2 t System.err.println ("Exception stepRule: " + e.getMessage ());
' ?" {* F4 Z4 g% \( S }
0 i" Z3 x" _7 e* f: f) N" Y1 r' W! n, q. t4 I
try {: L1 B+ y' }4 e: Q
Heatbug proto = (Heatbug) heatbugList.get (0);
, J+ n3 O" M( a4 b3 H6 ~0 \ Selector sel = & v/ D, ?7 C& ]4 ?/ ~4 n/ ?- d
new Selector (proto.getClass (), "heatbugStep", false);
; ^0 b9 {% i4 K! F+ d+ @+ _! d actionForEach =
% e) A* a0 E# P' k, V3 t/ b modelActions.createFActionForEachHomogeneous$call
7 o. k, Q$ e A/ Y- t (heatbugList,2 Z" C, n2 A; O. ^" p- {
new FCallImpl (this, proto, sel,
( f. R5 @( N& e2 Q' n1 K new FArgumentsImpl (this, sel)));
- I% v5 T) ?/ l# b: E& h& J } catch (Exception e) {
+ [: C7 E/ _5 Z- o" n e.printStackTrace (System.err); a% N3 G- V) x; L
}' z! T R) `& w' `- _9 E
/ ` Z4 [6 o1 |; ]: _9 T0 N2 h
syncUpdateOrder ();2 e( ~- a# X, q8 }' ]
2 M* W0 i, f7 n( d6 Z' I& a7 G% o try {
$ X! L% }5 }" g4 Z9 C modelActions.createActionTo$message
# X6 d& f+ W; z (heat, new Selector (heat.getClass (), "updateLattice", false));
4 ^- ^" k6 q+ G) b: W7 N: ? } catch (Exception e) {5 S4 A' P- `& {6 Y
System.err.println("Exception updateLattice: " + e.getMessage ());; v- @" y; A3 z1 V
}
6 o3 ?7 n2 k! G! z $ N' [' [' h n5 S4 w3 u0 t
// Then we create a schedule that executes the: n: r2 O5 U* O/ D
// modelActions. modelActions is an ActionGroup, by itself it
1 P( c. w, [3 F# w% _5 }1 n // has no notion of time. In order to have it executed in
( }1 ?: l0 @( F& A6 [& v6 B+ b // time, we create a Schedule that says to use the
7 N% a j; j+ ]) d3 X3 m/ C7 ~ // modelActions ActionGroup at particular times. This
) F- a4 V/ D% q! F // schedule has a repeat interval of 1, it will loop every7 `( P; v; H# S. Z4 v9 W4 U
// time step. The action is executed at time 0 relative to
/ f s; W9 T. D7 T& C( H // the beginning of the loop.
( D% A; C7 g% J, v5 ]- q" \$ ~3 s8 i" I1 O; Z! A
// This is a simple schedule, with only one action that is
6 A8 k, T" e5 w) ~ // just repeated every time. See jmousetrap for more
3 }1 k6 ] g2 I9 w! Z. [ // complicated schedules.
7 D. Z5 ?. t/ W( T( p
$ y! L% d+ N2 z+ ] modelSchedule = new ScheduleImpl (getZone (), 1);# C+ D+ D0 T% O5 b/ C# a7 i
modelSchedule.at$createAction (0, modelActions);
" T/ {% q+ k! b! X
. _- p6 D0 c' N- {( n; q return this;$ H! w/ [' X: z% V
} |