HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ r* |3 q/ @% v, r% @* V& W
7 Y- W; G( B' N. ]# G& [2 F) @
public Object buildActions () {& }/ z. ^8 v1 }
super.buildActions();
8 j, h; }6 n9 p: X: g; V / w8 F5 E3 E; a b: r
// Create the list of simulation actions. We put these in9 n, o6 K3 W& p/ u r/ {6 ?
// an action group, because we want these actions to be# Z! R& r+ F" f) w) Q O6 b
// executed in a specific order, but these steps should
2 Q: C6 b* W+ Q. e) n8 |! v2 C // take no (simulated) time. The M(foo) means "The message
$ t3 Y( Y) J: l# b- c // called <foo>". You can send a message To a particular
. v, I$ ] f0 M2 w* G8 b( l. c( g // object, or ForEach object in a collection.
" W" X' O9 l& o+ B; j1 n) ` $ M: y9 z+ N; ?2 r
// Note we update the heatspace in two phases: first run
& [$ w) E# R& K( T& G$ Z // diffusion, then run "updateWorld" to actually enact the/ J* f2 N$ i3 h$ N
// changes the heatbugs have made. The ordering here is$ N/ F6 d% T ~4 L" M& D3 s* s
// significant!
7 k+ U& N0 B1 l0 S6 a: K ) |2 z1 l: q- W; d& g: h
// Note also, that with the additional v' b4 ]4 A. R; i Z' z
// `randomizeHeatbugUpdateOrder' Boolean flag we can( ?7 i5 e' E3 g3 C- v- u
// randomize the order in which the bugs actually run
5 l' c5 t. J' y& M" \ // their step rule. This has the effect of removing any
+ \* F4 e: m' L/ E. d // systematic bias in the iteration throught the heatbug
; A% R# l U& C( _ // list from timestep to timestep. I7 ]7 _/ Q7 f4 c d) j
3 j# j/ g. W% g9 d
// By default, all `createActionForEach' modelActions have
; C3 S7 U, p, I/ {! B7 @+ s // a default order of `Sequential', which means that the
8 j& Z: {+ t! s, e" S // order of iteration through the `heatbugList' will be8 i% Y: [6 Y4 Y/ v! g7 N+ W
// identical (assuming the list order is not changed3 g; F0 `0 ~" O3 \
// indirectly by some other process).! _# D3 i1 j. r* u1 t4 |
* G6 c- g+ h& e8 y& [. j+ V
modelActions = new ActionGroupImpl (getZone ());
) g2 l, `, ?1 J
6 X" m) s C3 |* F9 r' W0 V1 q* G1 ] try {
! j) I; O! f6 G( V* O5 q modelActions.createActionTo$message
$ h% Z% m; J) G$ g (heat, new Selector (heat.getClass (), "stepRule", false));
/ j- s% q+ ~$ n# l) | } catch (Exception e) {1 Y$ B# N9 C: {
System.err.println ("Exception stepRule: " + e.getMessage ());
* w* y/ a/ w# q2 \& t }
# v$ W: n. f3 L# j R& x4 ^( X* `3 o6 L1 o: j
try {8 r. L) \% \' r
Heatbug proto = (Heatbug) heatbugList.get (0);
) b2 ]1 _( U+ I8 l' n: ]0 V Selector sel =
& n8 g- B0 T! X new Selector (proto.getClass (), "heatbugStep", false);
( Y% t) F& U+ k( d actionForEach =% t* |- Q7 @, C
modelActions.createFActionForEachHomogeneous$call! D. d2 z3 z- v' O
(heatbugList,8 o) D$ R& ~$ k9 m* j
new FCallImpl (this, proto, sel,
4 {/ M& p. e" j a3 V new FArgumentsImpl (this, sel)));
4 k# C; @1 W, z P7 ^! C" a3 H' R8 v } catch (Exception e) {/ Y3 y; v) f3 G4 w7 o7 b* H4 p
e.printStackTrace (System.err);
) p$ O1 b. G; j% e, f }/ B. x# V0 h+ `' q9 x+ A
H% Y/ i+ Y- A9 s syncUpdateOrder ();
4 U8 V i; l. e/ W% j) @
5 k% K4 R2 ~5 X6 g try {9 ?& Y, J7 }- g x: _0 m
modelActions.createActionTo$message
9 F" h& P+ q5 F4 \4 }0 @ (heat, new Selector (heat.getClass (), "updateLattice", false));
5 I: v N" _7 N$ F. z } catch (Exception e) {
) |4 j1 U0 V/ y7 q& E! o8 J System.err.println("Exception updateLattice: " + e.getMessage ());/ \' Q, O; Q7 E9 v4 \8 l9 h6 u
}
/ d$ I( D$ Z. s8 X( }1 _% }1 O' p: i" I
: x, b# X- n8 L1 F( f) F // Then we create a schedule that executes the
, q* Q& a! K0 S, Q4 r" R/ W R // modelActions. modelActions is an ActionGroup, by itself it
; C3 Z% K7 e" M4 a // has no notion of time. In order to have it executed in4 w& {( T3 E6 Q* l1 Y3 s* X2 e' ]# k+ @
// time, we create a Schedule that says to use the0 c& A9 S: G2 ~5 }2 H7 R& e9 j
// modelActions ActionGroup at particular times. This% m% k# Y' x1 c. `9 i: b# b
// schedule has a repeat interval of 1, it will loop every# K) c! k. E% [0 ]+ n- a
// time step. The action is executed at time 0 relative to$ p6 r1 f1 G! s! |' O4 P2 a3 Z
// the beginning of the loop.& _9 S+ }3 z2 {- k6 d
) [ ^2 ~( @9 B- a* l" N- Y // This is a simple schedule, with only one action that is2 M$ J- X! M) a+ f' W7 F9 q. ~; H
// just repeated every time. See jmousetrap for more5 E. U6 t9 F0 _) j( ]; ?' C/ ]
// complicated schedules.% b, A" [$ t( n" m
. ^/ t0 B: e' V/ Z- _ modelSchedule = new ScheduleImpl (getZone (), 1);
' R2 Y I: ^. _$ _: [2 J modelSchedule.at$createAction (0, modelActions);* o+ a1 i; A. D) m
i- N: q/ C$ a8 d* R W# t
return this;- T b% k* N3 E$ t4 g6 q, o! u
} |