HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- I+ o3 a9 [) M. R
/ E$ G# X. Y( _ public Object buildActions () {
! P# `9 o% P; w3 d6 u; E super.buildActions();
$ v7 B5 W+ I) R% U2 J9 L . u+ ]% r6 s% Y6 i" x# s3 [' Q
// Create the list of simulation actions. We put these in3 r; a4 J* W+ ^
// an action group, because we want these actions to be, y6 I8 X- N) B8 R; h6 K" U" `
// executed in a specific order, but these steps should+ r' g6 J5 {& h* w7 s
// take no (simulated) time. The M(foo) means "The message( @5 U; v& w: d6 \& r( L( l
// called <foo>". You can send a message To a particular4 _3 U$ D) [# Y8 c+ z
// object, or ForEach object in a collection.
* J* j' N, g& {2 ?3 Z
) A/ N7 J5 C% R& w // Note we update the heatspace in two phases: first run
- s3 w U" H! w+ n // diffusion, then run "updateWorld" to actually enact the
4 \. T: c3 A; O9 Y) x( M4 p7 j- |2 j // changes the heatbugs have made. The ordering here is
$ \9 Z" V4 v! b% M7 {2 M // significant!
: s4 @* t7 o- P( D0 s6 m" N( z! U9 \ ; `; @' q* a1 G
// Note also, that with the additional& x/ Z( B! Y- [8 q- M
// `randomizeHeatbugUpdateOrder' Boolean flag we can
5 I0 S8 k ~; p* ~ // randomize the order in which the bugs actually run2 U: k% z9 O. N% ~# m
// their step rule. This has the effect of removing any
$ [6 _. h7 ~# ]2 O- y5 C // systematic bias in the iteration throught the heatbug
! Q" M% ?4 ~3 h( _4 c7 O // list from timestep to timestep+ K+ E3 t4 j! h3 y1 E; v
! d) z% P9 l5 Z6 z# Y6 c& g d // By default, all `createActionForEach' modelActions have5 |- r; I8 P. j G. u l1 U! u
// a default order of `Sequential', which means that the
- Q2 g& D- {6 g# \' r // order of iteration through the `heatbugList' will be! z1 G! r" x* ?& c k- t- S
// identical (assuming the list order is not changed; Q0 M6 }# u; o9 P0 m0 H" \9 c
// indirectly by some other process).5 f% c( }* a9 m0 V9 v7 ]
) [0 c$ B: }1 l# t% E modelActions = new ActionGroupImpl (getZone ());7 o! b: Y4 M7 x
) z7 V7 Q' y$ U. i6 }
try {
; B. f( f4 Q# b N( R modelActions.createActionTo$message
1 I, v8 f. A. u/ y9 l( n! m, A: ] (heat, new Selector (heat.getClass (), "stepRule", false));$ d- ~( g' d2 ?/ A
} catch (Exception e) {, R9 Z/ ?3 S# k8 _! `8 m
System.err.println ("Exception stepRule: " + e.getMessage ());
( I2 T: S) }$ U+ P5 L% F }7 r( f' G$ I" {5 b# T: S
$ P1 X$ G5 h' n9 u( q try { f. _/ c. I) R& S) k
Heatbug proto = (Heatbug) heatbugList.get (0);7 R0 M# _( @1 q8 s( R u6 l
Selector sel = / O" n7 C4 x; G1 p7 I7 {) \! J
new Selector (proto.getClass (), "heatbugStep", false);/ z- f+ Y8 M+ H* k! y2 H9 c4 s
actionForEach =
9 E; f3 |; H) C4 u modelActions.createFActionForEachHomogeneous$call
1 f$ S4 K/ Q# K (heatbugList,3 W# Q+ F- B( P( G0 D% e" l
new FCallImpl (this, proto, sel,
# Y/ m9 p+ ?; x" e new FArgumentsImpl (this, sel)));- E7 j) q0 b; `2 Z2 Q
} catch (Exception e) {
) Z4 A7 {7 W' X' z# g' r' p e.printStackTrace (System.err);6 `" o& d. {7 u# G/ _' ?
}9 b. w+ d* `/ m
7 o, H1 F$ ]5 v1 k. Z# ^( S* a syncUpdateOrder ();
2 ?% f* j: K0 r1 i# I
' ^1 P; }, p" B1 t) w try {" \( A, `) C4 L8 A- o; l2 L
modelActions.createActionTo$message
& o/ L% Z0 F4 v2 N0 P (heat, new Selector (heat.getClass (), "updateLattice", false));
Y; p% Y: q. r4 X0 Y( T/ U' E } catch (Exception e) {2 m4 b$ {# v% ~& s
System.err.println("Exception updateLattice: " + e.getMessage ());5 k, Z u5 c ?$ l' y
}) A E1 H* ^- t% o( ]
, W C/ t9 z% X, E1 N1 u // Then we create a schedule that executes the
( M$ s* r$ g8 T3 \0 i* t; b$ i // modelActions. modelActions is an ActionGroup, by itself it) {+ \. z( {6 z# J4 P6 V
// has no notion of time. In order to have it executed in4 ~# L) Z m4 o% f
// time, we create a Schedule that says to use the" t% Z9 m+ ?7 K4 X
// modelActions ActionGroup at particular times. This: s- R7 A" L8 ^* k- p
// schedule has a repeat interval of 1, it will loop every
. d+ E" @+ F! Z- W* u6 j // time step. The action is executed at time 0 relative to
; G/ Y0 j" i) I+ t; g // the beginning of the loop.9 _ Z; s' m9 R2 y! n9 k
* X \4 S( A- S l! l
// This is a simple schedule, with only one action that is4 W- N8 Z: F+ G6 W# ?$ J
// just repeated every time. See jmousetrap for more
. W: T# J$ M2 V/ c$ { // complicated schedules.+ T. r _/ ?; @/ ^- ?
( J4 h/ y4 J0 {) Y
modelSchedule = new ScheduleImpl (getZone (), 1);
% k! z5 v# P" J modelSchedule.at$createAction (0, modelActions);0 w. T, O* a# d" i9 D
9 S7 j- [% U) W( q1 j4 D, E3 L return this;
5 E$ s( k: }9 `: \0 A } |