HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( d' M% r1 O7 z( Q
, x5 z* Y9 r, H- u* P/ E
public Object buildActions () {* z, @0 }, Z1 H- z
super.buildActions();3 ]6 @5 \0 R9 B
* N4 z% p/ c) t // Create the list of simulation actions. We put these in
8 G j, c+ s, {9 T // an action group, because we want these actions to be) ?2 I3 {; Y+ M8 e/ V5 s; x6 O! H
// executed in a specific order, but these steps should; d8 J; T3 a) i2 h' }- _
// take no (simulated) time. The M(foo) means "The message
/ ]1 j( S# ?2 p( I+ D' k- Q // called <foo>". You can send a message To a particular/ ]: p% k* G; T
// object, or ForEach object in a collection.
6 B7 n7 R4 u" E4 D/ ]* J
. T* ?; j1 r: @, U1 B8 D // Note we update the heatspace in two phases: first run
p# N, K7 X n& }( o // diffusion, then run "updateWorld" to actually enact the+ p- j% c" b- m+ i6 ~
// changes the heatbugs have made. The ordering here is l$ v3 i# H; B& l4 v
// significant!: @, z9 x# i) s( r9 E U
7 X1 z" A8 r$ ^6 {( |0 O" K$ N // Note also, that with the additional
. }0 [6 M( c8 D5 z$ q% ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ N3 J6 M& y8 R' V- X2 {, c // randomize the order in which the bugs actually run
. a6 s( h% d' ?0 K1 B // their step rule. This has the effect of removing any
4 x1 W1 T9 J) w# h0 j! o6 O6 U // systematic bias in the iteration throught the heatbug! Z. o: W1 A# L& z4 r6 B) v; g+ y% K
// list from timestep to timestep3 P( q; Q' ~$ J
5 }- x$ E. I1 U7 k$ r# ]5 n( K
// By default, all `createActionForEach' modelActions have
5 s7 @+ r2 G" n' Q6 l // a default order of `Sequential', which means that the ]* ]/ k& L8 s* t. v: |
// order of iteration through the `heatbugList' will be
, v; G! e. j. W! a // identical (assuming the list order is not changed
- G. {0 y% x' c // indirectly by some other process).: o2 v+ v7 T7 z
: Q" `% e2 @1 r2 R
modelActions = new ActionGroupImpl (getZone ());2 \! y$ G) u: t4 N( i
$ a% S- F: y( |: J1 |( r try {
, M$ W9 Y' d5 B1 ? modelActions.createActionTo$message2 w3 O3 q! a# y6 ]& K- h
(heat, new Selector (heat.getClass (), "stepRule", false));
" Y4 |* H6 E( u# B6 z1 H } catch (Exception e) {6 \, k# B7 G" ^* s
System.err.println ("Exception stepRule: " + e.getMessage ());( y6 O: p m9 R S4 E
}
3 b0 C, s% [3 k) z: t+ e5 b; n: n) v# g+ x
try {; X$ o/ Z6 T6 E* Y3 \8 N
Heatbug proto = (Heatbug) heatbugList.get (0);
7 H/ g+ D9 s& r5 X7 p) O0 j" k" ] Selector sel = ; U6 S$ {+ t9 i* o0 t+ D
new Selector (proto.getClass (), "heatbugStep", false);
' U& Q0 x; l/ h- k' | actionForEach =; u, f! ?9 a+ B/ U* H& Q
modelActions.createFActionForEachHomogeneous$call
1 f2 K6 `1 A8 R (heatbugList,9 ^& `% _" x; ~4 j) z8 f
new FCallImpl (this, proto, sel,$ X4 g& y# J5 b3 Z8 H
new FArgumentsImpl (this, sel)));8 M6 a+ K4 U1 U# R/ ]& [
} catch (Exception e) {
, c z4 T4 l3 ]2 h' D/ Y! F e.printStackTrace (System.err);
& M' o: j# e o+ b2 g* s }* K3 _% J3 B+ X; z7 @
) N' G' q% S5 y# ?! H3 [+ F2 R7 y syncUpdateOrder ();, O0 ~4 t/ m* {$ i8 E
9 F" j j% g9 c; O7 p% h* E
try {2 _# X. U8 [6 x. \
modelActions.createActionTo$message % A" {0 m' R! O
(heat, new Selector (heat.getClass (), "updateLattice", false));
& |* P2 c! g% E- T$ c' N } catch (Exception e) {
3 w5 E' l% @) a* ]7 L System.err.println("Exception updateLattice: " + e.getMessage ());
* \# h1 f- B$ ]$ {3 K4 ]+ g }7 F/ H1 q+ F6 h* {) `4 i
! f; X2 X' e$ q2 K1 D2 C3 m // Then we create a schedule that executes the" J8 B9 @0 i5 V* K- o% m
// modelActions. modelActions is an ActionGroup, by itself it* e' H7 o8 F6 c6 Z9 O3 ~' X
// has no notion of time. In order to have it executed in
% ~- ~% s: v w6 G5 K( n // time, we create a Schedule that says to use the* O* t+ z0 r* `- Z6 Z
// modelActions ActionGroup at particular times. This. k2 q7 O2 r& L8 s9 `
// schedule has a repeat interval of 1, it will loop every! Z, Z; {/ Y# P2 I x4 r. ] k# z
// time step. The action is executed at time 0 relative to
" A3 Q" \; U! K1 \2 i& g // the beginning of the loop. r# |6 B# ^; G
0 ~6 @7 j) b6 i( ?: U2 Y
// This is a simple schedule, with only one action that is; K/ w. [! \* x5 I* G+ \
// just repeated every time. See jmousetrap for more- n$ V9 S6 \& d+ m' e: U
// complicated schedules.
7 K9 [( U2 l0 X: R
( I0 U# f: S6 } modelSchedule = new ScheduleImpl (getZone (), 1);3 K+ e1 Z6 n5 j+ f) b, h
modelSchedule.at$createAction (0, modelActions);$ N9 o2 w5 m, w3 {1 B U
, |, c! G9 `( M5 k5 n, H/ y
return this;1 ~6 I9 D) T$ x
} |