HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 J! @. w9 X! m n5 e) C1 J1 k
) p) M' U; K7 h* k public Object buildActions () {
+ e$ P) u* I+ D$ J super.buildActions();/ s$ ] L+ G) y# R0 f' r u
5 |. c8 v! c. Z // Create the list of simulation actions. We put these in4 k1 f9 u( Q( H
// an action group, because we want these actions to be
6 \# \5 W% l7 {7 I8 U% P) V // executed in a specific order, but these steps should
, N$ `; _, e8 O q // take no (simulated) time. The M(foo) means "The message P/ d) B) t/ h; G) I
// called <foo>". You can send a message To a particular1 j4 q. \5 {) ~+ v3 z
// object, or ForEach object in a collection.4 n/ E0 a# x) S! D
6 g8 I5 C' ]1 p/ X2 S+ n, { u
// Note we update the heatspace in two phases: first run2 n; k* P& ^! ?1 J: e, `1 ^0 C
// diffusion, then run "updateWorld" to actually enact the; M" {8 g; \- n1 i* d; H
// changes the heatbugs have made. The ordering here is e. a% ~5 m5 W- v6 A4 e: N# G5 K
// significant!' b! F$ o& p! W" A* p: S
# a' `2 z( S7 e7 J3 O# n$ J
// Note also, that with the additional/ T$ p1 E/ r9 ~ |0 Q1 g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ Q& I- @: z1 h2 I! z0 c // randomize the order in which the bugs actually run% E: L8 v7 T& _9 Z; l0 C- B% W* h% [
// their step rule. This has the effect of removing any! ^: f5 l7 X; y( [! |
// systematic bias in the iteration throught the heatbug
) R J: V7 f5 O: R) G0 h8 y // list from timestep to timestep6 ]% y5 y6 d! H- R
- ]0 Z" `- R j! h4 u3 ]
// By default, all `createActionForEach' modelActions have
& e2 k6 Y: h" ^/ y) \* n // a default order of `Sequential', which means that the6 D/ @' H: D! _- l0 l% b
// order of iteration through the `heatbugList' will be( {0 I6 @7 d; `: S. y4 y9 m
// identical (assuming the list order is not changed. e# F& _- U, K$ c% X* q
// indirectly by some other process).
- ^8 n! `' T# S% X. G2 ~1 D$ T
3 _" A* d E$ f# |4 P# X modelActions = new ActionGroupImpl (getZone ());
' q0 W& c, ~- n6 C8 @+ r; l. _+ [, S B4 b* e! @& j+ N, a2 U7 D: V
try {
7 m% e+ R7 r/ G- V, i modelActions.createActionTo$message
2 Y+ h. L6 d: D N' V (heat, new Selector (heat.getClass (), "stepRule", false));8 Q" |/ M$ I: v8 y% J
} catch (Exception e) {2 h* I" Q- `; g8 {7 M0 ]/ R! x) }
System.err.println ("Exception stepRule: " + e.getMessage ());/ U7 D. r; ^& A' \/ n3 N
}6 s1 P: f6 d# S
+ Z! f9 J2 S! b/ L4 C7 v6 z' ], t" Z8 Z try {
7 G3 A, v0 \0 r/ Y/ h/ W$ h" g5 D Heatbug proto = (Heatbug) heatbugList.get (0);8 H* u# R& d4 t* q2 R
Selector sel =
5 L c3 R* ^3 { A/ I- y, V new Selector (proto.getClass (), "heatbugStep", false);1 A' ~. M. G; C' w# V
actionForEach =
6 M0 j) w P4 \ modelActions.createFActionForEachHomogeneous$call+ P; Z) V( G8 S
(heatbugList,2 Q5 R# a& `/ {
new FCallImpl (this, proto, sel,
4 x+ n- p6 P5 e0 j6 t4 n4 R new FArgumentsImpl (this, sel)));7 e1 n* O3 G( n2 [8 B
} catch (Exception e) {
; _) x8 n) X+ I4 j e.printStackTrace (System.err);
* U; n0 g& r- O! I# r0 ]/ H6 y8 p }
& X6 r% w5 K! n$ s& S
|$ _! o5 J+ b syncUpdateOrder ();: K; c2 ~# P) t O" H, ]8 X
7 Q6 H! ~* E! S* F8 C' D, y3 N
try {
$ V7 Z7 c9 E: i5 E9 z# G: M modelActions.createActionTo$message 4 Q- \, p- T5 l! ^
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 V; g* V8 ~0 ?- p7 s6 u } catch (Exception e) {! G" o, i* j0 F2 A
System.err.println("Exception updateLattice: " + e.getMessage ());1 @8 I& M: N \8 q M+ h
}# W; `0 g7 B8 B. \9 {
) L& X8 r: W# b2 y
// Then we create a schedule that executes the
8 G5 U m* ~ u // modelActions. modelActions is an ActionGroup, by itself it+ d8 v; F( x6 x9 v
// has no notion of time. In order to have it executed in6 Y1 G' s& ?+ d
// time, we create a Schedule that says to use the
3 o! o$ Z4 k& r* D7 b& _ // modelActions ActionGroup at particular times. This
0 _& Z5 c: Y" p- \. s r/ b // schedule has a repeat interval of 1, it will loop every
( i/ ?3 E t6 c // time step. The action is executed at time 0 relative to- K4 D/ w& Z9 X) G8 d8 ]# p6 i
// the beginning of the loop.
# [+ g9 ?9 Q0 l) S
0 \6 S$ m& b: w! \- e" B4 V9 F b // This is a simple schedule, with only one action that is
7 C# k. h" n- Q // just repeated every time. See jmousetrap for more
/ m# S( {- o: D* J // complicated schedules.
+ `2 _0 f) \: ^* y* C4 P
( d0 P1 @2 J- J$ f, j2 S' [6 |! g modelSchedule = new ScheduleImpl (getZone (), 1);$ F7 F9 t, K6 o4 e b6 |
modelSchedule.at$createAction (0, modelActions);" j- M4 {' z% v5 A7 G% ?
" t9 G! j9 Z# X; ]6 w+ F) u
return this;
/ g1 N7 t1 R' ^( V } |