HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# b% x* j$ \) Z" L% e
9 [6 w9 w, r$ e5 O! A# G- T public Object buildActions () {
5 O, b' J+ N. k, A1 `; W- b super.buildActions();' U R9 G! [* K J
* T, E7 ?' L. k8 _
// Create the list of simulation actions. We put these in
1 i) M0 Q7 U& T7 H: l) P! V // an action group, because we want these actions to be7 z; J# ?/ r5 L( P, X
// executed in a specific order, but these steps should
b) W5 d( `' T // take no (simulated) time. The M(foo) means "The message! g s& t. M& q. Y
// called <foo>". You can send a message To a particular3 z- E! y1 S, @; k) v+ O
// object, or ForEach object in a collection.& u; K9 v" n3 S1 G \" T6 Z
# V& [2 U5 J- }/ Z) |, K
// Note we update the heatspace in two phases: first run3 w2 C8 _' {/ Z6 a9 @
// diffusion, then run "updateWorld" to actually enact the' A5 Z+ o/ D7 Z8 D( q7 @7 N
// changes the heatbugs have made. The ordering here is- z9 P$ g3 o- D3 `" G: p, n! i
// significant!, q7 L7 H: C3 d S
& q, N3 U$ Q+ s
// Note also, that with the additional+ T# x6 U, _2 e' @* h; x! j% e6 o
// `randomizeHeatbugUpdateOrder' Boolean flag we can; R4 }1 Q' \, u/ H/ R! X/ _
// randomize the order in which the bugs actually run
( o' P& x* c* L: H" N // their step rule. This has the effect of removing any# n V7 \( b7 y
// systematic bias in the iteration throught the heatbug$ V$ x: s3 [2 O: r4 H
// list from timestep to timestep: _; s2 r C: l1 F/ f
9 V. }; o0 S( m
// By default, all `createActionForEach' modelActions have& ?& F& M/ E5 U/ d
// a default order of `Sequential', which means that the
( `5 b/ R# x& M' y // order of iteration through the `heatbugList' will be
4 O6 z) H; \9 o! E6 E // identical (assuming the list order is not changed$ C) w' P- s4 L5 C* C1 m- Z |
// indirectly by some other process).% e: q% p& Z8 _! x" H7 S4 U
& k; P! E2 _+ [% a6 ~3 d! w1 P modelActions = new ActionGroupImpl (getZone ());
; P" p4 O. l5 C8 V9 v+ B
; x/ O2 `3 ?8 h* X' S. j# Z w( j try {
% p; g$ w7 r2 g$ K. Z H$ @; `0 z modelActions.createActionTo$message
, R4 f; o3 C1 _& s$ ]/ F (heat, new Selector (heat.getClass (), "stepRule", false));) Y) u$ f" W: D* B4 P# q
} catch (Exception e) {! d R- |; c2 c( R6 X$ |
System.err.println ("Exception stepRule: " + e.getMessage ());0 |# D' }6 d" f% H# g
}
/ m) e+ l/ ~: \
) s! ^1 F7 C2 E6 M" `. ?7 a" ]! H try {
5 S* X4 a+ N* ]- Y. k9 o( r Heatbug proto = (Heatbug) heatbugList.get (0);
/ O/ M3 d) H3 b' O Selector sel =
j: \) d6 X( R* B( v9 [ new Selector (proto.getClass (), "heatbugStep", false);
6 J9 f+ X; }. K" M2 C5 b. f: r* } actionForEach =
4 E$ O; u2 h3 `* K" U modelActions.createFActionForEachHomogeneous$call
P& h, C9 }( a; r3 L x (heatbugList,
% \) I) @2 s2 G1 P, {: {* J% h! D9 m new FCallImpl (this, proto, sel,
( _3 o( c" q& V0 v. @+ d' C z new FArgumentsImpl (this, sel)));
* H5 C8 L1 H8 }$ x; R } catch (Exception e) {+ e [- u+ [- F* l5 o7 H
e.printStackTrace (System.err);
" ^* K& ~9 g2 x# R. S t5 y5 _ } r+ l% q, d" o M' i
! r( }2 A1 c. ^6 n u! p syncUpdateOrder ();
" c+ [( H/ v N i9 y2 E+ C- m1 m* o
try {
$ ^3 n6 R" V `* Q: O modelActions.createActionTo$message ; m7 F: y; I7 b! T# T7 e
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 @( O2 M0 R- R% W* S% |% S$ Q) ?4 P } catch (Exception e) {, _$ r% p! P3 d) F/ T2 x
System.err.println("Exception updateLattice: " + e.getMessage ());& a- X$ d% l0 c# e: A" d
}! K: B5 w8 M# r6 x O
: u X* x" g K2 k; x, T7 W& R // Then we create a schedule that executes the
; O& N, s1 M$ w" v8 h' e6 [ // modelActions. modelActions is an ActionGroup, by itself it: q4 Y8 c' r( Q0 M% E# X1 p, ^
// has no notion of time. In order to have it executed in
8 r( j; ] ?; s$ P8 C5 L // time, we create a Schedule that says to use the
3 o8 J: e5 d$ } // modelActions ActionGroup at particular times. This
. x0 M$ @3 `1 T; L // schedule has a repeat interval of 1, it will loop every
5 K& J; J+ [1 ~; `" K. r% c // time step. The action is executed at time 0 relative to! z+ ~- P/ h; N, r" D- z
// the beginning of the loop.
e$ ^) D/ V' b( |. d! w2 P) A
7 y8 L+ p: V3 f1 H8 x // This is a simple schedule, with only one action that is4 Q2 h, `/ o1 [% G
// just repeated every time. See jmousetrap for more
2 `3 D2 P8 _" I( A3 W // complicated schedules.
) [" c, }3 Y; r1 f1 a! R7 C / P G& S7 d4 O3 D/ T( `" L7 H
modelSchedule = new ScheduleImpl (getZone (), 1);
1 k" {5 G! ^0 ]) ?1 }. m2 o* } modelSchedule.at$createAction (0, modelActions);
6 r d1 f- G/ f5 u% O5 W
! ^' c% K" K% U& ^/ B return this;
. v1 }0 b$ z4 J0 x5 k; Q } |