HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 ]8 l* u3 F. u
^, K+ S2 G7 H9 `
public Object buildActions () {
) {9 _- y9 @1 e5 _# A4 J* f7 r super.buildActions();8 m5 c$ b5 X. v; i, L/ r) C" m _
' [( O$ k9 Z3 S // Create the list of simulation actions. We put these in$ D* F" F- [! F% Y6 \$ C5 Z9 G9 e
// an action group, because we want these actions to be
) B0 L+ R2 x# ]3 a // executed in a specific order, but these steps should* P- c4 T9 o6 {! f0 H; L) ~1 s- A
// take no (simulated) time. The M(foo) means "The message
: c4 Q7 y$ V+ d, l7 T // called <foo>". You can send a message To a particular
, V g0 W4 |* h! U // object, or ForEach object in a collection.
5 x- ]6 ^% v% [' ?: z, e& x 8 O- ]# x: k; f9 M( n8 R
// Note we update the heatspace in two phases: first run
6 g- [) J) {, F h( q // diffusion, then run "updateWorld" to actually enact the
3 m* f: }) o- U) W( _ // changes the heatbugs have made. The ordering here is
. W1 \; a, i* C, S7 X // significant!
3 V% |7 T; H' a& H% @! Z
0 E$ R, N/ X- U! v0 B // Note also, that with the additional
" M. V+ F% ^' R // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 [0 ^% {2 D$ [ // randomize the order in which the bugs actually run
v4 @' F% f# g9 l // their step rule. This has the effect of removing any9 H$ n9 A: y f( @+ _
// systematic bias in the iteration throught the heatbug5 z2 ^. U' E( k5 R8 ]
// list from timestep to timestep
" C/ u" n6 I+ f* l
- ~+ _: h# P3 c4 B // By default, all `createActionForEach' modelActions have, R0 {0 n6 f5 g$ m
// a default order of `Sequential', which means that the) n0 v$ |: ?9 z/ c, F6 u1 R2 C- j
// order of iteration through the `heatbugList' will be6 c. F" o8 c9 R& Z" f* @$ ~
// identical (assuming the list order is not changed: [0 }! E) i. r$ J# U
// indirectly by some other process).
- g# h- C" D+ p% `8 D2 ^& a
/ J2 |" \$ a, G$ s( J modelActions = new ActionGroupImpl (getZone ());
; q9 K& w+ A5 l8 t
( o0 x( n/ h% D; V/ n& ] try {3 V. Y# }% ?' E0 q6 e \3 E
modelActions.createActionTo$message
5 z- ]' b) x5 ]+ O1 ^5 {4 r (heat, new Selector (heat.getClass (), "stepRule", false));! J; X: l- \$ Z/ W4 s
} catch (Exception e) {" x5 m% W8 |- V d) X
System.err.println ("Exception stepRule: " + e.getMessage ());; R# Q& U* K" K! t
}4 _7 ^* |, v" v; I( S" o1 [
* h. a6 D3 [% L4 }# g& i: Y8 c try {
/ S1 S/ ?8 z/ U& J$ U Heatbug proto = (Heatbug) heatbugList.get (0);6 l* _ { U1 a5 E
Selector sel =
$ a. E1 u) i7 p, x+ `5 J new Selector (proto.getClass (), "heatbugStep", false);$ f! ?; ~2 M! m
actionForEach =
" Y1 s1 X, i3 f) D7 s' {9 S modelActions.createFActionForEachHomogeneous$call
1 B2 B1 T: h9 N# J8 S8 a (heatbugList,
, o' m7 }! X/ J: f) l$ D# l) H4 K new FCallImpl (this, proto, sel,2 o! O$ Z; ~$ l7 E" Q; q% U
new FArgumentsImpl (this, sel)));
: r2 m2 m7 R7 h% g, Z; f* v } catch (Exception e) {# ^) s" |% y/ h, I4 C% L
e.printStackTrace (System.err);
& D4 u! g# q3 x% s }' I! |. D/ P* ~- K" c; S; P- ^2 P
7 t4 f! c; y- n- N+ ^
syncUpdateOrder ();
/ ~9 j5 l; _4 E; I( \7 E
! ]* r1 j% b$ Y- L, e$ B try {
. _0 F, v7 J, O5 h0 e4 ~ modelActions.createActionTo$message
* D# {! k W' }+ {/ ~ f9 w (heat, new Selector (heat.getClass (), "updateLattice", false));
7 m: T* a* d( e9 B } catch (Exception e) {
- b4 ?+ f" U1 c( N2 c: B H System.err.println("Exception updateLattice: " + e.getMessage ());
" a. g8 d0 X! _ }, z) ?9 L* b5 \
* B% | `4 G) F2 v5 S9 m1 m
// Then we create a schedule that executes the( |& h8 R4 u- i- a# Z0 ^
// modelActions. modelActions is an ActionGroup, by itself it
* Q+ g! ^( D! V" G9 t // has no notion of time. In order to have it executed in
- }+ D3 T) ]" S" T9 @# G // time, we create a Schedule that says to use the M& Z0 S5 p3 \
// modelActions ActionGroup at particular times. This8 k& s3 g4 `2 i, k: `8 z6 V! j
// schedule has a repeat interval of 1, it will loop every
* k r$ @+ A8 A* l |; A // time step. The action is executed at time 0 relative to" U# y/ R* b/ J2 j+ F
// the beginning of the loop.& s# Y p: Q$ F$ F1 n
* o6 S/ b' k" n5 @0 d // This is a simple schedule, with only one action that is
- d* L6 k" ^! F7 q; z& r // just repeated every time. See jmousetrap for more
0 M- [, w e5 t9 j // complicated schedules.
3 x8 ]/ b# ~5 X - ?9 p" ~2 y- ?6 K9 N
modelSchedule = new ScheduleImpl (getZone (), 1);
: y: y3 q- C+ t) g% y; g modelSchedule.at$createAction (0, modelActions);
( [. ]! x; X# K; C
# G$ m: B9 T* O) h2 K5 x/ o return this;
0 r; k3 x7 w/ A) q# D s) K4 t } |