HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; Z' _" l) @& b! w9 s/ s' l) B1 B8 U5 Q# ~
public Object buildActions () {
8 B) q- X9 T9 H$ e super.buildActions();; l& B H: ^9 g* V [2 |7 s. D5 N
5 D: z9 Y" o& v! Y6 A // Create the list of simulation actions. We put these in
' v5 {* Y9 R0 C0 g# E/ L+ r! o // an action group, because we want these actions to be* P. d/ ?1 G' L
// executed in a specific order, but these steps should5 B% O' I/ L$ H
// take no (simulated) time. The M(foo) means "The message" b1 N+ O1 f2 I
// called <foo>". You can send a message To a particular9 t# m4 b: @% M, E' N+ a
// object, or ForEach object in a collection.
' L* m6 V5 G! C8 f ( g, T# |; n+ d4 l' E' F
// Note we update the heatspace in two phases: first run; `: w( j- [5 d+ L) N
// diffusion, then run "updateWorld" to actually enact the
. K! X; C) k8 }( K; P8 M // changes the heatbugs have made. The ordering here is
: C3 C) M2 v; ] // significant!
# J& {) i! G7 Q: i / f' U% `7 {( e4 {
// Note also, that with the additional
# x5 o" ]$ T$ Z3 H: P // `randomizeHeatbugUpdateOrder' Boolean flag we can' p; _7 L( [, _7 l9 C- Y) n
// randomize the order in which the bugs actually run+ M0 D5 |! s" Y$ j: c6 z' x# h
// their step rule. This has the effect of removing any; M) q3 k% b1 L" A3 i
// systematic bias in the iteration throught the heatbug
* D5 U9 K5 L* H' L! k$ t t y. Z" i // list from timestep to timestep" t! {; w/ o/ F& G
; r5 ~$ y: ^, m5 @( }2 r" y // By default, all `createActionForEach' modelActions have
& r- g3 C; y8 S1 `% V2 I/ L // a default order of `Sequential', which means that the: U2 l+ S1 }7 `) g- q2 j; D
// order of iteration through the `heatbugList' will be8 |% Z6 I$ R' H' `$ R/ T- |+ M
// identical (assuming the list order is not changed
* I4 e9 p( A9 K1 V+ S // indirectly by some other process).
F4 x7 d0 K' ` ; N+ @8 o, R$ U7 ~2 L
modelActions = new ActionGroupImpl (getZone ());& v! E& y" \+ G2 o( x
2 X$ C9 O& R. f: t1 O( C
try {4 L4 F' z9 _; a9 ^% V: H
modelActions.createActionTo$message
; K% {2 I& k* h, T7 @ (heat, new Selector (heat.getClass (), "stepRule", false));
5 ^" M% @& P0 |$ a } catch (Exception e) {# z/ r r" _/ X9 ]6 u
System.err.println ("Exception stepRule: " + e.getMessage ());, s. B4 I$ y/ y* m
}/ c4 Y3 y) j/ Q6 Z3 Q% d! d a' P
p9 B7 ?8 g+ U5 |$ Z; l/ b) K try {; ? e/ E" J1 O6 g, V
Heatbug proto = (Heatbug) heatbugList.get (0);2 v( o0 z( d0 n1 h6 N
Selector sel = " J% p; A/ e9 P1 O$ k
new Selector (proto.getClass (), "heatbugStep", false);. B, j) c" V7 q7 J7 G7 q
actionForEach =
6 z2 j0 b( S$ k3 ~) \7 u% d modelActions.createFActionForEachHomogeneous$call
( W4 R9 V. v, O% z( z (heatbugList,/ l/ Q4 |$ Z" b/ s; J; w) B
new FCallImpl (this, proto, sel,0 p- v: H, t3 Q) u; h
new FArgumentsImpl (this, sel)));
L* C1 x# v3 o } catch (Exception e) {
4 `/ v9 O6 i' [* b: d8 | e.printStackTrace (System.err);& L& v& R! ~# |7 W# |
}3 R$ v* y2 P. a# W {
! G* }# ]5 y3 \- d syncUpdateOrder ();
4 }0 A2 P5 v. a- v1 A- c' }& N1 L& }) }- D. t0 w( @- u) Y
try {, N! Y; W5 H+ m
modelActions.createActionTo$message - ~3 S' K* A" E' U) E
(heat, new Selector (heat.getClass (), "updateLattice", false));
! d: q: S+ L/ r/ |! T) e6 o } catch (Exception e) {
4 }0 D8 l: q k) S) n System.err.println("Exception updateLattice: " + e.getMessage ());& i9 K9 y' {# c- S4 ]
}
+ E& I# p2 f' t+ J# N, k% [! [
1 q+ s u+ X. T+ E! H // Then we create a schedule that executes the
- J4 z3 W3 e" M. F( q // modelActions. modelActions is an ActionGroup, by itself it
* k- ~) c, `3 J; f // has no notion of time. In order to have it executed in
2 q, z0 ^" R6 k! }8 b // time, we create a Schedule that says to use the7 A7 k& O. @; u
// modelActions ActionGroup at particular times. This( W! q) j/ R4 ?) ?" @- o7 c2 u
// schedule has a repeat interval of 1, it will loop every/ h! `( \; e- r6 } v0 d
// time step. The action is executed at time 0 relative to- p+ [( ^7 Q& l# v0 S' ?9 v: F5 {
// the beginning of the loop.
! e" l4 }: I6 c" P2 q1 w/ {3 m1 `) v
// This is a simple schedule, with only one action that is
' @/ Q1 |; M$ o7 E0 z- _$ V // just repeated every time. See jmousetrap for more, \1 t9 \( f0 M
// complicated schedules.) L: c2 d% C2 G1 {" P
9 D( X1 i1 s# U% S modelSchedule = new ScheduleImpl (getZone (), 1);
& x! z; v! u8 K7 R( @- \( Y modelSchedule.at$createAction (0, modelActions);
# Z4 i, ]- B7 v/ b . ~# l, ?2 \9 ~1 L8 {8 C- v+ B
return this;3 e( ]) y4 y! u+ V
} |