HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; c( x" d, q3 n3 l3 v# ]" ?+ }% f
% ]9 T5 e3 }" ?
public Object buildActions () {
) _1 Y/ ^! n" \( O2 s1 J super.buildActions();' M+ b+ r" B0 J5 G! p, j% f
6 L* g9 c2 A) D // Create the list of simulation actions. We put these in4 b6 m1 o8 R) _' \% i L5 ?
// an action group, because we want these actions to be
# c7 Y* f, A E1 S2 q0 O( x // executed in a specific order, but these steps should
( @, }% T0 X& W9 H$ H // take no (simulated) time. The M(foo) means "The message
. \8 ~7 S/ U# Y2 D2 {* e // called <foo>". You can send a message To a particular
9 m' |$ B8 R, H. L9 h6 W // object, or ForEach object in a collection.+ Q7 E0 S: o+ @2 P- ]
2 e7 f% s& Q! Q- v6 S; k" t // Note we update the heatspace in two phases: first run
/ [ {: ^( q. P' w1 U$ _# h7 ?$ Y // diffusion, then run "updateWorld" to actually enact the q9 a0 q+ b; K: Y
// changes the heatbugs have made. The ordering here is& F/ U9 _8 v$ |" z6 j& Y
// significant!
, v+ O6 U; l0 V9 b. M + \& }/ {+ x6 _+ N2 ]& B! [
// Note also, that with the additional
& c, T( ~. ]: ?- r. O* r* L // `randomizeHeatbugUpdateOrder' Boolean flag we can. p- Y* {. ]$ n$ K9 w; p. [
// randomize the order in which the bugs actually run$ P) {( i# K# K5 c0 k
// their step rule. This has the effect of removing any3 a0 z5 J2 k/ h3 Z0 t
// systematic bias in the iteration throught the heatbug
3 n, t: o& w8 s6 T- l5 V* h // list from timestep to timestep
6 I: _' p/ X7 |- @$ I' \ 4 v* {, \. P9 S. A
// By default, all `createActionForEach' modelActions have; Z/ X; k8 v$ n" J* `8 @+ V! B- p" \
// a default order of `Sequential', which means that the
6 b. ]2 G& r$ J9 ?7 }7 Y4 T/ ~. v // order of iteration through the `heatbugList' will be
1 z$ d, r! m! K; ~, } // identical (assuming the list order is not changed
6 W. b- ~9 t: _' B4 H // indirectly by some other process)., J5 m7 n# O y8 h
* z" p& g, }0 P7 v modelActions = new ActionGroupImpl (getZone ());6 ]% u( b9 r; A# j N" @" k2 Z
' r& q, q9 Y( J% Y" l; B try {
, K4 _5 _, ]0 n9 V: T8 ~ L modelActions.createActionTo$message
, W8 c$ K0 X( A (heat, new Selector (heat.getClass (), "stepRule", false));
5 i4 S6 D* y; ~ } catch (Exception e) {' f7 y( e6 N- I" c# K: k6 `
System.err.println ("Exception stepRule: " + e.getMessage ());
# p8 i$ y7 e: ~ }' n. R5 n* A0 L; q
$ o4 _0 X1 }" c; W/ ^7 E5 T
try {. g' L' x6 i+ T) ]# I4 }
Heatbug proto = (Heatbug) heatbugList.get (0);
% q: i! W; [8 B* _! l5 C7 x- l Selector sel =
& |4 \1 b- w) u! u4 K new Selector (proto.getClass (), "heatbugStep", false);
/ d1 l8 t7 P# C: f* N. l. `5 p actionForEach =
2 D; |( p5 S7 w% T. G, O! c, i modelActions.createFActionForEachHomogeneous$call
, S) r6 X l! i4 r: x (heatbugList," i' a0 u' r! t3 E
new FCallImpl (this, proto, sel,4 b- q$ J$ b1 d7 D6 w( T
new FArgumentsImpl (this, sel)));: s' k! l4 o8 f% i' m! q; m! |6 b% i
} catch (Exception e) {
" S# i2 b, `( T/ ]) L$ F e.printStackTrace (System.err);6 y8 E$ a# X2 E4 |, y: H. `5 f
}0 v4 Y7 P! E4 U2 D2 { l; y4 f4 i. h
2 s! d/ d% \0 C" z. N& t7 j7 ^ syncUpdateOrder ();
. T, e1 B+ o& \7 }! [, P4 ?; c: {, Y* |
try {1 F- `; c) q% u
modelActions.createActionTo$message
4 t( N9 G# Q7 F' Q (heat, new Selector (heat.getClass (), "updateLattice", false));
& W1 I' s1 s4 j* D8 y# S. e1 z" j } catch (Exception e) {% |5 r7 T8 ?& x K* @8 i# z
System.err.println("Exception updateLattice: " + e.getMessage ());, \ G2 K/ U1 W b
}
" z1 G* j7 O [. U; U+ B ]3 l) x) F" A! x! c
// Then we create a schedule that executes the
' R: z% y0 Q+ I // modelActions. modelActions is an ActionGroup, by itself it
* m' c% U9 L' R( b# V+ m* {+ } // has no notion of time. In order to have it executed in
( j/ p2 e, C" |" _9 e; T3 B) B: M. B7 j // time, we create a Schedule that says to use the
( ?6 I7 x) K& `3 z // modelActions ActionGroup at particular times. This
# W9 N. q! a% P/ k8 w: H/ r // schedule has a repeat interval of 1, it will loop every, J$ G) \- C+ f/ e( d
// time step. The action is executed at time 0 relative to# O6 |2 r, f4 y
// the beginning of the loop.# m* u; j8 @- S% q& D) A6 I6 K
: y I4 G5 H* F5 x/ Z: A // This is a simple schedule, with only one action that is
) n( {0 x* o2 B // just repeated every time. See jmousetrap for more
, T/ |6 l# j w // complicated schedules.8 i$ Y0 V6 R2 P; Y4 C" I N# ]8 D
! X+ ]3 a( t. I3 c
modelSchedule = new ScheduleImpl (getZone (), 1);4 y9 j5 x9 k1 ?$ k
modelSchedule.at$createAction (0, modelActions);" |5 E2 S+ \* w% @) r
; B% R- i8 V9 ?* | W$ U
return this;& `+ j1 u% [7 u$ E
} |