HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- u% U* `( ^5 g$ O! b3 E
. O0 G! ?% n$ n' s
public Object buildActions () {
4 {! P! {0 c# }- z ^6 h super.buildActions();
* D, v; q, a; T+ m% G/ { / k. F. U [7 i6 a" H7 Z4 A
// Create the list of simulation actions. We put these in
# C2 k8 a8 w1 h$ b' }; ~# l0 V; Y# i // an action group, because we want these actions to be
' l* j; U. `. T7 v // executed in a specific order, but these steps should( A7 |+ S2 w ]0 c8 ~/ I
// take no (simulated) time. The M(foo) means "The message
! r r' m! I7 a5 w4 c // called <foo>". You can send a message To a particular& s( X7 s! R# p( H4 r4 U) s2 W/ ^
// object, or ForEach object in a collection.6 V4 R* N6 E& u. p0 X% b
3 R+ v0 }7 p9 O6 P! p4 U
// Note we update the heatspace in two phases: first run
: f' d* D n2 | q+ g+ o% F. R // diffusion, then run "updateWorld" to actually enact the
8 m$ m8 h) Y. @( q- b6 L. ]' z, @ // changes the heatbugs have made. The ordering here is1 s8 Y& E1 O" Z8 q% i& Q ^
// significant!* a+ n* F4 T' \4 w
0 }7 N, \0 b3 @- f% m( ]
// Note also, that with the additional
7 j+ N2 E; S2 N // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ b! M" j; L. ?, M" {( v" b8 X6 i // randomize the order in which the bugs actually run
0 ?1 @& U( ~; [ // their step rule. This has the effect of removing any
/ f, A# h0 P0 S+ {- @5 A( w% i // systematic bias in the iteration throught the heatbug
* n( H2 I2 w( M4 q, x // list from timestep to timestep
2 A p% J6 `1 ?4 o4 Z( p) U 2 U8 L! J" j+ W9 S& X' v
// By default, all `createActionForEach' modelActions have
2 R& |: G: I5 t4 d( n; Z // a default order of `Sequential', which means that the; P# V+ \# B# h" L+ H6 d4 v5 E
// order of iteration through the `heatbugList' will be6 `' l, C. ]( Q
// identical (assuming the list order is not changed
" m+ a& Z- S" A# q( J0 L( _ // indirectly by some other process).
$ l4 P- o: C5 j 2 }( d; _) K: x) P; Y
modelActions = new ActionGroupImpl (getZone ());& J6 E; ^' h. S
/ G2 j f+ \6 X& z6 H
try {: H' u, W1 u4 c/ Q6 |2 S# C$ s
modelActions.createActionTo$message0 e( ?' w: y5 E+ [5 m/ O2 h7 y' F
(heat, new Selector (heat.getClass (), "stepRule", false));* N! G! Z- A2 O7 p2 X* a
} catch (Exception e) {- Q) y R& w& M k
System.err.println ("Exception stepRule: " + e.getMessage ());
- ~8 y0 U1 Z8 y- s }
4 A0 q5 B7 D4 n6 b3 E3 d+ K5 k3 j- y
try {( c C8 J g7 g, @2 k$ e" X+ ^
Heatbug proto = (Heatbug) heatbugList.get (0);- s" A% S, [ z+ q; U3 K; W9 z
Selector sel =
2 D; Q9 U7 x# g6 ?; U new Selector (proto.getClass (), "heatbugStep", false);
) j! N2 g; Q' \/ x) _) Y! M: [: T actionForEach =
/ w% S& @1 G* p5 E modelActions.createFActionForEachHomogeneous$call
* C9 R, [. T1 _! i; ^! `; Y+ t1 r3 R (heatbugList,
& e ^- G: d: ~+ d) L" \% S- p6 c new FCallImpl (this, proto, sel,+ \3 W2 C7 Y9 z
new FArgumentsImpl (this, sel)));
: V. B+ D+ _. V" v/ { } catch (Exception e) {: N! v& ]$ m# {! d8 T
e.printStackTrace (System.err);) |7 O3 N$ q3 C1 u# J. Q
}
4 `! d) N4 |0 H( g. k1 r- W6 Q& d
/ L, Z# [* U% E syncUpdateOrder ();
, T) ~+ ~/ a, t$ M8 |1 ^
' ]. U/ q5 R, U$ Y4 U try {4 V6 N. i# i. j4 T5 D) z, C# {1 q& \
modelActions.createActionTo$message
* p2 G/ q/ W6 y3 S2 O (heat, new Selector (heat.getClass (), "updateLattice", false));
; N' R, V( D: x/ a5 p/ V/ i' s4 Q: k } catch (Exception e) {: n$ {+ A$ Q; ~$ f0 g- }* T+ v
System.err.println("Exception updateLattice: " + e.getMessage ());
3 T/ w; x- h6 I }
5 H6 Q5 V- o+ `
1 x6 U4 I/ ^' B2 \: ? // Then we create a schedule that executes the
a6 h9 h& e J, {! ~& O0 U // modelActions. modelActions is an ActionGroup, by itself it& B* }" Y3 x2 k5 E
// has no notion of time. In order to have it executed in
+ O% M2 N! J2 e8 P // time, we create a Schedule that says to use the
+ Q4 }4 E" i! b( x$ C // modelActions ActionGroup at particular times. This* V3 R0 ^9 y! T! g( c
// schedule has a repeat interval of 1, it will loop every5 d4 A7 n$ \# V, r' P
// time step. The action is executed at time 0 relative to" L& F& Q, B" }" B5 m. d# J
// the beginning of the loop.
& _$ l3 ^: R7 x; r5 h: U& n+ s1 j5 O, Q3 f! h% n4 q' l
// This is a simple schedule, with only one action that is7 ]/ V0 [ F& @
// just repeated every time. See jmousetrap for more
3 ]0 ~3 O* h. V: L* g- } // complicated schedules.
7 U6 h/ }* ^% _7 I- B: @3 w& [
% ?/ F8 r `" `6 X- P+ {% |- S modelSchedule = new ScheduleImpl (getZone (), 1);
+ u! W8 Z C9 _5 Q modelSchedule.at$createAction (0, modelActions);
+ V4 O% U! _, i$ k) j ; g& }1 |2 i7 M( o) o. E- E/ ?. p: ?
return this;9 s3 [5 J7 F+ w6 V1 J
} |