HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- y; i$ w) g7 Z( m/ _- P( s, r+ x0 V7 l0 x. V
public Object buildActions () {! R! ^- Y5 s7 H/ X) k g
super.buildActions();- p2 M' g5 M& ?7 m
3 ^. `; q$ X! w* ?. }- J T i
// Create the list of simulation actions. We put these in
1 ~8 J, V1 e$ F" d7 R) ^7 o // an action group, because we want these actions to be
# L9 Z( {! c5 m5 ~; y // executed in a specific order, but these steps should
+ Q( P* t/ l& d" B# P& W; C& e // take no (simulated) time. The M(foo) means "The message: M% }% Z1 L2 n8 a
// called <foo>". You can send a message To a particular
4 [5 U+ B# n0 V$ R$ S i // object, or ForEach object in a collection.
5 @5 |4 g" Z# C) y4 l
, t% N$ d$ |7 P) W( a // Note we update the heatspace in two phases: first run
2 r1 K7 a' r" b T // diffusion, then run "updateWorld" to actually enact the
5 v' A/ l, M1 J4 |$ ~0 f8 C* o; N6 U" w // changes the heatbugs have made. The ordering here is1 ~0 @: u; w% m; @, a
// significant!
# t4 n8 G% S# U5 E$ O2 C7 W
( \8 L' y1 c' u! E // Note also, that with the additional
( P2 O& k9 d4 P7 ?2 ~* j/ `; {# L // `randomizeHeatbugUpdateOrder' Boolean flag we can' `+ s8 v! ]/ U! s" D
// randomize the order in which the bugs actually run
2 X5 R6 \! Z& x2 s8 q/ X% f: t // their step rule. This has the effect of removing any8 Y, ^0 ?# F/ t' O
// systematic bias in the iteration throught the heatbug2 A$ I v& k. Q2 ?& _3 M% Q! d
// list from timestep to timestep
6 Q6 J" X& O; ^% d9 J5 w
; q" M% i2 Z1 { // By default, all `createActionForEach' modelActions have
( o/ Y8 W) |! d. G3 D' l // a default order of `Sequential', which means that the
* ?9 z: y- E, x {5 q% ` // order of iteration through the `heatbugList' will be
) }- \7 r Z# h0 H. | // identical (assuming the list order is not changed
) a* o: \3 @8 Z; ] // indirectly by some other process).
- m/ U6 l& K @" J5 ^1 M: w" x
+ x' ~* V3 I. Y; x% G3 d3 _ modelActions = new ActionGroupImpl (getZone ());
+ x$ k* l) e9 p3 J% C) M
1 Z$ b l8 v0 J4 ^7 B! ~6 @ try {, Q9 v& N8 q$ y" {2 S- O6 Q8 y
modelActions.createActionTo$message
; x! b9 V* a5 n& A* d (heat, new Selector (heat.getClass (), "stepRule", false));
$ P, H9 t0 P$ B$ }- m! f4 e } catch (Exception e) {
$ ]5 y' H9 i: @) d. x& q! i3 P System.err.println ("Exception stepRule: " + e.getMessage ());4 q! F4 e& J# L+ L# Y- @% T
}7 b0 z+ u9 h Z6 [5 U! Y* E
) W$ |) u" s9 B' A1 q4 \+ x5 W* y' i5 t
try {
' s8 b- _: r; m* ^/ T4 P Heatbug proto = (Heatbug) heatbugList.get (0);( F4 q) m: Y0 `1 `8 S
Selector sel = 1 d' X: Q6 Z3 }( h# L* x
new Selector (proto.getClass (), "heatbugStep", false);
f, C/ `( z8 Q( d. R. [# O! w actionForEach =
' @; m. J$ J/ `8 C modelActions.createFActionForEachHomogeneous$call
& A% T" }# W% }0 a( a. O& ~ (heatbugList,. s+ b# r" t, _7 }
new FCallImpl (this, proto, sel,
1 T' N+ e ?) B+ r new FArgumentsImpl (this, sel)));3 Z. X5 G) k# z
} catch (Exception e) {7 r) n+ V2 f; S
e.printStackTrace (System.err);
S/ q! X/ _! k/ \, o6 K% C } t# d3 W4 }& ?" M7 n8 a
% C. z4 k3 N q6 i: H/ T7 M. x* h
syncUpdateOrder ();1 M, G1 c! O1 ^5 D, o
: E# W2 J% c) u5 J. |7 |9 z; C- d
try {
( Z3 |# K( ^; S" j; M t* U modelActions.createActionTo$message
; V5 `& J% p' x9 f: N7 j (heat, new Selector (heat.getClass (), "updateLattice", false));
; `" l \/ N& O: f4 o } catch (Exception e) {
$ c4 i* b8 A; V3 {! { System.err.println("Exception updateLattice: " + e.getMessage ());8 M, q+ C/ t7 Y: d5 b
}
; _3 o( M5 G. L: L3 i$ Y! ? * N" V; c9 g0 |' m
// Then we create a schedule that executes the
5 ?( Y- u! {" X$ ~0 r0 s // modelActions. modelActions is an ActionGroup, by itself it& M( D$ B/ w) {. `& ]
// has no notion of time. In order to have it executed in
; a7 x$ G* r1 h( X& M // time, we create a Schedule that says to use the
9 z; m! Z8 L/ h% M // modelActions ActionGroup at particular times. This
; G/ m, l2 q0 B+ {) n2 h: L // schedule has a repeat interval of 1, it will loop every
; D& d: K h: w // time step. The action is executed at time 0 relative to
: ?3 V: E3 F* t( X+ \ // the beginning of the loop.
5 X' T" U5 w, A0 C4 t3 v( x! ?
6 b: ]/ w9 S: K+ o z* b. w/ F // This is a simple schedule, with only one action that is
" _' P& Y; W& E3 c( O // just repeated every time. See jmousetrap for more
# `( `! l9 }3 F# {# o2 H5 n, J // complicated schedules.
* L: v$ c& h$ Q
( g8 H8 x6 E: L# E; e1 C6 s0 R- l modelSchedule = new ScheduleImpl (getZone (), 1);
. c8 j: |% X |# D1 l modelSchedule.at$createAction (0, modelActions);+ o$ \8 U8 X- e8 u: H% t
+ p7 P$ @& ^* e; h( i% ]5 u
return this;
' _' I& K2 [/ B& g& l' b2 B } |