HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! M8 r; f" m3 j8 z3 z; b
* F5 V" u" e; q
public Object buildActions () {
3 t! h2 F# G3 H/ |- o; O1 ? F super.buildActions();
$ U9 j; N: | Q4 U! A 2 O/ w# a1 A. O# {6 ^
// Create the list of simulation actions. We put these in( ?# w* N" Z8 Q* {/ ~) o* {
// an action group, because we want these actions to be( Z* I# S/ z( o8 G* ]7 ~
// executed in a specific order, but these steps should
1 J% ?5 u1 H; y // take no (simulated) time. The M(foo) means "The message
% P+ T& y: n: G) r // called <foo>". You can send a message To a particular
- U: P- W1 u0 ~2 m I0 P% L // object, or ForEach object in a collection.
" T5 a6 m% ?8 d4 g2 [
6 B q9 H v9 G& }9 z' f; P# a // Note we update the heatspace in two phases: first run
, H/ \$ M: N9 P; m/ C) K8 r // diffusion, then run "updateWorld" to actually enact the8 J8 s6 b$ D" t. m
// changes the heatbugs have made. The ordering here is
, G. W: b: s+ _3 K- p" J // significant!
) V: T8 Z2 o/ D: C/ P' I
' S% ^, o1 g( [, c/ z1 a# q+ c4 F // Note also, that with the additional
. ?; R9 f& B/ _7 p" n5 _2 I4 Q // `randomizeHeatbugUpdateOrder' Boolean flag we can3 A; i, m4 \' h+ s5 o
// randomize the order in which the bugs actually run- S Z' x& z' W4 A
// their step rule. This has the effect of removing any6 O6 D P2 b/ C
// systematic bias in the iteration throught the heatbug
. g* d; K' H% f, K5 j/ n // list from timestep to timestep
2 B1 }8 `( h- [/ S1 C
; ]5 G. A. ~7 t // By default, all `createActionForEach' modelActions have
0 n: D, Z) @/ R/ M! v // a default order of `Sequential', which means that the
- T" s( `3 ]1 S4 |5 R3 ?9 } // order of iteration through the `heatbugList' will be
7 u8 I. x# W% R // identical (assuming the list order is not changed+ {/ k' K! Y6 |7 H! [: V9 F: R
// indirectly by some other process).
4 u$ X% \* R/ U$ j7 U. u8 i x( n
! [( v# v9 }" h, e7 c9 G) u1 ` modelActions = new ActionGroupImpl (getZone ());
& ~& C) a% T) B+ m2 W" s4 r/ Z0 r
& b0 M$ U3 M; H1 I& @ try {8 R4 X: P) e. R @, Z& b( |
modelActions.createActionTo$message
4 a \/ q# v6 e (heat, new Selector (heat.getClass (), "stepRule", false));5 K7 y1 w Q B6 J% M! N5 V
} catch (Exception e) {! d+ D3 e# |" E4 b1 }; e7 G
System.err.println ("Exception stepRule: " + e.getMessage ());, m- x) W. G0 P% { w8 Q
}
( `$ |$ C4 S! P
3 [' I f8 w- H- R1 E. Y2 r: E! J; P try {8 W' t$ g) j- j/ W9 k+ x
Heatbug proto = (Heatbug) heatbugList.get (0);; R: B+ d+ k9 R
Selector sel =
3 t! |* n0 i/ F+ D new Selector (proto.getClass (), "heatbugStep", false);
8 n- G8 f6 e) o actionForEach =
* D8 I2 W3 _" Y! c- ~4 c0 W: r modelActions.createFActionForEachHomogeneous$call
8 F) j: k3 B. c' u0 | (heatbugList,
8 `! ^! g( `' I" n& A& j) d, d new FCallImpl (this, proto, sel,
w* ]3 w5 `/ A, a! h) N% z7 | new FArgumentsImpl (this, sel)));
& @% F0 s; R/ L! g4 _# U } catch (Exception e) {+ e4 M8 T3 V T i$ B( T& \% n) y; p
e.printStackTrace (System.err);
9 ]( j9 T/ [ y1 Y6 {2 n. M: L, v }* N- m! t# G8 D% b
. K; n/ D" Q0 K" [ m/ W' J; f syncUpdateOrder ();8 _; d2 L( W6 i. G) M
! d! D# r6 M' i2 h6 B: z try {
% Q2 T, K4 a' h modelActions.createActionTo$message
) h/ R0 ~7 `8 g4 i+ i) k (heat, new Selector (heat.getClass (), "updateLattice", false));
( U- p% R; }" H$ w } catch (Exception e) {% e/ T( [. w, G% p9 e
System.err.println("Exception updateLattice: " + e.getMessage ());# [ V; U/ M8 m( |6 \8 ]
}
! Y c- q' f8 V( W `8 x1 N/ e6 F( A; X
// Then we create a schedule that executes the
# y6 y0 G( ]" y3 c D& D/ k // modelActions. modelActions is an ActionGroup, by itself it, {4 v; v, h7 O" l+ m D
// has no notion of time. In order to have it executed in1 I* Y8 b- M3 g# x d
// time, we create a Schedule that says to use the
* q2 b! A( P W. K6 h; v; r // modelActions ActionGroup at particular times. This
+ w- K [* ^0 L3 \4 o$ s // schedule has a repeat interval of 1, it will loop every- j y& W( _7 c# P& E6 b$ C
// time step. The action is executed at time 0 relative to. Z' L+ r' o8 n5 [' U" G: D
// the beginning of the loop.
. Q S# L" }, B/ p/ i1 E
' k5 x) U( k3 N! `$ g // This is a simple schedule, with only one action that is' a, I" B$ S) B& o8 ?
// just repeated every time. See jmousetrap for more. U# N* |/ Y% z6 h: ]! ?7 j
// complicated schedules.
+ f# F+ \9 I3 E, a5 O
# N1 q' o0 s. \/ M. a/ W) r modelSchedule = new ScheduleImpl (getZone (), 1);7 I; i7 z# Z: P- K
modelSchedule.at$createAction (0, modelActions);
. X7 |& S) Y7 h9 [" L0 q
/ T0 W' o. |. ]) C7 ^9 W return this;7 i5 r: u6 x2 r7 R& G9 Y
} |