HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 b) v) i( M# A' ?$ T% l9 T3 x4 O0 C) x. N
public Object buildActions () {
7 S- y' y H4 `9 k; R" s0 U! I super.buildActions();
- P; k% z5 W5 n; P
/ I$ j- w% Q- P, \2 X" i0 g" { // Create the list of simulation actions. We put these in. S9 E' D T( h5 N) ~0 w
// an action group, because we want these actions to be
( @1 l& x" j3 W, r5 p6 k T, h // executed in a specific order, but these steps should3 J. ~/ Y/ J$ l! i: B8 }
// take no (simulated) time. The M(foo) means "The message
f' s& U. g }! x% o# x2 E# Y; z9 j // called <foo>". You can send a message To a particular
3 q0 Q0 |6 N; @ // object, or ForEach object in a collection.5 G, q! r# v. m% @$ f2 w
1 [7 o: D, d7 w& H
// Note we update the heatspace in two phases: first run) h" V7 ^8 C7 ^( c4 \6 h
// diffusion, then run "updateWorld" to actually enact the" {) L* {' f* j4 ~
// changes the heatbugs have made. The ordering here is
- @0 ~; h' Z8 r3 u+ S1 ~2 @ // significant!
- ?/ d& E# M- f + p- z/ p' P4 p- @# t( r
// Note also, that with the additional
. T" T y. J) ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can( h" D! `) Y# c8 c0 m4 \3 Q7 W D
// randomize the order in which the bugs actually run
4 h. V7 r! l, `$ Y+ ^ // their step rule. This has the effect of removing any
* F% t2 `' x9 [# h% i$ B, P // systematic bias in the iteration throught the heatbug
) n2 H1 I* S2 t Y @: V // list from timestep to timestep
/ k* W$ {& w& B; P& m9 y
6 i* C) X+ t+ f# ^ // By default, all `createActionForEach' modelActions have: b7 B, u% P* ]( q! L* Z7 v; G* n
// a default order of `Sequential', which means that the
7 i1 ^; s& @# b; Y; {- x Y // order of iteration through the `heatbugList' will be4 F/ P: x/ D# \. b* v
// identical (assuming the list order is not changed R2 B. t4 S6 ^* H; ^
// indirectly by some other process).% q3 ~- T! _" |+ B4 i5 s! a. h5 x
, N% O5 o+ v3 h- p! m- c1 G
modelActions = new ActionGroupImpl (getZone ());' y* H% b8 Y9 _4 |5 l1 s9 c
0 Q: I4 E# v, U4 U) X
try {
! `# M) V- S" R, R9 Q1 m) g modelActions.createActionTo$message
+ r* o4 n7 w5 @( i+ A! n, h (heat, new Selector (heat.getClass (), "stepRule", false));
C4 S% M, M, g$ p2 z } catch (Exception e) {, b6 w" V- T; K6 w% }
System.err.println ("Exception stepRule: " + e.getMessage ());, l6 D$ D, E4 d- V. x9 q% z
}
' `9 |. v- m7 z9 x) X
) ^! h; e, _5 P% Y& c5 X try {
A, {$ [+ q3 D( N* u( ~ Heatbug proto = (Heatbug) heatbugList.get (0);
7 [7 j. x0 X9 L6 V9 V Selector sel =
* H& s/ x3 D1 e% @# d9 h2 M; i new Selector (proto.getClass (), "heatbugStep", false);
; J2 r% {+ P3 X: ^ actionForEach =+ C1 ?- O" c- z$ e9 a
modelActions.createFActionForEachHomogeneous$call7 P( k1 B0 d5 C
(heatbugList,1 W8 {( O6 V$ e) t& U; n @
new FCallImpl (this, proto, sel,+ Z- F0 q" }. \$ _+ w
new FArgumentsImpl (this, sel)));
9 A. a5 C1 |, @$ d } catch (Exception e) {
2 S; O: A! p6 d& L O5 s- ] e.printStackTrace (System.err);. R' x' f' s( c$ U3 d# z
}+ ?( ^9 h. K$ M& L# C9 e
' p2 G& ?9 L& Y8 k, E0 Q0 m syncUpdateOrder ();/ N7 b4 z, M, N/ I; o6 j) i/ h
' S% Z4 E$ l* @+ E. N4 v) b9 Y
try {2 o7 B- `9 b1 i# l
modelActions.createActionTo$message , {6 N1 Z7 y/ o& f- x: O
(heat, new Selector (heat.getClass (), "updateLattice", false));8 O7 ^3 M7 g6 Q* G) ^* z
} catch (Exception e) {! j/ o3 f( I( R6 Q
System.err.println("Exception updateLattice: " + e.getMessage ());
" ^3 x8 x. t0 c) |1 c; t" R }
" S7 p v5 V- ?2 f8 K * `! f/ R, L- C. |& @
// Then we create a schedule that executes the
4 c4 y3 T# b7 o8 P! J) B // modelActions. modelActions is an ActionGroup, by itself it
3 _, j" f0 B7 H, B$ ` // has no notion of time. In order to have it executed in8 y l8 y- H; |
// time, we create a Schedule that says to use the8 K# E* b7 ?/ u! q+ @7 m
// modelActions ActionGroup at particular times. This
, W# z0 `2 _- D S a1 p // schedule has a repeat interval of 1, it will loop every
$ M9 g r! |& j7 D // time step. The action is executed at time 0 relative to0 a7 q% c% p+ \9 e% m6 U
// the beginning of the loop.
' A3 ]$ h& C4 U8 _2 q( e) d& k3 E4 {3 l+ X
// This is a simple schedule, with only one action that is$ g" J7 Q; |- k/ K) C
// just repeated every time. See jmousetrap for more: I$ w7 `+ G0 r' W9 k% U; _
// complicated schedules./ O5 ]& F8 y- E H: p. ~
: O0 E( o( b5 A* j
modelSchedule = new ScheduleImpl (getZone (), 1);) g K+ A. }3 N+ O
modelSchedule.at$createAction (0, modelActions);
1 G3 H" y/ [, P3 l' W& l! k4 g6 U ' L1 @! L6 k/ @8 I* m3 q' g
return this;0 w+ v C$ ?7 c5 p4 G
} |