HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 d% H+ ]7 [% B9 o w' H
9 x) ] C/ G8 k8 x# \. j- c& P7 }3 T public Object buildActions () {
) e. _- y5 f% H; o% v super.buildActions();" k. c3 o4 j3 P
& `7 ?* n( z: H8 w // Create the list of simulation actions. We put these in
' }# q$ w2 k( z // an action group, because we want these actions to be
/ k9 `( s. L" E/ p R0 T // executed in a specific order, but these steps should$ r2 y* u9 H- K0 C
// take no (simulated) time. The M(foo) means "The message
: k7 c9 y( A0 V. q& }2 t0 S // called <foo>". You can send a message To a particular
- M& K/ ]$ `* R; m8 K9 b // object, or ForEach object in a collection.
, H: I! k, J$ \' V3 k+ ? 5 H' v/ u5 F$ @2 k
// Note we update the heatspace in two phases: first run K* @2 f! c s$ I0 ^) Z
// diffusion, then run "updateWorld" to actually enact the
! R4 @7 k( E: u6 I2 ?9 o# O* A // changes the heatbugs have made. The ordering here is
* k- n4 R/ Z9 W$ C8 W4 H // significant!9 @& e9 M) j+ B2 v
4 |% `( x; T/ {9 y( t9 a6 P
// Note also, that with the additional2 m3 D6 d8 f% |" L- ^+ x
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 Z0 n4 T* O! Q% _8 I3 ?$ W7 k
// randomize the order in which the bugs actually run
/ {$ t. o. H6 n9 _3 ^ // their step rule. This has the effect of removing any
! `8 _( z+ s7 x% q, h- ~/ V // systematic bias in the iteration throught the heatbug, N( M4 g) O$ O8 }, m
// list from timestep to timestep/ w# F3 d7 M# F8 n& g1 q) _' l
: u; P- K5 w5 ~# F% Q2 e
// By default, all `createActionForEach' modelActions have
; c5 p# f6 n! _2 q# }4 w // a default order of `Sequential', which means that the; r! L3 m: Q* X4 A
// order of iteration through the `heatbugList' will be# a, z1 H! k2 D9 _
// identical (assuming the list order is not changed
7 t2 _1 a; E; y$ n+ G // indirectly by some other process).7 q3 |6 P5 T G* g$ z9 k* l0 K; R
1 i( d% B+ k3 N
modelActions = new ActionGroupImpl (getZone ());* L8 X2 O7 S C1 v" z* e
; ]/ l4 ]; s, Y5 `5 p try {" a: \7 B* b& G8 L( F9 Y) @7 w# |
modelActions.createActionTo$message
3 |+ [! ~' j( n (heat, new Selector (heat.getClass (), "stepRule", false));; w+ m# O* ?0 o( P4 B# f; m
} catch (Exception e) { ], r. W) `+ \, ]' s/ |) J
System.err.println ("Exception stepRule: " + e.getMessage ());9 ^5 ^7 p, a8 ^9 t- \7 m+ ^- ^
}
5 m. h( g4 | _; ?6 ?" U$ y# r7 ?
) a C+ j& y" G7 X+ g# Q! V try {; z; }4 y# @, s& {* X9 p, n9 [+ k
Heatbug proto = (Heatbug) heatbugList.get (0);: m$ W0 J6 i; X# P+ A( p/ L
Selector sel =
& P. o# [; E0 a new Selector (proto.getClass (), "heatbugStep", false);" \, i3 }7 }& k6 q8 A3 o( m
actionForEach =5 w! d7 r: D3 A. U9 X$ w
modelActions.createFActionForEachHomogeneous$call2 e8 ^$ Q. M1 q' \
(heatbugList,
5 c E7 J9 p J6 Z0 b9 s7 r new FCallImpl (this, proto, sel,! r1 d3 k @: @! ]
new FArgumentsImpl (this, sel)));
5 ?0 z F+ ]* z } catch (Exception e) {
) [- P6 s4 h7 L$ [2 [6 q/ }6 ^ e.printStackTrace (System.err);
! c" A' A }" d7 A' e }
4 f5 i/ p- o" e, U3 ^ j; p8 K% \2 Z$ u
syncUpdateOrder ();3 B- e) J2 z. Y
3 D+ n# f/ R- U; c2 Y try {
1 F5 F/ G2 O* ?8 L: j4 c9 F modelActions.createActionTo$message
S0 G& T' s) v (heat, new Selector (heat.getClass (), "updateLattice", false));1 _- i% r q9 y7 z
} catch (Exception e) {
: Q) D2 z" T* x System.err.println("Exception updateLattice: " + e.getMessage ());
$ M# N' \. L2 R# V }
T. C F2 b4 a& g* Y4 k
* A5 @* O) w. ^+ H, ?& P7 P+ o // Then we create a schedule that executes the
/ ?+ Q: y* a# r& B1 ] // modelActions. modelActions is an ActionGroup, by itself it
$ g8 U6 p7 O! P // has no notion of time. In order to have it executed in( s0 j- X2 g z5 p) ^
// time, we create a Schedule that says to use the
: T& `1 O1 t8 F+ x4 f1 e; |" u // modelActions ActionGroup at particular times. This
* {! A& c. W) R5 W# j" R8 U // schedule has a repeat interval of 1, it will loop every
7 ]$ g0 s' M3 r // time step. The action is executed at time 0 relative to
" G/ p/ s3 V! {+ i0 U; @( Z // the beginning of the loop.( K3 T& O( X9 k/ B. a
6 c+ A; C8 h' t3 ]0 b r; A& |
// This is a simple schedule, with only one action that is; H- Y1 n$ @' O( Q/ E! v6 _/ k
// just repeated every time. See jmousetrap for more
6 t4 x1 s3 H7 B5 D" `) ] // complicated schedules.
# z J( `0 U7 [) }; s% W2 C * [) K0 z- W' `: A" w2 U
modelSchedule = new ScheduleImpl (getZone (), 1);* a0 [1 l7 E0 t( ^* E
modelSchedule.at$createAction (0, modelActions);' g! ?# z" S9 t, F
) B* D9 ]4 e+ O5 s: J% F) V$ o2 U return this;, k4 U% y4 |% j7 b7 }: p3 e/ b( r
} |