HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% V r: l0 v9 S, l _* v5 r5 @( s; L3 q
public Object buildActions () {
6 A: ~0 a- S* ]" N# r3 R0 i super.buildActions();. P! ^4 U, K% B% j
# x2 ~, S, D% i1 |: P // Create the list of simulation actions. We put these in
" M" I( @* Q, _ // an action group, because we want these actions to be7 k3 Q- g. D1 Y$ M; Y, {: d' \
// executed in a specific order, but these steps should: J8 N' G7 g8 F8 g+ J2 v: b( R( G9 b
// take no (simulated) time. The M(foo) means "The message
1 J3 p" r; J. P$ A // called <foo>". You can send a message To a particular
- B% l) ]. o Q // object, or ForEach object in a collection.4 T" }* G( h) w8 ]: a
`( Y$ X! Q8 ]. N. `$ } // Note we update the heatspace in two phases: first run) p# x1 k$ q6 N" E& M; \6 X
// diffusion, then run "updateWorld" to actually enact the6 K9 @8 k Z' v
// changes the heatbugs have made. The ordering here is' w. k0 O* Y Z0 z* ~; e7 R
// significant!
6 H E6 G ]0 O- h1 ^- A
$ a7 D) D, q8 P" n2 M, Y) } // Note also, that with the additional: `9 z# m4 N/ ]# @! A; J' ]" j
// `randomizeHeatbugUpdateOrder' Boolean flag we can# o0 g* t& ^) v3 |3 M
// randomize the order in which the bugs actually run
- F0 U% ]# R6 h0 Z // their step rule. This has the effect of removing any
7 W* a: A% V7 @* ?8 o1 y) N3 Q // systematic bias in the iteration throught the heatbug, R8 v. v+ i4 t: H+ }
// list from timestep to timestep3 }# d/ m0 V( O, r1 v% K
! h4 V: Y- N1 K( v( b( l( @
// By default, all `createActionForEach' modelActions have3 C$ x6 v& m) J7 _) L. C' K
// a default order of `Sequential', which means that the4 P" K9 A& D; \0 D3 c
// order of iteration through the `heatbugList' will be2 |' Z4 Y+ S# |+ c
// identical (assuming the list order is not changed
4 [ V$ r/ {7 V: R1 P // indirectly by some other process).) a1 C5 S4 u9 K$ `
' T. Y7 h8 k8 N: G0 U! N5 w) [ modelActions = new ActionGroupImpl (getZone ());. a$ D3 |" Y C. I& D' n
% M5 q$ F# Y) |. | try {
3 t5 Q" S7 M5 W5 T modelActions.createActionTo$message
7 h& L$ Z! |; H' r4 W0 s (heat, new Selector (heat.getClass (), "stepRule", false));
( b5 _6 M0 s$ n- K$ s& M } catch (Exception e) {
6 q; K8 t. h4 e System.err.println ("Exception stepRule: " + e.getMessage ());
+ N2 o& ~1 Y6 F8 J }
+ i7 w) y# R: M' L
- E7 E! k+ ~, ` try {- ?+ H1 o) z. B4 ~5 A
Heatbug proto = (Heatbug) heatbugList.get (0);
1 k0 g' s: L0 ?/ O+ D Selector sel =
7 i0 U& f4 g5 a new Selector (proto.getClass (), "heatbugStep", false);' j& q+ w. {/ s
actionForEach =4 B9 t O8 {% G
modelActions.createFActionForEachHomogeneous$call% D8 v( V; D" \8 ^+ W8 q7 I$ u
(heatbugList,, N% Z- k* ?. t* \7 j& Y
new FCallImpl (this, proto, sel,8 b- f% K: y8 n" ? B% W
new FArgumentsImpl (this, sel)));
" C6 W* V: v; g% W } catch (Exception e) {0 a1 j: a6 J& S# p9 d. K& [& C
e.printStackTrace (System.err);
! f, ^, O+ j5 y+ ^7 V7 u }2 `$ y* H% x3 S9 ?) R8 D
7 _- J% {. T3 S( U5 D# c% d
syncUpdateOrder ();, e+ r/ B& Y7 A8 S7 ]6 J
2 b. ~ a0 {0 h5 P
try {
/ J# p2 r$ s, G/ o6 w' u& W2 C- g modelActions.createActionTo$message
. E2 l; H: n2 U3 w (heat, new Selector (heat.getClass (), "updateLattice", false));' ^+ y0 r, o4 A' P( E
} catch (Exception e) {% ~( ]1 I5 ?$ P' ^% \( e
System.err.println("Exception updateLattice: " + e.getMessage ());
8 P' \3 p$ z% j; V9 I+ D }- u1 V+ J, V, E% J4 n! x
9 V g' B& M4 k4 }" H- o- u. l; S
// Then we create a schedule that executes the2 f$ J& f% s3 w0 N: ^5 \
// modelActions. modelActions is an ActionGroup, by itself it
4 i u1 V1 n6 H, G7 y: n/ R // has no notion of time. In order to have it executed in9 _% R/ `' H, k. j, j
// time, we create a Schedule that says to use the
* i( I1 [9 A3 y+ {: k( L // modelActions ActionGroup at particular times. This
8 J+ o: W( c# G1 T1 k J$ T3 K& G // schedule has a repeat interval of 1, it will loop every+ y! h9 g* T5 `' J1 o
// time step. The action is executed at time 0 relative to
* D* R2 {1 }0 [2 S5 y# R // the beginning of the loop.
$ b. Q2 ?. T$ O: Q8 Q; M6 l
" |& _, y( }2 {1 i/ q& A; t // This is a simple schedule, with only one action that is
/ w- [6 ~9 ~1 j( h3 J1 Z // just repeated every time. See jmousetrap for more* A7 z0 o5 Z* [. I
// complicated schedules.! @% f- I8 _6 A( j) j* I6 v9 y
' s2 z% q/ J' H8 o; D) c
modelSchedule = new ScheduleImpl (getZone (), 1);
6 Z1 i4 G# K$ A8 C* {) X modelSchedule.at$createAction (0, modelActions);- F$ U& n4 w3 i$ R1 o: }
0 a Q0 T, k0 _/ O- s0 W
return this;, j* F- F$ J" b/ C1 w* z U4 m. |
} |