HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ v8 J6 R! S7 w: [) B5 H* w% ~- |- _* s4 |' g
public Object buildActions () {
$ g( Z. a8 P: ]- q* g: T$ ^ super.buildActions();* V Z9 X, t2 P+ G- v
! {1 ]7 O' j: ]6 a* {2 ?5 a' @; S" q // Create the list of simulation actions. We put these in6 Y7 C! f! ^" I' ?, _7 @% ~
// an action group, because we want these actions to be
) g& j9 T- m' V8 p5 }) ] // executed in a specific order, but these steps should' u/ o! D6 \. m) s A
// take no (simulated) time. The M(foo) means "The message
% l& c: M# l: d# |* K9 ] // called <foo>". You can send a message To a particular! r" l2 j4 V) T6 d' M
// object, or ForEach object in a collection.: B$ x6 ?5 {, E& m) b
1 \" T, _, D( ^4 p+ r // Note we update the heatspace in two phases: first run/ \' o. T% F: [. K& |1 l% ^8 D
// diffusion, then run "updateWorld" to actually enact the
8 q' s* j/ c6 M) Z) N, ^9 I // changes the heatbugs have made. The ordering here is
9 K ^9 ^4 Y) E0 c- q' C# o) k* b // significant!' A. e, Z# }. U5 P' E
9 c6 ? J4 @% q( @$ H* N // Note also, that with the additional+ Q$ m9 _) M0 v0 W0 C! D
// `randomizeHeatbugUpdateOrder' Boolean flag we can! V8 f# Q! P5 K C( F4 X# Z& `
// randomize the order in which the bugs actually run
0 B7 Q* r# r. ?. n6 I7 J // their step rule. This has the effect of removing any7 o0 u- l, b4 E- e! R
// systematic bias in the iteration throught the heatbug1 d6 A0 r5 }& ~5 ^$ }( {4 z
// list from timestep to timestep4 B% `& r. R. M( l7 F6 [
- ]. j0 Y; }* c/ @, `: v% m; }$ v
// By default, all `createActionForEach' modelActions have
" E8 o9 [/ W# i! h" V // a default order of `Sequential', which means that the% [! H# \( E8 z$ w& `9 ?0 h
// order of iteration through the `heatbugList' will be; A% V- x6 Y/ k0 M7 N( z
// identical (assuming the list order is not changed
4 Z* K' u! E0 u3 [3 Q // indirectly by some other process).6 ^/ k% w5 R5 u
# [* M! ?- `$ \/ s5 v5 l! [ modelActions = new ActionGroupImpl (getZone ());
# j0 D* C1 Z* S
4 C k' I" m$ `6 g, P' R" r try {: K% @" u+ r& ^* l
modelActions.createActionTo$message
$ L! \, {0 t+ ?: K (heat, new Selector (heat.getClass (), "stepRule", false));
8 h! R* @! w. Z: ~* X } catch (Exception e) {
7 O7 c* E% Y- Z1 m7 h5 O; _! B% N System.err.println ("Exception stepRule: " + e.getMessage ());
) r( U: R' W# ]* J( ~ }
- E9 u) p( @5 q0 _- ?2 m
% x0 J4 P0 c! A( z try {
" `$ E- g) H; T+ l Heatbug proto = (Heatbug) heatbugList.get (0);; f% {! d0 |* l! G
Selector sel = 8 q [! g- ?, Z1 P0 P5 ?1 _7 R# f8 l
new Selector (proto.getClass (), "heatbugStep", false);
' W3 g' L3 @8 i8 S* K- E) c actionForEach =+ v' k* f1 J: t" K) G
modelActions.createFActionForEachHomogeneous$call
* D D6 z# _" E$ o! Y: p# x$ G (heatbugList,
; x$ }9 t8 X: E" H% Q3 t+ i new FCallImpl (this, proto, sel,
7 Y( X- L1 l% H7 z# ^+ B+ ^ new FArgumentsImpl (this, sel)));0 \. i; G7 y0 E
} catch (Exception e) {0 y; e2 \; D6 k# S
e.printStackTrace (System.err);+ r5 ?" B B# [+ C& b1 n5 N
}
; D' m) E4 k9 z
9 V- m* N8 S% g9 |1 ] syncUpdateOrder ();& g) ~0 a! q0 ?9 G) `5 [& D4 ]
8 ?1 b6 Z) s% p9 V$ H3 t
try {
0 S: [0 t. g7 U+ B modelActions.createActionTo$message
: i; t6 B6 E b V- \6 U& t- [ (heat, new Selector (heat.getClass (), "updateLattice", false));
1 g+ @" M6 T$ F% j } catch (Exception e) {9 r3 T/ K: E8 b: _* l( P6 R/ A: u
System.err.println("Exception updateLattice: " + e.getMessage ());
4 _: h7 v+ w. u }7 J9 |" b3 C6 O+ L& v
+ J6 z, @. m" ^$ ]# D
// Then we create a schedule that executes the
% f* \, S5 V$ F' ^/ r // modelActions. modelActions is an ActionGroup, by itself it
% s1 X4 I) m1 V' [6 n // has no notion of time. In order to have it executed in
% E M" [: ]% n( g' D2 V // time, we create a Schedule that says to use the
: w8 F- N* p: X& q // modelActions ActionGroup at particular times. This+ y8 m2 e& i1 K7 [9 I
// schedule has a repeat interval of 1, it will loop every2 Y* j! D$ b5 D5 u$ a) ^
// time step. The action is executed at time 0 relative to
8 c3 V' M" m6 K, F // the beginning of the loop./ h& m: {7 c S
6 X2 H- N. a; x9 t // This is a simple schedule, with only one action that is
# h$ k4 C R8 D // just repeated every time. See jmousetrap for more& \+ m6 b2 L! o+ n/ l% ?
// complicated schedules.
( y0 i, z/ [$ D# i8 u$ p; e. a # ]' N, T5 }4 _
modelSchedule = new ScheduleImpl (getZone (), 1);
% R& Z9 N. `% v modelSchedule.at$createAction (0, modelActions);; R+ t9 ?1 w$ j: B v) Y. ^* Z% G
% g! c6 I% {+ C return this;
" i$ |2 p5 {1 t5 s: ^/ M } |