This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogical_id_condition.pas
313 lines (256 loc) · 7.22 KB
/
logical_id_condition.pas
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
{ This file is a part of Map editor for VCMI project
Copyright (C) 2015-2017 Alexander Shishkin [email protected]
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit logical_id_condition;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, fpjson, editor_classes;
type
{ TLogicalIDCondition }
TLogicalIDCondition = class(TPersistent, ISerializeNotify, IReferenceNotify, IFPObserver)
private
FOwner: IReferenceNotify;
FAllOf: TStrings;
FAnyOf: TStrings;
FNoneOf: TStrings;
function isAllOfStored: Boolean;
function isAnyOfStored: Boolean;
function isNoneOfStored: Boolean;
procedure SetAllOf(AValue: TStrings);
procedure SetAnyOf(AValue: TStrings);
procedure SetNoneOf(AValue: TStrings);
public
constructor Create(AOwner: IReferenceNotify);
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Minimize;
procedure Clear;
function IsAllowed(AId: AnsiString): Boolean;
function IsRequired(AId: AnsiString): Boolean;
function IsEmpty: Boolean;
function IsPermissive: Boolean;
procedure SetPermissive(AFullList: TStrings; const AValue: Boolean);
procedure SetPermissive(AFullList: THashedCollection; const AValue: Boolean);
public //ISerializeNotify
procedure AfterSerialize(Sender:TObject; AData: TJSONData);
procedure BeforeSerialize(Sender:TObject);
procedure AfterDeSerialize(Sender:TObject; AData: TJSONData);
procedure BeforeDeSerialize(Sender:TObject; AData: TJSONData);
public //IFPObserver
procedure FPOObservedChanged(ASender: TObject; Operation: TFPObservedOperation; Data: Pointer);
public //IReferenceNotify
procedure NotifyReferenced(AOldIdentifier, ANewIdentifier: AnsiString);
published
property AnyOf: TStrings read FAnyOf write SetAnyOf stored isAnyOfStored;
property AllOf: TStrings read FAllOf write SetAllOf stored isAllOfStored;
property NoneOf: TStrings read FNoneOf write SetNoneOf stored isNoneOfStored;
end;
implementation
{ TLogicalIDCondition }
procedure TLogicalIDCondition.SetAllOf(AValue: TStrings);
begin
if FAllOf=AValue then Exit;
FAllOf:=AValue;
end;
function TLogicalIDCondition.isAllOfStored: Boolean;
begin
Result := FAllOf.Count >0;
end;
function TLogicalIDCondition.isAnyOfStored: Boolean;
begin
Result := FAnyOf.Count >0;
end;
function TLogicalIDCondition.isNoneOfStored: Boolean;
begin
Result := FNoneOf.Count >0;
end;
procedure TLogicalIDCondition.SetAnyOf(AValue: TStrings);
begin
if FAnyOf=AValue then Exit;
FAnyOf:=AValue;
end;
procedure TLogicalIDCondition.SetNoneOf(AValue: TStrings);
begin
if FNoneOf=AValue then Exit;
FNoneOf:=AValue;
end;
constructor TLogicalIDCondition.Create(AOwner: IReferenceNotify);
begin
FOwner := AOwner;
FAllOf := TIdentifierSet.Create(Self);
FAllOf.FPOAttachObserver(Self);
FAnyOf := TIdentifierSet.Create(Self);
FAnyOf.FPOAttachObserver(Self);
FNoneOf := TIdentifierSet.Create(Self);
FNoneOf.FPOAttachObserver(Self);
end;
destructor TLogicalIDCondition.Destroy;
begin
FAnyOf.Free;
FAllOf.Free;
FNoneOf.Free;
inherited Destroy;
end;
procedure TLogicalIDCondition.Assign(Source: TPersistent);
var
src:TLogicalIDCondition;
begin
if Source is TLogicalIDCondition then
begin
src := TLogicalIDCondition(Source);
AllOf.Assign(src.AllOf);
AnyOf.Assign(src.AnyOf);
NoneOf.Assign(src.NoneOf);
end
else
begin
inherited Assign(Source);
end;
end;
procedure TLogicalIDCondition.Minimize;
var
s: String;
idx: Integer;
begin
for s in FNoneOf do
begin
idx := FAnyOf.IndexOf(s);
if idx >=0 then
begin
FAnyOf.Delete(idx);
end;
idx := FAllOf.IndexOf(s);
if idx >=0 then
begin
FAllOf.Delete(idx);
end;
end;
for s in FAllOf do
begin
idx := FAnyOf.IndexOf(s);
if idx >=0 then
begin
FAnyOf.Delete(idx);
end;
end;
end;
procedure TLogicalIDCondition.Clear;
begin
FAllOf.Clear;
FAnyOf.Clear;
FNoneOf.Clear;
end;
function TLogicalIDCondition.IsAllowed(AId: AnsiString): Boolean;
begin
Result := (FNoneOf.IndexOf(AId)<0)
and ((FAllOf.Count = 0) or (FAllOf.IndexOf(AId) >=0))
and ((FAnyOf.Count = 0) or (FAnyOf.IndexOf(AId) >=0));
end;
function TLogicalIDCondition.IsRequired(AId: AnsiString): Boolean;
begin
Result := (FNoneOf.IndexOf(AId) < 0) and (FAllOf.IndexOf(AId) >= 0);
end;
function TLogicalIDCondition.IsEmpty: Boolean;
begin
Result := (FAnyOf.Count = 0) and (FAllOf.Count = 0) and (FNoneOf.Count = 0);
end;
function TLogicalIDCondition.IsPermissive: Boolean;
begin
Result := (FAnyOf.Count = 0) and (FAllOf.Count = 0);
end;
procedure TLogicalIDCondition.SetPermissive(AFullList: TStrings; const AValue: Boolean);
var
idx: Integer;
begin
if AValue then
begin
//anyOf -> noneOf
for idx := 0 to AFullList.Count - 1 do
begin
if AnyOf.IndexOf(AFullList[idx]) < 0 then
begin
NoneOf.Add(AFullList[idx]);
end;
end;
end
else
begin
//noneOf -> anyOf
for idx := 0 to AFullList.Count - 1 do
begin
if NoneOf.IndexOf(AFullList[idx]) < 0 then
begin
AnyOf.Add(AFullList[idx]);
end;
end;
end;
end;
procedure TLogicalIDCondition.SetPermissive(AFullList: THashedCollection; const AValue: Boolean);
var
idx: Integer;
id: String;
begin
if AValue then
begin
//anyOf -> noneOf
for idx := 0 to AFullList.Count - 1 do
begin
id := TNamedCollectionItem(AFullList.Items[idx]).Identifier;
if AnyOf.IndexOf(id) < 0 then
begin
NoneOf.Add(id);
end;
end;
end
else
begin
//noneOf -> anyOf
for idx := 0 to AFullList.Count - 1 do
begin
id := TNamedCollectionItem(AFullList.Items[idx]).Identifier;
if NoneOf.IndexOf(id) < 0 then
begin
AnyOf.Add(id);
end;
end;
end;
end;
procedure TLogicalIDCondition.AfterSerialize(Sender: TObject; AData: TJSONData);
begin
end;
procedure TLogicalIDCondition.BeforeSerialize(Sender: TObject);
begin
Minimize;
end;
procedure TLogicalIDCondition.AfterDeSerialize(Sender: TObject; AData: TJSONData
);
begin
Minimize;
end;
procedure TLogicalIDCondition.BeforeDeSerialize(Sender: TObject;
AData: TJSONData);
begin
Clear;
end;
procedure TLogicalIDCondition.FPOObservedChanged(ASender: TObject;
Operation: TFPObservedOperation; Data: Pointer);
begin
FPONotifyObservers(Self, Operation, Data);
end;
procedure TLogicalIDCondition.NotifyReferenced(AOldIdentifier,
ANewIdentifier: AnsiString);
begin
if Assigned(FOwner) then
FOwner.NotifyReferenced(AOldIdentifier, ANewIdentifier);
end;
end.