HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% u/ z% k5 z5 @
6 h1 l. F! {+ p' i) ?" a/ _
public Object buildActions () {
* b$ t! D$ {( h! C* }& E super.buildActions();
; `* j# g$ k1 p0 B. w- B% X . A- u2 e, G0 ?! @ R; n
// Create the list of simulation actions. We put these in
- u) b$ {- J o- V& _! \8 G // an action group, because we want these actions to be
1 e2 V; X5 |; p, b: P& l // executed in a specific order, but these steps should
# f+ `/ _/ Q: k7 m' G( x5 J // take no (simulated) time. The M(foo) means "The message( {7 {3 g" A$ S: |; ?% I \
// called <foo>". You can send a message To a particular
% r2 D4 [! @5 I* n5 _$ }- e" g& X // object, or ForEach object in a collection.* t+ Z4 z9 i) Z6 C
# r4 r- r- P1 T' c4 J" |6 N // Note we update the heatspace in two phases: first run
* a% v' J% {4 k // diffusion, then run "updateWorld" to actually enact the' X% }, H7 x4 h
// changes the heatbugs have made. The ordering here is( ^! f4 V* n6 X' u
// significant!: Y* t5 ?7 g l
/ h$ L! G5 i5 n/ V6 H8 w9 C // Note also, that with the additional ~4 s# a2 ]2 b5 p9 O
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 b- @8 L; y; M4 B3 v) X, {
// randomize the order in which the bugs actually run
$ c+ `2 z$ @+ A/ M* g$ ~ // their step rule. This has the effect of removing any5 ^4 D0 I" ?2 O( o: u ?$ X6 z
// systematic bias in the iteration throught the heatbug
, F6 E }3 J3 S2 g7 M // list from timestep to timestep/ `* R m, [$ o3 V6 N
" ~1 w+ r5 |9 w: r4 O( c' T
// By default, all `createActionForEach' modelActions have$ k. b' h* v3 B% j
// a default order of `Sequential', which means that the/ ^- m5 L1 L! X8 d/ ~) q2 E2 ]% ]
// order of iteration through the `heatbugList' will be) P5 V( a, y( B# J$ I6 q
// identical (assuming the list order is not changed; R7 {9 u' g2 F2 Z# A n' }
// indirectly by some other process).
( C: ]( w$ ^9 S2 u8 E6 r, m * \. z4 u. M8 k$ F1 r" X* b5 I' i
modelActions = new ActionGroupImpl (getZone ());8 S% b J) d- H) `
: ^) a4 h. N. x; V6 k7 p
try {
* f7 t4 N# q0 ^ modelActions.createActionTo$message
. [: m, E+ k8 d8 `8 `9 q' ~& {2 M (heat, new Selector (heat.getClass (), "stepRule", false));
! v5 U5 G8 m2 P7 r3 o: {( D4 A; d } catch (Exception e) {
( \* o2 J* [2 ^( u$ k1 J* U System.err.println ("Exception stepRule: " + e.getMessage ());5 u: r' W' _( e0 m: R. r
}. v" `1 X3 Y6 R! |
3 U, C& V7 Q& F/ B( g: [( _ try {$ n/ A, R, a& ]4 ~$ w' u
Heatbug proto = (Heatbug) heatbugList.get (0);
2 V! [2 t1 G/ a- ] Selector sel = . D- t4 M2 A" y7 F' r t x4 K; u
new Selector (proto.getClass (), "heatbugStep", false);
$ c; \4 m. R; r. ^ N1 S& e i4 x: s7 d actionForEach =
3 X3 a( n3 c5 x) _ modelActions.createFActionForEachHomogeneous$call5 m- P) \6 f$ }: |2 ~
(heatbugList,
P% [/ c1 x/ G) Z4 o new FCallImpl (this, proto, sel,
& |8 p0 B. J; }0 Z1 A4 S6 G- W+ m new FArgumentsImpl (this, sel)));
) A" Q. n3 z0 Y; O( j } catch (Exception e) {
* ]4 V) N/ k$ B1 R" }( a e.printStackTrace (System.err);) n a4 G" }& g. F
}
/ d3 s# K* N' B& \0 d 1 I! c# E S, {
syncUpdateOrder ();
0 g: K- W! T# R/ h1 Y# J3 }8 p: J$ A0 a7 m2 X& Y% Q: a
try {
& S1 o& D$ \/ V modelActions.createActionTo$message
; H9 ~# h: M( N- n$ ?. ]/ M (heat, new Selector (heat.getClass (), "updateLattice", false));
/ o$ L- L! t$ B/ R: e1 ] } catch (Exception e) {
; r3 ^3 `" C5 z# z* R System.err.println("Exception updateLattice: " + e.getMessage ());
; b" {6 z; s) t4 e' [/ [- z" s0 ` }5 d- a, C2 ?* |9 `* P: s9 C
1 n5 g+ ~' J/ \/ q5 y$ r/ ?+ D$ g$ j
// Then we create a schedule that executes the# R- P2 u4 x \3 W& Z1 c. t7 y
// modelActions. modelActions is an ActionGroup, by itself it
5 V4 Q2 h% m$ F8 M // has no notion of time. In order to have it executed in) p" Q9 k' i; T
// time, we create a Schedule that says to use the
- k9 N! V- Q* S6 A# q // modelActions ActionGroup at particular times. This
. J; i4 V, Y+ W // schedule has a repeat interval of 1, it will loop every
2 h, l4 _+ s" G2 n4 b8 ~ // time step. The action is executed at time 0 relative to9 i1 f$ o. H7 {$ m1 }8 b( v/ N; }( W) ?
// the beginning of the loop.
* m* _ x4 x9 I% T
; N' I* ^5 Q- m @7 N4 w- Y* ^ // This is a simple schedule, with only one action that is
3 W% W; |9 R' e d // just repeated every time. See jmousetrap for more' {8 q& z/ L0 m& w. ?
// complicated schedules.6 c5 G X+ a; W. }0 \1 n
\2 L/ }" f2 G3 q) y, Y' v& A% f
modelSchedule = new ScheduleImpl (getZone (), 1);% T% u# {: v8 z5 X" ~1 e! Q
modelSchedule.at$createAction (0, modelActions);
( K1 X5 s$ U' ? % M7 w5 P1 s6 N; p' `& p. c; }) v
return this;
1 s* s( c0 M, ? a: s! W } |