HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 @; m: e: ]6 @, i8 o
, K7 {/ E. K) {) R" M0 N- Q public Object buildActions () {7 F. W, p" v* {$ T& ?6 R
super.buildActions();
: X, h$ J) T' ]2 [! M/ b # [4 {7 \2 \7 a# d6 T3 T
// Create the list of simulation actions. We put these in9 U1 B* H; w' P
// an action group, because we want these actions to be
& R# ? i2 ^6 e2 N0 p+ J6 t' ] // executed in a specific order, but these steps should' l5 _- E4 Q1 R+ D) A
// take no (simulated) time. The M(foo) means "The message
6 o9 ~7 D b( o6 i7 ^; b // called <foo>". You can send a message To a particular
9 [) V& Z$ a6 X2 Y // object, or ForEach object in a collection.
B k* e" I6 W- o, g: t0 E6 ]! t
4 _5 ? q4 }- x0 M: k2 Q% A // Note we update the heatspace in two phases: first run
# Q$ N5 K0 B2 a4 o6 a+ s7 I // diffusion, then run "updateWorld" to actually enact the
y$ f' a; A* o* [ // changes the heatbugs have made. The ordering here is
0 z' ^; y3 J8 l // significant!
# B0 c5 r6 u& H
% p+ ?! ? _4 C8 W w! P // Note also, that with the additional& M7 K7 a( c' `' c
// `randomizeHeatbugUpdateOrder' Boolean flag we can Z0 a% s; c6 u: P5 }2 U
// randomize the order in which the bugs actually run4 k# b( |$ y8 d9 I+ k
// their step rule. This has the effect of removing any
6 ~ C& n$ z2 }, j) ` // systematic bias in the iteration throught the heatbug
2 g2 c5 g+ O' Z2 C! L" B* ~! V // list from timestep to timestep
! K+ x, b3 ]. H: q2 R, d
7 S8 T9 i- a2 D/ \# q // By default, all `createActionForEach' modelActions have
, }, B5 e: N u' O+ F! a: C* m // a default order of `Sequential', which means that the$ o$ Y8 ~9 q0 W2 D
// order of iteration through the `heatbugList' will be+ R+ J* N& l: w* V0 g$ {
// identical (assuming the list order is not changed; Q% \2 o5 Q9 |' j
// indirectly by some other process).9 r$ h3 U _- X3 o; h- e/ V5 G. M
9 T1 @1 s; d" x3 L* k% X
modelActions = new ActionGroupImpl (getZone ());
* _1 Q k$ N* w0 _$ j/ U7 x. N
7 ]" n' B6 y5 ^$ c4 m try {
2 ^! W1 n! r6 T0 R8 X8 y9 {# a# m modelActions.createActionTo$message0 j! T8 ?* `$ L& y
(heat, new Selector (heat.getClass (), "stepRule", false));
' H0 T1 L5 }! p) @ } catch (Exception e) {
1 ] Y# w$ n4 R6 N System.err.println ("Exception stepRule: " + e.getMessage ());& n" T" |# O# Y6 [* Z% ?# e
} F N4 d) Z8 v+ P+ Z2 T" h
8 e4 m9 F' `$ _' A" e try {
- p g6 E" P) j$ W8 f Heatbug proto = (Heatbug) heatbugList.get (0);* q; n9 X! P$ n. z- _
Selector sel = 8 d! Z9 p0 u% n$ v7 _
new Selector (proto.getClass (), "heatbugStep", false);
. E3 i9 q, L8 ~/ E- X0 \ actionForEach =
3 R) w0 g, v2 D0 X8 I. H! _7 S modelActions.createFActionForEachHomogeneous$call( T D5 i% ~& m! k$ G9 @
(heatbugList,/ `7 w9 X; p% f; ^, W' G% ^
new FCallImpl (this, proto, sel,; M/ d" ~) d2 T. m5 i; B. h$ ?) o
new FArgumentsImpl (this, sel)));
6 K+ D; S! L z) E/ X6 a4 U1 n1 l" e } catch (Exception e) {6 b0 X/ C, R" ^0 P3 M) q
e.printStackTrace (System.err);
( F) E) d% U G" Z( U, p) g6 s }
' u0 Q6 m& N4 K/ q0 ~% Y' Q& ] ?2 L( q0 Z# U, p' | P: C' |& q
syncUpdateOrder ();1 W5 U1 h0 `0 K+ G
3 u; w t' b, u3 `. y8 M
try {8 ?, P3 \- {, c+ n
modelActions.createActionTo$message 6 T5 X" e2 r; X2 v
(heat, new Selector (heat.getClass (), "updateLattice", false));# c2 l$ l3 @; D c# f# a6 o: j+ x
} catch (Exception e) {
0 E. k7 r' Z( a; l" y' F/ _3 @8 c System.err.println("Exception updateLattice: " + e.getMessage ());- V9 _" R5 Y, l- v" A% V- D1 v, \
}- ~* Z- L% ]' R
% {. e2 |) S/ z8 o# o5 C // Then we create a schedule that executes the
' L- ]0 i2 A; H' j3 Z // modelActions. modelActions is an ActionGroup, by itself it+ |6 f/ Q' Q/ E* k6 w
// has no notion of time. In order to have it executed in" i9 ^* J% F9 D3 @8 ?. C, P: E
// time, we create a Schedule that says to use the7 M3 x4 {$ U& F2 k" Q, y9 n% m
// modelActions ActionGroup at particular times. This
9 h8 D! D4 D0 H5 r7 @' | // schedule has a repeat interval of 1, it will loop every) [$ k) ^0 K5 B# C6 H4 x
// time step. The action is executed at time 0 relative to- Q3 ^2 R1 b6 N4 o" w
// the beginning of the loop.
/ ?* G7 |0 |! n
. e5 q; ~# O: g' h1 S, E; ?; D // This is a simple schedule, with only one action that is
' g. d2 {5 W/ ]7 \' a6 m // just repeated every time. See jmousetrap for more
8 j" P q! |1 b! T3 R* ~ // complicated schedules.
; Y7 A4 c p2 p2 j2 e3 Q& u. y
, Q: e; }7 ^& K modelSchedule = new ScheduleImpl (getZone (), 1);
, @/ l& l: F" s$ H, j, r. { modelSchedule.at$createAction (0, modelActions);0 A& C e1 b: i. X4 c
8 J! \3 V/ _- m return this;8 q8 O7 O* u, @4 v
} |