HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. B) E+ b/ X4 U6 v4 _7 Q; \
0 t/ _* o$ `& J3 s public Object buildActions () {
+ G s$ [& G p% K/ {+ |/ J3 _ super.buildActions();4 O7 l& w1 _2 O" U
8 F) b0 Q% F% L2 R // Create the list of simulation actions. We put these in
% n; J6 T; X, [( T // an action group, because we want these actions to be; P8 m3 T; U b* H2 T0 o- W
// executed in a specific order, but these steps should) X. F# i5 k: ?# e& w/ t& I! h3 }
// take no (simulated) time. The M(foo) means "The message
" E" B L7 @! {: i) t( ~" D // called <foo>". You can send a message To a particular3 B$ s* \. B3 L& _4 a6 E
// object, or ForEach object in a collection.
. l0 p9 U$ H- y7 S1 m# U2 t * |" F: y& `4 k8 b
// Note we update the heatspace in two phases: first run
0 S9 i' y. E! o# J // diffusion, then run "updateWorld" to actually enact the8 \+ J/ S }3 |5 }/ ^$ r+ [
// changes the heatbugs have made. The ordering here is
. _" g6 L s! Z // significant!7 N" _1 Z4 x( Y
4 \0 Q5 N, N2 ]" \- R // Note also, that with the additional
- J( X S' I1 C# J! x$ L // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 a! R7 f8 m) s1 k0 v // randomize the order in which the bugs actually run" I5 C& |" l" r
// their step rule. This has the effect of removing any! Q) i6 _8 e: N2 k- X: Q
// systematic bias in the iteration throught the heatbug
4 b5 a& y3 @- `" e/ C. b. H // list from timestep to timestep
* m- h$ Y5 ]+ n% q
5 N, m% Z/ ~! Q' k# `7 ^( C; ?- L8 h // By default, all `createActionForEach' modelActions have
* a% u; t* \5 c1 |5 k1 } // a default order of `Sequential', which means that the
( j: x9 G+ n2 ^! o2 b8 \- t4 @ // order of iteration through the `heatbugList' will be
" P" O F* G8 N: ] // identical (assuming the list order is not changed/ n7 g/ u1 ~- r% l+ U; ~0 `8 ~
// indirectly by some other process).; h" [! ?5 b: g2 @3 e& }/ V
( h0 j4 h% }! W( k+ c modelActions = new ActionGroupImpl (getZone ());
4 f% l. f, \' G
. R. E' |% I. A: ~" i% G% p try {$ `2 ^7 A6 X4 ?: p+ b
modelActions.createActionTo$message0 N9 w5 I* y" L/ M2 ]
(heat, new Selector (heat.getClass (), "stepRule", false));
. i; W- u' S1 o) J } catch (Exception e) {
1 K8 m0 L5 F( w( Z System.err.println ("Exception stepRule: " + e.getMessage ());1 v* q3 H% f1 t
}
0 F/ I4 i- T( }6 e& U0 ]
& B1 D# w! v; m$ P! e$ l try {
1 ]( t% v" `1 o0 ?+ O# S# D& j Heatbug proto = (Heatbug) heatbugList.get (0);$ _# [% ]9 q: _8 @. M
Selector sel =
! P- Z( h' ^# t9 C- ~ new Selector (proto.getClass (), "heatbugStep", false);
8 j$ |7 }" H7 i+ z) f actionForEach =
, B3 W- ]/ z' w& ]. ]+ }8 Z5 C modelActions.createFActionForEachHomogeneous$call6 |6 c* _; v/ ^3 j( p/ ~
(heatbugList,
+ b. R1 z( H# U7 c new FCallImpl (this, proto, sel,
; n% J& s! j* S) x) d; q% C new FArgumentsImpl (this, sel)));8 W5 D; E/ D, }: O2 m# q, X
} catch (Exception e) {
+ R* k- b; W4 k6 P2 r e.printStackTrace (System.err);
. _% |9 H, D0 f }8 m" ^8 t2 F. Y( r1 Z% Y
$ ]( Z$ f5 P, e" Z! t6 P' e* u syncUpdateOrder ();& r; k1 d7 {/ M" }/ M2 ]' F
0 c5 ~, Y- X" j5 i* S6 y
try {
8 a- ^, ^2 o0 j- r& J/ I. e modelActions.createActionTo$message
4 L3 p, \+ s7 r; X) d (heat, new Selector (heat.getClass (), "updateLattice", false));
2 V0 h; I6 q& g, K8 _ } catch (Exception e) {0 z3 x% k2 i k4 u& D
System.err.println("Exception updateLattice: " + e.getMessage ());
1 [# ]. d* D; n( l1 P }: U6 m& H/ X" l5 {" V( ~/ P
( J( T8 _$ V! I# [5 @$ ?( _4 _; ?
// Then we create a schedule that executes the! p+ W& F/ I) B7 B; x; I
// modelActions. modelActions is an ActionGroup, by itself it- E7 A" t- _- ~, C# C' E3 D- m/ b
// has no notion of time. In order to have it executed in
1 D5 p( Q" J: @( Q // time, we create a Schedule that says to use the
( u( t# D: q8 f5 f) [ // modelActions ActionGroup at particular times. This
& J' G. ]9 a9 M // schedule has a repeat interval of 1, it will loop every
. \( s* G) G+ w" m- E! o+ S4 F // time step. The action is executed at time 0 relative to: l( u9 ~; s: E7 ?
// the beginning of the loop.
+ Y5 F+ H; P1 h! ]- e2 q! e" Q, s5 c* k" @ R( ~' P; W5 h
// This is a simple schedule, with only one action that is, I2 R3 Y& |! r0 ^# e2 z5 _! B
// just repeated every time. See jmousetrap for more
( v$ X/ D# _% W7 x$ j1 m# g // complicated schedules.. H# {/ r& p, _3 x+ \* c
7 s. m$ z# L) d$ I3 H. ] modelSchedule = new ScheduleImpl (getZone (), 1);
! X- L- ~! A0 z- ` modelSchedule.at$createAction (0, modelActions);3 Z8 l! E! A% d, ]3 Y
" K) {& \" \% n return this;
$ A7 x% e( ]! ~/ V } |