HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ Y4 `) V8 L$ O9 L% X6 S4 ^
1 I$ E0 p2 V0 h( c' D
public Object buildActions () {
& }/ Q' V7 c5 L; Q8 ]* j super.buildActions();
8 ?* } L" n% X
8 z" b0 C$ k& L( \ // Create the list of simulation actions. We put these in3 o& J: r& g, K5 X' c% g5 j6 G5 H
// an action group, because we want these actions to be: Z1 b& r |% _2 ?! C6 S
// executed in a specific order, but these steps should# R. E! ]8 Z0 k8 U( Y
// take no (simulated) time. The M(foo) means "The message
: n3 o# Z9 N# Y# u% X // called <foo>". You can send a message To a particular
$ {( Y7 s" `5 {. h0 J& n" {# S- t // object, or ForEach object in a collection.
, V' ]- u6 h% d3 {, j' z! }9 j ' S1 u4 H& M; M* }. m
// Note we update the heatspace in two phases: first run
. _: |, p# }* g- L+ r0 t, L // diffusion, then run "updateWorld" to actually enact the
$ j. k, K9 |" s! N5 c // changes the heatbugs have made. The ordering here is
) @" w' c+ T. U7 ` // significant!" V, ]9 I& W+ @4 e$ l9 l# @3 M9 @
% n5 E# `% x2 I, g, K0 U
// Note also, that with the additional
: F2 J; G' c# g4 j+ `) O // `randomizeHeatbugUpdateOrder' Boolean flag we can7 r# T( l' A {
// randomize the order in which the bugs actually run
0 F% j: j9 F. d, s3 ?, v // their step rule. This has the effect of removing any* R/ I7 a! j; c# G/ j9 @, ~3 V6 C$ f
// systematic bias in the iteration throught the heatbug7 n( y; Z5 g$ Y
// list from timestep to timestep8 z+ c' Q8 I) b& \
2 ?) X$ Y1 f9 f2 g" j; R
// By default, all `createActionForEach' modelActions have- F, B4 Z- m1 y/ |! _7 E
// a default order of `Sequential', which means that the
, L/ b/ G3 A! g' D' V; U // order of iteration through the `heatbugList' will be
7 O% R% y" b \1 n$ F // identical (assuming the list order is not changed
0 U* h4 z" g7 H8 s' F; M1 G // indirectly by some other process).
' I0 u: Y+ W4 S _2 z2 B
, N; w, D6 s" z/ L modelActions = new ActionGroupImpl (getZone ());7 G6 Z4 Z# \9 i' y2 b& A
/ f6 p" Y0 F2 c try {) a7 A. \7 o# i6 I% }7 o
modelActions.createActionTo$message- J# ]' f5 W% ~3 N* U- ~
(heat, new Selector (heat.getClass (), "stepRule", false));
! i4 v" N0 Y7 ~" J3 \2 ^, t } catch (Exception e) {4 E( S4 G1 V6 z1 Z' v" k/ H; ~: ^4 C
System.err.println ("Exception stepRule: " + e.getMessage ());
; V- [$ z. d: `4 v7 x; h }
' d4 ^ f, q/ I0 v- A2 ~
8 |8 l) X9 i3 O2 }( c9 n try {$ B: T3 P( ^# I0 r9 j9 t
Heatbug proto = (Heatbug) heatbugList.get (0);
5 ?" \& m* m" I9 }& Y Selector sel =
6 v O# u. C; C, u! ] new Selector (proto.getClass (), "heatbugStep", false);% ~2 C' p; [, S% n. e! ^# T% Z% E
actionForEach =1 G. N0 g9 m# t8 |5 j/ s! D
modelActions.createFActionForEachHomogeneous$call- A# h7 I1 X3 ^% ^' k: q" P
(heatbugList,
* e A' g( `% U! R new FCallImpl (this, proto, sel,: e- X" J4 v4 q4 n K( v/ v
new FArgumentsImpl (this, sel)));
& e( ~& |7 e% Y7 o6 d0 X+ w } catch (Exception e) {* s( |. U. ^( D4 Q% w1 `' w
e.printStackTrace (System.err);
! I, ]2 g" Y2 X6 {) ^6 `2 T }1 J/ X' l" z* I3 W" a+ A" Y3 o# L
2 F) J, ]- C# Z* A syncUpdateOrder ();3 d% L" e" ~. ?8 v+ q
) N& W& k% D" S& @! r/ d try {2 v& H% j# t( e1 O
modelActions.createActionTo$message
6 u9 ^4 V1 h. x" ]& t (heat, new Selector (heat.getClass (), "updateLattice", false));8 U3 Z4 a6 t9 U
} catch (Exception e) {
N. F+ q$ Y+ C, q: W+ _- W System.err.println("Exception updateLattice: " + e.getMessage ());
: j1 } Y1 j5 S" g1 C% E }' b! _& z0 m1 q
- E0 T: g! `" X3 s3 R( }
// Then we create a schedule that executes the
- L9 Q" |8 I# i! q* T2 f# D; _ // modelActions. modelActions is an ActionGroup, by itself it) Q5 v+ C$ l, \! ~9 B, ?5 @
// has no notion of time. In order to have it executed in
t2 J9 `7 h. g' T! i5 q // time, we create a Schedule that says to use the2 g5 Z7 G" X5 S% y D
// modelActions ActionGroup at particular times. This
/ {1 F0 Q/ w1 D // schedule has a repeat interval of 1, it will loop every
2 l( Q- a0 ^; I. G // time step. The action is executed at time 0 relative to
, V& K b7 Q- \9 }5 W+ u# E // the beginning of the loop.
" Y* @3 ^( s @% |- W( R1 O1 b9 F, `, q- |: A/ v: H
// This is a simple schedule, with only one action that is
. r3 }0 a& P! u+ F; A6 R0 H' K$ F8 P // just repeated every time. See jmousetrap for more) |0 R4 [" Z9 ~0 R8 o
// complicated schedules.
: H" g3 P. @7 s3 X& B6 H 0 S( N1 O# N7 X, X! X9 c
modelSchedule = new ScheduleImpl (getZone (), 1);
) E$ t9 g1 n* g7 v7 r# B/ U, Q1 H& z modelSchedule.at$createAction (0, modelActions); {( T& Z4 r+ w% l
" S6 D u4 x# p: f$ v1 @! X
return this;
7 O& b5 S7 e; w" G" {' @% a } |