HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ A' o/ g) _9 q7 I" v4 C8 r7 R8 H9 ]8 y- d& b3 U
public Object buildActions () {
. l! N) v/ o6 D5 P5 ?* X super.buildActions();6 l0 h. i& w6 _' `
. f2 O/ C C& _( [5 ]7 _6 e
// Create the list of simulation actions. We put these in5 b2 h* ?5 P! J2 _5 M& J/ A
// an action group, because we want these actions to be
+ n" G9 f9 s& n" [ // executed in a specific order, but these steps should
/ F: N) Q+ G w' [* z% g ] // take no (simulated) time. The M(foo) means "The message: h9 O5 [, |9 O
// called <foo>". You can send a message To a particular$ m5 A- h4 j4 r- f0 p1 T6 n
// object, or ForEach object in a collection.2 [: @" M4 @9 h7 i
, o) I5 r: {' R // Note we update the heatspace in two phases: first run
! y% m, L. M5 g // diffusion, then run "updateWorld" to actually enact the4 j6 x" b3 Y9 K# p1 o! @6 n/ _2 A
// changes the heatbugs have made. The ordering here is
D" J/ x) K/ s6 a A // significant!
" F8 R2 M9 P, Y7 L: z* Q* C" ^% Q , [, |" A. ^* n6 y. v+ l' t' r, U. |
// Note also, that with the additional
$ ~7 p" l; `9 Z7 _ // `randomizeHeatbugUpdateOrder' Boolean flag we can
. j+ @8 @3 h: d [ // randomize the order in which the bugs actually run; X6 Q6 V6 X5 x( d/ n5 q5 ]4 a
// their step rule. This has the effect of removing any
# b& N8 ]1 U1 s9 z0 x* v( z( E // systematic bias in the iteration throught the heatbug1 k' I9 z5 ], S! j( [& x/ p
// list from timestep to timestep) j S% t! A0 h% _0 l T9 i
9 c" h H3 l/ p5 ~+ |5 Y( L
// By default, all `createActionForEach' modelActions have. f( U5 B7 x3 S
// a default order of `Sequential', which means that the
0 q$ I7 b5 h* V( z5 w // order of iteration through the `heatbugList' will be0 |1 Z5 Y/ ?+ X* G3 W$ x
// identical (assuming the list order is not changed' j# [+ b7 r3 z9 b3 P6 B. z0 s
// indirectly by some other process).. a% n& S% m8 D d3 D" t
. L8 I5 N2 {- R0 B modelActions = new ActionGroupImpl (getZone ());7 i8 ?! K: v5 t# l. }* v
- v Z* p: ]7 I7 L9 v try {
% U; X; Q' c# J% t( ` modelActions.createActionTo$message8 Y2 X v G! C
(heat, new Selector (heat.getClass (), "stepRule", false));
+ i2 s4 O9 r4 z) }" j& a } catch (Exception e) {+ I5 S* a, T8 U( D& n. z! D
System.err.println ("Exception stepRule: " + e.getMessage ());
! ~% W# T3 h* ]7 Z }
4 N6 w; d Z! ]; @1 ~3 f' o6 m1 P/ D: V9 e1 u
try {, ^# P8 v7 H/ B2 K7 r5 i6 J/ Q
Heatbug proto = (Heatbug) heatbugList.get (0);" Q2 u" Q! v' V
Selector sel =
3 D3 S- S3 }! ]; z5 y% f9 E4 G new Selector (proto.getClass (), "heatbugStep", false);$ u. M2 j# {' q
actionForEach =. m9 U5 T* t8 J4 ?& S6 a
modelActions.createFActionForEachHomogeneous$call
0 z+ g) x" P& l1 ?( @1 G (heatbugList,
! r. k4 k1 Q4 J# n new FCallImpl (this, proto, sel,, E9 ~# o* Y! x8 _
new FArgumentsImpl (this, sel)));
% B) H' e6 K$ Z/ Z" J( p( s1 d } catch (Exception e) {
4 x, O/ x/ v5 E- d e.printStackTrace (System.err);
3 O [- v+ q# }: V2 \ }7 t0 S4 R" l! a0 t% z; ^+ k/ G
1 R4 j- n. @! {* u# ^" \2 g syncUpdateOrder ();
* m; O9 L" p' Y% t) | `. A: T% i% w4 l3 m! v" I" _
try {+ ~6 t- W% H7 s# b, L
modelActions.createActionTo$message 9 U; b$ t$ i" |; v5 Y) s( j
(heat, new Selector (heat.getClass (), "updateLattice", false));, T8 M9 T8 L" A' N [0 `) P; x
} catch (Exception e) {% T' r: r+ y% y6 x3 O; w
System.err.println("Exception updateLattice: " + e.getMessage ());# S% X/ E/ L7 k2 p6 W( J
}, O9 U. G. S; P6 E h
2 M( Y) ?5 w" h( z# y // Then we create a schedule that executes the
3 z0 {! I7 W0 X; S" k! M // modelActions. modelActions is an ActionGroup, by itself it
! H) c$ ~! Z, ~& @" b // has no notion of time. In order to have it executed in
/ M/ F- r& {, K: d. ` // time, we create a Schedule that says to use the h( O6 {+ N; q6 v, j2 q- M
// modelActions ActionGroup at particular times. This1 S( v" D+ J& t% V j% R
// schedule has a repeat interval of 1, it will loop every! J+ U1 a9 a' W3 z
// time step. The action is executed at time 0 relative to
0 K( V4 {* P6 Q1 n4 l o // the beginning of the loop.8 W F$ }- t! t
# V4 F. L' c( I* t! q
// This is a simple schedule, with only one action that is4 [+ q' M3 E/ v' A" ^+ |3 q
// just repeated every time. See jmousetrap for more: N1 N$ X) Z W5 G/ T9 v
// complicated schedules.
* Q9 _/ A h3 n! o4 a! v8 j P9 ` 9 Z6 A9 Q/ s2 R9 |3 C1 }
modelSchedule = new ScheduleImpl (getZone (), 1);9 b8 M8 g9 [: D! r N( T
modelSchedule.at$createAction (0, modelActions);3 x" p+ ^: Y5 ^, {
9 z& u) H p( D. W/ _5 `" j& d* Q5 W
return this;
6 h- b W X# a" l4 |( |! m! t } |