HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 M i, P5 U/ p9 P
( y/ p' @* H, |1 b4 K public Object buildActions () {
# S) {8 v8 C4 V7 v# W$ {; A( R super.buildActions();; M+ O. ~/ f- O% h8 n
' W# y( c: x k* G8 g9 d
// Create the list of simulation actions. We put these in
$ k- j0 n; S) G) ]: U // an action group, because we want these actions to be
% W' c5 [$ B7 s" L+ Z' b // executed in a specific order, but these steps should
/ S# i& I9 _2 j* p$ F( G // take no (simulated) time. The M(foo) means "The message+ M1 b, r; X3 m1 P) F
// called <foo>". You can send a message To a particular! Y, b) B9 h0 }- @
// object, or ForEach object in a collection.
# d8 |% W% e; s6 E( D4 j V6 J0 \4 u6 F. M* i
// Note we update the heatspace in two phases: first run( i2 Q0 C0 d6 T% h9 Q6 b
// diffusion, then run "updateWorld" to actually enact the
" X2 @2 x- P; X. \0 K2 P' y9 u0 u: B8 w9 \ // changes the heatbugs have made. The ordering here is7 O; I O& d) a- I: f
// significant!3 e/ u1 L( \, I/ T m
7 q/ h. s2 O$ H# }
// Note also, that with the additional
; h: p) `8 V7 t5 Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 C8 g( g! p# A; o+ K // randomize the order in which the bugs actually run
& U) I+ D% Z$ `! X* t // their step rule. This has the effect of removing any- p4 T1 x9 ?4 g% v
// systematic bias in the iteration throught the heatbug
, \. _2 h7 q% O5 ~/ F9 ^ // list from timestep to timestep6 V) O) \) Q, Q
3 e* t( `' t* \$ @
// By default, all `createActionForEach' modelActions have
5 c( u# D( b1 E' s8 k) P1 F // a default order of `Sequential', which means that the- c6 ]& s( r) C$ F
// order of iteration through the `heatbugList' will be- k5 D" l4 Y: d7 I9 r
// identical (assuming the list order is not changed
: P" ?# B$ Y9 ?3 p1 e/ ?2 \) r // indirectly by some other process).. |+ U- [4 R( j$ H/ D
: C( Z/ ^5 U7 w E
modelActions = new ActionGroupImpl (getZone ());6 Q; k0 T. W, ]% N, K' M; C. P
; f! S& P: Q- K, _; Y3 {& N
try {' S P- u. n9 Y# A" P
modelActions.createActionTo$message
3 ?( s P& u+ O! y& _ (heat, new Selector (heat.getClass (), "stepRule", false));+ N3 ?3 U" k3 w. B
} catch (Exception e) {
! Y* h9 }% N5 u9 _( U- G! r4 Y4 c) ^ System.err.println ("Exception stepRule: " + e.getMessage ());
8 |7 ~9 y, D' O5 x/ [, U n4 a' S }
I) p Y$ ]$ `) p' `, r+ u
' C9 |3 y: n/ O X8 q$ }+ v try {$ O2 g3 [4 n7 @% x% t' C L3 u I Q
Heatbug proto = (Heatbug) heatbugList.get (0);
+ L9 Q5 E+ J- R, H0 |1 a6 ?6 } Selector sel =
+ \: [9 I/ w( {5 D L7 F0 k new Selector (proto.getClass (), "heatbugStep", false);
5 J L0 E. z. `1 g actionForEach =
2 S; s, g4 k- L) |& t modelActions.createFActionForEachHomogeneous$call
5 W5 h# U2 U d; w8 B2 q (heatbugList,/ B" M: M7 Q v8 ]# [: p
new FCallImpl (this, proto, sel,8 E: O6 M. I2 [* K
new FArgumentsImpl (this, sel)));8 w. B2 G S, S6 j
} catch (Exception e) {# |& D, _) k. x- {" E7 M0 `4 t
e.printStackTrace (System.err);2 t/ r; @: |/ }: }+ q+ f4 ~
}
* b0 {3 S. I7 {$ ^9 p6 Q2 Q
9 s9 l% q' \8 L J8 a2 f syncUpdateOrder ();7 S, U. `- x% {+ `: G
- y! F0 y. q: r- `3 j try {, ?( V0 G; J9 c0 a% r
modelActions.createActionTo$message ' w P- C3 N z4 L) {$ W- C
(heat, new Selector (heat.getClass (), "updateLattice", false));* i7 ^: j) k( d+ |" g' Z4 v; A
} catch (Exception e) {3 M! I6 m# j$ b+ m* G
System.err.println("Exception updateLattice: " + e.getMessage ());, p4 ` `3 m& B4 O" \
}. Z5 O( I% D8 P" M1 e) [
; g5 x0 i5 r) g e' d5 m" B // Then we create a schedule that executes the
6 P% X) a9 l% ?! u // modelActions. modelActions is an ActionGroup, by itself it o! K6 O) ?" T1 L1 D0 _, X/ N1 @
// has no notion of time. In order to have it executed in
/ A* `9 a4 d& O5 } // time, we create a Schedule that says to use the6 l* `( j9 b- g1 T; [, i9 B! k* E( B
// modelActions ActionGroup at particular times. This7 h( N8 J: P: i6 N4 n4 z& i
// schedule has a repeat interval of 1, it will loop every! c; o. T" }7 e; ?% [( Q1 T
// time step. The action is executed at time 0 relative to
1 M3 n; X3 R$ }+ t* v // the beginning of the loop.
( a+ w; W6 `/ ] y: h# D1 W. m: ^4 L3 ^9 p9 f- E
// This is a simple schedule, with only one action that is6 H* ]. L! N0 ^7 O# v' D1 g
// just repeated every time. See jmousetrap for more
2 Z5 [7 x. o' w( }3 r // complicated schedules.( g* I9 ?$ P4 T! q8 Z" s& y* r5 f
# v0 T; x7 K+ v0 T2 R/ a2 R/ Z modelSchedule = new ScheduleImpl (getZone (), 1);
9 ~8 L2 o8 n- J; X, J) Y modelSchedule.at$createAction (0, modelActions);7 C/ i- N. o# G
' |1 k7 Y; q% L, D4 n
return this;
h7 w: _# p9 ?* U } |