HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! h& B2 r+ w0 f8 x" w8 e
8 S- U) h! H& }* p& t public Object buildActions () {9 C! u3 f. B* t7 ]
super.buildActions();
) T9 ^9 r* a, `
$ B2 G. }- [+ L$ O5 d // Create the list of simulation actions. We put these in
. \ o, v$ `+ l* G Q M4 A, [) u // an action group, because we want these actions to be- H, `0 U$ O' C$ y _ h: h
// executed in a specific order, but these steps should2 F- U/ F. r# ~) x
// take no (simulated) time. The M(foo) means "The message, \7 x9 J! [. z& w
// called <foo>". You can send a message To a particular2 `( K8 M, ?* R% _6 k
// object, or ForEach object in a collection.
" `, \$ h, Q7 B' \6 m$ C. r( z 5 V$ \% @/ x& i6 d( Z
// Note we update the heatspace in two phases: first run: e! d/ j/ y9 G
// diffusion, then run "updateWorld" to actually enact the5 _$ l) L- Z- b$ a0 V- V9 `
// changes the heatbugs have made. The ordering here is
6 X% B: R9 y8 F4 F // significant!
2 E, ?/ n/ r0 N( S+ b( q
" ~8 K& r9 S. W // Note also, that with the additional
/ r, P7 Y) f: [1 Y; t3 W* R# v // `randomizeHeatbugUpdateOrder' Boolean flag we can
& I8 \7 u* W4 H, ?, n8 {& i Z // randomize the order in which the bugs actually run1 y/ p2 Z$ S: U
// their step rule. This has the effect of removing any; x" L7 v$ _- T
// systematic bias in the iteration throught the heatbug
) J4 F' q- N2 B' b' \- `6 M; u // list from timestep to timestep
+ ?7 v! H+ @ i) p$ O' F 8 c9 P! F( G) x( q: w
// By default, all `createActionForEach' modelActions have4 k" _- C& n1 ~+ F2 p, \- P
// a default order of `Sequential', which means that the
: ^, p8 ^6 h' }6 W" O // order of iteration through the `heatbugList' will be
( l7 c' L1 ?5 C; ]+ h // identical (assuming the list order is not changed
4 Z0 q/ v: l, r1 ~ R+ ^ // indirectly by some other process).
5 x, N0 B$ [3 V2 h% H
1 `+ V3 K5 B9 V* a modelActions = new ActionGroupImpl (getZone ());4 l7 o- R- Z% ^) D4 h3 N! F
) _( b) Q: R5 n T$ ]8 p" J Y
try {5 @( Z( P9 [! b
modelActions.createActionTo$message$ r$ }3 H6 R5 E L! ^3 g6 k2 p' Z
(heat, new Selector (heat.getClass (), "stepRule", false));
8 W+ A+ f/ G$ z! t+ m/ l } catch (Exception e) {9 F9 v1 |' ?! o& K X% R9 n! F
System.err.println ("Exception stepRule: " + e.getMessage ());5 {1 z/ E3 o! V% l) ^
}
0 P5 t) M o7 U# z
% r4 G9 u! o, O: P0 } try {' O0 b& E0 s, x( i2 y1 m% `8 M
Heatbug proto = (Heatbug) heatbugList.get (0);
' I% v1 N$ h1 X. g0 l Selector sel = . W/ }0 a5 W4 r8 _. m+ H
new Selector (proto.getClass (), "heatbugStep", false);
& n: U( G5 j1 B' s6 w6 O; S actionForEach =
# r& _8 G. L( k/ O! n! }, j8 h modelActions.createFActionForEachHomogeneous$call
, |& A3 J6 u$ \6 |8 u& t* g (heatbugList,
, q! X. _" u R) G: D+ P# ?' v% [7 c new FCallImpl (this, proto, sel,- z- n Z4 P) w& N( k
new FArgumentsImpl (this, sel)));; I ?4 U6 b' d# s' j5 P3 s& u
} catch (Exception e) {
1 m: S7 ~$ T3 {! V2 Q7 Z7 R e.printStackTrace (System.err);! C! E. Z" Y6 w3 l, A
}
: ~# f0 r, P! _- D( e; y : z: d2 ?4 F; I9 d+ f+ g) ?
syncUpdateOrder ();9 z8 g; ?% Y2 b5 @
6 t" z" x2 y3 z6 y0 f: ^2 V* _
try {
9 X+ q) U/ ^: X1 s- s$ [) y" m modelActions.createActionTo$message & V9 w. D, [8 s M: h" g
(heat, new Selector (heat.getClass (), "updateLattice", false));5 ]2 W5 V* a/ o! T7 w0 u l
} catch (Exception e) {+ {. O2 Z5 V3 m1 w+ I
System.err.println("Exception updateLattice: " + e.getMessage ());$ U$ K- ~% A* |. }% v3 d( [. Y$ F
}# [' J' a! e5 J3 m6 L
& Q: X3 [" y K; z" I" y
// Then we create a schedule that executes the
; [7 j6 G2 K! Q$ S. ` F // modelActions. modelActions is an ActionGroup, by itself it
$ T1 c5 c" z# C G // has no notion of time. In order to have it executed in
0 X8 g) c9 ]# I* m, R% C // time, we create a Schedule that says to use the1 j/ b ~( F8 ^8 |0 l% v
// modelActions ActionGroup at particular times. This L6 {* Z2 V& |$ b! z' m
// schedule has a repeat interval of 1, it will loop every9 y2 ~1 F" e) ^4 h
// time step. The action is executed at time 0 relative to
: i) ^6 Y5 a( L L9 K // the beginning of the loop." @' n; H! }+ ^& x, X
1 i" e2 g7 r' r- d) L. N9 L // This is a simple schedule, with only one action that is% H3 q; V8 n# B1 L# T
// just repeated every time. See jmousetrap for more
. i. u: Q" x# ^6 _ // complicated schedules.
- z" r, F& E" O7 ] $ k5 R$ f+ |1 R+ G2 b3 @
modelSchedule = new ScheduleImpl (getZone (), 1);5 ^. y/ b3 e t, A7 D
modelSchedule.at$createAction (0, modelActions);1 J+ q* O6 ]& I2 Y! z2 b6 e8 \2 {
; s3 \# }4 D+ Z! Y% r2 x, y
return this;
6 Z) `4 L" g: `" a, g5 g$ ` } |