Skip to content

Commit c2c2919

Browse files
committed
Add inferred division to CodeGen
1 parent c951590 commit c2c2919

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

CodeGen/Generators/QuantityRelationsParser.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
5959
})
6060
.ToList());
6161

62+
// We can infer division relations from multiplication relations.
63+
relations.AddRange(relations
64+
.Where(r => r.Operator is "*")
65+
.Select(r => r with
66+
{
67+
Operator = "/",
68+
LeftQuantity = r.ResultQuantity,
69+
LeftUnit = r.ResultUnit,
70+
ResultQuantity = r.LeftQuantity,
71+
ResultUnit = r.LeftUnit,
72+
})
73+
// Skip division between equal quantities because the ratio is already generated as part of the Arithmetic Operators.
74+
.Where(r => r.LeftQuantity != r.RightQuantity)
75+
.ToList());
76+
77+
// Remove inferred relation "MassConcentration = Mass / Volume" because it duplicates "Density = Mass / Volume"
78+
relations.RemoveAll(r => r is { Operator: "/", ResultQuantity.Name: "MassConcentration", LeftQuantity.Name: "Mass", RightQuantity.Name: "Volume" });
79+
6280
// We can infer TimeSpan relations from Duration relations.
6381
var timeSpanQuantity = pseudoQuantity with { Name = "TimeSpan" };
6482
relations.AddRange(relations
@@ -72,7 +90,7 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
7290

7391
// Sort all relations to keep generated operators in a consistent order.
7492
relations.Sort();
75-
93+
7694
var duplicates = relations
7795
.GroupBy(r => r.SortString)
7896
.Where(g => g.Count() > 1)
@@ -84,7 +102,7 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
84102
var list = string.Join("\n ", duplicates);
85103
throw new UnitsNetCodeGenException($"Duplicate inferred relations:\n {list}");
86104
}
87-
105+
88106
foreach (var quantity in quantities)
89107
{
90108
var quantityRelations = new List<QuantityRelation>();
@@ -133,7 +151,7 @@ private static QuantityRelation ParseRelation(string relationString, IReadOnlyDi
133151
{
134152
var segments = relationString.Split(' ');
135153

136-
if (segments is not [_, "=", _, "*" or "/", _])
154+
if (segments is not [_, "=", _, "*", _])
137155
{
138156
throw new Exception($"Invalid relation string: {relationString}");
139157
}
@@ -151,11 +169,9 @@ private static QuantityRelation ParseRelation(string relationString, IReadOnlyDi
151169
var rightUnit = GetUnit(rightQuantity, right.ElementAtOrDefault(1));
152170
var resultUnit = GetUnit(resultQuantity, result.ElementAtOrDefault(1));
153171

154-
if (leftQuantity.Name == "1")
172+
if (resultQuantity.Name == "1")
155173
{
156174
@operator = "inverse";
157-
leftQuantity = resultQuantity;
158-
leftUnit = resultUnit;
159175
}
160176

161177
return new QuantityRelation

0 commit comments

Comments
 (0)