HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 y) K/ e' w% v* ?5 s& P
6 n! ~! {6 Y+ K! Z$ r! ?
public Object buildActions () {
1 n1 r% ?7 p* k super.buildActions();. I- S( Z: Q6 f( }
) m& U6 _+ Q: y5 g1 a8 k1 _$ g! d // Create the list of simulation actions. We put these in4 J6 P* ~4 l& r+ d# I
// an action group, because we want these actions to be
( E! V- b- T! p7 R // executed in a specific order, but these steps should
: y e) S! w/ J- L // take no (simulated) time. The M(foo) means "The message
9 b, A/ y$ S6 c5 Z // called <foo>". You can send a message To a particular5 o3 T0 r# e0 l% q6 }+ Y& j. }+ n
// object, or ForEach object in a collection.- U- o/ _& M( R- L* S7 I
- l+ V; P, @' @8 c, m
// Note we update the heatspace in two phases: first run9 l( b _# y& U6 ~- A. v
// diffusion, then run "updateWorld" to actually enact the5 o n- \2 H. d" `8 G
// changes the heatbugs have made. The ordering here is. T9 [* t: h( v. h" x
// significant!
7 T$ \7 P# }/ H8 X+ Q7 E3 J
7 F7 f9 x) ?: r. N) q) O- U& E) t // Note also, that with the additional! P0 V9 L8 T) }# c3 @
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ |+ h' ]/ v2 E% f; F! k) e' e // randomize the order in which the bugs actually run
1 V5 E, q/ u; R // their step rule. This has the effect of removing any: Q6 y3 D9 p; t& t
// systematic bias in the iteration throught the heatbug
+ t' i+ T Y9 N0 q0 x // list from timestep to timestep: [* Z5 P4 B, I: D+ Q, o
. w- I4 ]4 H% S7 ]
// By default, all `createActionForEach' modelActions have
" N' |/ y: C7 n* D // a default order of `Sequential', which means that the" w2 T9 K. r- @
// order of iteration through the `heatbugList' will be
" \' z# f7 F/ I2 h' P* a // identical (assuming the list order is not changed
7 e/ d4 P! Z8 _ // indirectly by some other process).
+ c0 i [) \) h8 T: A/ y * a4 l1 O/ U! S+ {+ _
modelActions = new ActionGroupImpl (getZone ());
[. |! m. k& W8 B. K6 V7 [( U0 k( \
try { ^5 [/ T( ?4 f5 ]
modelActions.createActionTo$message
, K, K4 c( C. @- k7 I (heat, new Selector (heat.getClass (), "stepRule", false));
* {3 u1 `3 [+ Z& P) v: Z } catch (Exception e) {( l, Z$ K7 Z2 x4 g
System.err.println ("Exception stepRule: " + e.getMessage ());
& q& D6 s5 J. M3 T: r6 R' |- n }
% G8 ?- o W0 |+ _& ?* B! ?4 R. u& k) A+ C! W* P- Z4 Z" _6 v" ?- Q
try {
# L8 ^; S! `2 c: l6 @# O Heatbug proto = (Heatbug) heatbugList.get (0);
& X% ~" o1 ^% [. p3 G Selector sel =
) T2 s9 O& H5 ] new Selector (proto.getClass (), "heatbugStep", false);5 M+ I3 q2 x! M, e7 o4 W5 \8 w1 B5 d
actionForEach =: }! z' x8 ]7 v$ z. a$ A
modelActions.createFActionForEachHomogeneous$call
4 u! P0 }! M; H* `. U' ~* A (heatbugList,8 A) @: s* k6 }* ]: b
new FCallImpl (this, proto, sel,
5 g X+ x8 `# ` Q4 }- { new FArgumentsImpl (this, sel)));
5 }4 y" h0 g, ], X8 r3 s } catch (Exception e) {/ [. C. |& A% `0 a# y5 t
e.printStackTrace (System.err);: C" T: t' O5 N( I& I; N
}
# |$ |4 W2 z' P& c! W* k4 i # \; p. B& z$ ^ C5 a+ L
syncUpdateOrder ();
) n6 z! I2 p: X2 s" H5 _9 a% Q' A) \% T
try {! ~3 _- w9 X7 ^1 ]! O3 l- O1 X
modelActions.createActionTo$message
$ e% n* j/ k9 N) V/ \$ C; e- r9 P (heat, new Selector (heat.getClass (), "updateLattice", false));
& F: {! y3 X6 j. m( j } catch (Exception e) {- m3 _& X' a0 v# v S8 \
System.err.println("Exception updateLattice: " + e.getMessage ());
3 B i/ K) k V: T* t9 Z+ V }1 c! M& w) u( }! L) U
, w) a* d& R- Q- v // Then we create a schedule that executes the: o4 y3 p, x) z
// modelActions. modelActions is an ActionGroup, by itself it
8 f; r/ V" a+ a" D // has no notion of time. In order to have it executed in: B: ~8 g. h. J
// time, we create a Schedule that says to use the
! o! q3 ]$ t1 r: ^ o2 l, R/ k% Y' Y // modelActions ActionGroup at particular times. This+ t* ^4 c# F4 ]& I& j
// schedule has a repeat interval of 1, it will loop every
3 r+ `7 ^3 ?+ O; M( `0 ~, p // time step. The action is executed at time 0 relative to
; V4 t3 P! R5 u/ z7 @ // the beginning of the loop.
' A% R9 n% ^# o7 S1 Z" z2 j
! Z5 D! a! E! T+ C' K // This is a simple schedule, with only one action that is
. j X# m" {6 m" a // just repeated every time. See jmousetrap for more L4 e( {& l( o, N, n
// complicated schedules.& _- L& g7 C/ Y) b# P& F) x' i
9 `: E% N9 R* e* y, m' k& x# f modelSchedule = new ScheduleImpl (getZone (), 1);
& A/ H0 x9 { B modelSchedule.at$createAction (0, modelActions);
% m& H9 ?/ \/ P 6 o) r4 E6 X/ I6 |7 G
return this;$ G8 S; f& m/ A6 r' d
} |