HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 F- f7 g& @6 u
: A& ~! S& a3 R4 S' S% N; o public Object buildActions () {
; m4 O; c, |6 h( Y7 F+ e+ ] super.buildActions();! |5 k9 j9 a0 M, K2 K, C
! f! n1 d& u5 w- G5 V // Create the list of simulation actions. We put these in
: `+ z/ V1 h$ Q0 t( Z // an action group, because we want these actions to be
+ O g; P" }" @' [8 d // executed in a specific order, but these steps should
6 e7 _: p) i# N# [8 W i // take no (simulated) time. The M(foo) means "The message; B8 i! E/ F: a, t8 Z, M* T
// called <foo>". You can send a message To a particular
! L- G6 S' M; }* s // object, or ForEach object in a collection.
- |7 b+ d. I/ ^8 P0 w ; K* o0 Q0 C8 i. J$ \
// Note we update the heatspace in two phases: first run
; |9 v1 c8 o2 |" D- l% w // diffusion, then run "updateWorld" to actually enact the1 A' T2 Q) T$ I |. e4 a, o D
// changes the heatbugs have made. The ordering here is
* [6 {3 j% }4 N, t; [; p2 y // significant!/ i+ p% W: y( h, K
1 Q) `% S* @+ T' t: b3 |8 s
// Note also, that with the additional8 w+ _. `) a) U) g/ l3 Q, w
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 F0 V: t3 w% k# Q0 e
// randomize the order in which the bugs actually run5 t; z! w# P& f
// their step rule. This has the effect of removing any% L6 A. N- f/ U, W3 W% J) ?
// systematic bias in the iteration throught the heatbug
& @2 k3 U3 b5 j. U+ Q5 L+ M! U" | // list from timestep to timestep
% E) t7 c4 Z5 @/ ` 3 b, f0 I1 u7 t# N+ D
// By default, all `createActionForEach' modelActions have
) E: ]' a" I% H4 n. L! N: R4 A) p // a default order of `Sequential', which means that the
+ u$ \7 u" U, \+ `& J7 n2 y& y% G // order of iteration through the `heatbugList' will be- g6 l4 _9 m5 ]! @/ @
// identical (assuming the list order is not changed
2 b1 P4 ^8 r( ?7 c* U! X# E // indirectly by some other process).
5 Y' M7 z" e% J, `( R2 V8 @
2 X. A3 ~ k* E+ R* t) L3 u# v modelActions = new ActionGroupImpl (getZone ());
8 [9 Y+ w, F+ `
3 q" E# C& F7 `5 ^6 ] try {
0 \/ f1 S' G. _4 b. g* p* q: @ modelActions.createActionTo$message
# [5 l6 [% _0 Y0 W (heat, new Selector (heat.getClass (), "stepRule", false));
' q! V, d% {) X0 g } catch (Exception e) {/ l/ @+ N: \- u6 H8 x- K
System.err.println ("Exception stepRule: " + e.getMessage ());
# G) }( h) ?4 |: f% L }9 H$ S( m& T- M1 M$ }/ H7 h. H
# P# ^( R9 t- @/ v0 W5 j9 G* U try {
, y: Y, `( q4 w; K$ E Heatbug proto = (Heatbug) heatbugList.get (0);
( ]' E7 ^) D& d y: s2 E Selector sel =
1 v: }7 u7 F7 { new Selector (proto.getClass (), "heatbugStep", false); G0 l+ ?+ V/ e8 }% |3 i! \% I
actionForEach =
$ G6 F( @# a& Q' |/ p# f. T modelActions.createFActionForEachHomogeneous$call
5 O- \ u1 d: H- q (heatbugList,5 J0 ] \: T* |. |
new FCallImpl (this, proto, sel,
H' ^, B: ?/ |! y! f o/ p: d new FArgumentsImpl (this, sel)));9 q2 d) ]" }* \- a
} catch (Exception e) {
8 r! W+ Y$ d) _( z9 | e.printStackTrace (System.err);
9 X6 [+ P. ~5 V6 R3 g `- ^& X+ n. w }
4 D+ U c- g, o& A$ i: e. V % c7 I) u0 C, M) H H* ^ ]" P
syncUpdateOrder ();
% g( O$ T4 S- M ^% x* `8 Y" i2 Z, R- K6 `
try {) Y7 f* b* ^0 ^- b O. t
modelActions.createActionTo$message
7 D7 N5 W5 }; N. \* Q7 M (heat, new Selector (heat.getClass (), "updateLattice", false));
( _* @/ i) i3 q0 a5 L } catch (Exception e) {
: m J R- l9 E! L2 a; [8 X+ e* d System.err.println("Exception updateLattice: " + e.getMessage ());
1 K2 E) }' n4 i7 a- P+ b) |9 b }% W& \* q: f# s# O
, A8 [! F+ E) b: I5 r' Q, ` // Then we create a schedule that executes the ?- s+ r$ ?2 ?+ b5 E8 c
// modelActions. modelActions is an ActionGroup, by itself it
6 w; t+ c; Q: p( k% c& d7 o // has no notion of time. In order to have it executed in) \' R' g. E) t4 A" w3 d# O( ]* g
// time, we create a Schedule that says to use the# W+ E% [/ ? u4 \
// modelActions ActionGroup at particular times. This
( I! }1 g4 e! I9 o+ c5 K; z // schedule has a repeat interval of 1, it will loop every
5 ~; W' O ~) y/ v // time step. The action is executed at time 0 relative to A7 W4 p* v' |* Z0 P$ i
// the beginning of the loop.
7 e" K& U1 {" i, u! g8 B
& Y* s: p2 X7 @- y" H! @& ~ // This is a simple schedule, with only one action that is
' h6 S7 w; a) `4 d2 ^! T9 F // just repeated every time. See jmousetrap for more: Y% N0 l/ ~; l) }: v6 `+ \
// complicated schedules.
8 ]; e% X C3 X3 U/ K . [) c& p& r; u; X2 ?6 V
modelSchedule = new ScheduleImpl (getZone (), 1);
8 j* F% q( `# R8 s2 s, }- a, H/ _ modelSchedule.at$createAction (0, modelActions); K" _, x7 \7 R- G' U
* e- t- w- Z, H! c
return this;
' }! U/ j, s: Z* t6 T2 V1 A& ?: z5 @; V } |