HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 h( v( Z5 L3 n! Q- d. u
/ M' P5 Q+ q6 g f9 R1 F6 Q* T public Object buildActions () {
8 R+ x$ d" d |2 W+ j super.buildActions();
) d9 x6 r; h) I# x . r" S8 M& L1 R ^2 Y. i
// Create the list of simulation actions. We put these in
( u( I9 S/ @# |% z; n( ]% }) B // an action group, because we want these actions to be5 Y7 U, [" E% ?* z9 T: }- p4 s
// executed in a specific order, but these steps should
8 ]% @! r/ L9 m // take no (simulated) time. The M(foo) means "The message
& {( z1 f4 r9 y1 M // called <foo>". You can send a message To a particular
& U I7 a* u" L' n: m: e/ K // object, or ForEach object in a collection.
* x6 p, T. w6 U1 Z- T# T0 V
# |: r; V, [/ u, [- ^ // Note we update the heatspace in two phases: first run
' I$ A% W# R6 D$ U# \ // diffusion, then run "updateWorld" to actually enact the
0 `4 ~3 r! h0 a7 }+ [ // changes the heatbugs have made. The ordering here is
+ [ |6 r5 t5 V/ }' l* n9 z. P // significant!% H4 w9 ?/ a( b* {
/ l7 c; ]6 `0 N) {, I // Note also, that with the additional: D6 |" L( ?+ \7 h5 I9 S/ u( c0 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' O' _- [) u3 T5 D9 a: l5 c; ? // randomize the order in which the bugs actually run, r% F; i7 Z4 P# ~5 W% q
// their step rule. This has the effect of removing any
" f6 w8 i9 d4 _ // systematic bias in the iteration throught the heatbug
' n+ a7 h4 R* _$ w3 X1 h% P // list from timestep to timestep, @+ ^* V6 R5 M, o* S8 |; A
, o$ _1 D( `8 j! Y
// By default, all `createActionForEach' modelActions have3 M! f. [; q: I: w9 S ^
// a default order of `Sequential', which means that the0 `# ?3 Q3 j( h4 p
// order of iteration through the `heatbugList' will be9 c7 w) M$ B) ?/ i- K8 e; f9 p
// identical (assuming the list order is not changed$ L4 Z$ q% ^1 A7 G
// indirectly by some other process).
2 t5 O3 u$ p2 N8 D' f
! l# Z2 j- r7 [/ S A) B modelActions = new ActionGroupImpl (getZone ());# P0 ^' @3 I# |
# ~/ J: Y; R9 Y. ^7 O try {* v9 _3 d2 R# B. X* o
modelActions.createActionTo$message
7 a6 i3 \: R1 w( F; Y1 l (heat, new Selector (heat.getClass (), "stepRule", false));
& @5 y5 x* y) J$ [ } catch (Exception e) {% ?$ k! F$ d6 W- X3 Y% T( \
System.err.println ("Exception stepRule: " + e.getMessage ());- I4 W8 s2 G U3 u" q1 m
}
' n' @5 B z; J7 G7 n1 P+ f' p' `" G
2 U) ~4 `& ?! M1 s. R try {# v, w5 D8 c( K5 \' b
Heatbug proto = (Heatbug) heatbugList.get (0);$ O) o7 P# E+ r, a7 W u
Selector sel = ) o9 x) O7 g' v8 n: B" k
new Selector (proto.getClass (), "heatbugStep", false);+ J% e: d- z) [& k
actionForEach =+ J% Y2 H6 i) v% x7 O5 T8 ?! @& b9 S
modelActions.createFActionForEachHomogeneous$call E9 X2 y( ^+ X6 s0 o' Z5 ^
(heatbugList,
: V+ q' `& Z+ _% W+ S new FCallImpl (this, proto, sel,/ `4 A2 k- D3 X$ ?
new FArgumentsImpl (this, sel))); _" ^# X% ]0 O k; H8 l1 v) n
} catch (Exception e) {
& f$ E1 ^5 H. W% G- f0 c$ o( d% w5 b e.printStackTrace (System.err);& E6 q. x0 s6 k- h
}
% D- B: P7 {* o" f 3 ^& c8 f2 h* E# s/ l+ r
syncUpdateOrder ();# o7 B- T# F$ c& U$ N5 N! A
, o6 D$ W( l. N# k G5 k, V; q, K5 v
try {
8 I5 \/ g/ W, R& \. g modelActions.createActionTo$message
0 d: x5 w: N: r (heat, new Selector (heat.getClass (), "updateLattice", false));
# b5 j) K" \! f) S$ \1 }7 Z } catch (Exception e) {
\3 h/ N2 K$ F3 A7 s, g System.err.println("Exception updateLattice: " + e.getMessage ());
9 m4 e5 v2 O! |4 D4 x2 N' E. F }
, L" \% Z* h) v: o$ |0 C) W4 f4 I5 O( W # K Z4 I4 j# a+ \; w
// Then we create a schedule that executes the6 L; F$ |. I" s: R# {. i1 Z
// modelActions. modelActions is an ActionGroup, by itself it8 X; @, {* w) A9 m
// has no notion of time. In order to have it executed in0 u9 A" P3 r! q2 i
// time, we create a Schedule that says to use the) d, u$ O# p, z) _
// modelActions ActionGroup at particular times. This+ n6 F8 o) a! |8 S" }. P8 @
// schedule has a repeat interval of 1, it will loop every4 j3 u1 ]; U( ?4 Q4 g9 C6 Q t
// time step. The action is executed at time 0 relative to
" p% }( T* w% J! j // the beginning of the loop.
+ p+ [; ~2 g% X
. I: k( t$ v7 x& j; l$ S- w // This is a simple schedule, with only one action that is9 D' D( Y# V% d/ S
// just repeated every time. See jmousetrap for more- e+ q4 g# _) M- V. D
// complicated schedules./ G% e6 g) M8 F/ P" G$ h5 p$ h# G- Z6 w
! s5 [' t# ^9 z6 @: L# p
modelSchedule = new ScheduleImpl (getZone (), 1);
$ T' I4 k$ b9 u( u, L% T modelSchedule.at$createAction (0, modelActions);
; U ^2 g+ c, N' j) F+ T6 i2 h$ f 6 O7 r( i; s. N! v* e/ b' g
return this;! U: b0 {' `+ c$ ]# G) V5 V
} |