HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 N$ x; @5 b$ o
2 l7 h' {) H! ~* C: ]4 [ public Object buildActions () {
+ i* E8 H$ o# W. v. {% ?7 | super.buildActions();
; `0 H% j4 f T! `; |* Q7 o / w+ \+ d2 R4 H+ j! t- R* i
// Create the list of simulation actions. We put these in3 i& D5 M; i% P" H0 P7 V7 S
// an action group, because we want these actions to be
. `7 Y9 \+ g$ I1 N% c$ E // executed in a specific order, but these steps should
3 t' p# X4 g$ G) C. r& c // take no (simulated) time. The M(foo) means "The message
! s; m2 z2 U u( U0 V // called <foo>". You can send a message To a particular
) A, c) ?1 `+ {8 u2 S // object, or ForEach object in a collection.
: W9 S9 Y5 B6 X 8 H+ s) H k, h) v( U2 j2 D, Y
// Note we update the heatspace in two phases: first run* a; u# t7 t" s" o: R# [4 v- L. \( _( S
// diffusion, then run "updateWorld" to actually enact the6 I* A; t0 o& C. |4 `# P1 `, W
// changes the heatbugs have made. The ordering here is
% ^' Z* Y* M' ?$ S // significant!9 G2 T: ?' L0 J6 K
6 I8 L' b3 \# M/ \/ r/ X // Note also, that with the additional
& Z6 g9 o/ K2 [- Z( L" u" Z // `randomizeHeatbugUpdateOrder' Boolean flag we can5 X- s7 z, O/ h7 c
// randomize the order in which the bugs actually run0 T+ k7 R2 N7 P/ x3 U
// their step rule. This has the effect of removing any
8 ]! M7 J0 ]. H1 ~ // systematic bias in the iteration throught the heatbug. w$ J4 J4 v( b! k* d, ^
// list from timestep to timestep* E( P# L7 q6 O
) X2 a8 n( T9 g3 }" n" A( U
// By default, all `createActionForEach' modelActions have
- Q! n* M7 ~! N0 V, Q+ d6 O9 ` // a default order of `Sequential', which means that the
* X% l6 A7 K9 a4 b' j // order of iteration through the `heatbugList' will be) C! q3 r8 c8 g
// identical (assuming the list order is not changed
. i1 G8 _# `% d7 u, J9 X% \+ W // indirectly by some other process).
7 j, x9 S! j% y : G. H3 t. d* H5 j" G* @; Y
modelActions = new ActionGroupImpl (getZone ());
$ p0 H8 R& C5 X2 l+ W. J9 f" M& T, M% y- s/ I
try {
: H/ J+ P, X; y; T4 v7 r modelActions.createActionTo$message
* L6 G$ u) L+ i$ M% W! i (heat, new Selector (heat.getClass (), "stepRule", false));) }4 \0 h4 E7 Y% h7 d
} catch (Exception e) {4 m" L% M0 n" N3 \) v
System.err.println ("Exception stepRule: " + e.getMessage ());
5 d7 S3 T) x3 |- U' d8 P }
( B9 W: g1 T% M) ? U3 [$ ~, Y' k2 M% h, ~
try {3 Y( [7 P1 Z. j$ D' l6 T
Heatbug proto = (Heatbug) heatbugList.get (0);
+ O9 [ h5 F- d, P Selector sel =
' H5 T/ Y& j- G8 g7 O; u4 r new Selector (proto.getClass (), "heatbugStep", false);7 M$ i H# ]3 {2 N
actionForEach =& O0 u7 Q9 R6 `) N$ Q
modelActions.createFActionForEachHomogeneous$call
2 h, s) r% g' h; W! B% c (heatbugList,& S; m* z: X c5 e8 m
new FCallImpl (this, proto, sel,
% c* U% ?$ U" P new FArgumentsImpl (this, sel)));
" \7 b1 _4 h3 c0 F( [( |& Q } catch (Exception e) {
d; |# G' [0 ]: l e.printStackTrace (System.err);
6 V( ?& X/ w, B }
/ ?# M6 D# A; A+ C$ V3 Z; N : k$ t; b. N# ?: a# i1 @: n
syncUpdateOrder ();
6 f1 \* \) c% X+ ?2 J3 _
/ c9 r% h! j9 v3 z try {& x6 q* e" Q+ S2 k
modelActions.createActionTo$message 7 s/ _: i/ ?! M F4 k% {- n* D
(heat, new Selector (heat.getClass (), "updateLattice", false));& [1 I9 M* x5 m. U
} catch (Exception e) {
+ u' H9 O' Z6 r2 ^ System.err.println("Exception updateLattice: " + e.getMessage ());
# l# T @2 n6 h. S9 g }
1 J: _. R% K/ _8 n6 z% Y Q4 B* |& O, i5 N" E; I3 C
// Then we create a schedule that executes the
) k8 R* l( P7 y* e7 u% Q9 O1 S // modelActions. modelActions is an ActionGroup, by itself it8 h# @. B$ Q. i ]+ `% {( J0 y
// has no notion of time. In order to have it executed in; }$ p% E- i4 ?: [
// time, we create a Schedule that says to use the
2 ^- X% r: c1 Q! I" d2 m1 W // modelActions ActionGroup at particular times. This
- l* _& ]( K, g) k Q5 m // schedule has a repeat interval of 1, it will loop every' M7 L/ `2 G8 T# @) {
// time step. The action is executed at time 0 relative to0 l1 k+ |2 Q. g, ?/ i, h9 \) V
// the beginning of the loop.
) m! T* [7 m X
& w' ~9 r7 e* s // This is a simple schedule, with only one action that is
" F( N' s/ B% h // just repeated every time. See jmousetrap for more
{# q2 O: U/ M // complicated schedules.
& i. g: @8 z$ P+ l 2 v6 ~+ r9 \1 h; N: a4 n
modelSchedule = new ScheduleImpl (getZone (), 1);0 Y8 G5 n2 s8 b3 [# j
modelSchedule.at$createAction (0, modelActions); b* ~$ Z b9 M. a- K7 N1 ~4 d
9 k( P% q/ F' W: @8 `8 m return this;# V( J& [* f5 ]) f6 F% _, ]
} |