HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( R5 k& w6 l) g
3 i: U9 x+ u5 q" F
public Object buildActions () {# s4 G7 j0 `9 m- W$ |# t8 S
super.buildActions();
; \ P! o- i2 L8 X' y$ ^( m 4 M9 o. P) l1 t: d
// Create the list of simulation actions. We put these in
$ D. c0 \, ?% k; m2 F# F+ [ // an action group, because we want these actions to be
0 X) H5 l/ H- m3 D; a // executed in a specific order, but these steps should' i$ E1 q1 p% {3 N. p @0 w9 J; U
// take no (simulated) time. The M(foo) means "The message
2 w- Y* [: I5 e. X% j: @2 z // called <foo>". You can send a message To a particular
3 p9 z- d1 I/ C7 N# g$ j O- k, _- u // object, or ForEach object in a collection.# z. k6 C( h2 i
6 y5 H4 i# D# K2 H8 [( w
// Note we update the heatspace in two phases: first run8 E: W( }, K' b, A9 F" O
// diffusion, then run "updateWorld" to actually enact the$ C+ ^1 p3 \/ Y9 T9 n
// changes the heatbugs have made. The ordering here is
- n8 ?! G5 k5 ]$ l$ B& s( A% G& s // significant!( i" |- K0 C# |4 d. i/ z
- Q. f* {6 @/ Q+ C- j/ I6 l- S R // Note also, that with the additional
4 g! C9 L) i9 N! ]. F // `randomizeHeatbugUpdateOrder' Boolean flag we can- Q% s% c* r* p
// randomize the order in which the bugs actually run$ t+ u/ X8 c f% ?: f, J0 F
// their step rule. This has the effect of removing any
# J- r8 x$ i/ C/ M% C // systematic bias in the iteration throught the heatbug
; e% ~( Y7 s. u2 a' I$ A // list from timestep to timestep
8 P, W( ~9 Q* y* [
u/ R8 I u% _ // By default, all `createActionForEach' modelActions have
* P# _% [* @- T" C8 G0 h0 l8 E // a default order of `Sequential', which means that the
3 A% x" Q/ {8 M+ K3 W/ K // order of iteration through the `heatbugList' will be6 k. {3 U- ~& Z
// identical (assuming the list order is not changed
. D" X6 i a0 S+ j0 }7 m // indirectly by some other process).
3 w# E2 C: \6 B5 o* L& [: M
. P% @. M/ U- u5 O, y modelActions = new ActionGroupImpl (getZone ());$ X4 v, A7 g- y; i( z
7 a4 L- ~, v! g1 D0 U) z+ v% Q
try {
& t, M' w# |+ q$ h modelActions.createActionTo$message* m% H# t! ]" A
(heat, new Selector (heat.getClass (), "stepRule", false));) J4 E, E( ~9 S5 S* A0 h
} catch (Exception e) {
7 E* G. X$ e) e( O Y2 ~ System.err.println ("Exception stepRule: " + e.getMessage ());% p/ ^9 G$ |' r; x4 R6 J) u) `
}
- S: \: e" }) g5 N" o3 n1 Q4 H, w) U
$ a$ g) |7 \5 ^& P3 q" ?. w try {8 c& [7 N' r' w& }. ]
Heatbug proto = (Heatbug) heatbugList.get (0);6 T. p2 z& G7 U9 ^& g6 T- p
Selector sel =
$ L# E& l% \, ^, D* p new Selector (proto.getClass (), "heatbugStep", false);
/ s+ }8 m2 O6 k' y2 o% J5 F+ E5 ` actionForEach =
0 r1 C) Y+ c( e0 a modelActions.createFActionForEachHomogeneous$call' h" b a# Y4 I0 M; Y
(heatbugList,
& C& G- q- Z- V) ~$ `6 E! {9 l6 w new FCallImpl (this, proto, sel,
9 L/ N1 t* @ E1 I! `) ` W+ |# U new FArgumentsImpl (this, sel)));
" x4 ?3 [* t$ v$ X! e } catch (Exception e) {
4 K$ H2 c! {: Y e.printStackTrace (System.err);
& s6 W$ K" i; O; ^+ c" p4 d; { }$ \: c3 L5 g" B! u$ ~, e; f
+ ^$ r& f# L% Q2 H/ i
syncUpdateOrder ();
4 }6 M3 Z% Q9 h B3 v( q
1 q2 A8 ?0 y! @9 R* z3 o1 u; \ try {8 @8 L a2 B1 _& B9 B' ~# h
modelActions.createActionTo$message
* q* m5 {+ R+ w% D9 s (heat, new Selector (heat.getClass (), "updateLattice", false));
2 t# K* J+ q% C! V } catch (Exception e) {
; O- I& ?; c& R$ M9 y- _3 }0 R( } System.err.println("Exception updateLattice: " + e.getMessage ());- \( a- ]9 s! ~3 z+ r4 z
}6 X- K/ ?$ w0 g
% @/ T" @! \: G: b* }
// Then we create a schedule that executes the1 k0 s6 y- U! H H% f
// modelActions. modelActions is an ActionGroup, by itself it! J; s3 }" W; o0 _
// has no notion of time. In order to have it executed in! H8 t* p9 ]! U! v
// time, we create a Schedule that says to use the9 Z7 g, C$ c, g5 ^6 a6 j0 n
// modelActions ActionGroup at particular times. This
" j1 v1 P# ?0 g& y, V; s& S o // schedule has a repeat interval of 1, it will loop every5 m/ y3 k; T& X; v, m6 W
// time step. The action is executed at time 0 relative to
! c$ i' ^2 W6 X! i3 D; V // the beginning of the loop.
# ~3 M" @/ m: M8 Z' C0 v7 L* u; ]# @# A9 e" z& O
// This is a simple schedule, with only one action that is
& Z- f8 Y# d z# [- Q // just repeated every time. See jmousetrap for more
. X* R/ _! A/ S // complicated schedules.
: ~* I# R" [8 h E: l- `
& z$ o* t% y& y+ N, j modelSchedule = new ScheduleImpl (getZone (), 1);6 ^) ~4 u+ L+ E' ^' f# v5 V
modelSchedule.at$createAction (0, modelActions);' `& l4 y- o) a2 [6 i4 k
E( [+ K2 W) {+ [ return this;
: ]% P/ W' y6 Y/ o! Y } |