HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 l. `% h+ R8 T# F* n2 v5 W, O. V
2 ^( y" ?- R8 G X
public Object buildActions () {
$ o3 u, g/ s! l A, {% m- i; W# d super.buildActions();8 g/ O; X9 t. o6 p, Z2 o5 S/ ~' Q
( V! X& h) [( r9 L( S
// Create the list of simulation actions. We put these in9 S4 i# u2 Y* y$ ^+ h
// an action group, because we want these actions to be% `* w8 |+ |% K- R) g4 m+ n
// executed in a specific order, but these steps should
. j; @4 D+ K# P( {$ x4 u' v8 _, K: f // take no (simulated) time. The M(foo) means "The message8 d! v# J6 B3 |; W' q- m2 h/ L
// called <foo>". You can send a message To a particular
4 p# A" Q8 T* d2 ^, | n // object, or ForEach object in a collection.4 z# l/ z- S' q1 h8 q
, ~$ i( z. h1 d Z& p# l // Note we update the heatspace in two phases: first run
! ~) O/ T5 ~' ?: |# n // diffusion, then run "updateWorld" to actually enact the
9 `: U- ~ I2 q' y/ a2 d6 B% X // changes the heatbugs have made. The ordering here is' `, i4 c" g$ _8 p b5 L4 {
// significant!9 W% @, Q# k# @* \ |) l
- a( e6 j$ H* f# |3 r1 R // Note also, that with the additional
/ ?: ^1 d: Y" J5 p // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 Y: A- n& Q- P6 |/ a' D // randomize the order in which the bugs actually run0 r3 O) W1 O& Y7 ]6 y
// their step rule. This has the effect of removing any8 D2 T; {2 T. _
// systematic bias in the iteration throught the heatbug
. Q5 q! }0 O# y9 n // list from timestep to timestep
1 ^" m8 m9 X6 |8 p! c& g6 ~
& j! o; i, t3 z // By default, all `createActionForEach' modelActions have* f& d' g9 @8 D2 y: t
// a default order of `Sequential', which means that the7 z( l8 N9 V( X' U. m
// order of iteration through the `heatbugList' will be. Z! _0 _( E9 R8 Z2 `
// identical (assuming the list order is not changed) z4 e* _, V6 z9 |, h
// indirectly by some other process).
! H' k [+ P- b) ^8 ^) k
8 F4 |2 A9 Y- Y( I modelActions = new ActionGroupImpl (getZone ());& O7 y+ @0 I( g" c2 i0 ]
- ?1 ~# V# \. ^) E8 s8 r8 V
try {
6 X9 i' |# y1 W modelActions.createActionTo$message% j3 i% V# [7 ]
(heat, new Selector (heat.getClass (), "stepRule", false));
" i) E( E! S1 E Q, | } catch (Exception e) {( ]- A% j# O% ?) o) \6 j
System.err.println ("Exception stepRule: " + e.getMessage ());' P% }9 ^/ r: F0 @) R( Z d! M
}4 P% Z. O1 w2 ?; z
# S% ~: p7 Y" I- F try {
) x* Y2 a2 w( [7 m, @ Heatbug proto = (Heatbug) heatbugList.get (0);: j2 \/ |% O8 L8 Q
Selector sel = . W+ B& w4 O, [1 Z# |
new Selector (proto.getClass (), "heatbugStep", false);
" |6 M- m' X& S o0 | actionForEach =( O2 _# B8 A& T4 _ `( c; `3 ^
modelActions.createFActionForEachHomogeneous$call6 Q$ ]9 _: n- U5 `8 v4 p/ z' H
(heatbugList,& p1 C" D( h' |$ P; G- g( |, M, K
new FCallImpl (this, proto, sel,0 E/ N+ ]4 A/ E9 q ?. d
new FArgumentsImpl (this, sel)));: n0 ?4 `, x7 C0 F
} catch (Exception e) {
, ^8 q2 W1 E* T; X7 i5 w3 k) F) I e.printStackTrace (System.err);
' ~+ S5 E. }. ?$ ]3 C) {/ g/ X l }
: I- U$ J: T3 P7 W
4 _2 E7 A, P, |) ]1 N: k5 ]8 R5 d syncUpdateOrder ();
2 u/ ? B ]( v& m6 L' H- X
& g( w$ O/ L6 W3 L try {
( R4 {6 E K1 C Z6 k, J: I modelActions.createActionTo$message
1 U# ]- g3 K7 h1 D$ i+ ? (heat, new Selector (heat.getClass (), "updateLattice", false));) p' T' z& ^8 E2 Z* n: k
} catch (Exception e) {/ d d- @; z$ X2 [
System.err.println("Exception updateLattice: " + e.getMessage ());
# y2 ^' G& b: ]: B9 p }
* w& F/ }% ~- ^0 \3 K , L1 X: V, L9 n( L
// Then we create a schedule that executes the
+ d7 X3 C" c; [- E2 ~- p2 |9 m5 c9 d+ U // modelActions. modelActions is an ActionGroup, by itself it
K& F! A: b; o$ V" V' Z- Z // has no notion of time. In order to have it executed in' J/ D$ E. t0 k4 Q8 W+ @
// time, we create a Schedule that says to use the
% Q9 x4 n& M& n/ q // modelActions ActionGroup at particular times. This
# `7 A. a' a+ | // schedule has a repeat interval of 1, it will loop every+ O6 w9 ~0 Y x6 j
// time step. The action is executed at time 0 relative to
. W) x* ?+ ~1 [ // the beginning of the loop.
" z) P0 O! Z( n$ }3 N6 _- m# v4 \: n7 S# j
// This is a simple schedule, with only one action that is$ c) N/ b5 i; E5 ~2 ?
// just repeated every time. See jmousetrap for more: f7 e: f6 k2 p6 O, _" {6 i
// complicated schedules.! u! b$ l# m; E& y; {2 P5 g$ @
' ?) C$ K& o" L
modelSchedule = new ScheduleImpl (getZone (), 1);
; H# _* X, f7 N9 a modelSchedule.at$createAction (0, modelActions);
b7 Z) q( m- ~: \$ ^ / w7 E& }. p" z3 J# t
return this;: p1 J+ Y% T$ F5 C* Q6 z7 `
} |