HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: S6 L" f; u* A. u
9 M7 A# K8 }6 F/ U5 b public Object buildActions () {% n9 J" ~9 |- T# j5 n' ^5 L
super.buildActions();
) n2 U% `& {3 a' C w 6 A, x. |- Q. r: E
// Create the list of simulation actions. We put these in
7 b8 L( u* ` k6 |* K' U // an action group, because we want these actions to be. V; a9 J5 u' R! U: L
// executed in a specific order, but these steps should) k1 o* B( K# u& b2 U) y$ N
// take no (simulated) time. The M(foo) means "The message
6 v9 x' u2 Q( q) v5 t // called <foo>". You can send a message To a particular6 @( m% q6 `+ P+ M
// object, or ForEach object in a collection.2 L. f' v& q) R: c2 A
' d& \- U3 ]4 Q# S! j6 W8 g8 [ // Note we update the heatspace in two phases: first run1 }) _: j. ?8 O
// diffusion, then run "updateWorld" to actually enact the
% [9 f# J; b' M; P; I1 W& x( |* y // changes the heatbugs have made. The ordering here is% ^0 y0 C0 @+ X! n
// significant!
( Q7 Q- T& I2 X
! C7 y, g8 h2 q" ^, O // Note also, that with the additional
" w( T+ j. o$ b% {0 ]2 e9 K& ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
- `: N5 T( y) l4 ?! J& O+ s // randomize the order in which the bugs actually run) J6 g* H: F3 U. I$ z/ |3 m
// their step rule. This has the effect of removing any
/ l) u3 M" J: n' ^0 X // systematic bias in the iteration throught the heatbug
; O! q5 R6 @% @ Z9 i' a. s/ s // list from timestep to timestep% ?2 X) Q G% k# K- y
/ N& T/ P% F& ^) G$ a
// By default, all `createActionForEach' modelActions have
8 D5 t9 [, o% j5 P // a default order of `Sequential', which means that the! |( D" n8 o: H0 G! m# A: d
// order of iteration through the `heatbugList' will be. G: N! Z5 \1 K; w0 {6 r
// identical (assuming the list order is not changed. C% H% d2 q$ Z3 m
// indirectly by some other process).
. h' |5 \: Y' q' C5 ]7 k/ F! O
; r4 y& ]2 @3 ]* _ modelActions = new ActionGroupImpl (getZone ());$ i7 G6 J- h4 p: l- |
6 I( @% ^" ?2 O* [' q- ~ try {+ ?! d+ Q; M, S, {* l: R# Q
modelActions.createActionTo$message
# P2 {/ u% f1 k (heat, new Selector (heat.getClass (), "stepRule", false));
w" T: V2 O1 t! T, _1 i } catch (Exception e) {
y0 A! {1 S' ~' M% _6 @ V8 T System.err.println ("Exception stepRule: " + e.getMessage ());% r. |- j9 f5 L! F
}
% F4 ^- |/ v4 N9 a# W. G9 q
- U4 ~ d7 f& T5 J try {
) i: r- m6 Z1 p0 Y Heatbug proto = (Heatbug) heatbugList.get (0);
! _7 N8 r; V) Y( [9 @ Selector sel =
' _9 \- ~- G @/ |; k6 R6 |+ F new Selector (proto.getClass (), "heatbugStep", false);( i: K/ {, ?: ]+ o& L
actionForEach =
8 L" d8 L4 ^# d/ X5 O5 r3 i. G3 R modelActions.createFActionForEachHomogeneous$call* _3 D# }, e! S } l* t! X
(heatbugList,
# q( ?* P/ D8 m6 n% a5 O' d new FCallImpl (this, proto, sel,
! L) w: U) {) A2 S new FArgumentsImpl (this, sel)));* U" T1 H3 a9 G" K2 d
} catch (Exception e) {
7 w P# C: d: w e.printStackTrace (System.err);
3 N, N6 }* j* d& ` }* S6 t' k4 }4 `, ~; ]
$ w9 A, m$ ]2 d$ o2 J% [ syncUpdateOrder ();
) y; K) r+ r, m* f2 L5 c6 H$ g$ S& `% J! M/ G, Q# D
try {
4 f4 _& m8 b+ Z" A$ L2 Y modelActions.createActionTo$message ; z# ~5 u9 W. F3 `+ P% `0 r; t
(heat, new Selector (heat.getClass (), "updateLattice", false));
Q' d$ [0 f5 `, H+ H# L' O9 @ } catch (Exception e) {) A4 v) g- a( V' l- Z+ f6 P0 W; X$ H
System.err.println("Exception updateLattice: " + e.getMessage ());* A" d/ {6 g7 {0 Y
}' O6 a0 Q" I# Z: n' H. ]) t( d
/ K* A; t$ |: u) c
// Then we create a schedule that executes the
9 }7 u& s# I3 B // modelActions. modelActions is an ActionGroup, by itself it+ w3 I7 ^6 l# ~- }/ F. w( l
// has no notion of time. In order to have it executed in
8 ^/ b, ]) ?* s2 g4 z7 \3 t // time, we create a Schedule that says to use the
9 y$ e& w! ?: W // modelActions ActionGroup at particular times. This8 |# p- l- P7 @" [
// schedule has a repeat interval of 1, it will loop every' \3 u# b8 N9 W2 P4 h
// time step. The action is executed at time 0 relative to9 d! } E* b4 u2 h( S: x( F
// the beginning of the loop.
8 j% p5 \8 y1 B, k, D% @+ y/ q8 E4 V) c
// This is a simple schedule, with only one action that is
, Q2 i }+ h, e0 b0 R7 L( c // just repeated every time. See jmousetrap for more7 f# {& w5 ]' o" Y7 B: H
// complicated schedules.
0 w' v3 ~; l5 q5 q9 h' v- }% M 6 I$ T( ~" L7 L
modelSchedule = new ScheduleImpl (getZone (), 1);
. R1 ~6 M9 t# U; U/ V, J modelSchedule.at$createAction (0, modelActions);- e/ O7 d- c# ~9 e9 H% l
5 `1 D7 h5 k) _- d return this;" c# U0 P: G: k2 R* a8 S, }% a
} |