HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 B+ V& h4 E/ L# ^0 Z* U6 \% s; C z7 F, A0 N. C
public Object buildActions () {
5 z2 i; b! U: e: S8 D( u super.buildActions();4 z/ b4 V+ f9 n6 P. w7 T, }+ y* b
% C$ d: c7 k5 D/ b2 r6 A // Create the list of simulation actions. We put these in9 U9 O7 h0 d/ s- Q- Q
// an action group, because we want these actions to be
' C/ i# q; N( s! F4 l3 R; T // executed in a specific order, but these steps should: i B* A) X% p+ P
// take no (simulated) time. The M(foo) means "The message, O5 A ]# | B- o7 B
// called <foo>". You can send a message To a particular
% ?+ g5 r) g% K' l: J. W1 e // object, or ForEach object in a collection.
- x' f! [; o" I* s2 r3 ]
}& ?9 N* Z2 \ // Note we update the heatspace in two phases: first run4 Z* d% t2 z$ {
// diffusion, then run "updateWorld" to actually enact the
/ o( ]: O1 j1 }+ M C // changes the heatbugs have made. The ordering here is
% X, Z) r$ f9 K // significant!
$ z% l+ U* r2 {1 N( }+ `0 T& M4 W
7 I. _9 E* Q4 p% C0 ` // Note also, that with the additional
* g" x2 m" g7 g // `randomizeHeatbugUpdateOrder' Boolean flag we can
) i) p* |1 \ f: _ // randomize the order in which the bugs actually run
1 w1 n( S W4 h8 t. { // their step rule. This has the effect of removing any
) p6 E0 {& B! a$ k // systematic bias in the iteration throught the heatbug! ~. H" A& z( P+ v( y7 f' B
// list from timestep to timestep& f7 N2 w, F' ^) B+ v
, |' H2 l( J0 [9 u$ K
// By default, all `createActionForEach' modelActions have- e3 _4 c9 ?7 q) i. [
// a default order of `Sequential', which means that the
9 g/ X3 H+ |# V0 b# }4 K6 {9 Q( N1 Q3 j // order of iteration through the `heatbugList' will be6 V+ u9 j) ^6 J( Z' Y6 G* h
// identical (assuming the list order is not changed. Q, ^2 z1 w- g/ D+ p& J) H1 [
// indirectly by some other process).# \1 |. F+ s/ h8 l3 e; h
+ |0 l* d* t( \1 B: v3 M
modelActions = new ActionGroupImpl (getZone ());8 q' s5 Z+ Q3 k% K) `
3 ? M0 B8 \" h; M
try {
% b7 [+ S) q! F# z+ [ modelActions.createActionTo$message
# @8 ^1 ^. ?4 v (heat, new Selector (heat.getClass (), "stepRule", false));5 i4 r/ F$ i. Z4 s1 v9 a
} catch (Exception e) {: x8 F/ Y+ Z. |: h) d
System.err.println ("Exception stepRule: " + e.getMessage ());& y! M7 g% ?! H- ~
}
' }( L- g* d& Q: M9 R9 Q: Y0 `' K1 y9 q+ O" t( b
try {/ Q5 j3 j$ R: o
Heatbug proto = (Heatbug) heatbugList.get (0);/ `+ m3 }7 A: u6 u( S
Selector sel = 7 ?5 B+ K# x5 m, ~" L
new Selector (proto.getClass (), "heatbugStep", false);
3 h8 ^# @% V1 s8 @" }5 }# o' z+ D actionForEach =
6 b& J# C# q' l* B1 z3 R) {* W modelActions.createFActionForEachHomogeneous$call9 i5 t2 R4 n4 q; F D
(heatbugList,
7 e8 O6 w& c. H) I* U) J new FCallImpl (this, proto, sel,
3 \8 ?4 M1 `) q new FArgumentsImpl (this, sel)));7 S. N7 d7 p1 K
} catch (Exception e) {
1 b; c$ F" U+ f' D' l) g8 l6 H/ e e.printStackTrace (System.err);
9 D* a; m' w; z7 r# | }! y4 f8 n ` H
# K& M7 W$ _8 p syncUpdateOrder ();$ z/ \9 O' z- S, p7 ~0 K; Z
) M8 A6 {: Y+ }; x& S: t8 U5 F
try {
% M" u% a+ Y% s) M; s modelActions.createActionTo$message
6 D/ h4 @4 V5 h5 V% y: o% V& l (heat, new Selector (heat.getClass (), "updateLattice", false));9 f% X" P7 J7 r( a, _/ m
} catch (Exception e) {; m* e' y( ~1 F. y* c9 v( M9 a
System.err.println("Exception updateLattice: " + e.getMessage ());
2 C- F/ w# Z: y {& Q; s8 \ }
* a. W# [0 H) n8 x& j
6 h' V# q3 U+ v // Then we create a schedule that executes the
" j5 q2 V: a" t+ s // modelActions. modelActions is an ActionGroup, by itself it' ~& Q8 @. ~: [1 S/ x
// has no notion of time. In order to have it executed in9 N9 X9 E& g, s$ A% W! w
// time, we create a Schedule that says to use the
- A% `4 r9 F' S) n1 s5 R `9 T // modelActions ActionGroup at particular times. This
0 M9 d: R+ e& G // schedule has a repeat interval of 1, it will loop every; ^1 C& b3 Q; c
// time step. The action is executed at time 0 relative to
& m) P4 T4 b- b2 U5 N* | // the beginning of the loop.
k5 g$ h* K, z4 b& I$ ^8 o% H+ ~4 u2 g0 O
// This is a simple schedule, with only one action that is8 J4 t8 I2 \# o9 W* t8 s" h
// just repeated every time. See jmousetrap for more
9 f k! T6 s( y( N9 L7 m( h, L* @ // complicated schedules.2 O+ r8 f: x" c8 A7 _/ ^$ k
1 F5 K" i1 R! m% U& u7 N" y modelSchedule = new ScheduleImpl (getZone (), 1);
' E. K$ o% V% @/ H modelSchedule.at$createAction (0, modelActions);8 w# F0 P0 {8 |/ W- G k
+ B1 y1 [* f! Q
return this;
- j h, s0 u; U6 y. E6 S( i% K4 a } |