HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! P3 e- s1 @$ z9 E- z" Q* {) U9 V" D7 d4 A. _; F9 D+ |' [
public Object buildActions () {
) D$ E- u3 _& x* M- H super.buildActions();
5 K$ y# T- E7 ?- s. M
- `6 t; r4 [+ u! o7 k4 G // Create the list of simulation actions. We put these in J# C! j; V) x
// an action group, because we want these actions to be
/ i/ P2 a2 `3 C Y // executed in a specific order, but these steps should/ c/ ]" b& z0 L. `5 h7 ~, p
// take no (simulated) time. The M(foo) means "The message* I% h2 t6 k$ f- d9 w
// called <foo>". You can send a message To a particular
) B6 `# p/ {/ x. u* T // object, or ForEach object in a collection.$ T/ d7 c7 K; k
- \8 t" j4 z" J+ U; [ // Note we update the heatspace in two phases: first run
$ z" C+ s7 N# W/ Q+ y% @ // diffusion, then run "updateWorld" to actually enact the( S( E2 W' Q# \8 W: u' ^; v# x
// changes the heatbugs have made. The ordering here is
$ {) n" d& g# n& U4 [ // significant!
+ h7 J/ {6 y' E( O1 \" q , T! ?2 Y2 N# G5 c1 t/ r5 F# p( b4 O% n
// Note also, that with the additional
4 D+ E! I) C5 Y* w% e6 z // `randomizeHeatbugUpdateOrder' Boolean flag we can% K$ I5 F9 z3 g! O- _
// randomize the order in which the bugs actually run
2 N, }) f$ t- l2 j( z% T // their step rule. This has the effect of removing any9 {+ u8 k6 Y l4 w$ f( P. y" d9 o) Q
// systematic bias in the iteration throught the heatbug- B! I( H9 v! w8 T
// list from timestep to timestep2 }* s, |" W ?2 H2 a% p/ b3 t8 I ]( ^
; f2 |8 e4 g; [3 }
// By default, all `createActionForEach' modelActions have
# p( G0 ?, r: T' M. K1 M) X, d // a default order of `Sequential', which means that the
]7 q; }9 F' }4 L$ F // order of iteration through the `heatbugList' will be
# q+ K% I. _( s/ H0 U // identical (assuming the list order is not changed
% Y C& I4 i2 m: h6 Z W: E // indirectly by some other process).
6 Q p D. P8 p 0 r' O% a3 ]$ }& u4 m
modelActions = new ActionGroupImpl (getZone ());
/ J' c* n( q. n/ v9 T2 {# }3 f
* g" R. g8 K, L6 i try {4 a( r( ]2 M& h5 l2 b+ T
modelActions.createActionTo$message
3 i+ R! s" ^! q' ? e; o: | (heat, new Selector (heat.getClass (), "stepRule", false));
$ C) X+ H) B) r4 ?4 ?/ P } catch (Exception e) {
5 s- w7 K% H: E5 I: s/ i# R System.err.println ("Exception stepRule: " + e.getMessage ());. e' P3 [/ r# V& Z+ R
}4 a! K4 v/ n N# Z+ Y+ T+ s
8 F8 A" J! o# r8 `0 D try {8 B4 S* k8 y3 b) i( { [7 m
Heatbug proto = (Heatbug) heatbugList.get (0);
. Y% Y( v4 c1 w7 D w Selector sel = + }1 K/ K0 D2 M$ L. _" q; o* I
new Selector (proto.getClass (), "heatbugStep", false);3 P" w4 Z5 e1 r) c* M# W7 Q1 |; F
actionForEach =' g5 }3 p& u) Q! y
modelActions.createFActionForEachHomogeneous$call
W" i2 C* m- _. m) l- ` (heatbugList,
; T# `1 Q- F- G new FCallImpl (this, proto, sel,2 d# Q* `$ u$ F( e4 ]3 M
new FArgumentsImpl (this, sel))); P; y- {5 Z) l, U4 I/ x- x
} catch (Exception e) {, W7 }0 `+ N" z
e.printStackTrace (System.err);1 r& U" H6 P% V0 }
}; |) V5 h7 |: O
0 q! j, o/ h9 A+ L# v. w5 T
syncUpdateOrder ();
! {2 z/ S8 v$ T
" E% C) y' k3 L; S, [ try {
J! B" u1 o/ @7 Y+ r' v/ @3 ` modelActions.createActionTo$message G5 Q+ g/ `* g L
(heat, new Selector (heat.getClass (), "updateLattice", false));
( V3 W, ~ `7 P. m# ` } catch (Exception e) {/ @$ z+ A# T4 @0 G8 \& ~
System.err.println("Exception updateLattice: " + e.getMessage ());
( `6 Q0 B; v8 r j1 p }
4 |( O; ]2 z$ _" j- \) W/ H& S! O1 p: M
! h8 [5 y, w/ x // Then we create a schedule that executes the
/ b/ K/ K2 g1 C- n8 R8 T+ y3 r& t // modelActions. modelActions is an ActionGroup, by itself it* G5 N/ z$ \" ^6 r$ m
// has no notion of time. In order to have it executed in9 o0 r" }! z* A- O5 R
// time, we create a Schedule that says to use the
7 v5 v3 F7 R6 [- o& p: W // modelActions ActionGroup at particular times. This. Z; j$ H1 S9 I4 ?* M
// schedule has a repeat interval of 1, it will loop every: p( |. l% H4 c* M
// time step. The action is executed at time 0 relative to$ w+ [5 W& L. N/ R; ~& m
// the beginning of the loop.
0 r; [2 ~) ^# E$ L
7 R$ @& T) {8 y // This is a simple schedule, with only one action that is
) z- e$ `. p d! v, ^ // just repeated every time. See jmousetrap for more0 M( j; I5 I3 ~; f* w0 T" W8 [
// complicated schedules.
) \0 Q( K, @# P- q! T6 A5 t8 T1 I
' r/ V* Q" Q# U9 F- j3 }) K0 k modelSchedule = new ScheduleImpl (getZone (), 1);
* H) k; L9 N- P6 _ modelSchedule.at$createAction (0, modelActions);6 N9 ]/ J. ~) D- W
1 {+ m0 I* T& S return this;' A% k( x4 V, Q s3 K
} |