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_expression.pas
235 lines (196 loc) · 5.92 KB
/
logical_expression.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
{ This file is a part of Map editor for VCMI project
Copyright (C) 2015-2017 Alexander Shishkin alexvins@users.sourceforge.net
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_expression;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, editor_classes, fpjson, vcmi_json;
type
TLogicalOperator = (nop, anyOf, allOf, noneOf);
type
TLogicalExpressionItem = class;
TEvaluate = function(AExpression: TLogicalExpressionItem) : boolean of object;
TLogicalExpression = class;
TLogicalExpressionClass = class of TLogicalExpression;
{ TLogicalExpressionItem }
TLogicalExpressionItem = class (TCollectionItem)
private
FSubExpressions: TLogicalExpression;
FLogicalOperator: TLogicalOperator;
procedure SetLogicalOperator(AValue: TLogicalOperator);
protected
procedure AssignTo(Dest: TPersistent); override;
class function GetSubExpressionsClass():TLogicalExpressionClass; virtual;
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
property LogicalOperator: TLogicalOperator read FLogicalOperator write SetLogicalOperator;
property SubExpressions:TLogicalExpression read FSubExpressions;
function Evaluate(ACallback: TEvaluate): Boolean;
end;
TLogicalExpression = class (TCollection, IArrayCollection, ISerializeSpecial)
private
FIsRoot: Boolean;
protected
function IsRoot:Boolean;
public
constructor Create(AItemClass: TCollectionItemClass); virtual;
constructor CreateRoot(AItemClass: TCollectionItemClass);
procedure Assign(Source: TPersistent); override;
//ISerializeSpecial
function Serialize(AHandler: TVCMIJSONStreamer): TJSONData;
procedure Deserialize(AHandler: TVCMIJSONDestreamer; ASrc: TJSONData);
function Evaluate(ACallback: TEvaluate): Boolean;
end;
implementation
{ TLogicalExpression }
function TLogicalExpression.IsRoot: Boolean;
begin
Result := FIsRoot;
end;
constructor TLogicalExpression.Create(AItemClass: TCollectionItemClass);
begin
Inherited Create(AItemClass);
FIsRoot:=false;
end;
constructor TLogicalExpression.CreateRoot(AItemClass: TCollectionItemClass);
begin
Create(AItemClass);
FIsRoot:=true;
end;
procedure TLogicalExpression.Assign(Source: TPersistent);
begin
if Source is TLogicalExpression then
begin
FIsRoot := TLogicalExpression(Source).FIsRoot;
end;
inherited Assign(Source);
end;
function TLogicalExpression.Serialize(AHandler: TVCMIJSONStreamer): TJSONData;
begin
if IsRoot then
begin
if Count = 1 then
begin
Result := AHandler.ObjectToJsonEx(Items[0]);
end
else begin
Result := CreateJSONArray([]);
end;
end
else begin
Result := AHandler.DoStreamCollection(Self); //use default
end;
end;
procedure TLogicalExpression.Deserialize(AHandler: TVCMIJSONDestreamer;
ASrc: TJSONData);
var
Item: TCollectionItem;
begin
if IsRoot then
begin
If ASrc.JSONType = TJSONtype.jtArray then
begin
Item := Add;
AHandler.JSONToObjectEx(ASrc, Item);
end;
end
else begin
AHandler.DoDeStreamCollection(ASrc,Self); //use default
end;
end;
function TLogicalExpression.Evaluate(ACallback: TEvaluate): Boolean;
begin
Result := False;
if FIsRoot then
begin
if Count = 1 then
begin
Result := TLogicalExpressionItem(Items[0]).Evaluate(ACallback);
end
else if Count = 0 then
begin
Result := true
end;
end;
end;
{ TLogicalExpressionItem }
procedure TLogicalExpressionItem.SetLogicalOperator(AValue: TLogicalOperator);
begin
FLogicalOperator:=AValue;
end;
procedure TLogicalExpressionItem.AssignTo(Dest: TPersistent);
var
dest_typed: TLogicalExpressionItem;
begin
if Dest is TLogicalExpressionItem then
begin
dest_typed := TLogicalExpressionItem(Dest);
dest_typed.LogicalOperator:=LogicalOperator;
dest_typed.SubExpressions.Assign(SubExpressions);
end
else
begin
inherited AssignTo(Dest);
end;
end;
class function TLogicalExpressionItem.GetSubExpressionsClass: TLogicalExpressionClass;
begin
Result := TLogicalExpression;
end;
constructor TLogicalExpressionItem.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FSubExpressions := GetSubExpressionsClass().Create(TCollectionItemClass(ClassType));
end;
destructor TLogicalExpressionItem.Destroy;
begin
FSubExpressions.Free;
inherited Destroy;
end;
function TLogicalExpressionItem.Evaluate(ACallback: TEvaluate): Boolean;
var
i: Integer;
begin
case LogicalOperator of
TLogicalOperator.nop: Result := ACallback(Self);
TLogicalOperator.anyOf:
begin
for i := 0 to SubExpressions.Count - 1 do
begin
if TLogicalExpressionItem(SubExpressions.Items[i]).Evaluate(ACallback) then
exit(true);
end;
Result := false;
end;
TLogicalOperator.allOf:
begin
for i := 0 to SubExpressions.Count - 1 do
begin
if not TLogicalExpressionItem(SubExpressions.Items[i]).Evaluate(ACallback) then
exit(false);
end;
Result := True;
end;
TLogicalOperator.noneOf:
begin
for i := 0 to SubExpressions.Count - 1 do
begin
if TLogicalExpressionItem(SubExpressions.Items[i]).Evaluate(ACallback) then
exit(false);
end;
Result := True;
end;
end;
end;
end.