HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* W" r* c0 {. @
& k4 p9 n2 k( _5 z4 k q$ I
public Object buildActions () { }: A2 a& z- U
super.buildActions();
* |5 I* N- D9 X7 u6 {, W: m
6 w, f) m4 J: l0 s6 c // Create the list of simulation actions. We put these in- K2 n. l/ m2 k0 X2 P4 i1 n5 I2 m" l
// an action group, because we want these actions to be3 H; C1 |9 x+ O2 l
// executed in a specific order, but these steps should
4 ^( g% w4 F* F" h, ~- C; i: t // take no (simulated) time. The M(foo) means "The message
+ H# u- i% z8 |+ y1 S' j // called <foo>". You can send a message To a particular
* Y1 V3 u/ @$ R& z: }, O8 t; ]/ f // object, or ForEach object in a collection.
% v! c8 n; m! @( |. ^+ E+ e $ b+ a& I+ ?' x6 j
// Note we update the heatspace in two phases: first run N! ?1 r" F: w) Y" x, U7 O; ]! Y! z
// diffusion, then run "updateWorld" to actually enact the2 d, I# _4 n! o
// changes the heatbugs have made. The ordering here is6 u5 a' t3 H3 I" y6 o& i) I
// significant!
' y5 r3 `0 O2 o* `: u3 w( U
' x1 J8 Y2 p" N* F; _ // Note also, that with the additional; _* e( q8 Y D' ^# P7 h
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 I% _7 A3 }6 e' F6 E // randomize the order in which the bugs actually run: H6 W. C2 e4 `. w4 |1 c
// their step rule. This has the effect of removing any0 s" U9 J8 }: D; r# |. [
// systematic bias in the iteration throught the heatbug9 r7 X6 S- s: v. D9 K2 [, p
// list from timestep to timestep/ b' t( s4 f7 a
2 v% A5 `0 U( g# I' a4 G" o
// By default, all `createActionForEach' modelActions have
, \" a- a8 z# r( P I" G8 s# p // a default order of `Sequential', which means that the
! s/ Y5 J* O$ u2 r" Y5 S+ z // order of iteration through the `heatbugList' will be
9 @* e1 K5 W C // identical (assuming the list order is not changed
0 b2 Q% n* ^* K% W3 I% U# K // indirectly by some other process).* Z* A/ ^' _% E' Y$ Z
( w( H- i A: l: K6 X
modelActions = new ActionGroupImpl (getZone ());) M! G) W' X2 q# _
: I7 E9 _! ^/ ?
try {; ~1 X. p7 q. B! V( `- J5 E$ b
modelActions.createActionTo$message
' D/ `. e( r e# w! r: g* T- t (heat, new Selector (heat.getClass (), "stepRule", false));/ C* H) `; n+ x# }
} catch (Exception e) {1 ]' N6 f; L6 Q' o: u
System.err.println ("Exception stepRule: " + e.getMessage ());- E$ N# ]7 n, N2 C+ L" S
}) h( x* j; l, Q# g! J: I
O5 c+ G @1 {0 Y
try {% C- j& R' W- P* k- ]( T9 k( w
Heatbug proto = (Heatbug) heatbugList.get (0);! M0 U, U. O2 I. f0 [
Selector sel = 5 G( N6 f5 H1 w/ D7 v( _9 m
new Selector (proto.getClass (), "heatbugStep", false);, ?7 w }+ q8 e' d( Z7 l* p8 u
actionForEach =
6 @( t5 H$ W" f# e modelActions.createFActionForEachHomogeneous$call
+ f! v( |- R5 g' `: i+ v$ a: w& S4 J (heatbugList,9 p' L+ J6 J2 N% M; ~. F' v
new FCallImpl (this, proto, sel,& \. a, x* Z! x) f8 A2 P
new FArgumentsImpl (this, sel)));
6 D" a4 \) ` ]3 E } catch (Exception e) {
2 \/ ] n* E+ v2 w" b e.printStackTrace (System.err);
5 D9 W, {) K7 z: q }* _% E- `: L; a7 S7 A# x
! R( i4 W# @# p- @ syncUpdateOrder ();
9 Q7 v0 h' |' c- J1 w* i1 z/ L8 M7 Q8 k' C5 ~ { q
try {4 W; Y4 Q) t4 u0 `
modelActions.createActionTo$message ! w' E! M$ g; u' n# h! D
(heat, new Selector (heat.getClass (), "updateLattice", false));$ ~7 h5 w( g- e }( h; j! \
} catch (Exception e) {3 u8 _9 I/ ]& k; e
System.err.println("Exception updateLattice: " + e.getMessage ());
5 L" X- _2 h; v& a: a) b1 O }
0 t5 p# F* O4 h" W7 p" X8 B* w$ F) N + q! d* c& T S! H) T0 e
// Then we create a schedule that executes the- [. K& {* X k5 p' B: Q
// modelActions. modelActions is an ActionGroup, by itself it
8 r6 ~. L" i! B8 s& x // has no notion of time. In order to have it executed in
0 R0 b0 `" Q$ C6 E5 C // time, we create a Schedule that says to use the0 b, @4 _; V6 a' |
// modelActions ActionGroup at particular times. This
; w1 \/ p- }2 T3 T0 Q/ l // schedule has a repeat interval of 1, it will loop every
4 [: d- q% \5 z# k8 b/ t% j% ? // time step. The action is executed at time 0 relative to
0 e6 U% X7 E5 S* W& y // the beginning of the loop." q+ j! m$ J1 w# L% z; y. i
/ o& c7 L. w" k
// This is a simple schedule, with only one action that is
/ g- }* S# k7 Y7 O0 j // just repeated every time. See jmousetrap for more: D. v+ M, ^: _' f& t5 D6 O
// complicated schedules.- e! s' c( h# s7 d' M$ @
# Y: I1 |- }: N t/ L" ?4 g modelSchedule = new ScheduleImpl (getZone (), 1);
3 l4 r- s/ d w% k ]$ |$ |! V6 Y modelSchedule.at$createAction (0, modelActions);! Q8 f7 o; ?% g
, ]$ B) Y6 X( ~ return this;
0 P$ {: x7 S( Y } |