HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* G6 T* W5 d( Y# G5 o; C$ K# E
8 H# j W) \0 U4 T( r0 ^" [
public Object buildActions () {
9 z8 n! C; l) e# U, F$ o& J1 U7 M9 g S super.buildActions();0 P7 N+ Q9 p( Q. j* f i8 W4 H
: Q- b7 D! [* s( T( I // Create the list of simulation actions. We put these in
+ \. ~ T, G/ C) H- d // an action group, because we want these actions to be, T/ |! q8 J* D) i7 x; ^3 m
// executed in a specific order, but these steps should
. N$ G1 Y$ d, Z; c) P+ F // take no (simulated) time. The M(foo) means "The message, O6 G4 F$ Z. }0 b) d* c, F$ r
// called <foo>". You can send a message To a particular% Y M0 p% m3 D2 r
// object, or ForEach object in a collection.
. F$ G1 O1 C3 W1 _& } 1 g& U2 T8 p8 e
// Note we update the heatspace in two phases: first run7 `# w. F# U8 A; J/ o
// diffusion, then run "updateWorld" to actually enact the- W; [: d. ^+ i' |5 U
// changes the heatbugs have made. The ordering here is
0 ~. w% f* P7 @ // significant!
& u5 G7 o: ?( F% d ( F7 B; h& f3 Z! y: A I; _
// Note also, that with the additional
4 ~0 ^# j4 d, K+ e9 G' c- D // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ z4 @5 ?. `8 T // randomize the order in which the bugs actually run
, Q5 h# M" {2 w" C // their step rule. This has the effect of removing any
5 Q1 h+ g$ Q |3 U. D0 D // systematic bias in the iteration throught the heatbug+ q7 w6 @8 e$ F$ U3 T5 P- |
// list from timestep to timestep
- k3 d( R- g% p4 k) m+ c- j4 Z. m
: x5 u' c1 `8 D I7 t* V // By default, all `createActionForEach' modelActions have& L) A8 [$ s$ ^/ I5 d8 a, J
// a default order of `Sequential', which means that the8 y6 ^/ @# e% d2 t8 `% u
// order of iteration through the `heatbugList' will be, P/ S F K! Z8 o0 C
// identical (assuming the list order is not changed) T& `7 {8 j- i4 e2 b6 R& Y
// indirectly by some other process).
: _- O! Y0 J1 |" n4 G5 K1 N 0 V( J4 B4 `5 g4 B* C
modelActions = new ActionGroupImpl (getZone ());( f/ n' j1 p& n3 I
; {1 W1 K3 @. [( m) ?7 y* s8 A$ Z
try {
% C+ N9 T- M8 K! T modelActions.createActionTo$message
$ U+ k$ X) ~2 }5 N (heat, new Selector (heat.getClass (), "stepRule", false));& Y; J+ V+ x+ ]( `& l
} catch (Exception e) {
# R j4 y( @# t' @3 m System.err.println ("Exception stepRule: " + e.getMessage ());- R) I& K9 m' P7 ?0 m
}' z4 W7 }5 z8 g7 B; \. e2 p
+ V9 [+ ]. k' @& g* ]6 ~
try {
" r& _, y" s O, q; v9 e% V Heatbug proto = (Heatbug) heatbugList.get (0);
0 W8 R' f& z& b0 @, V$ Y) { Selector sel =
' L- f) L5 Y6 m5 w. C6 d( W new Selector (proto.getClass (), "heatbugStep", false);
6 ]3 n* X0 u) m8 c* F8 T5 k2 N actionForEach =
& d5 D& x5 w/ E0 S modelActions.createFActionForEachHomogeneous$call u, [" N. e; w* S0 ^2 r
(heatbugList,- S" a% ^) l; }0 d8 Q4 d M
new FCallImpl (this, proto, sel,' E4 d% e6 H$ @+ ?9 G
new FArgumentsImpl (this, sel)));
7 W' I; |/ [3 W6 r; m3 g } catch (Exception e) {
/ L% t& A, [: d V' x e.printStackTrace (System.err);( B- Q, l; z o# ] p
}5 g% J7 k7 @& p5 g' u9 B8 ]
, Y& I9 p% S' y$ E8 c" m. |& v
syncUpdateOrder ();
$ M! e; o1 i3 O! R: g9 T& [6 y( a& @$ u7 g$ C! {
try {
4 |. G1 C. q0 u5 F modelActions.createActionTo$message
5 j8 ^ y# O' p! m; K0 f" f (heat, new Selector (heat.getClass (), "updateLattice", false));
, Z0 v# o5 g) x& ^, |3 v4 M& p } catch (Exception e) {4 V! k! A. @# A% p; v3 d' g1 g) |
System.err.println("Exception updateLattice: " + e.getMessage ());: y6 | \7 K4 o' H/ W
}: T% I5 P) r4 ?2 B2 |
0 ^0 T, g0 E' g. k+ S // Then we create a schedule that executes the
# E' o* E" R: u* i+ J2 m) C. y // modelActions. modelActions is an ActionGroup, by itself it, X/ ]; Q7 A" e* J, p
// has no notion of time. In order to have it executed in7 r, r! V |) O! X3 ~2 y
// time, we create a Schedule that says to use the5 Z* }$ N7 a5 c! g; R2 V9 S) a
// modelActions ActionGroup at particular times. This( Z8 g! w& ]! {3 V' J) w R) `5 M) ?
// schedule has a repeat interval of 1, it will loop every8 P* m- r& @, D
// time step. The action is executed at time 0 relative to
; s) @$ ~" [7 Q1 ~8 Y* k' H! L // the beginning of the loop.
; w; {# \5 y7 i5 w4 M4 _# m8 \8 h" y: C2 p
// This is a simple schedule, with only one action that is
7 f7 H/ U& e( \6 v X/ Z8 v$ Q" z // just repeated every time. See jmousetrap for more, @" ^% K; Z5 @& R6 W) p
// complicated schedules.
- T' ]# b8 e+ X. L: b# n# v
9 F0 J s, N0 l" L) f. r modelSchedule = new ScheduleImpl (getZone (), 1);
+ O8 u' L6 }1 N2 N4 i9 v! Q* I modelSchedule.at$createAction (0, modelActions);; r6 v4 c7 g3 j
# R- a, x" G% |9 r1 Y# h: Y: ` return this;* B; z/ V" b- `1 w. h' ]9 G N
} |