-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlad2pas.pas
More file actions
473 lines (430 loc) · 17.8 KB
/
lad2pas.pas
File metadata and controls
473 lines (430 loc) · 17.8 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
unit lad2pas;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils,usimplegraph,laddersymbol,fgl,forms;
type
{ TSymbolList }
TSymbolList = class(specialize TFPGObjectList<TSymbol>)
constructor Create(aFreeObjects: Boolean);
end;
type
{ TladToPas }
TladToPas = class
protected
listeSymbolRung : TSymbolList;
fcopieGraphe : TEvsSimpleGraph;
fworkGraph : TEvsSimpleGraph;
ffilename : string;
public
constructor create(workGraphe,copieGraphe:TEvsSimpleGraph);
constructor create;
destructor Destroy; override;
// utilitaires
procedure startConvert(graphName:string);
procedure getSymbolRung(terminal : TSymbol);
function findAndReduceSerial:boolean;
function findAndReduceParrallel:boolean;
function tryToOptimise(var equationList:TStringList):TStringList;
end;
implementation
uses xmlunit,main;
{ TSymbolList }
constructor TSymbolList.Create(aFreeObjects: Boolean);
begin
inherited Create(aFreeObjects);
end;
{ TLAD2PAS }
constructor TladToPas.create(workGraphe,copieGraphe:TEvsSimpleGraph);
begin
listeSymbolRung:=TSymbolList.create(false);
fworkGraph:=workGraphe;
fcopieGraphe:=copieGraphe;
end;
constructor TladToPas.create;
begin
end;
destructor TladToPas.Destroy;
begin
freeandnil(listeSymbolRung);
inherited;
end;
procedure TladToPas.startConvert(graphName:string);
var
i,j,index : integer;
nbGraphObject : integer;
matchSerial : boolean=true;
matchParrallel : boolean=true;
terminalList : TSymbolList;
terminal : TSymbol;
virtualContact : TSymbol;
equation : string;
xmlObject : TXmlObject;
linkList : TEvsGraphObjectList;
listEquation : TStringList;
filename : String;
fichier : text;
begin
// build terminal list for workGraph
terminalList:=TSymbolList.Create(false);
for i:=0 to fworkGraph.ObjectsCount()-1 do
if fworkGraph.Objects[i] is TSymbol then
if TSymbol(fworkGraph.Objects[i]).isTerminal then
terminalList.Add(TSymbol(fworkGraph.Objects[i]));
linkList := TEvsGraphObjectList.Create;
while terminalList.Count>0 do
begin
// build symbol list for current rung
getSymbolRung(terminalList[0]);
// build list of link beetween rung symbols
for i:=0 to listeSymbolRung.Count-1 do
begin
if listeSymbolRung[i].symbolInput<>nil then
for j:=0 to TConnexion(listeSymbolRung[i].symbolInput).LinkInputCount-1 do
linkList.add(TConnexion(listeSymbolRung[i].symbolInput).LinkInputs[j]);
end;
// make a copy of symbol in workgraph with the clipboard
for i:=0 to listeSymbolRung.Count-1 do
listeSymbolRung[i].Selected:=true;
for i:=0 to linkList.Count-1 do
linkList[i].Selected:=true;
xmlObject:=TXmlObject.create;
xmlObject.saveClipboardXML(fworkGraph);
xmlObject.loadClipboardXML(fcopieGraphe,100,100);
FreeAndNil(xmlObject);
// replace variable with space in name with _
for i:=0 to fcopieGraphe.ObjectsCount-1 do
if fcopieGraphe.Objects[i].IsNode then
if (fcopieGraphe.Objects[i] is TSymbol) and
not(fcopieGraphe.Objects[i] is TPowerRail) and
not(fcopieGraphe.Objects[i] is TVirtual) then
for j:=0 to TSymbol(fcopieGraphe.Objects[i]).getVarList.Count-1 do
if TSymbol(fcopieGraphe.Objects[i]).getVarList[j].IndexOf(' ')<> -1 then
TSymbol(fcopieGraphe.Objects[i]).SetVar(j,TSymbol(fcopieGraphe.Objects[i]).getVarList[j].Replace(' ','_'));
// update listeSymbolRung for new graphe
listeSymbolRung.Clear;
for i:=0 to fcopieGraphe.ObjectsCount()-1 do
if fcopieGraphe.Objects[i] is TSymbol then
listeSymbolRung.Add(TSymbol(fcopieGraphe.Objects[i]));
matchSerial:=true;
matchParrallel:=true;
// reduce and simplify
while (matchSerial or matchParrallel) do
begin
// serial reduction
matchSerial:=findAndReduceSerial;
// parrallel reduction
matchParrallel:=findAndReduceParrallel;
application.ProcessMessages;
end;
// delete link in workGraph
while TConnexion(terminalList[0].symbolInput).LinkInputCount>0 do
begin
index:=fworkGraph.Objects.IndexOf(TConnexion(terminalList[0].symbolInput).LinkInputs[0]);
fworkGraph.Objects.Delete(index);
end;
// delete terminal in workGraph
index:=fworkGraph.Objects.IndexOf(terminalList[0]);
// remove terminal from list and graph
terminalList.delete(0);
fworkGraph.Objects.delete(index);
// clear link list for next rung
linkList.clear;
end;
// build equation list
nbGraphObject:=0;
listEquation:=TStringList.Create;
equation:='// '+graphName;
listEquation.add(equation);
while nbGraphObject<fcopieGraphe.ObjectsCount() do
begin
equation:='';
if (fcopieGraphe.Objects[nbGraphObject].isNode) and
(fcopieGraphe.Objects[nbGraphObject] is TSymbol) and
(TSymbol(fcopieGraphe.Objects[nbGraphObject]).isTerminal) then
begin
terminal:=TSymbol(fcopieGraphe.Objects[nbGraphObject]);
equation:=equation+terminal.allias+terminal.codeBefore;
virtualContact:=TConnexion(terminal.symbolInput.LinkInputs[0].HookedObjectOf(0)).symbolOwner;
if virtualContact.isVirtual then
equation:=terminal.codeBefore+virtualContact.equation+terminal.codeAfter
else if virtualContact.ClassName<>'TPowerRail' then
equation:=terminal.codeBefore+virtualContact.equation+terminal.codeAfter;
if virtualContact.ClassName='TPowerRail' then
equation:=terminal.codeBefore+'(TRUE)'+terminal.codeAfter;
listEquation.add(equation);
end;
inc(nbGraphObject);
end;
// try to optimise code
listEquation:=tryToOptimise(listEquation);
// save code
filename := form1.projectCurrent.fpath+form1.projectCurrent.fname+'-data/'+
graphname+'.pas';
if FileExists(filename) then
DeleteFile(filename);
AssignFile(fichier,filename);
Rewrite(fichier);
for i:=0 to listEquation.Count-1 do
writeln(fichier,listEquation[i]);
CloseFile(fichier);
end;
//-------------------------------------------------------------------
// build list symbol for rung
//-------------------------------------------------------------------
procedure TladToPas.getSymbolRung(terminal : TSymbol);
var
i,j,k : integer;
listeAmont : TSymbolList;
duplicatedSymbol: boolean;
begin
listeAmont:=TSymbolList.Create(false);
listeSymbolRung.Clear;
listeAmont.Add(terminal);
listeSymbolRung.add(terminal);
while listeAmont.Count>0 do
begin
i:=0;
while i<listeAmont.count do
begin
for j:=0 to listeAmont[i].symbolInput.LinkInputCount-1 do
begin
if TConnexion(listeAmont[i].symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner.ClassName<>'TPowerRail' then
begin
k:=0;
duplicatedSymbol:=false;
while k<listeAmont.Count do
begin
if listeAmont[k]=TConnexion(listeAmont[i].symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner then
duplicatedSymbol:=true;
k:=k+1;
end;
if not(duplicatedSymbol) then
begin
listeAmont.Add(TConnexion(listeAmont[i].symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner);
listeSymbolRung.Add(TSymbol(TConnexion(listeAmont[i].symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner));
end;
end
else
begin
if listeSymbolRung.IndexOf(TConnexion(listeAmont[i].symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner)<0 then
begin
listeSymbolRung.add(TSymbol(TConnexion(listeAmont[i].symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner));
end;
end;
end;
listeAmont.Delete(i);
i:=i+1;
end;
if listeAmont.Count=1 then
if listeAmont[0].ClassName='TPowerRail' then listeAmont.Clear;
end;
end;
//-------------------------------------------------------------------
// recherche et réduction des symboles en serie
//-------------------------------------------------------------------
function TladToPas.findAndReduceSerial:boolean;
var
i : integer;
symboleAnalyse : TSymbol;
symboleConnecte: TSymbol;
virtualSymbole : TVirtual;
temp : string;
begin
i:=0;
while i<listeSymbolRung.Count do
begin
symboleAnalyse:=listeSymbolRung[i];
if (not(symboleAnalyse.isTerminal) and (symboleAnalyse.ClassName<>'TPowerRail')) then
// si il n'y a qu'un fil à l'entrée du symbole
if symboleAnalyse.symbolInput.LinkInputCount=1 then
begin
symboleConnecte:=Tconnexion(symboleAnalyse.symbolInput.LinkInputs[0].HookedObjectOf(0)).symbolOwner;
// si le symbole connecté n'est pas un powerRail
if (symboleConnecte.ClassName<>'TPowerRail') and
(symboleConnecte.symbolOutput.LinkOutputCount=1) then
begin
// créer le symbole equivalent
virtualSymbole:=TVirtual.create(fcopieGraphe,'contactNO',symboleAnalyse.allias+'&'+symboleConnecte.allias);
listeSymbolRung.Add(virtualSymbole);
// check system var and bit
temp:=symboleConnecte.equation;
virtualSymbole.equation:=('('+symboleAnalyse.equation+' AND '+temp+')');
// fabriquer les anciennes connexions
// connexions de sortie
while symboleAnalyse.symbolOutput.LinkOutputCount>0 do
// creer le symbole virtuel equivalent
begin
fcopieGraphe.InsertLink(virtualSymbole.symbolOutput,
Tconnexion(symboleAnalyse.symbolOutput.LinkOutputs[0].HookedObjectOf(1)).symbolOwner.symbolInput);
fcopieGraphe.Objects.Remove(symboleAnalyse.symbolOutput.LinkOutputs[0]);
end;
// supprimer le conducteur entre les deux symboles en serie
fcopieGraphe.Objects.Remove(symboleAnalyse.symbolInput.LinkInputs[0]);
// supprimer les connexions du symbole 1
fcopieGraphe.Objects.Remove(symboleAnalyse.symbolOutput);
// supprimer le symbole 1
listeSymbolRung.Remove(symboleAnalyse);
fcopieGraphe.Objects.Remove(symboleAnalyse);
// connexions d'entree
while symboleConnecte.symbolInput.LinkInputCount>0 do
// creer le nouveau lien et supprimer l'ancien
begin
fcopieGraphe.InsertLink(TConnexion(symboleConnecte.symbolInput.LinkInputs[0].HookedObjectOf(0)).symbolOwner.symbolOutput,
virtualSymbole.symbolInput );
fcopieGraphe.Objects.Remove(symboleConnecte.symbolInput.LinkInputs[0]);
end;
// supprimer les connexions du symbole 2
fcopieGraphe.Objects.Remove(symboleConnecte.symbolInput);
fcopieGraphe.Objects.Remove(symboleConnecte.symbolOutput);
// supprimer le symbole 2
listeSymbolRung.remove(symboleConnecte);
fcopieGraphe.Objects.Remove(symboleConnecte);
findAndReduceSerial:=true;
end;
end;
i:=i+1;
end;
findAndReduceSerial:=false;
end;
//-------------------------------------------------------------------
// recherche et réduction des symboles en parrallele
//-------------------------------------------------------------------
function TladToPas.findAndReduceParrallel:boolean;
var
i,j,k : integer;
n : integer;
symboleAnalyse : TSymbol;
tenant1,tenant2 : TSymbol;
listeParrallele : TSymbolList;
virtualSymbole : TVirtual;
connexion : TConnexion;
lien : TEvsGraphLink;
begin
listeParrallele:=TSymbolList.create(false);
i:=0;
while i<listeSymbolRung.Count do
begin
symboleAnalyse:=listeSymbolRung[i];
if symboleAnalyse.ClassName<>'TPowerRail' then
if symboleAnalyse.symbolInput.LinkInputCount>1 then
begin
// faire une liste des symboles en //
for j:=0 to symboleAnalyse.symbolInput.LinkInputCount-1 do
listeParrallele.add(Tconnexion(symboleAnalyse.symbolInput.LinkInputs[j].HookedObjectOf(0)).symbolOwner);
// chercher les tenants communs
j:=0;
while j<listeParrallele.Count do
begin
tenant1:=TConnexion(listeParrallele[j].symbolInput.LinkInputs[0].HookedObjectOf(0)).symbolOwner;
for k:=j to listeParrallele.Count-1 do
begin
tenant2:=TConnexion(listeParrallele[k].symbolInput.LinkInputs[0].HookedObjectOf(0)).symbolOwner;
if (listeParrallele[j]<>listeParrallele[k]) and (tenant1=tenant2) then
// les tenants sont communs
begin
// créer le symbole equivalent
virtualSymbole:=TVirtual.create(fcopieGraphe,'contactNO',listeParrallele[j].allias+'+'+listeParrallele[k].allias);
listeSymbolRung.Add(virtualSymbole);
virtualSymbole.equation:='('+listeParrallele[j].equation+' OR '+listeParrallele[k].equation+')';
// fabriquer les anciennes connexions
// connexions de sortie
fcopieGraphe.InsertLink(virtualSymbole.symbolOutput,symboleAnalyse.symbolInput);
n:=0;
while n<symboleAnalyse.symbolInput.LinkInputCount-1 do
begin
if TConnexion(symboleAnalyse.symbolInput.LinkInputs[n].HookedObjectOf(0)).symbolOwner=listeParrallele[j] then
fcopieGraphe.Objects.remove(symboleAnalyse.symbolInput.LinkInputs[n]);
if TConnexion(symboleAnalyse.symbolInput.LinkInputs[n].HookedObjectOf(0)).symbolOwner=listeParrallele[k] then
fcopieGraphe.Objects.remove(symboleAnalyse.symbolInput.LinkInputs[n]);
n:=n+1;
end;
fcopieGraphe.Objects.remove(listeParrallele[j].symbolOutput);
fcopieGraphe.Objects.remove(listeParrallele[k].symbolOutput);
// connexions d'entree
fcopieGraphe.InsertLink(tenant1.symbolOutput,virtualSymbole.symbolInput);
n:=0;
while n<tenant1.symbolOutput.LinkOutputCount-1 do
begin
if TConnexion(tenant1.symbolOutput.LinkOutputs[n].HookedObjectOf(1)).symbolOwner=listeParrallele[j] then
fcopieGraphe.Objects.remove(tenant1.symbolOutput.LinkOutputs[n]);
if TConnexion(tenant1.symbolOutput.LinkOutputs[n].HookedObjectOf(1)).symbolOwner=listeParrallele[k] then
fcopieGraphe.Objects.remove(tenant1.symbolOutput.LinkOutputs[n]);
n:=n+1;
end;
fcopieGraphe.Objects.remove(listeParrallele[j].symbolInput);
fcopieGraphe.Objects.remove(listeParrallele[k].symbolInput);
listeSymbolRung.Remove(listeParrallele[j]);
fcopieGraphe.Objects.remove(listeParrallele[j]);
listeSymbolRung.Remove(listeParrallele[k]);
fcopieGraphe.Objects.remove(listeParrallele[k]);
findAndReduceParrallel:=true;
exit;
end;
end;
listeParrallele.clear;
j:=j+1;
end;
end;
i:=i+1;
end;
listeParrallele.Destroy;
findAndReduceParrallel:=false;
end;
function TLadToPas.tryToOptimise(var equationList:TStringList):TStringList;
var
i,j : integer;
currentLine : string;
workline : string;
condition : string;
conditionCurrentLine : string;
action : string;
action1 : string;
listAction : TStringList;
newEquationList : TStringList;
begin
listAction:=TStringList.create(false);
newEquationList:=TStringList.create(false);
i:=0;
while i<equationList.count do
begin
listAction.Clear;
currentLine:=equationList[i];
if pos('if',currentLine)<>0 then
begin
condition:=copy(currentLine,0,pos(' then',currentLine)-1);
action1:=copy(currentLine,pos('then ',currentLine)+5,length(currentLine));
j:=i+1;
while j<equationList.count do
begin
workline:=equationList[j];
conditionCurrentLine:=copy(workline,0,pos(' then',workline)-1);
if (condition=conditionCurrentLine) and (pos('else',workline)=0) then
begin
if listAction.IndexOf(action1)=-1 then listAction.Add(action1);
action:=copy(workline,pos('then ',workline)+5,length(workline));
listAction.Add(action);
equationList.Delete(j);
end
else
j:=j+1;
end;
end;
if listAction.Count<1 then
begin
newEquationList.Add(equationList[i]);
end
else
begin
newEquationList.add(condition+' then begin');
for j:=0 to listAction.Count-1 do
newEquationList.Add(listAction[j]);
newEquationList.add('end;');
end;
listAction.clear;
i:=i+1;
end;
result:=newEquationList;
FreeAndNil(listAction);
end;
end.