HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 S+ d5 p+ U" \( k1 ?, q/ E! p& {% V' A8 }3 s
public Object buildActions () {
" s" ]% e9 P3 Z1 ?2 b. ^- l super.buildActions();0 u X9 n! l/ @1 U' `
$ l- ^& d+ a1 F! z3 {& {! j
// Create the list of simulation actions. We put these in0 V# r% | l; l1 ^
// an action group, because we want these actions to be
; U0 r5 f1 e" _3 q // executed in a specific order, but these steps should% y; L8 X( x. b& s
// take no (simulated) time. The M(foo) means "The message
3 `! R U* G- r' c. u1 f6 p // called <foo>". You can send a message To a particular C' C! G* z8 G1 o( G Z: K
// object, or ForEach object in a collection.* U- j& x( m7 [. d0 n
( d$ ?$ q ]0 J: {: ^$ D9 O9 I s
// Note we update the heatspace in two phases: first run
9 }9 l% O& m5 X, |/ T0 v // diffusion, then run "updateWorld" to actually enact the9 P, j) ?1 `5 c# T4 R% q
// changes the heatbugs have made. The ordering here is( m1 d5 m9 l8 n, `
// significant!) ?! V# Y& ?* A+ p$ x6 P8 |2 s
2 ~- Q' v* I$ Z, M7 I1 ]
// Note also, that with the additional2 n* g, Z7 v, Z' ]; w. \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* W7 V) p1 e2 A+ a: k // randomize the order in which the bugs actually run/ U2 X4 y9 m9 y% B3 ~3 ]! e
// their step rule. This has the effect of removing any# ?, c, c3 F& ]7 P2 a
// systematic bias in the iteration throught the heatbug
$ M: w9 t' D+ x' f // list from timestep to timestep
. m+ K+ z: E7 \7 D
5 D% S" g- \$ P: I! Q* j. L // By default, all `createActionForEach' modelActions have* c& j, R. W* n- y+ W+ _
// a default order of `Sequential', which means that the
0 W+ U% r9 J7 X% g$ W+ k" [. { // order of iteration through the `heatbugList' will be$ I7 j) N; l* e6 n
// identical (assuming the list order is not changed
! l6 F0 G: N" v. z // indirectly by some other process).
3 H# E' ]3 S: @' E+ K: j; \* x i! p+ V
2 e1 }! _8 s1 Z) @! n& ] modelActions = new ActionGroupImpl (getZone ());
# O$ V. g0 [/ |! [9 X2 F [% X2 V. N2 @3 [$ \) P- ]+ M2 g
try {- b# n0 x+ Q. F8 L, y. u$ r, x
modelActions.createActionTo$message3 ^ ]3 h3 k" n- P2 j
(heat, new Selector (heat.getClass (), "stepRule", false));
0 X- X) e2 [. `/ Q* K1 \ } catch (Exception e) {
7 ?0 R/ B$ ?9 D/ ~3 Z) R System.err.println ("Exception stepRule: " + e.getMessage ()); A& c) U$ Q. s5 A; S
}
- Q; A, l! m* s7 {* E1 I1 L8 w2 E8 {# g6 v P
try {: V/ Q- I: B* m- `! u
Heatbug proto = (Heatbug) heatbugList.get (0);" ?1 n3 `" k4 `6 Q- U$ h: ?
Selector sel =
, H, j- b+ a& X0 P8 c* D8 n2 f& v5 [* D new Selector (proto.getClass (), "heatbugStep", false);
* i R2 I+ ], K, S8 i- k0 c actionForEach =6 f8 \" Q3 Z$ @) T3 `! I z
modelActions.createFActionForEachHomogeneous$call
3 I8 ^" B* ^% I* T2 L6 |# M, O (heatbugList,9 V0 h" l T. h! C
new FCallImpl (this, proto, sel,2 _/ M4 z5 s# a+ j. B/ _9 w
new FArgumentsImpl (this, sel)));% Y$ H) |/ g6 T" ] T9 ~
} catch (Exception e) {5 e, b+ r5 r, p1 B. g& G
e.printStackTrace (System.err);
f$ W1 F& Z! f$ T8 F2 Q- G5 h }
) C* r- |% e; H6 f% t4 P* B( b
, h% J. w6 S7 s2 y, t5 m syncUpdateOrder ();
% N8 N; K$ c' o6 t5 |" n. _' D( j9 V# |$ q& l3 f) ~3 p
try {; i. z0 J' i3 \ T
modelActions.createActionTo$message 1 }- @2 E8 C. J$ |5 C( f
(heat, new Selector (heat.getClass (), "updateLattice", false));9 Y" y8 U w/ H8 P
} catch (Exception e) {6 {* q' o" X' N* c+ k3 H
System.err.println("Exception updateLattice: " + e.getMessage ());$ v2 ?: E/ Q- e0 V7 p# ~
}2 |7 s: V+ X9 L2 Y
" J2 O! x% Z! u) _; [. Y
// Then we create a schedule that executes the
/ E3 N; y# z; C3 H0 H6 B8 z3 ~7 z% W // modelActions. modelActions is an ActionGroup, by itself it( X3 J9 b+ }: p5 D$ W
// has no notion of time. In order to have it executed in$ M8 B) A2 N8 P8 K* l; \$ q0 ?
// time, we create a Schedule that says to use the* F& h( R0 r2 P$ j
// modelActions ActionGroup at particular times. This
6 K0 u/ F$ Z* K, ~6 z // schedule has a repeat interval of 1, it will loop every
. R9 z% d# W" v% O/ c/ o; A% ~. H // time step. The action is executed at time 0 relative to
$ u; @$ ?6 |2 @& b // the beginning of the loop. w0 F- U3 @1 ]' W( m
) L4 ]; [3 m* g: \" Y
// This is a simple schedule, with only one action that is* Q- H! o9 `0 V- A! {# ]
// just repeated every time. See jmousetrap for more
8 V7 V: Q# o4 P- ]7 P5 J // complicated schedules.% M5 L' a, [# N& ~
0 Q7 |3 a# h6 a# S8 n' v. z modelSchedule = new ScheduleImpl (getZone (), 1);- f0 c1 L: ^3 c+ O* X, j
modelSchedule.at$createAction (0, modelActions);! E# m8 z; x# a k1 T
# |$ T4 e/ `1 w) J2 g return this;. v; B- W3 m3 W% `
} |