HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" ~5 U0 \) r3 |- ~ s; | Y9 a2 ~, A$ g# V0 E" E
public Object buildActions () {. W [; x3 {, s+ `3 l+ r5 V
super.buildActions();
! Z+ n& H' G% f, _: m ) r$ F# Y1 G$ T- U4 ?2 t! i0 m$ C
// Create the list of simulation actions. We put these in
1 W3 ?6 x4 H! X3 G0 d9 u: ` // an action group, because we want these actions to be7 J) d/ |( L [
// executed in a specific order, but these steps should
" k. C! h. `6 w& z: Q* [0 P // take no (simulated) time. The M(foo) means "The message
6 a9 I7 q4 m1 v+ V$ [ // called <foo>". You can send a message To a particular% B2 V4 L$ r6 R) b( V* Y- i
// object, or ForEach object in a collection.+ w- l% F9 c' Y; I
$ E9 k6 W8 Z4 {4 V // Note we update the heatspace in two phases: first run2 |! {! X) v+ R
// diffusion, then run "updateWorld" to actually enact the
7 ~3 a* |0 `* P9 b$ @; L // changes the heatbugs have made. The ordering here is6 e$ ^ N6 X" _/ P$ q0 P) H
// significant!
7 H& H+ o1 X; n
* ~% ?8 [$ z+ P4 G8 a6 M0 { // Note also, that with the additional, b$ J' @& G. y$ i& a) q" l
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; j: Z; C7 Q6 L4 B( ~6 g- K2 { // randomize the order in which the bugs actually run$ F0 V5 {- N3 V( V
// their step rule. This has the effect of removing any; M' L, H* D6 t* n
// systematic bias in the iteration throught the heatbug( I6 M. B; z4 n' ]6 ?
// list from timestep to timestep. C9 e, X: F1 R: a- p
* V6 J* N) u, ]8 d$ x
// By default, all `createActionForEach' modelActions have. p4 I7 s! k, v/ Q/ |. N/ J. Z% g! V
// a default order of `Sequential', which means that the
5 t) L" Q. i$ R' q% x) E // order of iteration through the `heatbugList' will be
/ S8 ^5 b" |5 U7 z // identical (assuming the list order is not changed* H( a7 ?; y5 C# @
// indirectly by some other process).+ Z* K2 G6 Q9 b; W
; k! U2 ?5 w a
modelActions = new ActionGroupImpl (getZone ());/ X3 T' O& \- H) F
R, g, T3 K" Q8 ?; s( `7 x K! c1 ^7 ?
try {
) k, Y4 H, G) O: L, b( H modelActions.createActionTo$message8 m- W# Z. _; k( R4 W' C; p
(heat, new Selector (heat.getClass (), "stepRule", false));
$ `+ ^. F& G% ~. A6 ? } catch (Exception e) {
* r& m: q# f& M3 R1 k System.err.println ("Exception stepRule: " + e.getMessage ());: M0 t; n8 q9 R" C% A
}
) F) Q7 Z7 U6 v) h3 r
% n6 a! b1 A! w try {
. _* Q, ~/ l; p Heatbug proto = (Heatbug) heatbugList.get (0);
7 t( @3 V4 x9 F z4 @+ A Selector sel =
) F, a: @( a7 x) z4 ?, j new Selector (proto.getClass (), "heatbugStep", false);& y' U) L* s" b( O4 }4 d# Y
actionForEach =
! a% h0 _ ~0 k3 Z) m; d0 N modelActions.createFActionForEachHomogeneous$call& @$ B5 Z6 b# h0 G, }# L$ s4 z3 ?
(heatbugList,
" z; ^7 z; J6 @* a& b) k new FCallImpl (this, proto, sel,; h' X' r% i6 [: C) L4 [ w
new FArgumentsImpl (this, sel)));
" D+ t" c1 o* m8 l y# O0 W+ Q3 j } catch (Exception e) {1 V R+ }+ X5 J- d9 d
e.printStackTrace (System.err);5 E6 k* N5 v1 ~0 J! I; p% X
}1 k: ]9 V( S, a6 v9 U2 m, `! W
' J% v0 O0 V7 _1 |
syncUpdateOrder ();
- [: M, [" E& C4 L8 {6 q* H5 M7 d3 x9 A* J8 u3 D2 j
try {! C1 J9 U% D: J7 J! |* U0 I2 `+ E" m
modelActions.createActionTo$message
8 a7 ~, Q' A/ [ (heat, new Selector (heat.getClass (), "updateLattice", false));; }( J8 {" s1 T9 V
} catch (Exception e) {
' X: \6 ^0 Y# d/ b System.err.println("Exception updateLattice: " + e.getMessage ());$ ^8 ]# O+ v8 o. ?2 h& D
}, S7 Z: N6 B4 ]5 \1 ~# {. S8 j
" ?1 O1 o3 n1 \5 X" _ // Then we create a schedule that executes the3 h) r+ c$ A9 }( c0 g4 e
// modelActions. modelActions is an ActionGroup, by itself it
, ]5 t& e7 P, j6 V# h6 o, } // has no notion of time. In order to have it executed in- d5 S8 E/ o% r; W+ u
// time, we create a Schedule that says to use the+ \% `: c4 E1 M! L/ ^/ f
// modelActions ActionGroup at particular times. This; x% _5 `: J3 ?/ o
// schedule has a repeat interval of 1, it will loop every
: q- E* b$ G, ^$ X7 H: s6 I // time step. The action is executed at time 0 relative to
1 w: \) N8 O# u! V( K3 \; A // the beginning of the loop.) o9 ^5 x. L1 X: C/ B: S, M- c
) g: A: p* w- ]5 _! u0 _) N% _+ @
// This is a simple schedule, with only one action that is
" [! a5 z2 {; _+ Z1 ^ J/ i1 I" z // just repeated every time. See jmousetrap for more
9 p/ S1 A7 W+ C/ w4 o, C // complicated schedules.
4 f" J5 j# A" T% [( R$ Z+ n ! c, V9 g; q. j; T Q. i5 _
modelSchedule = new ScheduleImpl (getZone (), 1);
/ Z f* [# U8 Y) I" d modelSchedule.at$createAction (0, modelActions);
/ O" o0 t6 }# X' N& r( E
5 l" u& `* j6 Y8 E7 z' ? return this;
, [9 ?8 J2 g3 [+ N! T2 } } |