HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: ^1 ~' T B3 F+ z0 R( G+ j
/ M$ X5 X" k: p l: f
public Object buildActions () {
" c6 a5 w1 W) ]' P/ p# m super.buildActions();5 R! ^6 Q x4 q$ D8 h# }9 y9 W! W
: o ?4 J! D" z' s7 C+ ~ // Create the list of simulation actions. We put these in" u( J F7 v5 X+ g ?- }; t: ?
// an action group, because we want these actions to be1 f7 U" q T5 ~
// executed in a specific order, but these steps should a" p; g' o/ G+ d% ~" K
// take no (simulated) time. The M(foo) means "The message
( X4 \0 M8 O" L7 V# r // called <foo>". You can send a message To a particular
8 a# e2 g" v' w3 j% q. P8 C) W [1 H // object, or ForEach object in a collection.% H7 }7 S# F5 H, N
9 B5 k4 s) W4 n" T" K" t$ @ // Note we update the heatspace in two phases: first run. J. f: P# L6 Y
// diffusion, then run "updateWorld" to actually enact the2 P0 ]6 ?$ m% L7 J1 p
// changes the heatbugs have made. The ordering here is
7 u: O) w0 N3 }/ Y. l // significant!
# ^6 p; A- o0 C3 |7 H 1 Y! X( b- K7 @" U7 \+ C
// Note also, that with the additional
+ a) ~* N4 X" ]; f( s; J5 R // `randomizeHeatbugUpdateOrder' Boolean flag we can* }7 Z! K( f0 r% ?2 B0 Z
// randomize the order in which the bugs actually run
7 ~; p) I: G5 u- n0 w( x( n# n // their step rule. This has the effect of removing any- N5 p. C/ p; h9 \! M; ]* \
// systematic bias in the iteration throught the heatbug3 }$ C9 X" k3 W( F- I u$ t
// list from timestep to timestep
) ?/ F. a% |* M
" _2 { h/ C% V' u* \: _ // By default, all `createActionForEach' modelActions have, U) X- y( Z S! P
// a default order of `Sequential', which means that the/ F& Y0 L! ?- U+ h% U m
// order of iteration through the `heatbugList' will be4 A1 M' s8 ]+ A/ D% d6 q) P. w
// identical (assuming the list order is not changed
- a9 e# o/ O" O% U1 |# w. s // indirectly by some other process).
% s- k0 Y6 V+ _4 ? ( O1 k) B7 k; h
modelActions = new ActionGroupImpl (getZone ());
/ I5 Q: C: o/ J% B9 z2 A* i5 ^' g0 B- b; {2 e0 p
try {) J$ E# L) f4 \3 M* q8 E
modelActions.createActionTo$message
9 V9 U5 F F/ f& b1 |. j (heat, new Selector (heat.getClass (), "stepRule", false));
: s6 L( [* m. N } catch (Exception e) {
% s1 Q$ T/ g9 s9 _ System.err.println ("Exception stepRule: " + e.getMessage ());( a9 J$ j& X6 Z& K
}) X; c" g4 u- A
' U/ O8 [$ b9 y
try {' Z, l$ ~3 {& Y0 J3 H# g
Heatbug proto = (Heatbug) heatbugList.get (0);
2 Y0 \5 U; a% B' N( M Selector sel =
N" f) y8 T7 j: g; q+ ~4 i% ^ new Selector (proto.getClass (), "heatbugStep", false);& V% x. r/ R- o- c+ T
actionForEach =
" `% f# e+ k; d modelActions.createFActionForEachHomogeneous$call" R R6 M3 G4 B% v7 C; u% H( D
(heatbugList,
; H7 x9 F6 x& r) {' O3 \; b5 a new FCallImpl (this, proto, sel,
; G! Q# W! d+ T' y( _2 ] new FArgumentsImpl (this, sel)));! ^2 D( w5 ~- [( T
} catch (Exception e) {
& O/ T9 @, J3 @% _+ D4 g+ c% ? e.printStackTrace (System.err);
?+ r/ ?+ m& e3 B }
$ f. _: q& Z, h, W' M9 B0 C/ @
% C4 I* ]9 T9 N/ M3 c+ r syncUpdateOrder ();, Q+ w' Z3 ?/ M% c% F& d* a
( n7 Q3 w1 g1 O7 [1 x try {
* R1 e6 W" A4 [( l modelActions.createActionTo$message 6 b% j/ \4 q* C- C
(heat, new Selector (heat.getClass (), "updateLattice", false));3 C0 ?' X7 ~6 L9 \5 [
} catch (Exception e) {' {$ K. ?% V- E
System.err.println("Exception updateLattice: " + e.getMessage ());
# X x: r4 {. {0 W+ z }
y/ x9 U- S* Y( F) [8 h
- |, p: o% [1 A& T // Then we create a schedule that executes the
$ {" V' X$ [; v/ m$ \$ X; k: i) P; r // modelActions. modelActions is an ActionGroup, by itself it: D, y7 U/ l ^( ~. M* W
// has no notion of time. In order to have it executed in
0 k' u" A: ]; R n7 q& e8 o // time, we create a Schedule that says to use the7 K2 |' w8 J" Z
// modelActions ActionGroup at particular times. This. d* d# B% _- O5 e3 ?. ^
// schedule has a repeat interval of 1, it will loop every" X& F2 X; @2 c1 {8 O! D
// time step. The action is executed at time 0 relative to
+ h. f$ Q" Y$ d; a* [2 ]) I // the beginning of the loop.$ o! N( o; n- P' m0 A
; o k3 z" p# }/ d D* R2 ]
// This is a simple schedule, with only one action that is
- x7 A( | x7 i% s- z) M // just repeated every time. See jmousetrap for more
' I1 }: z; B9 h* e // complicated schedules.
9 l2 Q5 p7 T" N& u" _7 w
! V4 f2 S; v3 S2 E- @; i3 M; H modelSchedule = new ScheduleImpl (getZone (), 1);
# J g& h) t( e; z) e7 t modelSchedule.at$createAction (0, modelActions);' m4 n; V+ |# b
- ~2 {) h# t4 z \! u X- P, b, K2 p
return this;
- @2 G4 _1 \2 M6 i4 C( {; ? } |