HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& M8 Q! q- H5 G! a2 [) E+ |, }5 a: Q. N' k
public Object buildActions () {. d9 ~) l2 D* y( i, R- v; G$ ^% S
super.buildActions();
, g* ~6 M/ b* J ' [5 Q! s1 a$ w+ |
// Create the list of simulation actions. We put these in9 G! D5 @# J* I/ r+ u2 {8 {' T
// an action group, because we want these actions to be
' p/ U, x, u7 d // executed in a specific order, but these steps should* e# F$ `+ [9 E: i, h) U X& l3 @
// take no (simulated) time. The M(foo) means "The message
: o, Q5 @' y1 T3 F) d // called <foo>". You can send a message To a particular( X/ I& `, R5 X O. w, A% j/ m! D
// object, or ForEach object in a collection.
+ \9 \3 ]& e( K; p" B9 X
8 i$ l/ k0 [9 \4 |! G // Note we update the heatspace in two phases: first run
- z2 k+ s n0 t( D# w3 a; b // diffusion, then run "updateWorld" to actually enact the
7 I0 t8 Y! ~9 Z r/ i6 } // changes the heatbugs have made. The ordering here is- D8 B5 O* @& `$ @( ?8 z
// significant!
3 |' } Z( H/ l' l' z H$ [
|% K. L. A! E3 y8 q% y // Note also, that with the additional
`7 p. q+ u) [ // `randomizeHeatbugUpdateOrder' Boolean flag we can ?3 T3 p5 w3 {6 s( C, p6 }
// randomize the order in which the bugs actually run/ g' j/ B% E+ s8 K k/ i% L
// their step rule. This has the effect of removing any
! t; h# i; b8 Q; f4 O' o // systematic bias in the iteration throught the heatbug- N& P4 q- u6 |8 Y+ e
// list from timestep to timestep
( H: v- \. u; F! I$ m) E
1 k, P P2 ?7 @0 ` // By default, all `createActionForEach' modelActions have6 A4 x+ c) V+ u) k( @! ^+ F
// a default order of `Sequential', which means that the4 |2 ^& Q6 X, ], ~1 @
// order of iteration through the `heatbugList' will be
' `! v) l$ |' `& H; _% m( S- W // identical (assuming the list order is not changed
% s8 @8 B- ~. F, q // indirectly by some other process)./ v, r( v$ P: @3 H( B) K
$ f8 Z! L0 Z+ G$ f z$ y) t: B/ q% U" A modelActions = new ActionGroupImpl (getZone ());
1 E) M& n/ F. l: ^) T* p3 J
" p4 x0 V# D( S6 [& X6 F try {
- ~8 Z6 A1 Q5 `2 @ modelActions.createActionTo$message
' x0 _& ^1 P$ ]' e, I (heat, new Selector (heat.getClass (), "stepRule", false));0 f$ M; }; J) ^4 n2 _1 b6 v5 p+ p8 W
} catch (Exception e) {
1 F, J& O" X5 t' c+ l% c System.err.println ("Exception stepRule: " + e.getMessage ());
: G" g! i- o* t$ F2 x }
0 v1 Y( A. `9 e5 k
( Z+ S2 z" E( q/ e8 S try {3 C% \4 v# Z. J9 w3 Y K
Heatbug proto = (Heatbug) heatbugList.get (0);) k( c. {# J P
Selector sel = 3 `' I' V0 Y: w/ ?2 ~- \
new Selector (proto.getClass (), "heatbugStep", false);
) A2 I2 t5 R3 B6 k7 s3 L actionForEach =0 T) E( A' S1 I: G. `
modelActions.createFActionForEachHomogeneous$call
3 Y! T, D" @( H6 L7 f, b2 n (heatbugList,
. c' |6 O) Q |' q1 H- R new FCallImpl (this, proto, sel,; ~# W. v- {) S7 v. c
new FArgumentsImpl (this, sel)));
$ ^, E! s% U" s, g& | } catch (Exception e) {' M6 h# q1 {; k3 Q
e.printStackTrace (System.err);
# ?4 G* L5 }0 H }* p; _# {9 P' `5 Z1 K' Z) H0 A
7 O( @( O9 l! \ E syncUpdateOrder ();
Y6 o* A& V5 E$ \+ y) @
, S' K' Q9 T) F5 h try {0 Z* h! R" G& \( s& g
modelActions.createActionTo$message 4 V! j/ P% B6 C r0 o5 N0 F! \' L
(heat, new Selector (heat.getClass (), "updateLattice", false));2 S3 ~* q) ~; z
} catch (Exception e) {4 k' j3 s0 @! ]
System.err.println("Exception updateLattice: " + e.getMessage ());
, v; t1 @6 E, p }+ i8 _/ J; n8 E+ |. q4 x8 o
- r; [5 @7 J% \( `9 l // Then we create a schedule that executes the
9 N; S' A1 _' E1 c // modelActions. modelActions is an ActionGroup, by itself it; H$ P$ c9 q) k# ?9 ?- a6 N
// has no notion of time. In order to have it executed in
/ r8 U" z9 T% p; w // time, we create a Schedule that says to use the
: y3 r: F, p9 [+ b( N7 ^ // modelActions ActionGroup at particular times. This7 F6 j4 P9 a) C) p+ _! h$ Q
// schedule has a repeat interval of 1, it will loop every
d0 \! }% |! g* R z // time step. The action is executed at time 0 relative to1 P$ R m, y. ~; E) @
// the beginning of the loop.
& _' j, Z2 O6 D/ ]; }& D
' Q! R0 k: {9 r2 A$ K // This is a simple schedule, with only one action that is, V4 a: y/ x. J; m: Z# [; N6 F4 U" h
// just repeated every time. See jmousetrap for more
6 u, c+ {* U! p/ i" ]/ E, e // complicated schedules.- V2 C3 C4 P/ a. X
" I7 u# F! r* }% ]5 P) O/ X% t modelSchedule = new ScheduleImpl (getZone (), 1);
% K$ s" S6 R1 J' l# o modelSchedule.at$createAction (0, modelActions);& [, K3 }" C# R
, Y# V* s/ o6 N; f0 i7 [
return this;0 o$ g$ h* ]7 x1 u
} |