Skip to content

Commit b984156

Browse files
author
sripp
committed
Fix MultipleOr with an And in front
1 parent b26c5c4 commit b984156

File tree

4 files changed

+94
-28
lines changed

4 files changed

+94
-28
lines changed

TiaCodeGen.Tests/SampleTests.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,14 @@ public void DistributorWithOr()
262262
new SCoil(new Signal("x")),
263263
new And(
264264
new Or(
265-
new Signal("a"),
266-
new Signal("b")
265+
new And(
266+
new Signal("a"),
267+
new Signal("h")
268+
),
269+
new And(
270+
new Signal("b"),
271+
new Signal("f")
272+
)
267273
),
268274
new SCoil(new Signal("c"))
269275
)
@@ -277,5 +283,37 @@ public void DistributorWithOr()
277283
block.Interface = TestInterface;
278284
var xml = block.GetCode();
279285
}
286+
287+
[Test]
288+
public void MultipleOr()
289+
{
290+
var codeblock = new CodeBlock() { Safety = false };
291+
292+
var nw = new Network("Test2", "Test2en");
293+
nw.Add(
294+
new Coil(
295+
new Signal("a"),
296+
new And(
297+
new Signal("1"),
298+
new Or(
299+
new And(
300+
new Or(
301+
new Signal("b"),
302+
new Signal("c")
303+
),
304+
new Signal("d")
305+
),
306+
new Signal("e")
307+
)
308+
)
309+
)
310+
);
311+
312+
codeblock.Add(nw);
313+
314+
var block = new Block("Test", "blabla", codeblock);
315+
block.Interface = TestInterface;
316+
var xml = block.GetCode();
317+
}
280318
}
281319
}

TiaCodegen/CodeGen/KopCodeHelper.cs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,13 @@ private void AddWires(IOperationOrSignal op)
519519
}
520520
else if (c is And && c.Children.FirstOrDefault() is Or)
521521
{
522-
foreach(var ch in c.Children.FirstOrDefault().Children)
522+
foreach(var chIn in c.Children.FirstOrDefault().Children)
523523
{
524-
var inName = ch is FunctionCall ? ((ch is InRangeCall || ch is OutRangeCall) ? "pre" : "en") : "in";
525-
_sb.AppendLine("<NameCon UId=\"" + ch.OperationId + "\" Name=\"" + inName + "\" />");
524+
foreach (var ch in GetAllOrSignals(chIn))
525+
{
526+
var inName = ch is FunctionCall ? ((ch is InRangeCall || ch is OutRangeCall) ? "pre" : "en") : "in";
527+
_sb.AppendLine("<NameCon UId=\"" + ch.OperationId + "\" Name=\"" + inName + "\" />");
528+
}
526529
}
527530
}
528531
else
@@ -653,20 +656,17 @@ private void AddWires(IOperationOrSignal op)
653656
_sb.AppendLine("<NameCon UId=\"" + ch.OperationId + "\" Name=\"out\" />");
654657
foreach (var orSignal in next.Children)
655658
{
656-
var opId = orSignal.OperationId;
657-
658-
if (orSignal is And)
659+
foreach (var os in GetAllOrSignals(orSignal))
659660
{
660-
opId = ((And)orSignal).Children.First().OperationId;
661-
}
661+
var opId = os.OperationId;
662+
var ipName = "in";
662663

663-
var ipName = "in";
664-
665-
if (orSignal is BaseOperationOrSignal && (((BaseOperationOrSignal)orSignal).GetFirstChildNotAnd() is CompareOperator || ((BaseOperationOrSignal)orSignal).GetFirstChildNotAnd() is InRangeCall || ((BaseOperationOrSignal)orSignal).GetFirstChildNotAnd() is OutRangeCall))
666-
{
667-
ipName = "pre";
664+
if (os is BaseOperationOrSignal && (((BaseOperationOrSignal)os).GetFirstChildNotAnd() is CompareOperator || ((BaseOperationOrSignal)os).GetFirstChildNotAnd() is InRangeCall || ((BaseOperationOrSignal)os).GetFirstChildNotAnd() is OutRangeCall))
665+
{
666+
ipName = "pre";
667+
}
668+
_sb.AppendLine("<NameCon UId=\"" + opId + "\" Name=\"" + ipName + "\" />");
668669
}
669-
_sb.AppendLine("<NameCon UId=\"" + opId + "\" Name=\"" + ipName + "\" />");
670670
}
671671
_sb.AppendLine("</Wire>");
672672
_currentId++;
@@ -712,19 +712,22 @@ private void AddWires(IOperationOrSignal op)
712712
if (akC is Or)
713713
l = akC.Children;
714714

715-
foreach (var s in l)
715+
foreach (var sIn in l)
716716
{
717-
if (s is CompareOperator || s is InRangeCall || s is OutRangeCall)
717+
foreach (var s in GetAllOrSignals(sIn))
718718
{
719-
_sb.AppendLine("<NameCon UId=\"" + s.OperationId + "\" Name=\"pre\" />" + " <!-- " + s.GetType().Name + " -->");
720-
}
721-
else if (s is FunctionCall || s is IFunctionOperation)
722-
{
723-
_sb.AppendLine("<NameCon UId=\"" + s.OperationId + "\" Name=\"en\" />" + " <!-- " + s.GetType().Name + " -->");
724-
}
725-
else
726-
{
727-
_sb.AppendLine("<NameCon UId=\"" + s.OperationId + "\" Name=\"in\" />" + " <!-- " + s.GetType().Name + " -->");
719+
if (s is CompareOperator || s is InRangeCall || s is OutRangeCall)
720+
{
721+
_sb.AppendLine("<NameCon UId=\"" + s.OperationId + "\" Name=\"pre\" />" + " <!-- " + s.GetType().Name + " -->");
722+
}
723+
else if (s is FunctionCall || s is IFunctionOperation)
724+
{
725+
_sb.AppendLine("<NameCon UId=\"" + s.OperationId + "\" Name=\"en\" />" + " <!-- " + s.GetType().Name + " -->");
726+
}
727+
else
728+
{
729+
_sb.AppendLine("<NameCon UId=\"" + s.OperationId + "\" Name=\"in\" />" + " <!-- " + s.GetType().Name + " -->");
730+
}
728731
}
729732
}
730733
}
@@ -1045,5 +1048,28 @@ public string GetXml(ref int id)
10451048
_sb.AppendLine("-->");*/
10461049
return _sb.ToString();
10471050
}
1051+
1052+
public IEnumerable<IOperationOrSignal> GetAllOrSignals(IOperationOrSignal sng)
1053+
{
1054+
if (sng is And)
1055+
{
1056+
if (sng.Children.First() is Or or)
1057+
{
1058+
foreach (var o in or.Children)
1059+
{
1060+
foreach (var a in GetAllOrSignals(o))
1061+
yield return a;
1062+
}
1063+
}
1064+
else
1065+
{
1066+
yield return ((And)sng).Children.First();
1067+
}
1068+
}
1069+
else
1070+
{
1071+
yield return sng;
1072+
}
1073+
}
10481074
}
10491075
}

TiaCodegen/Commands/BaseOperationOrSignal.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using TiaCodegen.Commands.Signals;
45
using TiaCodegen.Interfaces;
56

67
namespace TiaCodegen.Commands

TiaCodegen/Extensions/OperationOrSignalExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using TiaCodegen.Interfaces;
1+
using TiaCodegen.Commands;
2+
using TiaCodegen.Interfaces;
23

34
namespace DotNetProjects.TiaCodegen.Extensions
45
{

0 commit comments

Comments
 (0)