HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) s/ _- {' b% L7 V8 ~! e4 f
, w3 g! j% y+ G4 l0 f# Y public Object buildActions () {
; F( c# A. i" W9 f) R* B* l super.buildActions();, C: d( V9 a: s4 c8 z# ?7 O
( q L; Y# x4 N // Create the list of simulation actions. We put these in$ A( o& k- ^+ L$ u6 V% P+ F
// an action group, because we want these actions to be
$ f/ b# D X- j( p: M" S3 k // executed in a specific order, but these steps should. G6 e. L4 p) b& b4 D; y8 f
// take no (simulated) time. The M(foo) means "The message1 p& Y- \5 ~! V! V2 w/ H& |
// called <foo>". You can send a message To a particular$ _8 y6 o3 y6 r3 X% u; o0 O
// object, or ForEach object in a collection.0 F4 G- K! ~( B$ i4 z1 M
o8 m2 C `# i% Y8 x // Note we update the heatspace in two phases: first run
4 P8 j% Z6 J! ^4 g' W // diffusion, then run "updateWorld" to actually enact the9 {; }" u+ o5 b
// changes the heatbugs have made. The ordering here is
* P2 l" [0 ]( T: Z1 b) Z // significant!5 U6 E! A% S$ M, t$ s" y
( h# a% ^" V( |- `4 `1 Z A // Note also, that with the additional. p0 u/ s% t; A' ]# N6 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can" `8 c7 O6 }0 C% M
// randomize the order in which the bugs actually run
1 C& v' e0 W9 [* u& A/ A // their step rule. This has the effect of removing any# P0 b4 X) K: L$ R" _3 k b1 F7 _: x5 r
// systematic bias in the iteration throught the heatbug
$ e3 v* I. I- v/ w# n' V // list from timestep to timestep
! [5 g( l. W2 }9 V + J @+ F' U+ P9 }; i
// By default, all `createActionForEach' modelActions have# W$ _4 R3 S, _
// a default order of `Sequential', which means that the2 t5 W; j& v! S z; o
// order of iteration through the `heatbugList' will be
/ {$ d# U5 v( z7 x1 S // identical (assuming the list order is not changed
: Y$ a8 A% J" }/ {' a9 O, `, ^ // indirectly by some other process).
2 k$ ?/ U+ C* T( ~ : f: _" b" l" k' F" B+ M7 E: b% ]
modelActions = new ActionGroupImpl (getZone ()); F) e8 q# e' r ?0 ?5 O, z) n
. m4 p- ?& y' X3 q try {
2 v1 Y# H$ {. b modelActions.createActionTo$message
8 K6 C3 \- @ Q. i/ H* H (heat, new Selector (heat.getClass (), "stepRule", false));5 u6 h/ {2 `9 p) z. H4 D
} catch (Exception e) {! b) D3 i* N, ?1 g- Y
System.err.println ("Exception stepRule: " + e.getMessage ());
6 T; z! `! O' B0 H9 i/ }% Y }
6 E. @9 k% X' K! [; P, F
( M$ r3 Y. F9 @4 [+ S F; D try {/ m! l3 ^7 i6 w# N
Heatbug proto = (Heatbug) heatbugList.get (0);
$ t1 ?4 O& B6 C Selector sel = ! W8 M5 {! K, N1 j
new Selector (proto.getClass (), "heatbugStep", false);
5 J6 E% c, ]! h0 ]8 j+ x actionForEach =/ i f2 U( N" U7 O1 k! y
modelActions.createFActionForEachHomogeneous$call
4 s5 P7 O" f% P5 ` (heatbugList,
* a9 x& y. O& |* K( x6 |' {0 [ new FCallImpl (this, proto, sel,
- J9 J7 I7 J/ o" N2 P1 Q% ] new FArgumentsImpl (this, sel)));
2 m. o7 K9 e7 A9 m) |1 M3 r# j. ~% _$ S2 x } catch (Exception e) {
) Y H: a( C3 L- b7 }! \ e.printStackTrace (System.err);
- k. K e1 O, u2 [/ F }
- t, \" N4 q+ ]" S 1 M5 D8 O! M# l: z! K+ ^# Z
syncUpdateOrder ();
* w9 G% ~! g- c. x5 E, m* x* n, O$ A9 B# g
try {
5 K& |' B2 Z2 P$ o modelActions.createActionTo$message * X. x7 ^( |3 u; [/ H
(heat, new Selector (heat.getClass (), "updateLattice", false));
" s, o/ v; b7 |3 I6 T) I9 c } catch (Exception e) {
5 Y$ ]- x; Z. f System.err.println("Exception updateLattice: " + e.getMessage ());4 h C, P2 ~# b
} R% T2 M' h3 E, X, Q
5 P; y, j+ y7 ]; Q+ \ ^+ I // Then we create a schedule that executes the, ]7 _0 }! w3 ^
// modelActions. modelActions is an ActionGroup, by itself it
: A4 A$ i: @5 w1 k' M // has no notion of time. In order to have it executed in! j R# f# }5 V' K A# U! K
// time, we create a Schedule that says to use the/ d$ a" \% H) @$ p1 E3 ]1 w
// modelActions ActionGroup at particular times. This
) Y3 i( p2 O1 G# T // schedule has a repeat interval of 1, it will loop every( A9 _9 E/ n( t. w
// time step. The action is executed at time 0 relative to2 o3 q3 l9 y: U. F8 F) b$ w
// the beginning of the loop.& q8 s/ A, E F
( ~9 ?! }( j; F& b$ B: r, I+ U, V // This is a simple schedule, with only one action that is3 G9 g1 [& d3 e5 t
// just repeated every time. See jmousetrap for more) e, Y# z( }/ G+ n6 V2 c
// complicated schedules.
. @5 A8 ^7 F& H5 B; E9 S
; m9 B4 [1 E* s0 }, M' i modelSchedule = new ScheduleImpl (getZone (), 1);
' ~! s' s5 m: i4 c6 X' @% y modelSchedule.at$createAction (0, modelActions);
, C" d+ u$ \' J/ I+ u 2 [0 h$ U+ y" B& J' J% }. \) l
return this;4 f2 G6 i! ~% P* Z% V' }, q
} |