HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) D1 C% w8 g9 ^
4 I+ B' E/ ?/ d+ T
public Object buildActions () {
+ X# @% @8 T: M: G1 t _ super.buildActions();/ E9 g& c# n/ [% \% [
" e9 b9 K) [2 q; ~" F5 f // Create the list of simulation actions. We put these in6 @' I4 \2 y9 @9 q5 S1 p
// an action group, because we want these actions to be# w0 s, N" L. Z' u! D+ C
// executed in a specific order, but these steps should9 G) {8 M; c# Q: s, }2 x3 u
// take no (simulated) time. The M(foo) means "The message
, T& d" o; u# m% E6 S* @ // called <foo>". You can send a message To a particular
& M: M3 u$ E7 v7 \. ~+ }8 w // object, or ForEach object in a collection.
4 B" f3 i6 ]; u
; R3 H1 u' \7 o5 o4 I // Note we update the heatspace in two phases: first run: o Z* g j" o r1 z: P# f
// diffusion, then run "updateWorld" to actually enact the: K, O. P" K" w, |
// changes the heatbugs have made. The ordering here is7 n# {) q. ^4 W2 J( t* x9 ` f
// significant!7 H6 R4 k. a/ } O. [% z
5 N" N9 I; \8 L8 _) N, @. K; T // Note also, that with the additional
, `0 J w4 g9 C- t) X0 j // `randomizeHeatbugUpdateOrder' Boolean flag we can
. y! L& {, S8 d# G. n // randomize the order in which the bugs actually run2 _" r, \, X; F0 Z
// their step rule. This has the effect of removing any. S5 i% n8 V0 q1 Y* o6 Z2 c& I2 q( l
// systematic bias in the iteration throught the heatbug
; o- G" l4 A2 v+ F/ a // list from timestep to timestep
" }! a" G' L$ v 0 S7 f. A4 E q
// By default, all `createActionForEach' modelActions have+ r% K" Z2 H" ^% \* w1 W
// a default order of `Sequential', which means that the% [3 p( }! E% l3 B+ R; R5 g
// order of iteration through the `heatbugList' will be( L8 p- A7 U6 w; P/ V* G8 R9 G0 O
// identical (assuming the list order is not changed
g8 B0 g$ Q$ W5 u1 [" U' ]. Y3 F // indirectly by some other process).
8 P; n4 c- ^1 l. r8 ~! _$ _* F
' n+ l3 f& N7 C modelActions = new ActionGroupImpl (getZone ());
3 `7 s; i5 E0 |) s" w# R/ ]4 r6 p
try {
" _4 B7 @) {2 i2 @& T modelActions.createActionTo$message
. S, C; S/ S/ V; H: o (heat, new Selector (heat.getClass (), "stepRule", false));
m) c! H: C& F. x5 U3 x } catch (Exception e) {' A* W- c1 j p5 u, j
System.err.println ("Exception stepRule: " + e.getMessage ());
/ i( q+ {6 X; @/ o( O+ }% k }" F; k/ o, E( q- q' e/ W& l
8 G) m+ j4 n+ ~' D
try {' H" E* n1 L2 D' J/ k
Heatbug proto = (Heatbug) heatbugList.get (0);* ]3 z# z: Q a ?' `
Selector sel = ' p5 f$ w$ Y* z2 `4 S
new Selector (proto.getClass (), "heatbugStep", false);# s$ h+ c+ ]! h
actionForEach =
, e. R# h9 @; b5 w: ] modelActions.createFActionForEachHomogeneous$call
( a' Q) I$ C: K0 H; n# ?- F5 p (heatbugList,
" \2 p$ _1 x7 g4 c- v* X/ c new FCallImpl (this, proto, sel,+ J x- X# S4 |8 d' P
new FArgumentsImpl (this, sel)));
) K: R: D! z# y) Z, {6 O } catch (Exception e) {# ?" _8 ?: L4 U- g0 ~+ a3 g
e.printStackTrace (System.err);
. g; S8 E h$ u0 n+ h }8 a$ h; k2 W& C. j
9 v9 E4 M: m) l, U/ s6 S; U q) W syncUpdateOrder ();
# O4 c8 }) {, f$ F2 _. m% G; {1 t( {/ L7 k0 }* O
try {( E6 z2 y3 A5 m8 A* q
modelActions.createActionTo$message - J$ x; V# u$ _6 y8 A- |8 d; c
(heat, new Selector (heat.getClass (), "updateLattice", false));0 k7 w. }7 ?' F! ]$ T
} catch (Exception e) {9 `( m: B; d3 X4 u* S( v, w) B$ Z
System.err.println("Exception updateLattice: " + e.getMessage ());
- k% e0 n8 a! V- {7 f5 ? }( O7 a- ~) Q/ S
* o1 D8 Y7 v* Q; c" q1 E1 v
// Then we create a schedule that executes the4 g! Y1 s/ N0 I3 |6 a1 v8 {6 O' d
// modelActions. modelActions is an ActionGroup, by itself it x7 e$ O7 N- L+ ?
// has no notion of time. In order to have it executed in
1 X/ c, B/ Z/ r* U! e // time, we create a Schedule that says to use the1 Z, D S2 g, o
// modelActions ActionGroup at particular times. This7 f i- i7 ?. h% L' w
// schedule has a repeat interval of 1, it will loop every
5 @8 I' i) W+ L) g // time step. The action is executed at time 0 relative to3 P( o# j/ X/ U C1 O
// the beginning of the loop.
- c7 ~* Z. W$ O
* S) o- W: s( L# R // This is a simple schedule, with only one action that is
5 E0 T5 D8 u1 p- Z1 R* _0 N // just repeated every time. See jmousetrap for more' F! c& w, W4 T, h: w9 ?) A
// complicated schedules.
" i: S2 t- X: s) S$ n W# u% } # N2 [$ y! |1 \7 b; o; k
modelSchedule = new ScheduleImpl (getZone (), 1);
6 P, l M$ c( N+ e0 h+ `5 i f m modelSchedule.at$createAction (0, modelActions);" u0 U- e) y8 }# h
& P9 W5 S3 ?6 i( Q) c+ Q return this;9 j; J* o% b% k- |4 O& \/ C/ X8 v
} |