@@ -59,6 +59,24 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
59
59
} )
60
60
. ToList ( ) ) ;
61
61
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
+
62
80
// We can infer TimeSpan relations from Duration relations.
63
81
var timeSpanQuantity = pseudoQuantity with { Name = "TimeSpan" } ;
64
82
relations . AddRange ( relations
@@ -72,7 +90,7 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
72
90
73
91
// Sort all relations to keep generated operators in a consistent order.
74
92
relations . Sort ( ) ;
75
-
93
+
76
94
var duplicates = relations
77
95
. GroupBy ( r => r . SortString )
78
96
. Where ( g => g . Count ( ) > 1 )
@@ -84,7 +102,7 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities)
84
102
var list = string . Join ( "\n " , duplicates ) ;
85
103
throw new UnitsNetCodeGenException ( $ "Duplicate inferred relations:\n { list } ") ;
86
104
}
87
-
105
+
88
106
foreach ( var quantity in quantities )
89
107
{
90
108
var quantityRelations = new List < QuantityRelation > ( ) ;
@@ -133,7 +151,7 @@ private static QuantityRelation ParseRelation(string relationString, IReadOnlyDi
133
151
{
134
152
var segments = relationString . Split ( ' ' ) ;
135
153
136
- if ( segments is not [ _, "=" , _, "*" or "/" , _] )
154
+ if ( segments is not [ _, "=" , _, "*" , _] )
137
155
{
138
156
throw new Exception ( $ "Invalid relation string: { relationString } ") ;
139
157
}
@@ -151,11 +169,9 @@ private static QuantityRelation ParseRelation(string relationString, IReadOnlyDi
151
169
var rightUnit = GetUnit ( rightQuantity , right . ElementAtOrDefault ( 1 ) ) ;
152
170
var resultUnit = GetUnit ( resultQuantity , result . ElementAtOrDefault ( 1 ) ) ;
153
171
154
- if ( leftQuantity . Name == "1" )
172
+ if ( resultQuantity . Name == "1" )
155
173
{
156
174
@operator = "inverse" ;
157
- leftQuantity = resultQuantity ;
158
- leftUnit = resultUnit ;
159
175
}
160
176
161
177
return new QuantityRelation
0 commit comments