HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% _( l6 ~3 t5 c h3 y
s/ t8 Q; v) T
public Object buildActions () {, _" p# P3 N. C, i2 f' W
super.buildActions();
; O$ x( }) d& A* l4 U- N H3 ^ 3 T) ~2 v( @4 J6 t: k/ @
// Create the list of simulation actions. We put these in- T) m/ j6 Y8 i4 S$ i$ c6 N/ s
// an action group, because we want these actions to be
K$ e0 x9 ?% w8 S$ b // executed in a specific order, but these steps should
/ Z" z0 K' C4 t6 i. A; k$ [% ]" V // take no (simulated) time. The M(foo) means "The message
$ T2 n' a i; e, `# A9 l$ m* m // called <foo>". You can send a message To a particular
# A2 F! D9 b! |/ m7 A // object, or ForEach object in a collection. I- |1 u( H& s$ T3 A/ k( j
( f e* v# x8 G
// Note we update the heatspace in two phases: first run1 g0 G* ? [1 @5 P0 U1 s: ^
// diffusion, then run "updateWorld" to actually enact the
1 h0 o) T4 t* u // changes the heatbugs have made. The ordering here is
% L" t8 W% w+ T( V, R // significant!) a- G/ B6 I3 U3 |
$ M- J q5 d% w. M
// Note also, that with the additional# q+ i, U8 j+ F7 [6 y& f
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, }& _, _# f8 M( P$ W4 @! s/ [* a // randomize the order in which the bugs actually run$ f9 M4 |4 j: l: b
// their step rule. This has the effect of removing any: x, ]1 U2 \- W2 F" P
// systematic bias in the iteration throught the heatbug
7 F, k+ s+ h: d; }* u$ } // list from timestep to timestep1 B" \; [. k9 Z% S" W% _1 H
. T+ [0 r. {# ]9 p l" l( p
// By default, all `createActionForEach' modelActions have
0 U) t: | k+ M' P // a default order of `Sequential', which means that the7 z8 L" ~$ A; W& V: K, x/ o
// order of iteration through the `heatbugList' will be
' ]& k' m1 k# ^ // identical (assuming the list order is not changed
0 l7 }0 I3 j* G$ J // indirectly by some other process).: f; T" d8 S" C. [0 s! a1 \
1 q" a! ?1 `. a2 W modelActions = new ActionGroupImpl (getZone ());
+ U) F7 P& u( [- J* Q! u# E, k& T
try {
V# b5 x6 @9 J! C& u modelActions.createActionTo$message7 P) Y8 B, k( R4 B7 _- k+ a
(heat, new Selector (heat.getClass (), "stepRule", false));3 a% v3 Q9 p# t
} catch (Exception e) {6 ^- i. `4 S5 s# \
System.err.println ("Exception stepRule: " + e.getMessage ());( ?0 g9 K: i0 B8 T
}
; n3 u, g+ L+ X' a$ W! k: R! I M5 D: r1 T- l
try {0 O+ a6 u- M: t# I
Heatbug proto = (Heatbug) heatbugList.get (0);
0 \9 ^. |. m+ {. d Selector sel =
: f# \5 m' }. Y* ? new Selector (proto.getClass (), "heatbugStep", false);
7 ^# @& h; \$ v! |* U' r4 Z% g actionForEach =
7 ^3 [2 F8 Q' u' O0 }; ~ modelActions.createFActionForEachHomogeneous$call1 i; r! {2 n, s, J! H0 p; r
(heatbugList,
) _, p+ {4 y7 k7 h# Y new FCallImpl (this, proto, sel,
0 @: \( H1 ?6 K2 }3 |, B i new FArgumentsImpl (this, sel)));
1 {. \+ \8 H' R0 K5 u; V } catch (Exception e) {
/ G% _, `; E f1 P. h e.printStackTrace (System.err);
* p2 X7 l4 Z0 R- R }; V5 O. v" E" _6 z
3 e6 C3 I0 T; g0 ^7 k syncUpdateOrder ();4 u' ]1 Z2 u/ N6 ^: T$ @1 J! N0 W
( h' C0 h. T3 L- k
try {" ]9 E- P% a6 g; l- B0 H! K. t
modelActions.createActionTo$message
' z& h1 E6 ?2 _6 ^% p3 N8 u, M (heat, new Selector (heat.getClass (), "updateLattice", false));8 T& F f, F. F" Q! U- ] ?) Z! H
} catch (Exception e) {
- R; }3 U% |4 @. J System.err.println("Exception updateLattice: " + e.getMessage ());6 a( x9 O0 h7 J9 f' F9 `4 J
}4 Q9 S4 E5 ?/ r! a6 T5 C
4 Y& s" d3 k/ O, H, J" m // Then we create a schedule that executes the
: L0 G& V4 c( T- n0 [* Q1 } // modelActions. modelActions is an ActionGroup, by itself it
: p5 i, u4 |; Y$ d, ^ // has no notion of time. In order to have it executed in! F* v" a4 b+ n, w
// time, we create a Schedule that says to use the
& ^5 u' e1 d0 I2 [ // modelActions ActionGroup at particular times. This
~: ^- p; x/ q1 z1 o3 y; q // schedule has a repeat interval of 1, it will loop every
1 u, F/ @5 b- v. B; V: |% Q) B // time step. The action is executed at time 0 relative to
2 u/ y8 y5 S; }, k, h& B ^ // the beginning of the loop.
V, T$ r- I: d& j% P/ G0 c5 c$ U0 `7 | u1 E J: a, w! |$ H$ w1 G
// This is a simple schedule, with only one action that is7 B# `4 y; f# E* v: S
// just repeated every time. See jmousetrap for more! g9 |3 F, X1 D1 j& h
// complicated schedules.: P1 k% k. n4 z1 f6 h4 }7 g
0 @) T* G! \* Y1 |
modelSchedule = new ScheduleImpl (getZone (), 1);
: l/ q8 Z. K: j2 i4 d modelSchedule.at$createAction (0, modelActions);) Q! Z3 F7 m9 G* Y
7 Q: d1 q- e$ B% _ return this;+ q3 Q& b0 s6 V3 r* p' k
} |