HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& o I+ s' [* e/ |7 i5 Z% {7 O% F, c
. L. Z R* e" R2 B$ S public Object buildActions () {+ Z" m% k6 ^. W* F7 w
super.buildActions();
% z8 d3 l8 l9 o% R$ s
+ m9 M. M4 i7 E3 o! ?) Q7 w" h // Create the list of simulation actions. We put these in
1 s+ C+ f6 ^2 k q9 B! d: w. ?, n // an action group, because we want these actions to be
5 V, n/ u/ D, e // executed in a specific order, but these steps should
2 v4 L$ x) B% q2 V. R8 O0 ~ // take no (simulated) time. The M(foo) means "The message
# O g" A* j; H // called <foo>". You can send a message To a particular
$ s) z' i) n* f# n. r5 D( h // object, or ForEach object in a collection.) ]* P4 g1 C; a) Z- ~
4 T7 T& u) Q4 c- F" j // Note we update the heatspace in two phases: first run
2 m& y6 F8 B$ L6 }7 ^& ~/ f# J: r // diffusion, then run "updateWorld" to actually enact the0 @8 Y6 y& E. r0 m6 ` p4 o" A. J
// changes the heatbugs have made. The ordering here is7 M" `. p1 c5 b( C4 r, `
// significant!6 n4 v8 v1 x/ |# p% q
4 ^% U) w; t4 Y6 Y% N- {
// Note also, that with the additional
% G- J" u- k9 y) O8 r // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ E' U* D( l4 v/ I( Y // randomize the order in which the bugs actually run
( ?* p# ` A& V( p4 ?9 N // their step rule. This has the effect of removing any$ L! s2 g4 T, h1 |- h
// systematic bias in the iteration throught the heatbug; R, f# m# K! z
// list from timestep to timestep
# v0 f( w" L# W5 v 2 j6 }4 @9 y2 t5 B8 Z
// By default, all `createActionForEach' modelActions have6 V2 ~. \4 V3 b' t; E8 { i* R
// a default order of `Sequential', which means that the
- F9 h; v' N" O // order of iteration through the `heatbugList' will be
0 I# a6 N- m, Z! q* ~2 V3 Z // identical (assuming the list order is not changed
9 z: s5 B' E3 Q- \ // indirectly by some other process).& t3 \& u* x4 @. s( X. k
- X9 \9 ^& K0 u+ M7 e( n7 E modelActions = new ActionGroupImpl (getZone ());9 ]# Z6 Q1 y; {: c! h2 L
& c/ Y; k1 ^/ K8 @. q try {9 Y( | @) a3 K2 q& L# y& U
modelActions.createActionTo$message: S: i6 x- W5 C4 @# |
(heat, new Selector (heat.getClass (), "stepRule", false));
: s0 U. c. y+ ~& U6 d3 W# t } catch (Exception e) {
( l$ E: T) T" E# ~$ Q2 n. r System.err.println ("Exception stepRule: " + e.getMessage ());+ S; V, H1 R) y
}
. d J$ U, t. L6 Q# N( @0 `2 h) t q2 u1 U
try {
2 y4 g( G' u0 v; ]8 C' w/ y# b Heatbug proto = (Heatbug) heatbugList.get (0);
% S7 R# [& _* l |4 V; } Selector sel =
9 O9 u8 X( a% z6 t% [- @/ k# Q new Selector (proto.getClass (), "heatbugStep", false);
$ M7 P; n# u2 H' G. B+ y+ ?* F5 Y* { actionForEach =
7 z* z( _( }; A9 z: _( J- b modelActions.createFActionForEachHomogeneous$call0 a% B& y% ~2 X6 S& Y
(heatbugList,
; W4 [% l4 r Q* a0 n3 r% g9 \: m new FCallImpl (this, proto, sel,3 m) h% |8 \3 o( t- b$ K4 E
new FArgumentsImpl (this, sel)));
, m; `1 f8 w6 c5 w* l% |. I* {/ o } catch (Exception e) {# \/ m, a7 T* w/ b/ C0 k
e.printStackTrace (System.err);
6 a H3 C& H) t! I$ X% @/ w }+ ?4 i& w; g2 l8 ]
: H8 E" h: Z# d( X* K2 b2 ]$ f" m syncUpdateOrder ();
/ g% J% V1 \$ R$ A) s# O6 L5 ~" t$ d
4 H7 ? O: |% ^& z( z try {( T. I$ y }* B0 L0 U1 b
modelActions.createActionTo$message / O8 P. Z1 b* R' `& u. w g5 ?
(heat, new Selector (heat.getClass (), "updateLattice", false));
% A+ Q* a. A# r3 s9 B } catch (Exception e) {
6 m3 ?- w' k0 \% [# ~, V System.err.println("Exception updateLattice: " + e.getMessage ());8 |4 a! N% w. P" k* I
}
% U c5 r8 H( } / y/ Y, \1 P! d& y e" [$ g
// Then we create a schedule that executes the( {/ U& O& n U3 g$ K' z; E2 \$ R
// modelActions. modelActions is an ActionGroup, by itself it
M5 D9 u5 @' k* t6 u // has no notion of time. In order to have it executed in
7 d3 |) X% w4 W1 s // time, we create a Schedule that says to use the
9 N) D; X& a3 i9 [" _7 ^5 w // modelActions ActionGroup at particular times. This" s& a! E0 _4 o- |% J
// schedule has a repeat interval of 1, it will loop every0 W @: ^9 m9 y9 D( ?! q- z3 K
// time step. The action is executed at time 0 relative to3 f+ F" I( c& ^0 U: N; c8 z
// the beginning of the loop.
- A' a9 y1 X( J3 n* W
' K( X5 f& B& U/ n0 `- m3 k // This is a simple schedule, with only one action that is0 R, }% \/ U. z/ |3 h
// just repeated every time. See jmousetrap for more1 m- @: u9 Z2 f
// complicated schedules.
, A* u2 e1 g1 m/ E+ P" t0 o" H
' ?- `0 e( u7 G' | modelSchedule = new ScheduleImpl (getZone (), 1);
) V# o' V t' n, y, ] modelSchedule.at$createAction (0, modelActions);
. B! t+ N: R* }4 W) K3 c
+ Z+ q' @5 U" Y- s" @2 ? return this;& w" N* C/ M: k+ l x
} |