HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 E8 D+ A! U7 E6 H0 d9 `
/ i3 m8 @9 t; s* C% b) [/ p+ z
public Object buildActions () {) Q; b9 ^# V W8 ]7 p1 O
super.buildActions();. J! R4 y4 s' ~+ V6 g: q' X
) x" }1 D4 o4 D, z- Q' J' N // Create the list of simulation actions. We put these in* F7 h0 K! g+ e1 R+ C- m# f1 A; `. [
// an action group, because we want these actions to be, B7 K) _) C% H9 M7 T( t
// executed in a specific order, but these steps should
$ _& i& s$ M6 Q B- [ // take no (simulated) time. The M(foo) means "The message
8 A7 q, ]! U% q# J: h // called <foo>". You can send a message To a particular; W( I: Q6 L2 X J0 O0 A! {+ Y
// object, or ForEach object in a collection.( I. ^6 `' D# l. j! e9 r
4 ^. z' H, ^/ }) ]( Y
// Note we update the heatspace in two phases: first run O' G4 `4 |# j1 d
// diffusion, then run "updateWorld" to actually enact the
. x5 Z. j! \* C# J5 l0 R // changes the heatbugs have made. The ordering here is S, E; T: K1 E5 I- k, ^
// significant!
& s6 s- i* {5 D) b8 \2 s; ` / G& `. d& W" w
// Note also, that with the additional
" G' m2 I5 k& [5 I8 p" i# f // `randomizeHeatbugUpdateOrder' Boolean flag we can( i* E2 M+ h8 D. p% L6 }( n) z' l
// randomize the order in which the bugs actually run* s$ x3 A! x3 d; ~0 ~
// their step rule. This has the effect of removing any4 R- g6 @' c& q$ }
// systematic bias in the iteration throught the heatbug- q2 N) D$ r& X" D# U! M# W
// list from timestep to timestep+ b \' j# _ d! g
$ G3 G6 A# @4 ? g; E // By default, all `createActionForEach' modelActions have* k8 ?) H) @, c* y
// a default order of `Sequential', which means that the2 l) `3 `, F- j- M
// order of iteration through the `heatbugList' will be7 K' A/ C( F. r6 a' {7 n+ j) @
// identical (assuming the list order is not changed! Y( U' z7 V Y5 m1 |5 H
// indirectly by some other process).' G4 X) N& w, W% C
. }5 \, c. b* B modelActions = new ActionGroupImpl (getZone ());+ I' W* K1 ^* t% q) x
. i" e; N( g Q f. L7 e# _
try {
( v1 l" Q% g* H. ] `, P! H5 [ modelActions.createActionTo$message
: v) E/ I0 K7 Y. P8 u6 _ (heat, new Selector (heat.getClass (), "stepRule", false));
/ R+ I# a9 R4 i' I4 y1 T: g0 a% n } catch (Exception e) {7 \' B5 k4 M9 P7 E/ f$ q' L8 \
System.err.println ("Exception stepRule: " + e.getMessage ());- f9 g0 R$ M& A- X6 B
}$ m( H3 P b) e" A0 L. S! @* ~
3 C; M" A, L7 [- X% N' q: p5 e try {
, M* ?, r7 N y+ C& i Heatbug proto = (Heatbug) heatbugList.get (0);1 v0 v8 L( Z. J. S# P1 l
Selector sel =
! V1 {, D* O/ p new Selector (proto.getClass (), "heatbugStep", false);
4 B [9 \: V+ L5 e! Q, Q actionForEach =! a: h( @1 U0 d- j: K8 q
modelActions.createFActionForEachHomogeneous$call$ P* ~' u# {: j
(heatbugList,
4 P( a- \1 i1 r( v, I( c new FCallImpl (this, proto, sel,
- \6 } K2 _0 r9 J$ L new FArgumentsImpl (this, sel)));( L+ V6 a6 m( _5 L8 E2 b
} catch (Exception e) {
+ P3 w) x" J1 b* W& W e.printStackTrace (System.err);
1 l/ j) K+ k) L5 f- o3 M5 U) p7 I' E }
* N0 Z; `6 {- T+ O1 Y9 }! X 2 ? O5 a0 d9 ?2 i' l! h
syncUpdateOrder ();
/ U T, E0 G9 p" j+ G* g0 s1 N! f$ f6 ]
try {2 O: f7 G' l. v0 `) z. d. W
modelActions.createActionTo$message 4 v4 K& z; N3 H: K s @1 ~0 |/ x
(heat, new Selector (heat.getClass (), "updateLattice", false));# {) a. Y% c7 j' j
} catch (Exception e) {
$ {6 y8 K9 p8 `3 D System.err.println("Exception updateLattice: " + e.getMessage ());+ v( b5 l7 k, F1 J+ q/ R9 W
}
1 A1 C) s u0 U Z- b: O) \. l ) _& H, _+ K: e9 s" q+ [0 ^' S
// Then we create a schedule that executes the+ K$ h2 U4 \/ B# i
// modelActions. modelActions is an ActionGroup, by itself it
0 X( A" C( D6 y$ ~/ f$ v) S, u // has no notion of time. In order to have it executed in# C' ~9 x! J! H# i- C: |
// time, we create a Schedule that says to use the0 c# X# f( t6 w( Y9 A
// modelActions ActionGroup at particular times. This
, [1 j$ R% v O' } // schedule has a repeat interval of 1, it will loop every
! l2 m n5 }% b+ ^9 N% C // time step. The action is executed at time 0 relative to
( E4 Z. Z X2 o // the beginning of the loop.
; T9 L& y! d, O) D" J8 \, b( d2 v3 w7 |9 W
// This is a simple schedule, with only one action that is
: F% P& h- j+ j9 _9 W/ O // just repeated every time. See jmousetrap for more
( h1 Q1 b/ D, v1 @ // complicated schedules.
% o& S5 d2 V" b5 E
0 m" W+ _- L- k modelSchedule = new ScheduleImpl (getZone (), 1);
. U" f; D8 ^$ X4 N modelSchedule.at$createAction (0, modelActions);$ X5 R1 p9 e! W7 L. ~% C. T6 v+ W( ?& s
' a1 x1 f% \" A5 ~ return this;4 x U% h' C- C& `& I
} |