HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# Q; R# y3 l! T+ y4 v h& K1 S
2 h" j3 D, b! b- p& @, B% \2 P1 N9 C
public Object buildActions () {6 V3 Y0 @/ h2 e4 r# T
super.buildActions();
7 {( M7 ]" d6 Z$ ^
`" @2 e. f u D9 v) Z // Create the list of simulation actions. We put these in
; z+ T, L1 L4 i" Y // an action group, because we want these actions to be) e% x6 N: `: o# S* D
// executed in a specific order, but these steps should- Z( N. ]' H% i4 Y1 W' {6 b
// take no (simulated) time. The M(foo) means "The message$ G0 S& Q& B0 } N9 x$ J R
// called <foo>". You can send a message To a particular
/ {$ b1 {: F- K$ p // object, or ForEach object in a collection.: a- u& h$ x. I0 R' F
$ ~: G; o7 K: b B! e B6 ^3 P // Note we update the heatspace in two phases: first run# W. h% {+ R# ^9 v# k: ~
// diffusion, then run "updateWorld" to actually enact the) N' c D8 T7 s% g
// changes the heatbugs have made. The ordering here is
6 A. G T2 S7 f5 P! O" q // significant!# ^% B, l2 Z0 L* Q$ T) x
- j- n4 p( E2 e5 f: S- ~
// Note also, that with the additional! E; a- U8 D+ j; w I# D4 m
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* K+ V8 y% g1 L4 r+ a) m0 j9 {5 y1 n // randomize the order in which the bugs actually run4 H8 x# Y8 A7 C. o; x+ e
// their step rule. This has the effect of removing any
4 }6 h- i( ` o$ g$ ~9 E9 a // systematic bias in the iteration throught the heatbug1 j! ]& q3 }% `0 O
// list from timestep to timestep' [5 w8 Y6 e0 N0 W
& o# d+ N" y/ o" }
// By default, all `createActionForEach' modelActions have
* Y- e7 T# f% F; q // a default order of `Sequential', which means that the5 u# n9 R3 p( P. z/ y1 f$ O# F
// order of iteration through the `heatbugList' will be
$ [: X4 v5 A! p. @7 l9 b$ c // identical (assuming the list order is not changed
# H% l/ V! Z* a7 s // indirectly by some other process).
7 i4 k( }6 w5 n$ y6 J6 o0 T 2 {3 @7 j" D; s. v5 z
modelActions = new ActionGroupImpl (getZone ());
3 \" M4 ^$ u. j: ]9 r6 Q7 G
; D! o1 V p( z5 b: O; ^. @ try {
6 {" X+ ^4 n( d# V modelActions.createActionTo$message
7 v) W7 [/ c3 _7 J (heat, new Selector (heat.getClass (), "stepRule", false));
8 H1 M7 _" O: x/ O } catch (Exception e) {
9 ]% l9 N/ @6 ?. I& F4 g System.err.println ("Exception stepRule: " + e.getMessage ());! @# @$ R4 s$ A) e
}
3 P1 Q, F* v- j& K" F$ q
: s4 q( y$ d5 |/ W T7 Q2 L0 o" `2 _9 L try {
" Z! H& F. b' w5 W# Y$ P Heatbug proto = (Heatbug) heatbugList.get (0);$ m# | c! n) i
Selector sel =
( e; D8 d! K3 ~; w new Selector (proto.getClass (), "heatbugStep", false);. D# P$ b! }3 u+ m, _2 R, o/ Y
actionForEach =1 c' J% H `# R3 Z8 p
modelActions.createFActionForEachHomogeneous$call
% \" Z+ d) }/ F1 | (heatbugList,+ K# G7 r, ]* e. ~
new FCallImpl (this, proto, sel,
F* _6 L$ Z: l Z0 G new FArgumentsImpl (this, sel)));# p# W. d0 l* W4 ~& M
} catch (Exception e) {$ D9 ]/ U1 {1 j0 ^8 W' D) m
e.printStackTrace (System.err);
& v1 \$ P5 f! V, T$ {% W( x" a5 E C }
' R7 m3 d8 t3 \& E2 ]# q- z
( c% p" \& r y$ Z7 P syncUpdateOrder ();
) U4 I% @" Q' } \$ L& [( F! \
) p- t3 E* h+ E3 W5 u try { [4 U7 W7 {" T7 Z" ^6 n! h
modelActions.createActionTo$message 7 w9 e9 v: S5 t3 u, x ]
(heat, new Selector (heat.getClass (), "updateLattice", false));
& _, `/ t: s! j) j# r9 C$ D9 Y } catch (Exception e) {
3 l+ G# Z/ ]* A* ~0 E: Q% Y) g System.err.println("Exception updateLattice: " + e.getMessage ());
. \! f5 @; B0 s3 _ }
/ ~: r( e# a. b, @
7 m1 q% ~9 S: H/ u a# a7 y // Then we create a schedule that executes the
- ]" m4 ~7 T6 J // modelActions. modelActions is an ActionGroup, by itself it
6 L, u( H+ Q: R, ?9 Y // has no notion of time. In order to have it executed in) W# I( L* g& \" Y9 v$ D# O9 M9 J! H; L
// time, we create a Schedule that says to use the
$ b* I& g$ a( R* {+ k( | // modelActions ActionGroup at particular times. This2 [+ L5 h$ t2 G
// schedule has a repeat interval of 1, it will loop every
" f& K& \- M8 k! q9 u // time step. The action is executed at time 0 relative to" J! S, Q1 o: _2 u8 d9 i+ k+ J$ B
// the beginning of the loop.
# n3 ?) d8 l; d" Y2 K1 R8 V
9 Z+ H/ `9 G5 F* S/ P. B // This is a simple schedule, with only one action that is
: ~# U0 `, G1 Z) _" e v. C // just repeated every time. See jmousetrap for more9 Y6 O ^6 G0 x* D y9 I
// complicated schedules.; P2 x9 d7 g6 x, B' \' a
3 ]3 o' ^5 p1 y! H. K8 ~2 a modelSchedule = new ScheduleImpl (getZone (), 1); A/ ~- K: ~8 Y4 }* @5 s5 Q
modelSchedule.at$createAction (0, modelActions);
m; K9 {2 {. b; g" _
. z% P8 p% b) {: T return this;
( Z- T/ v1 B, m } |