Skip to content

Commit 33d579d

Browse files
authored
Verification_Engine added (#3431)
2 parents a6f9074 + affc4c9 commit 33d579d

27 files changed

+2830
-3
lines changed

BHoM_Engine.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.33927.289
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.7.34302.85
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BHoM_Engine", "BHoM_Engine\BHoM_Engine.csproj", "{AEAF161D-8206-40B8-93A7-67ABEBF2EE19}"
77
EndProject
@@ -59,6 +59,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ground_Engine", "Ground_Eng
5959
EndProject
6060
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Search_Engine", "Search_Engine\Search_Engine.csproj", "{7DDAC1D8-5B6A-471F-BC39-5B7E91A0E86C}"
6161
EndProject
62+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Verification_Engine", "Verification_Engine\Verification_Engine.csproj", "{3AC2FA07-E889-484B-A319-B5CA942C277E}"
63+
EndProject
6264
Global
6365
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6466
Debug|Any CPU = Debug|Any CPU
@@ -177,6 +179,10 @@ Global
177179
{7DDAC1D8-5B6A-471F-BC39-5B7E91A0E86C}.Debug|Any CPU.Build.0 = Debug|Any CPU
178180
{7DDAC1D8-5B6A-471F-BC39-5B7E91A0E86C}.Release|Any CPU.ActiveCfg = Release|Any CPU
179181
{7DDAC1D8-5B6A-471F-BC39-5B7E91A0E86C}.Release|Any CPU.Build.0 = Release|Any CPU
182+
{3AC2FA07-E889-484B-A319-B5CA942C277E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183+
{3AC2FA07-E889-484B-A319-B5CA942C277E}.Debug|Any CPU.Build.0 = Debug|Any CPU
184+
{3AC2FA07-E889-484B-A319-B5CA942C277E}.Release|Any CPU.ActiveCfg = Release|Any CPU
185+
{3AC2FA07-E889-484B-A319-B5CA942C277E}.Release|Any CPU.Build.0 = Release|Any CPU
180186
EndGlobalSection
181187
GlobalSection(SolutionProperties) = preSolution
182188
HideSolutionNode = FALSE

BHoM_Engine/Query/ExtensionMethodHierarchy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static List<List<List<MethodInfo>>> ExtensionMethodHierarchy(this IEnumer
5656
// Each item of the top list represents hierarchy for each input type
5757
List<List<List<MethodInfo>>> result = new List<List<List<MethodInfo>>>();
5858
int i = 0;
59-
foreach(Type type in types)
59+
60+
foreach (Type type in types)
6061
{
6162
if (type == null)
6263
{
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.Engine.Base.Objects;
24+
using BH.oM.Base;
25+
using BH.oM.Base.Attributes;
26+
using BH.oM.Verification;
27+
using BH.oM.Verification.Conditions;
28+
using System;
29+
using System.ComponentModel;
30+
31+
namespace BH.Engine.Verification
32+
{
33+
public static partial class Compute
34+
{
35+
/***************************************************/
36+
/**** Public Methods ****/
37+
/***************************************************/
38+
39+
[Description("Compares two values using the provided comparison requirement and tolerance.")]
40+
[Input("value", "Value to compare against the reference value.")]
41+
[Input("referenceValue", "Reference value to compare the value against.")]
42+
[Input("comparisonType", "Comparison requirement, i.e. whether the value should be equal, greater, less than reference value etc.")]
43+
[Input("tolerance", "Tolerance to apply in the comparison.")]
44+
[Output("result", "True if comparison of the input values meets the comparison requirement, otherwise false. Null in case of inconclusive comparison.")]
45+
public static bool? CompareValues(this object value, object referenceValue, ValueComparisonType comparisonType, object tolerance)
46+
{
47+
// Basic cases (check for nullity)
48+
if (referenceValue == null && value == null)
49+
return true;
50+
else if (referenceValue == null || value == null)
51+
return false;
52+
53+
if (value is Type && referenceValue is Type)
54+
return value == referenceValue;
55+
56+
// Try enum comparison
57+
if (value is Enum || referenceValue is Enum)
58+
return value.GetType() == referenceValue.GetType() && (int)value == (int)referenceValue;
59+
60+
// Try a numerical comparison
61+
double numericalValue, referenceNumValue;
62+
if (double.TryParse(value?.ToString(), out numericalValue) && double.TryParse(referenceValue?.ToString(), out referenceNumValue))
63+
{
64+
double numTolerance;
65+
if (!double.TryParse(tolerance?.ToString(), out numTolerance))
66+
numTolerance = 1e-03;
67+
68+
return NumericalComparison(numericalValue, referenceNumValue, numTolerance, comparisonType);
69+
}
70+
71+
// Try string comparison
72+
if (value is string && referenceValue is string)
73+
return StringComparison((string)value, (string)referenceValue, comparisonType);
74+
75+
// Consider some other way to compare objects.
76+
if (comparisonType == ValueComparisonType.EqualTo || comparisonType == ValueComparisonType.NotEqualTo)
77+
{
78+
bool? passed;
79+
80+
// If the referenceValue is a Type, convert this ValueCondition to a IsOfType condition.
81+
if (referenceValue is Type)
82+
{
83+
IsOfType typeCondition = new IsOfType() { Type = referenceValue as Type };
84+
passed = value.IPasses(typeCondition);
85+
}
86+
else
87+
passed = CompareObjectEquality(value, referenceValue, tolerance);
88+
89+
if (passed != null && comparisonType == ValueComparisonType.NotEqualTo)
90+
passed = !passed;
91+
92+
return passed;
93+
}
94+
95+
BH.Engine.Base.Compute.RecordWarning("Objects could not be compared because no meaningful comparison method has been found.");
96+
return null;
97+
}
98+
99+
100+
/***************************************************/
101+
/**** Private Methods ****/
102+
/***************************************************/
103+
104+
private static bool CompareObjectEquality(object value, object refValue, object tolerance)
105+
{
106+
if (value == null || refValue == null)
107+
return value == refValue;
108+
109+
if (value.GetType() != refValue.GetType())
110+
return false;
111+
112+
var cc = tolerance as ComparisonConfig;
113+
if (cc != null)
114+
{
115+
HashComparer<object> hc = new HashComparer<object>(cc);
116+
return hc.Equals(value, refValue);
117+
}
118+
119+
return value.Equals(refValue);
120+
}
121+
122+
/***************************************************/
123+
124+
private static bool? NumericalComparison(double value, double referenceValue, double tolerance, ValueComparisonType condition)
125+
{
126+
switch (condition)
127+
{
128+
case ValueComparisonType.EqualTo:
129+
return (Math.Abs(value - referenceValue) <= tolerance);
130+
case ValueComparisonType.NotEqualTo:
131+
return (Math.Abs(value - referenceValue) > tolerance);
132+
case ValueComparisonType.GreaterThan:
133+
return (value - referenceValue > tolerance);
134+
case ValueComparisonType.GreaterThanOrEqualTo:
135+
return (value - referenceValue >= -tolerance);
136+
case ValueComparisonType.LessThan:
137+
return (value - referenceValue < -tolerance);
138+
case ValueComparisonType.LessThanOrEqualTo:
139+
return (value - referenceValue <= tolerance);
140+
default:
141+
BH.Engine.Base.Compute.RecordWarning($"Comparison of type {condition} is not supported for numbers.");
142+
return null;
143+
}
144+
}
145+
146+
/***************************************************/
147+
148+
private static bool? StringComparison(string value, string referenceValue, ValueComparisonType condition)
149+
{
150+
switch (condition)
151+
{
152+
case ValueComparisonType.EqualTo:
153+
return value == referenceValue;
154+
case ValueComparisonType.NotEqualTo:
155+
return value != referenceValue;
156+
case ValueComparisonType.Contains:
157+
return value.Contains(referenceValue);
158+
case ValueComparisonType.StartsWith:
159+
return value.StartsWith(referenceValue);
160+
case ValueComparisonType.EndsWith:
161+
return value.EndsWith(referenceValue);
162+
default:
163+
BH.Engine.Base.Compute.RecordWarning($"Comparison of type {condition} is not supported for strings.");
164+
return null;
165+
}
166+
}
167+
168+
/***************************************************/
169+
}
170+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of the Buildings and Habitats object Model (BHoM)
3+
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
4+
*
5+
* Each contributor holds copyright over their respective contributions.
6+
* The project versioning (Git) records all such contribution source information.
7+
*
8+
*
9+
* The BHoM is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3.0 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* The BHoM is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
21+
*/
22+
23+
using BH.oM.Base.Attributes;
24+
using BH.oM.Verification.Extraction;
25+
using System.Collections.Generic;
26+
using System.ComponentModel;
27+
using System.Linq;
28+
29+
namespace BH.Engine.Verification
30+
{
31+
public static partial class Compute
32+
{
33+
/***************************************************/
34+
/**** Interface Methods ****/
35+
/***************************************************/
36+
37+
[Description("Extracts the objects from the input set based on the extraction instruction." +
38+
"\nExtraction means either simple filtering from the input set or generation of derived objects based on the input set.")]
39+
[Input("objects", "Set of objects to extract from.")]
40+
[Input("extraction", "Instruction how to extract the objects from the input set.")]
41+
[Output("extracted", "Collection of objects extracted from the input set based on the extraction instruction.")]
42+
public static List<object> IExtract(this IEnumerable<object> objects, IExtraction extraction)
43+
{
44+
if (objects == null)
45+
{
46+
BH.Engine.Base.Compute.RecordError($"Extraction failed because the provided objects to extract from are null.");
47+
return null;
48+
}
49+
50+
if (extraction == null)
51+
{
52+
BH.Engine.Base.Compute.RecordNote("No filter provided, all input objects have been verified against the requirements.");
53+
return objects.ToList();
54+
}
55+
56+
object filtered;
57+
if (!BH.Engine.Base.Compute.TryRunExtensionMethod(objects, nameof(Extract), new object[] { extraction }, out filtered))
58+
{
59+
BH.Engine.Base.Compute.RecordError($"Extraction failed because extraction type {extraction.GetType().Name} is currently not supported.");
60+
return null;
61+
}
62+
63+
return filtered as List<object>;
64+
}
65+
66+
67+
/***************************************************/
68+
/**** Public Methods ****/
69+
/***************************************************/
70+
71+
[Description("Extracts the objects from the input set based on the condition based filter.")]
72+
[Input("objects", "Set of objects to extract from.")]
73+
[Input("extraction", "Condition based filter, i.e. set of conditions for each object to pass in order to be returned.")]
74+
[Output("extracted", "Subset of the input objects that passed the conditions embedded in the input filter.")]
75+
public static List<object> Extract(this IEnumerable<object> objects, ConditionBasedFilter extraction)
76+
{
77+
return objects.Where(x => x.IPasses(extraction.Condition) == true).ToList();
78+
}
79+
80+
/***************************************************/
81+
}
82+
}

0 commit comments

Comments
 (0)