HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 P0 K5 [" \; @" w% | v+ s
8 d% j, q2 D0 x9 W
public Object buildActions () {8 _' [' ^" }. m) d
super.buildActions();
; m- i; V& H% t) n( S/ C/ B
( G) f% ?* ^4 ^: J // Create the list of simulation actions. We put these in" X& f- Y: z' h5 n Z* D3 Q
// an action group, because we want these actions to be- N/ ]6 S$ T% h1 w2 l H
// executed in a specific order, but these steps should% z$ l+ r0 J1 M* u a
// take no (simulated) time. The M(foo) means "The message' ^ Z# d; [( B2 `
// called <foo>". You can send a message To a particular E# l+ @/ D3 J, O3 J
// object, or ForEach object in a collection.
/ y# [9 b6 P+ J: b* h4 Q4 J
1 F3 e+ n, [ y V4 n- z // Note we update the heatspace in two phases: first run" o3 ~$ p) z- V U/ A5 u
// diffusion, then run "updateWorld" to actually enact the0 V; Z) J/ K* G: O6 `
// changes the heatbugs have made. The ordering here is2 ?7 N2 O% T; K6 A& ^
// significant!0 \6 b2 i" O6 R) K* m
7 Z$ ?6 c' X& A ?" |2 p X
// Note also, that with the additional( t7 E8 H3 M5 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ W7 D' q% _7 X3 L // randomize the order in which the bugs actually run$ c/ V1 {) t4 @ n, j$ c
// their step rule. This has the effect of removing any
9 ]4 q0 W" F$ z# y4 b. e& J // systematic bias in the iteration throught the heatbug
( Q/ J5 b% F3 z* T7 Z // list from timestep to timestep
9 M* z r' a/ B# i8 L
' o( f% _; }. r# E4 J! k& Q // By default, all `createActionForEach' modelActions have7 r; x$ \3 I" F- R- @
// a default order of `Sequential', which means that the+ h; c! G0 A; ~& w
// order of iteration through the `heatbugList' will be4 c6 S' c8 d& F0 i+ W K1 S- v E
// identical (assuming the list order is not changed
! g* E% |7 Y0 B" V // indirectly by some other process)." _5 U. Y: U/ Z: V
% h3 [1 B: n6 V+ Z/ Y2 e4 V! X modelActions = new ActionGroupImpl (getZone ());& T: p0 Q* X& q( z
$ w9 P7 [; R D: R5 X: x try {
4 t( `) {/ y" X9 ~, A modelActions.createActionTo$message
$ [0 d0 H, c0 O! ? (heat, new Selector (heat.getClass (), "stepRule", false));
/ }" q0 D: D7 t! h. H7 z4 N. `; o7 l } catch (Exception e) {
r2 t1 a5 @) e/ J( y; w9 j% u7 r System.err.println ("Exception stepRule: " + e.getMessage ());
7 q H$ O' I V) o }. v5 m( ~8 }. _) t) Z# s
3 n1 \7 {( r* s8 W0 ~! @
try {
& y9 I* V9 j0 u- G# R1 ^! b Heatbug proto = (Heatbug) heatbugList.get (0);1 S7 Y, H. w7 f
Selector sel = / Y3 w! D# Z& Z0 H/ D( V! ]
new Selector (proto.getClass (), "heatbugStep", false);" R3 R7 ^9 d5 ?8 E* X5 D
actionForEach =: n- m2 ~; \, o' H
modelActions.createFActionForEachHomogeneous$call
- I! o" [1 s% N% j6 J, g& G (heatbugList,% v7 M/ W3 M2 a8 S4 \' S9 b
new FCallImpl (this, proto, sel,9 q" }7 O' P/ E+ H* w [7 Y
new FArgumentsImpl (this, sel)));; }7 g p1 Q3 i0 U( k$ J& ^' x- P7 P2 ?
} catch (Exception e) {" {$ K$ a# B4 B- |/ q% s4 U
e.printStackTrace (System.err);
1 f/ B# _) }* A }
: |) L, B9 G" o3 v! Z 8 Y+ {. E$ G1 f) y
syncUpdateOrder ();
4 q. z% r, ~8 m; P* T1 F1 i2 X5 U5 r% W
try {" s; A3 J, T' L8 h
modelActions.createActionTo$message 6 @& k5 ?- J. [6 s
(heat, new Selector (heat.getClass (), "updateLattice", false));' h! L& F, V6 R& u3 E+ H
} catch (Exception e) {3 J! Q. f: a8 G; {
System.err.println("Exception updateLattice: " + e.getMessage ());. p6 W2 v( I& a7 ~) L/ ?8 g/ `
}6 }! G+ p: w9 P1 q
L& F$ X. `0 D2 A# W
// Then we create a schedule that executes the* h% A+ U, Q5 _: q% a
// modelActions. modelActions is an ActionGroup, by itself it
$ f1 \6 T0 j3 I // has no notion of time. In order to have it executed in' C3 l/ e7 [' P/ F/ H D. k5 |
// time, we create a Schedule that says to use the) z* C" V% A1 z
// modelActions ActionGroup at particular times. This
7 a# V* j3 n8 O7 C, J/ w: q! F) F // schedule has a repeat interval of 1, it will loop every
! ? ?/ }0 D% O% J& r) s) [ // time step. The action is executed at time 0 relative to3 k8 M; k$ c) }& Q
// the beginning of the loop.
. Q* F6 H% C, `8 K
, i/ N# _# h; `$ a9 G A9 O // This is a simple schedule, with only one action that is
% Z8 ]3 F/ L- E5 R3 S; `7 J- G // just repeated every time. See jmousetrap for more2 {" a8 L! w+ r$ i: ^0 Y- B9 U
// complicated schedules.6 G: H0 `# x- e( D7 t& F
9 q+ T' Z5 a$ q3 C, r
modelSchedule = new ScheduleImpl (getZone (), 1);
' Y8 a2 \+ U: Y3 O modelSchedule.at$createAction (0, modelActions);2 H _$ e l/ K* F. C8 }# K
6 F4 e* J2 D8 {, m/ N
return this;' D6 q A$ w8 T. l! f! U. T; U" S
} |