HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- }6 | g& A8 h* o! z) w& e3 G
/ w2 L, ^6 R: }* d s0 R I public Object buildActions () {
; }1 V* I' ~# n super.buildActions();2 ^/ k. [( _8 G+ M8 Z% Z
5 W: F/ B" B. b6 R+ M" @" V5 U // Create the list of simulation actions. We put these in; d8 e! j9 S1 z7 \' E3 c
// an action group, because we want these actions to be2 p- i" o, x$ m! b: M) J
// executed in a specific order, but these steps should2 P& f6 {* l' j# i7 m" m) M, Q- g
// take no (simulated) time. The M(foo) means "The message( {$ Y8 S. G3 }8 Q& k
// called <foo>". You can send a message To a particular
1 d$ K* I0 ?! R( I7 W // object, or ForEach object in a collection.
P8 H- J- d6 Y8 q+ J, w W ( U' U% G$ m9 o: u' X
// Note we update the heatspace in two phases: first run
. e p" V0 K9 p2 s9 X U // diffusion, then run "updateWorld" to actually enact the) u8 O( U# B: S. b. x9 P
// changes the heatbugs have made. The ordering here is
; a$ v; Y; p1 w // significant!+ j, W# U; X* S+ x( \
& w8 ^% t9 O4 t. v, k6 _* o
// Note also, that with the additional. c( G) d7 j1 X5 W8 h# w+ S1 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 I+ g/ b0 Z( v' f( `; ]2 T$ r) g
// randomize the order in which the bugs actually run
4 A9 X ]* g( u' _" y // their step rule. This has the effect of removing any% p( S% d6 c5 ?- {" y
// systematic bias in the iteration throught the heatbug
0 r* J! a* _' q3 r" V // list from timestep to timestep
# M9 A7 `) {- k& \" t
' q, t( t, P8 F/ }: ~ // By default, all `createActionForEach' modelActions have3 g8 @; i8 z9 [. G" {+ B, b0 k! R
// a default order of `Sequential', which means that the
6 B0 J$ x6 H" i5 W& | // order of iteration through the `heatbugList' will be' p- [% I, a8 V. W# }7 B& w
// identical (assuming the list order is not changed% i% `4 _" ?! d, W8 O8 q
// indirectly by some other process).
1 ~6 h. ~1 q O& @, J2 z8 U c 3 c4 K! E! }% y; s, [6 F& \
modelActions = new ActionGroupImpl (getZone ());
; A9 ~: g% `0 ?, H$ @. M/ Y
9 m% W1 B# e1 b/ p7 p/ [4 j try {
8 I& Z1 Z5 ]$ p# D modelActions.createActionTo$message
. \8 m E' e0 `4 w4 O( Q: s (heat, new Selector (heat.getClass (), "stepRule", false));2 O" L6 K' Z: o
} catch (Exception e) {
1 R0 v+ f; W# ?5 W System.err.println ("Exception stepRule: " + e.getMessage ());
1 j4 {8 W; p$ d4 ]' y }
( c$ z) {0 u/ Y4 k# e) ]% i' U9 D' T- N9 p4 H
try {
; A8 n/ I) A6 t! w' X Heatbug proto = (Heatbug) heatbugList.get (0);
- ? I( J8 r( V% S: K( w) Z0 f: P Selector sel = * n1 p# J% n, v1 t/ T
new Selector (proto.getClass (), "heatbugStep", false);
) ]! M+ H$ [8 @: \2 A actionForEach =9 P% t) r3 T9 x+ l
modelActions.createFActionForEachHomogeneous$call; h0 I* w' u& Q1 Y
(heatbugList,. G0 I5 C; }: p( W3 a4 l
new FCallImpl (this, proto, sel,
- C' f: J0 H8 @& t7 `* [. c8 b; K new FArgumentsImpl (this, sel)));
& J5 k2 v9 t0 k+ C" p1 m } catch (Exception e) {
5 } e/ D% X( ]# N e.printStackTrace (System.err);- b' c \; H% v9 ?/ e1 c/ q. D
}2 s0 B& X- `8 H" ^& g! Q
! ~/ ~4 l$ o7 Q1 d; g% _. f syncUpdateOrder ();
a7 w, h3 C. r1 k! U" c! [% G5 Q, ], t. T Q8 v
try {
0 I7 I) x7 d6 j2 |' ~5 O7 C modelActions.createActionTo$message
1 ^3 m3 C' F s1 p/ C# H (heat, new Selector (heat.getClass (), "updateLattice", false));
+ r6 m7 q" i6 ` [ } catch (Exception e) {
8 H5 z8 J# p. p& _. [" ~ System.err.println("Exception updateLattice: " + e.getMessage ());
- ^3 i4 a; C* S9 J. S7 f% x }4 B' n% b! q( z0 m: c; E. n
( N& X. d) n1 J; N // Then we create a schedule that executes the) W2 [. j& W+ W6 P* [- |6 `
// modelActions. modelActions is an ActionGroup, by itself it. }1 c. v) r+ i4 Y5 f
// has no notion of time. In order to have it executed in
# D; R- {% i* K: w // time, we create a Schedule that says to use the
0 ~9 ?) L4 ~1 o/ c7 {. X8 y: [ // modelActions ActionGroup at particular times. This
. C% Z# u5 `& ]- h" S L% f7 W // schedule has a repeat interval of 1, it will loop every
! N7 j! |% E8 @3 a3 t/ n: s8 z/ w // time step. The action is executed at time 0 relative to. |! Y8 z+ ~) @& |9 L& x# m2 W
// the beginning of the loop.9 H4 S+ v, f& q" X5 J
1 x" D+ H! i0 B8 F, P
// This is a simple schedule, with only one action that is% V' f, b' J& G& f
// just repeated every time. See jmousetrap for more% s5 h" a% C1 E1 R4 y
// complicated schedules.
. }$ O; D, |% I$ P
- |- ? C$ f" d* ` modelSchedule = new ScheduleImpl (getZone (), 1);3 k& z% z! h* N1 Y- h8 z
modelSchedule.at$createAction (0, modelActions);
/ o9 r3 C' [% D' V5 m; E
' S* y; h- A" l5 _2 z6 d0 b2 o return this;
M$ W/ }. a! R9 n! u0 g } |