-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuModelSession.pas
More file actions
409 lines (338 loc) · 11.5 KB
/
Copy pathuModelSession.pas
File metadata and controls
409 lines (338 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
unit uModelSession;
{ TModelSession owns the single TRoadRunner instance and tracks whether the
model is loaded and whether the Antimony source has been edited since the
last load.
Two broadcast events, each supporting any number of subscribers via
Add/Remove listener methods:
State-changed listeners (TNotifyEvent)
Fire on transitions of IsLoaded or IsDirty. Used for UI affordances
like the caption, a "model loaded" status indicator, or a frame
enabling / disabling compute buttons based on model state.
Model-reloaded listeners (TModelReloadedEvent)
Fire after EnsureLoaded successfully parses and loads the model.
AParameterSetChanged is True when the sorted set of global parameter
names differs from the previous load (or this is the first load).
Used to decide whether sliders attached to the previous parameter set
need to be cleared (structural change) or merely refreshed in place
(compatible edit). Any frame that caches model-derived data and
wants to invalidate on reload subscribes here.
Listener semantics:
* Add ignores duplicate registrations (the same method pointer can be
added only once).
* Remove does nothing if the handler isn't registered.
* Dispatch iterates a snapshot, so a listener may safely add or remove
listeners during firing without disturbing the current pass. }
interface
uses
System.SysUtils, System.Classes, System.Generics.Collections,
uRoadRunner;
type
TGetTextEvent = function: string of object;
TModelReloadedEvent = procedure(Sender: TObject;
AParameterSetChanged: Boolean) of object;
TModelSession = class
private
FRoadRunner: TRoadRunner;
FIsLoaded: Boolean;
FIsDirty: Boolean;
FLastError: string;
FLastParameterSig: string;
FStateListeners: TList<TNotifyEvent>;
FReloadedListeners: TList<TModelReloadedEvent>;
FOnNeedAntimonyText: TGetTextEvent;
procedure SetIsLoaded(AValue: Boolean);
procedure SetIsDirty(AValue: Boolean);
procedure DoStateChanged;
procedure DoModelReloaded(AParameterSetChanged: Boolean);
function ComputeParameterSignature: string;
function FindStateListener(const AHandler: TNotifyEvent): Integer;
function FindReloadedListener(const AHandler: TModelReloadedEvent): Integer;
public
constructor Create;
destructor Destroy; override;
procedure MarkDirty;
procedure ClearDirty;
function EnsureLoaded: Boolean;
procedure Unload;
function GetParameterNames: TArray<string>;
function GetParameterValues: TArray<Double>;
procedure SetParameterValue(const AName: string; AValue: Double);
function GetTunableNames: TArray<string>; { globals + boundary species }
function GetTunableValues: TArray<Double>;
procedure AddStateListener(const AHandler: TNotifyEvent);
procedure RemoveStateListener(const AHandler: TNotifyEvent);
procedure AddReloadedListener(const AHandler: TModelReloadedEvent);
procedure RemoveReloadedListener(const AHandler: TModelReloadedEvent);
function GetCurrentAntimonyText: string;
property RoadRunner: TRoadRunner read FRoadRunner;
property IsLoaded: Boolean read FIsLoaded;
property IsDirty: Boolean read FIsDirty;
property LastError: string read FLastError;
property OnNeedAntimonyText: TGetTextEvent
read FOnNeedAntimonyText write FOnNeedAntimonyText;
end;
implementation
uses
uAntimonyAPI, uCommonTypes, IOUtils;
{ ── method-pointer equality ──────────────────────────────────────────────── }
function MethodsEqual(const A, B: TMethod): Boolean; inline;
begin
Result := (A.Code = B.Code) and (A.Data = B.Data);
end;
{ ── construction ─────────────────────────────────────────────────────────── }
constructor TModelSession.Create;
begin
inherited Create;
FRoadRunner := TRoadRunner.Create;
FIsLoaded := False;
FIsDirty := False;
FLastParameterSig := '';
FStateListeners := TList<TNotifyEvent>.Create;
FReloadedListeners := TList<TModelReloadedEvent>.Create;
end;
destructor TModelSession.Destroy;
begin
FStateListeners.Free;
FReloadedListeners.Free;
FRoadRunner.Free;
inherited;
end;
{ ── listener registration ────────────────────────────────────────────────── }
function TModelSession.FindStateListener(const AHandler: TNotifyEvent): Integer;
var
I: Integer;
begin
for I := 0 to FStateListeners.Count - 1 do
if MethodsEqual(TMethod(FStateListeners[I]), TMethod(AHandler)) then
Exit(I);
Result := -1;
end;
function TModelSession.FindReloadedListener(
const AHandler: TModelReloadedEvent): Integer;
var
I: Integer;
begin
for I := 0 to FReloadedListeners.Count - 1 do
if MethodsEqual(TMethod(FReloadedListeners[I]), TMethod(AHandler)) then
Exit(I);
Result := -1;
end;
procedure TModelSession.AddStateListener(const AHandler: TNotifyEvent);
begin
if not Assigned(AHandler) then Exit;
if FindStateListener(AHandler) < 0 then
FStateListeners.Add(AHandler);
end;
procedure TModelSession.RemoveStateListener(const AHandler: TNotifyEvent);
var
Idx: Integer;
begin
Idx := FindStateListener(AHandler);
if Idx >= 0 then
FStateListeners.Delete(Idx);
end;
procedure TModelSession.AddReloadedListener(
const AHandler: TModelReloadedEvent);
begin
if not Assigned(AHandler) then Exit;
if FindReloadedListener(AHandler) < 0 then
FReloadedListeners.Add(AHandler);
end;
procedure TModelSession.RemoveReloadedListener(
const AHandler: TModelReloadedEvent);
var
Idx: Integer;
begin
Idx := FindReloadedListener(AHandler);
if Idx >= 0 then
FReloadedListeners.Delete(Idx);
end;
{ ── dispatch (snapshot-based, safe under concurrent add/remove) ──────────── }
procedure TModelSession.DoStateChanged;
var
Snapshot: TArray<TNotifyEvent>;
I: Integer;
begin
Snapshot := FStateListeners.ToArray;
for I := 0 to High(Snapshot) do
Snapshot[I](Self);
end;
procedure TModelSession.DoModelReloaded(AParameterSetChanged: Boolean);
var
Snapshot: TArray<TModelReloadedEvent>;
I: Integer;
begin
Snapshot := FReloadedListeners.ToArray;
for I := 0 to High(Snapshot) do
Snapshot[I](Self, AParameterSetChanged);
end;
{ ── state plumbing ───────────────────────────────────────────────────────── }
procedure TModelSession.SetIsLoaded(AValue: Boolean);
begin
if FIsLoaded = AValue then Exit;
FIsLoaded := AValue;
DoStateChanged;
end;
procedure TModelSession.SetIsDirty(AValue: Boolean);
begin
if FIsDirty = AValue then Exit;
FIsDirty := AValue;
DoStateChanged;
end;
procedure TModelSession.MarkDirty;
begin
SetIsDirty(True);
end;
procedure TModelSession.ClearDirty;
begin
SetIsDirty(False);
end;
{ ── parameter-set signature ──────────────────────────────────────────────── }
function TModelSession.ComputeParameterSignature: string;
var
Names: TArray<string>;
Lst: TStringList;
I: Integer;
begin
if not FIsLoaded then Exit('');
Names := GetTunableNames;
Lst := TStringList.Create;
try
Lst.Sorted := True;
for I := 0 to High(Names) do
Lst.Add(Names[I]);
Result := Lst.CommaText;
finally
Lst.Free;
end;
end;
{ ── load / unload ────────────────────────────────────────────────────────── }
function TModelSession.EnsureLoaded: Boolean;
var
AntText: string;
SbmlInfo: TModelErrorState;
NewSig: string;
ParamSetChanged: Boolean;
FirstLoad: Boolean;
begin
if FIsLoaded and (not FIsDirty) then Exit(True);
if not Assigned(FOnNeedAntimonyText) then
begin
FLastError := 'No text source wired to session.';
Exit(False);
end;
AntText := FOnNeedAntimonyText;
if Trim(AntText) = '' then
begin
FLastError := 'Antimony source is empty.';
Exit(False);
end;
try
SbmlInfo := getSBMLFromAntimony(AnsiString(AntText));
if not SbmlInfo.ok then
raise Exception.Create('Antimony parse failed: ' + SbmlInfo.errMsg);
if not FRoadRunner.loadSBMLFromString(SbmlInfo.sbmlStr) then
raise Exception.Create('RoadRunner: ' + FRoadRunner.getLastError);
FLastError := '';
FIsDirty := False;
FIsLoaded := True;
DoStateChanged;
{ Compare parameter-set signatures to classify the reload. The first
successful load is treated as "parameter set changed" since there
were no prior sliders to preserve anyway. }
NewSig := ComputeParameterSignature;
FirstLoad := (FLastParameterSig = '');
ParamSetChanged := FirstLoad or (NewSig <> FLastParameterSig);
FLastParameterSig := NewSig;
DoModelReloaded(ParamSetChanged);
Result := True;
except
on E: Exception do
begin
FLastError := E.Message;
FLastParameterSig := '';
SetIsLoaded(False);
raise;
end;
end;
end;
procedure TModelSession.Unload;
begin
FLastParameterSig := '';
SetIsLoaded(False);
end;
{ ── parameter helpers ────────────────────────────────────────────────────── }
function TModelSession.GetParameterNames: TArray<string>;
var
Ids: TStringList;
I: Integer;
begin
if not FIsLoaded then Exit(nil);
Ids := FRoadRunner.getGlobalParameterIds;
try
SetLength(Result, Ids.Count);
for I := 0 to Ids.Count - 1 do
Result[I] := Ids[I];
finally
Ids.Free;
end;
end;
function TModelSession.GetParameterValues: TArray<Double>;
var
N, I: Integer;
begin
if not FIsLoaded then Exit(nil);
N := FRoadRunner.GetNumberOfGlobalParameters;
SetLength(Result, N);
for I := 0 to N - 1 do
Result[I] := FRoadRunner.getGlobalParameterByIndex(I);
end;
procedure TModelSession.SetParameterValue(const AName: string; AValue: Double);
begin
if FIsLoaded then
FRoadRunner.SetValue(AName, AValue);
end;
function TModelSession.GetCurrentAntimonyText: string;
begin
if Assigned(FOnNeedAntimonyText) then
Result := FOnNeedAntimonyText
else
Result := '';
end;
function TModelSession.GetTunableNames: TArray<string>;
var
Ids: TStringList;
P: TArray<string>;
I, N: Integer;
begin
if not FIsLoaded then Exit(nil);
P := GetParameterNames;
Ids := FRoadRunner.getBoundarySpeciesIds;
try
SetLength(Result, Length(P) + Ids.Count);
for I := 0 to High(P) do
Result[I] := P[I];
N := Length(P);
for I := 0 to Ids.Count - 1 do
Result[N + I] := Ids[I];
finally
Ids.Free;
end;
end;
function TModelSession.GetTunableValues: TArray<Double>;
var
V: TArray<Double>;
I, N: Integer;
NB: Integer;
begin
if not FIsLoaded then Exit(nil);
V := GetParameterValues;
NB := FRoadRunner.GetNumberOfBoundarySpecies;
SetLength(Result, Length(V) + NB);
for I := 0 to High(V) do
Result[I] := V[I];
N := Length(V);
for I := 0 to NB - 1 do
Result[N + I] := FRoadRunner.getBoundarySpeciesByIndex(I);
end;
end.