HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* r! I V5 u$ f1 G8 ~$ G7 X
0 p$ Z# O4 s3 i; X. L public Object buildActions () {
4 S" n n; f0 N; h super.buildActions();
# o3 l7 _" e: k) w/ S g* O 9 Y* E( V. e2 A% d& h
// Create the list of simulation actions. We put these in
. _- ^* _/ Y- r/ W9 a8 M // an action group, because we want these actions to be/ Q( p# V7 j8 D) C% ~2 C
// executed in a specific order, but these steps should" a, U6 U7 l. H( g) Y' `
// take no (simulated) time. The M(foo) means "The message" }7 j# X0 B6 ?% g3 |$ ^3 S8 g
// called <foo>". You can send a message To a particular7 D' g. u6 a, V/ ^# x9 R8 q
// object, or ForEach object in a collection.2 m1 a, }! `. W
. y9 q9 u8 r% d* r& K7 s; h- \ // Note we update the heatspace in two phases: first run
0 h/ Z5 `/ s I/ A( G* f7 Y // diffusion, then run "updateWorld" to actually enact the3 V% v' c: k }% T5 q; V' S& J
// changes the heatbugs have made. The ordering here is* {2 B% A/ {4 N! e: }7 ~* v6 O
// significant!( W% m5 k, F$ f( B
+ p" Z, X( ^/ ?; r9 j // Note also, that with the additional- X3 v# u, ^8 P3 ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ @3 l- U! D8 k- c5 g* s
// randomize the order in which the bugs actually run8 B- C' W4 S0 E' V* U
// their step rule. This has the effect of removing any
" g/ l3 t, P) [% [( R& S+ | // systematic bias in the iteration throught the heatbug% m* j$ B9 `! t' ~$ m# |
// list from timestep to timestep( ?! E7 s8 y( T* D( d
5 {- V$ s/ w4 ?4 W( `" o // By default, all `createActionForEach' modelActions have+ y6 J: k) `8 m$ |$ G
// a default order of `Sequential', which means that the6 H6 x' N+ k' ~8 w% i' B
// order of iteration through the `heatbugList' will be
0 c% a8 h" U' |( l# Q0 ]: L // identical (assuming the list order is not changed
2 h& b2 ?" F6 `) j' u // indirectly by some other process).0 z& J/ ^' u5 }
" i! P! K/ P, C/ U) z modelActions = new ActionGroupImpl (getZone ());8 P, |, n! _) v; w* P. T
: f Y+ n: I' l7 A try {
+ X, R$ f$ z. @5 M4 Z modelActions.createActionTo$message
$ J* W. U1 w! E5 b O, J$ `" D (heat, new Selector (heat.getClass (), "stepRule", false));+ s& H% w/ a& x& U& H/ t
} catch (Exception e) {
! T' I1 k! b% ~. M! w System.err.println ("Exception stepRule: " + e.getMessage ());
2 D. e$ j C- C }# L% p' {9 W$ N/ ]# k' k
' F! W3 h- j6 B$ Z. v# r% Y& w
try {) N# e9 Z2 |: h) Y6 a
Heatbug proto = (Heatbug) heatbugList.get (0);
" c2 C* H# _+ M6 Y/ k$ ^ Selector sel = * X, W( R, d9 d7 R5 b, x
new Selector (proto.getClass (), "heatbugStep", false);! M9 j8 n1 a. S! ]
actionForEach =
; O" p9 K# F$ @( u: S modelActions.createFActionForEachHomogeneous$call
6 M; Z+ j; h7 |; e# J% y; r (heatbugList,
$ Q8 T/ Z% P8 m4 d new FCallImpl (this, proto, sel,
8 S8 U& g7 ^# u, n new FArgumentsImpl (this, sel)));
5 D1 ?. |; ^! m1 A* ?: I } catch (Exception e) {
& W B' y: ]) |- T e.printStackTrace (System.err);
. O# C4 R( b- i* Z4 O4 K }+ z5 {" J: ^( J- ^3 \% E: }
4 W' Z h+ |5 X$ o ]3 W5 n, r
syncUpdateOrder ();
3 y) B* g+ j1 X! d2 I1 B1 K! U# z7 O( f! u1 @) ^
try {1 Y9 ~. u w2 {* b( h
modelActions.createActionTo$message + L# }( K K( }; L/ W' q- S
(heat, new Selector (heat.getClass (), "updateLattice", false));
& _/ L* d O: o: R. i } catch (Exception e) {
; k( }6 U6 D9 ?. ~" r& d" e6 | System.err.println("Exception updateLattice: " + e.getMessage ());7 `# Q; l0 e* N
}/ y5 K* O9 e5 O
& K8 f; z$ V9 ^0 c3 r
// Then we create a schedule that executes the
$ w( N8 i+ X& L6 s // modelActions. modelActions is an ActionGroup, by itself it7 I8 s' T' ~$ u9 t
// has no notion of time. In order to have it executed in) }7 p" G* I( S2 i$ g" F7 x1 m, l
// time, we create a Schedule that says to use the4 M2 J1 d+ _. u( k0 j
// modelActions ActionGroup at particular times. This7 a, C5 k" f7 y( r
// schedule has a repeat interval of 1, it will loop every
7 O9 g# O4 S9 t3 a2 w; `: c // time step. The action is executed at time 0 relative to
0 Q/ r$ M4 Z- g: [1 [) B5 U // the beginning of the loop.1 v: m( _0 b* y, t
4 R# k4 F9 K* v; i
// This is a simple schedule, with only one action that is: X$ l5 P( d1 F- }
// just repeated every time. See jmousetrap for more0 a" H9 S& W" ?# I) H
// complicated schedules.) H2 [6 J' u9 `. P/ P* c1 D
% p- _) G8 T F7 ?. B modelSchedule = new ScheduleImpl (getZone (), 1);
) ?$ i, D9 I# R% w modelSchedule.at$createAction (0, modelActions);
9 F4 h& [5 ^0 \. Z) g. p+ } ; J f) B& E! I$ b8 p8 M9 g; S
return this;* x! y6 ]$ P m( N5 E+ |
} |