HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, ~& b( z+ v: M
, \. A9 H# _) X0 T$ ^ public Object buildActions () {. i' B9 n7 U; m6 D6 C
super.buildActions();% ~5 ^% v' S4 S7 j
2 K6 j0 S l' z, Q, T! |4 Q6 Q
// Create the list of simulation actions. We put these in
! @- K+ w1 \7 L9 a- `2 [ // an action group, because we want these actions to be
) F* R# F! V$ L6 v/ F // executed in a specific order, but these steps should6 }! F( |+ ?- y ], }
// take no (simulated) time. The M(foo) means "The message' w% ~7 y: o, w2 p- b4 l S% W9 F
// called <foo>". You can send a message To a particular2 U+ P$ I }& f7 T
// object, or ForEach object in a collection.+ b' I M a1 B1 Y, p$ S. v
) S, M1 \' } e, U
// Note we update the heatspace in two phases: first run8 i/ g; E9 H2 _2 [% j; T9 l% {6 v
// diffusion, then run "updateWorld" to actually enact the
+ z% j" N z# ^7 K: d9 f9 f& B // changes the heatbugs have made. The ordering here is2 k0 \, K) a- D3 j" _3 A
// significant!% z3 E; P0 f5 k
5 o- B& d, S( g3 g5 n2 t+ h: J // Note also, that with the additional
3 s$ s) t. Q% | // `randomizeHeatbugUpdateOrder' Boolean flag we can7 |! c+ U* S. e* `
// randomize the order in which the bugs actually run L( ~% K5 y7 ~9 ^6 h
// their step rule. This has the effect of removing any- ]2 f* |2 i( p3 Y7 y- o1 q
// systematic bias in the iteration throught the heatbug
% w6 C1 q b! v, ? // list from timestep to timestep
% {$ n- \) M& p5 V ^5 j! M
+ d* L% m9 Y" f% w // By default, all `createActionForEach' modelActions have
H# o L+ ?/ }; g6 J3 \3 H9 Z // a default order of `Sequential', which means that the* |7 {8 }! X) f& W. M [; R& p
// order of iteration through the `heatbugList' will be8 e9 s9 f$ @% s5 f' y: {. ~
// identical (assuming the list order is not changed) C n4 x' \4 }" q0 x5 C% i. W
// indirectly by some other process).1 Z+ {6 `# k! A% {# j7 U
) D! P2 S- M$ N. a4 ^! J { modelActions = new ActionGroupImpl (getZone ());3 `0 `% g0 P( h4 t4 i, d
% z) P& x! C+ y9 q1 A
try {% ]& V2 p E* Z# @$ \
modelActions.createActionTo$message
9 T; A( J% `, ~* A; H (heat, new Selector (heat.getClass (), "stepRule", false));; a1 a# F9 I# O/ S* r
} catch (Exception e) {
. G1 n6 s6 ^0 T! q0 g System.err.println ("Exception stepRule: " + e.getMessage ());
5 A0 l% K( \" z3 C/ @/ W9 i }
9 V% o* c. c8 _$ K: J
# s/ _* s4 |# D0 x8 C& D try {; ]( _2 P9 V6 x6 f# |
Heatbug proto = (Heatbug) heatbugList.get (0);$ r( T+ f/ B3 i% f+ Q6 z8 Q3 i
Selector sel =
6 G- J6 M- |+ e; e new Selector (proto.getClass (), "heatbugStep", false);+ Z* @, C% S g. C/ F" x9 ^
actionForEach =
* M: s! p2 Z* J% A modelActions.createFActionForEachHomogeneous$call
$ p- i2 D; A$ r k& L! E% H* n1 b (heatbugList,8 U* K/ H: ~% e* d
new FCallImpl (this, proto, sel, o$ b2 ?$ {$ F _9 T: H/ c. H
new FArgumentsImpl (this, sel))); |( j" `, r' W3 V
} catch (Exception e) {7 H1 A( I; g4 `' ^$ t
e.printStackTrace (System.err);0 {" W( R# s/ M+ d1 N
}
5 f- ]* t: X3 I
% j4 i7 @) q9 \4 x syncUpdateOrder ();
) `) H8 W4 c! T, U8 w Z7 T, o5 t
try {
1 l3 X# y3 A0 c/ n modelActions.createActionTo$message
' a0 E2 |8 y- l4 W; I7 W$ {* F (heat, new Selector (heat.getClass (), "updateLattice", false));% t+ l0 f5 }( g
} catch (Exception e) {0 O( |: A) M0 G$ {. @1 P
System.err.println("Exception updateLattice: " + e.getMessage ());+ l* X. F& {; X3 O$ C! y
}
+ x$ V Y4 Y5 t t* h, g }0 A& _ ; Q9 k# O$ K. W2 g. q
// Then we create a schedule that executes the) [ f( ^: h$ t8 ?$ h
// modelActions. modelActions is an ActionGroup, by itself it7 R" J: U2 t) k9 |" G( a0 S
// has no notion of time. In order to have it executed in
4 ~$ q* P9 O8 T- j, \; j // time, we create a Schedule that says to use the ^' U% L2 e4 ?0 v) ~; Y1 l- F/ z
// modelActions ActionGroup at particular times. This
% _* j7 D: j/ s- W" v // schedule has a repeat interval of 1, it will loop every
" G0 R" S% }1 s/ p // time step. The action is executed at time 0 relative to
2 b# m$ _8 S/ B! v# a // the beginning of the loop.
: V$ i% V/ g% U# G
$ W* l% \9 G) I- e$ a // This is a simple schedule, with only one action that is
. A9 n3 G- t$ l: r // just repeated every time. See jmousetrap for more/ t3 l0 _/ F/ I+ b, S4 X4 a
// complicated schedules.
( s- u8 S4 v6 y+ g9 B & ^" s# d6 K, E
modelSchedule = new ScheduleImpl (getZone (), 1);2 {% N5 N6 t. ^+ P. [1 f
modelSchedule.at$createAction (0, modelActions);3 d7 {6 f+ `/ J3 ?) [ d8 k
. w+ z' m( e+ G" Z! i! \' ^ return this;! e! F8 p1 n5 J. x; z& H
} |