HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& R' w% Z5 n, S0 V- p x! _* J0 ~. N- \4 Y& V) b
public Object buildActions () {
$ U% }& E* Y5 a3 K2 q" b6 v1 Q1 _ super.buildActions();2 W+ _0 _1 l- n* ]* y
% m5 Q3 j8 P8 b c2 ~ // Create the list of simulation actions. We put these in
1 F2 b, }8 t% j' R/ |" |3 \ c+ N8 b4 W // an action group, because we want these actions to be8 @3 a. [8 P5 u0 h
// executed in a specific order, but these steps should
# G$ H% b# r+ | |, Z' O) a% z0 h( b // take no (simulated) time. The M(foo) means "The message
' f9 G1 r& c8 p n // called <foo>". You can send a message To a particular% L% z- H/ E2 `& ^
// object, or ForEach object in a collection.
' E( ?1 [! ]: @" x* n 6 g3 `& J, B4 u9 z4 \) ^! U
// Note we update the heatspace in two phases: first run5 y* D7 K) y0 n! [
// diffusion, then run "updateWorld" to actually enact the
4 U0 @. g* i1 x' ~ // changes the heatbugs have made. The ordering here is
" {. d9 ?" Y7 j6 ? // significant!* D' l% d* S/ Y: {* Z" H3 k6 ^
, v* X, o/ x! D+ W' z' C6 c // Note also, that with the additional3 Q1 J& h$ z |$ g! M5 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 y$ {. L2 \, E
// randomize the order in which the bugs actually run1 X" ~- y" `9 N/ p( V" A+ P
// their step rule. This has the effect of removing any8 z0 C: |$ R7 T m6 F2 X
// systematic bias in the iteration throught the heatbug
$ e& k. U7 H% }* I! C // list from timestep to timestep
% [* v4 ]8 i8 Z: X/ X r5 V
5 a9 {' m) E/ t3 z. b% o4 i& z // By default, all `createActionForEach' modelActions have
( S5 R- W, l8 o% r+ Y! e' G // a default order of `Sequential', which means that the$ Q/ T1 [# F+ v
// order of iteration through the `heatbugList' will be
. C3 {+ w0 O7 p# B0 L/ q, Z7 V // identical (assuming the list order is not changed6 f: V1 k- a- @$ C
// indirectly by some other process).2 g& i* ~5 z* _4 z+ x
1 a5 ^$ k, a5 F) P! w Q
modelActions = new ActionGroupImpl (getZone ());
$ |% f; P+ o; } R3 a+ O' j$ b
2 }3 V! W- y3 q/ Y" t% s h try {
$ |9 M& V* D2 f4 j5 i modelActions.createActionTo$message
6 [& o- f- s1 [# X! k u (heat, new Selector (heat.getClass (), "stepRule", false));
6 V0 X* ~$ k$ g# G: L5 v } catch (Exception e) {
' @" j3 O1 r- {8 ] F System.err.println ("Exception stepRule: " + e.getMessage ());
f, c* z( I/ q0 U$ m2 o$ P$ G }
/ g) b A6 K, h2 y
5 o4 N/ S% ~6 v: N) ]0 p: M try {9 t! J% Y6 l2 g+ b$ _
Heatbug proto = (Heatbug) heatbugList.get (0);" D% o2 ~' T& Y, F$ D" w5 @
Selector sel = 4 u$ w+ n8 Z" {
new Selector (proto.getClass (), "heatbugStep", false);; I4 a {1 R$ N! ]/ f4 v8 U
actionForEach =
% V* i/ d j/ C( u modelActions.createFActionForEachHomogeneous$call4 @) q) k4 h7 S7 n8 Y
(heatbugList,7 p6 G* X8 C+ ?
new FCallImpl (this, proto, sel,
9 e j/ |0 P, k$ l9 S new FArgumentsImpl (this, sel)));- b! o) d9 ]. P
} catch (Exception e) {0 Q5 Z6 z# A. r% g7 g5 m- s
e.printStackTrace (System.err);
2 J( d+ U+ U9 A }$ W9 }: K' z* J
! d7 y# }" c5 G* X$ L7 z% ]7 ]
syncUpdateOrder ();4 C0 V+ r6 O/ R) N7 K6 e) A6 h
, k" c# Y3 O0 \! P8 Q5 E* _
try {3 Q4 p- N0 b+ S$ o; t! s2 t
modelActions.createActionTo$message
- l( _+ c6 {( P (heat, new Selector (heat.getClass (), "updateLattice", false));: Y+ G) F3 t2 W8 h% s, K/ X3 X- H
} catch (Exception e) {
6 G& e1 W3 d& k2 A9 l7 h System.err.println("Exception updateLattice: " + e.getMessage ());9 L! S7 ~. `- X% \7 Z3 r& ^; T
} p: n' c$ u B. X1 \# c$ f
6 W5 P8 B s/ d* F9 u; e& f
// Then we create a schedule that executes the
$ L8 K4 ]' \; E0 r // modelActions. modelActions is an ActionGroup, by itself it0 x4 V3 N/ b# Z8 Y$ t' k% _
// has no notion of time. In order to have it executed in
5 z& b5 d5 f9 i* K+ Z/ \) J/ a // time, we create a Schedule that says to use the
/ f; n, R* [5 j* { h: E // modelActions ActionGroup at particular times. This
+ R4 k3 g7 l5 U/ @* K3 U2 |6 g/ y // schedule has a repeat interval of 1, it will loop every9 B% w- T! a- `( n
// time step. The action is executed at time 0 relative to2 j) j1 [/ m3 e J
// the beginning of the loop.' ^/ ?, M0 q' m3 D
) i* R% k- P! V6 n( F3 n
// This is a simple schedule, with only one action that is
* o1 Q) B/ q/ p% W$ Z // just repeated every time. See jmousetrap for more, M, F4 q3 I5 }+ z8 K7 a: `
// complicated schedules.
$ m: L# U# R) B0 s2 k5 ?3 t 6 O, d- r# L& b9 P
modelSchedule = new ScheduleImpl (getZone (), 1);
x1 B! }5 F$ r, G4 U modelSchedule.at$createAction (0, modelActions);
/ v4 j1 Y- B, y& n* K 4 @' b( u9 s8 r( X2 X4 z j
return this;8 ?7 v5 m% t' S# G/ o
} |