HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 g; k$ n) M% a2 U; y- M( p
+ j9 d7 M. r: K, e) _9 Q1 b1 i- {
public Object buildActions () {
2 K1 S; P8 u2 I' i9 p8 ? super.buildActions();: g6 H0 x+ \( R9 j; f* H6 }
4 E c, T9 i/ i* I- f' J& K // Create the list of simulation actions. We put these in
2 z% \ w8 k$ ~' Q. @7 q y // an action group, because we want these actions to be0 U7 M9 g6 `# O) |. ^& p; ^
// executed in a specific order, but these steps should- D6 ], z7 W" f1 H( _# l
// take no (simulated) time. The M(foo) means "The message1 s% s3 f; r) A" j8 f" y
// called <foo>". You can send a message To a particular
( `. i- D* I. G: v5 b0 v# f // object, or ForEach object in a collection.
4 T$ T. G0 z8 U. y( P+ s
* _- x! a( d6 E // Note we update the heatspace in two phases: first run
o) ]/ s1 L5 ]! s // diffusion, then run "updateWorld" to actually enact the
4 O" \- t& ^# l( b // changes the heatbugs have made. The ordering here is' N. Y8 f- l& _& z7 X! `3 f H
// significant!
4 e$ L5 i; L2 d8 {
& W1 p* z' j1 `% |- {8 x6 b // Note also, that with the additional
9 k) \1 L% Q, C& _. N // `randomizeHeatbugUpdateOrder' Boolean flag we can
% h& M5 }% O0 a, L // randomize the order in which the bugs actually run
: S9 x( `" Q1 i! U* \% w5 i // their step rule. This has the effect of removing any
' `( ]/ [7 |& d5 x& w6 z5 _ // systematic bias in the iteration throught the heatbug9 j9 Y! | y! ]4 B3 u3 O
// list from timestep to timestep
, } B8 q6 r9 l% x# e+ e
* ?5 G5 y" `$ E7 [+ d- i // By default, all `createActionForEach' modelActions have
% d' K7 k5 i) ]. ?( C9 Q // a default order of `Sequential', which means that the' [; i O ]3 `/ L9 J/ a
// order of iteration through the `heatbugList' will be* o$ Y" }. J2 y" `, h% o
// identical (assuming the list order is not changed# d0 b* V# J- }0 ]* J2 l$ [# t
// indirectly by some other process).
% [9 p8 ]7 U) o4 V * a* C) l6 m9 U+ N% M% U1 `
modelActions = new ActionGroupImpl (getZone ());' e0 I% t8 @$ y" y1 Q
; s1 F5 J- F- f* }8 R
try {9 M4 u* D& F! a# {6 Y
modelActions.createActionTo$message
+ O3 D' s2 U# D; j m+ s. [ (heat, new Selector (heat.getClass (), "stepRule", false));
* r4 j. q0 m2 s+ y) |* B" @ } catch (Exception e) {
; K9 ?: k) J6 O8 l System.err.println ("Exception stepRule: " + e.getMessage ());
& B. V; G0 o5 n2 ^0 ^/ Y) s }
9 M6 R' G2 j9 w' N& U# S1 S8 k5 q: y: z; g; } j% A! k
try {
3 \, ~- n. E6 t5 s: C( S Heatbug proto = (Heatbug) heatbugList.get (0);
% t3 d6 [" w) q% [/ w! U0 Y2 s- u Selector sel = 6 K1 R' x& J7 T# T1 Z
new Selector (proto.getClass (), "heatbugStep", false);
9 c) h2 u3 t: J) Y2 l; N% l1 P% ^ actionForEach =' x3 e( @( I" i O
modelActions.createFActionForEachHomogeneous$call [, U6 P! X$ {8 o% a2 q4 ^
(heatbugList,+ K, T$ Q& M7 B# X
new FCallImpl (this, proto, sel,$ l ~7 Q: ?7 n$ x) W9 w
new FArgumentsImpl (this, sel)));
; i! J7 t) j0 D3 i! c1 U) M7 \+ l+ O+ c: q } catch (Exception e) {9 n" ^. {, W1 ~: o* H$ R' n
e.printStackTrace (System.err);
3 D, l9 X7 ~3 e( B8 z }: ?5 x* j( c( ?: V1 Q
. V( A# ?. E2 l
syncUpdateOrder ();
, z+ d" e9 q. g' V
; x! v% x2 p' n8 J8 G6 w try {
/ g7 }# h" H% V2 r( a modelActions.createActionTo$message ' G& W: l& k0 D' j1 C @5 k; ^
(heat, new Selector (heat.getClass (), "updateLattice", false));
# q' `% _6 ]* Z. m; b+ f; W } catch (Exception e) {) b" }; R! q8 k- V1 Z/ N# p4 k- W
System.err.println("Exception updateLattice: " + e.getMessage ());& d; T7 y2 Y# `. H6 F$ c- b
}
6 L1 _# m; o, K4 z6 B7 |
% Q6 b: b0 O& i" \7 a // Then we create a schedule that executes the& U) e/ |7 c5 V& G/ r4 ~, G
// modelActions. modelActions is an ActionGroup, by itself it
" q) H* o9 A. v // has no notion of time. In order to have it executed in! {- F+ C: c4 y9 A2 S O' m3 Z
// time, we create a Schedule that says to use the* K7 x# m- |. U
// modelActions ActionGroup at particular times. This
0 a2 _: h3 v& [3 v7 { // schedule has a repeat interval of 1, it will loop every' M+ n7 w# c" y5 B
// time step. The action is executed at time 0 relative to
( g8 P' P8 T1 ^$ | // the beginning of the loop.- {$ p* T9 n5 Y
9 e! I: v3 f6 M, X2 k // This is a simple schedule, with only one action that is
4 p) V$ p6 d- g' h n0 o5 s* F // just repeated every time. See jmousetrap for more; O+ P) `* s w, W
// complicated schedules.- J0 Z" Q& E( ~2 l, _1 l; Y
, r# ~: l/ } W; [) {
modelSchedule = new ScheduleImpl (getZone (), 1);
+ [* M1 z$ G2 ]$ f) c% ^ modelSchedule.at$createAction (0, modelActions);; G2 O8 W* d# Y4 C
0 C% M! f" P; P' C# i
return this;+ b8 [! S9 V9 p( |% T; @
} |