HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 n2 i$ j, e d- M0 K( i! O& ?. X( o9 L. F
public Object buildActions () {! B/ O7 H0 j6 N/ M9 J# v4 [
super.buildActions();* Z7 K6 O+ D5 { F; n
~* ]# z& s% U/ z) Z // Create the list of simulation actions. We put these in4 b) Q7 ^) I/ F Q' k
// an action group, because we want these actions to be9 i' e- c+ q& n6 k% l( n
// executed in a specific order, but these steps should' G. K' Y# H& _3 X7 U' I
// take no (simulated) time. The M(foo) means "The message
' B# ^! n) _+ s4 q" o6 B! a3 |4 ` // called <foo>". You can send a message To a particular
. q0 N( T( Q Z X // object, or ForEach object in a collection.: {( g# ^+ g+ R' Y' C0 n5 A
* v. z0 d/ u3 s: c4 d" Z- @
// Note we update the heatspace in two phases: first run* y) ?( j5 J9 f- n/ Y+ A5 K' i
// diffusion, then run "updateWorld" to actually enact the; G( J( j3 _! v! F# y+ a. T# a. x2 {
// changes the heatbugs have made. The ordering here is
3 l5 C, T* }; X7 u( R* ^4 S( v // significant!
- f* p8 a, G) U+ E
( m; w$ E( c# k0 H0 ~ // Note also, that with the additional3 ?- g' m% R% ]( ^2 w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" Z2 x" E! P4 p; a+ X- m- Y/ G // randomize the order in which the bugs actually run- S9 @/ t- B. c: {( ~3 e
// their step rule. This has the effect of removing any
" O: I8 c( `9 r( l& ^% q( _ // systematic bias in the iteration throught the heatbug
* I. x, n4 q* |3 J // list from timestep to timestep8 b5 E7 y+ e! n7 b
8 k/ E, k2 Z- h- z/ V // By default, all `createActionForEach' modelActions have+ f* l' t' c6 |. A$ z |! \
// a default order of `Sequential', which means that the3 q+ q, B- Y7 ?
// order of iteration through the `heatbugList' will be4 {3 z8 M8 G8 r) q
// identical (assuming the list order is not changed8 Q$ `2 g8 H7 D7 a2 e
// indirectly by some other process).
`. ^2 \7 s4 C& ~
! i, @$ J9 G3 ]; ~ modelActions = new ActionGroupImpl (getZone ());3 s- s. B7 d4 }1 m T8 F8 Q
) [; P! E) `! \; F
try {
( V" l$ t8 C5 H Z! {( l modelActions.createActionTo$message
# l" `* @4 t0 j+ o3 p (heat, new Selector (heat.getClass (), "stepRule", false));
" c( N# E3 |7 _1 a' f: H/ ?% ` } catch (Exception e) {; k9 N4 ]! l c
System.err.println ("Exception stepRule: " + e.getMessage ());+ P/ Q$ ^4 Z, ^8 n: U# T5 O
}; s4 P0 Y# U* b0 s2 y$ n' B! y$ X# }
$ ~1 ~, v9 v* V/ U# G; Q
try {6 E# R( j: Z) `% s5 f
Heatbug proto = (Heatbug) heatbugList.get (0);4 a% ?3 @/ c& J- n3 W
Selector sel =
; _* f( \, ?$ t* P: J, \" y6 m new Selector (proto.getClass (), "heatbugStep", false);
& y# k( s2 n6 Q [4 x- \ actionForEach =" \# O6 ^* I" _! U
modelActions.createFActionForEachHomogeneous$call
, e g! X- w' n) z) R5 ~5 I: X (heatbugList,
: W! G0 k" p7 ^0 g* U3 [ new FCallImpl (this, proto, sel,
. @; ~4 A Q1 x8 p new FArgumentsImpl (this, sel)));. S8 X" V, z, \2 V
} catch (Exception e) {
/ v' F$ F2 V* Y4 p6 I* j7 | e.printStackTrace (System.err);
! }* W8 E, n: ~5 T9 `+ h i }7 U% `+ r' ]! Y* ^0 c
6 x+ Z0 F) c0 H4 W* \, x! O& u
syncUpdateOrder ();
E. U7 M) q6 T: K( `4 @0 o$ _& [& X$ t8 W: x
try {
/ A- k- c @4 h& q& N, o modelActions.createActionTo$message 3 J1 o5 ?1 h8 |5 I. r
(heat, new Selector (heat.getClass (), "updateLattice", false));; i: {1 u) e, I/ z
} catch (Exception e) {
% D+ A- p# z0 q System.err.println("Exception updateLattice: " + e.getMessage ());
$ H3 Y+ x! R% y& d0 t }$ y/ h4 ?- j- [- b* T
0 d" o& j. [7 t; W& y3 U: `- X // Then we create a schedule that executes the
5 T3 |- y- |+ J- S4 f' B // modelActions. modelActions is an ActionGroup, by itself it& E* H0 P& a1 G8 N! P
// has no notion of time. In order to have it executed in" N9 H7 r4 ?3 A" @. X O' J& N
// time, we create a Schedule that says to use the. a0 }5 ? d- T, j2 \* R7 w
// modelActions ActionGroup at particular times. This K( b" F+ L1 a8 M, X
// schedule has a repeat interval of 1, it will loop every% e& I+ N q9 R, A' y' d$ Y
// time step. The action is executed at time 0 relative to
]6 O* _* W/ [4 z% _9 x // the beginning of the loop.+ n( U: K. k- |2 V. o [8 p. q2 X
# p! _+ S* k# E1 [4 v2 `
// This is a simple schedule, with only one action that is- I2 u: x1 B7 z* K% Z! n
// just repeated every time. See jmousetrap for more" D* N- {* e, u" ~
// complicated schedules.
+ t; p- l; X. `# h' W ! H/ `* B1 M7 H) q& N
modelSchedule = new ScheduleImpl (getZone (), 1);
2 \6 E1 D7 P! g, H0 X; K5 n modelSchedule.at$createAction (0, modelActions);
$ T1 n2 G5 Y; s: C 7 ~1 Z* T4 T( R' ~
return this;0 ]! ?. J& h0 t
} |