HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# c2 d9 }& A: S$ I, k% \
! ?6 D* W/ @0 v2 x6 d( N+ N, V, e public Object buildActions () {$ A& a$ [1 w9 `. }0 s6 {
super.buildActions();. U7 p: `+ y- ^1 Y: `1 n
, X1 h% f- \5 S& u // Create the list of simulation actions. We put these in9 O' S! `, j$ E: ~! O# y8 \; t# X" z
// an action group, because we want these actions to be
1 ~6 G4 K8 I' R7 o# v+ p // executed in a specific order, but these steps should8 ?( g9 Q1 M6 b
// take no (simulated) time. The M(foo) means "The message
' P9 V2 T9 q* j2 c7 Z // called <foo>". You can send a message To a particular3 N: y4 a$ Y- P; x
// object, or ForEach object in a collection. n7 ~& q6 i9 E, P5 v' b8 f* a' l
3 M% P( T5 L: |( S // Note we update the heatspace in two phases: first run
, p y: g$ K, p! G // diffusion, then run "updateWorld" to actually enact the
8 r0 d$ B9 p# j( z4 x // changes the heatbugs have made. The ordering here is
# t: h; j* E `3 `3 z // significant!
* G( c$ ^$ d1 g0 ^: p4 p: w * e4 {/ L* }( W2 f3 L! q
// Note also, that with the additional
- \' F+ A! M7 H) W // `randomizeHeatbugUpdateOrder' Boolean flag we can5 y; [$ |* w3 z8 l
// randomize the order in which the bugs actually run) b9 v3 z4 X5 Z W& g- H, D0 y4 K
// their step rule. This has the effect of removing any
/ I8 \7 C. O5 f% a4 r // systematic bias in the iteration throught the heatbug* e/ b/ s1 \% y; A
// list from timestep to timestep
/ E* |8 `7 q7 |) B) @ $ l8 A. [* ], ?0 z
// By default, all `createActionForEach' modelActions have
5 e1 q& U {- e! v9 W. W* j // a default order of `Sequential', which means that the
( B9 z# k# ]6 {# E$ b8 Y. k. X, L" b // order of iteration through the `heatbugList' will be
g' c$ C) U$ {! b // identical (assuming the list order is not changed9 z; z* ]: I% ]9 [% y& K
// indirectly by some other process).
4 Q* F \0 L% o5 T
6 j& B5 o# E6 x3 t" R6 b3 O4 ?" j+ H- k7 \1 x modelActions = new ActionGroupImpl (getZone ());
( f6 ?, n& h7 J7 k8 q9 U) _ m
try {
) X1 r# ]! O% E modelActions.createActionTo$message
3 O- n% y2 u5 P/ k& N$ ~3 k* p (heat, new Selector (heat.getClass (), "stepRule", false));/ f F, W! p& I. j2 s4 j8 D
} catch (Exception e) {6 f r0 K. V: i$ }4 _
System.err.println ("Exception stepRule: " + e.getMessage ());
8 k3 s) p& |5 M, m8 f% K) _ }. M' ^9 S) Q$ x `0 G' q8 Y% z
0 \. k) ]% a1 x$ e O
try {" [ z9 ?7 y4 I- z* i n
Heatbug proto = (Heatbug) heatbugList.get (0);, x A9 a1 z6 @3 `1 V2 Z8 F
Selector sel =
- O! F1 k/ @8 U% W new Selector (proto.getClass (), "heatbugStep", false);* w0 T( w- M" w7 G
actionForEach =
0 A! R/ c4 R5 }( [ modelActions.createFActionForEachHomogeneous$call, @5 {, `' E# B
(heatbugList,
" B$ M( T0 q. N' n6 U new FCallImpl (this, proto, sel,
" k$ l3 Q3 l% z: L& g new FArgumentsImpl (this, sel)));& k% P. p2 ]$ M# E4 |4 l
} catch (Exception e) {
& c" Z6 k6 R) P) o' F6 t e.printStackTrace (System.err);
6 H+ P8 c5 _: {' j }
: I# v: L- o) S. R6 s: n % \ `' R( \1 o
syncUpdateOrder ();( Z$ r4 ^4 A- A: [: U! j
}3 {$ i" W/ v- y/ R) J0 Z. C try {6 J! C/ R* h9 p0 V! d/ b: p/ L
modelActions.createActionTo$message / t/ m& x7 B" A# Z' x, J
(heat, new Selector (heat.getClass (), "updateLattice", false));6 j F+ H' H* S {! W- r6 F% ]
} catch (Exception e) {6 C% h* a# t# m% c5 H% H
System.err.println("Exception updateLattice: " + e.getMessage ());
6 |; D$ d% L$ S }
: F% N1 a+ ]" {2 j4 A3 a8 W$ u0 v
( V+ J% n7 B8 M* B( X* ~6 R- E // Then we create a schedule that executes the+ {! d. f8 v; ?& M
// modelActions. modelActions is an ActionGroup, by itself it
$ u( Q6 c* ^, f( n# i // has no notion of time. In order to have it executed in
8 ]1 u6 M* |: g, O0 n9 Z6 C6 e // time, we create a Schedule that says to use the
. T" d6 b2 H2 K# ^' g- o/ h // modelActions ActionGroup at particular times. This; c6 F/ K* F" T7 k
// schedule has a repeat interval of 1, it will loop every! F* o* z: n% z! }4 \
// time step. The action is executed at time 0 relative to2 e& b% @& J6 N5 t1 R- v5 g; d. ?
// the beginning of the loop.
) r) X! x8 n4 A+ Q, k% E0 R; d$ c& g* _3 t& o
// This is a simple schedule, with only one action that is; I4 y6 J. n- U( J# w
// just repeated every time. See jmousetrap for more0 e) o* M; i! I) I, ]$ v
// complicated schedules.
( K' D+ y. j9 C; E
5 i, S4 B3 ?; z, C' c" i7 U; Q$ L modelSchedule = new ScheduleImpl (getZone (), 1);
4 D. a& g* R0 q+ G! i6 t/ [ modelSchedule.at$createAction (0, modelActions);
: G Y7 j$ e/ D
' r6 r4 n/ [, f, b* R return this;$ e; ~4 }7 D% G
} |