This API executes inferences by fuzzy logic concept on C# Plain Old CLR Object associating an Expression object defined in native .NET Framework.

## 1) Before You Begin: Abstraction

If do you want to delve into Fuzzy Logic theory (such as mathematical theorems, postulates, and Morgan's law), it's strongly recommended to look for other references to satisfy your curiosity and / or your research need. Through this git post, you'll access only a practical example to execute the Fuzzy Logic in real world applications; then, the focus in this article is not diving on philosophical dialogue with only a practical purpose.

## 2) Fuzzy Logic Concepts

This figure from tutorialspoint site resumes the real concept of Fuzzy Logic: Nothing is absolutely true or false (for Fuzzy Logic); between 0 and 1, you have an interval from these extremes, beyond the limits of the boolean logic.

## 3) Using the API

### 3.1) Core Flow

The core concept had the first requirement: Defuzzyfication. In other words, generate a Fuzzy Logic results by Crisp Input expression built on Fuzzy Logic Engine (view figure below, from Wikepdia reference):

The rule of Fuzzy Logic Engine is: break apart any complex boolean expression (crisp input) that resolves a logical boolean problem in minor boolean parts rule (about the theory used of this complex boolean expression, view articles like Many-Valued Logic or /Classical Logic).

Based on the illustrative example above, let's create a `Model`

class that represents the Honest character like integrity, truth and justice sense percentage assessment for all and a boolean expression object that identifies the Honesty Profile, considering the minimal percentage to be a honest person:

[Serializable, XmlRoot]
public class HonestAssesment
{
[XmlElement]
public int IntegrityPercentage { get; set; }
[XmlElement]
public int TruthPercentage { get; set; }
[XmlElement]
public int JusticeSensePercentage { get; set; }
[XmlElement]
public int MistakesPercentage
{
get
{
return ((100-IntegrityPercentage) + (100-TruthPercentage) +
(100-JusticeSensePercentage))/3;
}
}
}
static Expression<Func<HonestAssesment, bool>> _honestyProfile = (h) =>
(h.IntegrityPercentage > 75 && h.JusticeSensePercentage > 75 &&
h.TruthPercentage > 75) ||
(h.IntegrityPercentage > 90 && h.JusticeSensePercentage > 60 &&
h.TruthPercentage > 50) ||
(h.IntegrityPercentage > 70 && h.JusticeSensePercentage > 90 &&
h.TruthPercentage > 80) ||
(h.IntegrityPercentage > 65 && h.JusticeSensePercentage > 100 &&
h.TruthPercentage > 95);

The boolean expression broken is one capacity derived from System.Linq.Expressions.Expression class, converting any block of code to representational `string`

; the derived class that will auxiliate with this job is BinaryExpression: the boolean expression will be sliced in binary tree of smaller boolean expression, whose rule will prioritize the slice where the conditional expression is contained '`OR`

', then sliced by '`AND`

' conditional expression.

h.IntegrityPercentage > 75;
h.JusticeSensePercentage > 75;
h.TruthPercentage > 75;
h.IntegrityPercentage > 90;
h.JusticeSensePercentage > 60;
h.TruthPercentage > 50;
h.IntegrityPercentage > 70;
h.JusticeSensePercentage > 90;
h.TruthPercentage > 80;
h.IntegrityPercentage > 65;
h.JusticeSensePercentage > 100;
h.TruthPercentage > 95;

This functionality contained in the .NET Framework is the trump card to mitigate the appraisal value that the evaluated profiles have conquered or how close they have come to reach any of the 4 defined valuation groups, for example:

HonestAssesment profile1 = new HonestAssesment()
{
IntegrityPercentage = 90,
JusticeSensePercentage = 80,
TruthPercentage = 70
};
string inference_p1 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_expression, ResponseType.Json, profile1);

Look at the "`HitsPercentage`

" property. The inference on Profile 1, with 66% of Honest.

{
"ID": 0,
"HitsPercentage": "66%",
"Data": {
"IntegrityPercentage": 90,
"TruthPercentage": 70,
"JusticeSensePercentage": 80,
"MistakesPercentage": 20
},
"PropertiesNeedToChange": [
"IntegrityPercentage"
],
"RatingsReport": [
false,
true,
true
],
"ErrorsQuantity": 1
}

HonestAssesment profile2 = new HonestAssesment()
{
IntegrityPercentage = 50,
JusticeSensePercentage = 63,
TruthPercentage = 30
};
string inference_p2 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_expression, ResponseType.Json, profile2);

The inference on Profile 2, with 33% of Honest, that is "Sometimes honest", like a tutorialspoint figure.

{
"ID": 0,
"HitsPercentage": "33%",
"Data": {
"IntegrityPercentage": 50,
"TruthPercentage": 30,
"JusticeSensePercentage": 63,
"MistakesPercentage": 52
},
"PropertiesNeedToChange": [
"IntegrityPercentage",
"TruthPercentage"
],
"RatingsReport": [
false,
true,
false
],
"ErrorsQuantity": 2
}

HonestAssesment profile3 = new HonestAssesment()
{
IntegrityPercentage = 46,
JusticeSensePercentage = 48,
TruthPercentage = 30
};
string inference_p3 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_expression, ResponseType.Json, profile3);

The inference on Profile 3, with 0% of Honest, that is "Extremely dishonest", like a figure above:

{
"ID": 0,
"HitsPercentage": "0%",
"Data": {
"IntegrityPercentage": 46,
"TruthPercentage": 30,
"JusticeSensePercentage": 48,
"MistakesPercentage": 58
},
"PropertiesNeedToChange": [
"IntegrityPercentage",
"JusticeSensePercentage",
"TruthPercentage"
],
"RatingsReport": [
false,
false,
false
],
"ErrorsQuantity": 3
}

HonestAssesment profile4 = new HonestAssesment()
{
IntegrityPercentage = 91,
JusticeSensePercentage = 83,
TruthPercentage = 81
};
string inference_p4 = FuzzyLogic<HonestAssesment>.GetInferenceResult
(_expression, ResponseType.Json, profile4);

The inference on Profile 4, with 100% of Honest, that is "Extremely honest", like a figure assessment.

{
"ID": 0,
"HitsPercentage": "100%",
"Data": {
"IntegrityPercentage": 91,
"TruthPercentage": 81,
"JusticeSensePercentage": 83,
"MistakesPercentage": 15
},
"PropertiesNeedToChange": [],
"RatingsReport": [
true,
true,
true
],
"ErrorsQuantity": 0
}

### 3.2) Design Pattern

The 'Fuzzy Logic API' developed with Singleton Design Pattern, structured with one `private`

constructor, which has two arguments parameters: one Expression object and one POCO object (defined in Generic parameter); but the developer will get the inference result by one line of code:

Inference<ModelToInfere> inferObj = FuzzyLogic<ModelToInfere>.GetInferenceResult
(_expressionArg, modelObj);
string inferXml = FuzzyLogic<ModelToInfere>.GetInferenceResult
(_expressionArg, ResponseType.Xml, modelObj);
string inferJson = FuzzyLogic<ModelToInfere>.GetInferenceResult
(_expressionArg, ResponseType.Json, modelObj);

### 3.3) Dependencies

To add Fuzzy Logic API as an Assembly or like inner classes in your Visual Studio project, you'll need to install System.Linq.Dynamic dll, that can be installed by nuget reference or execute command on Nuget Package Console (`Install-Package System.Linq.Dynamic`

).

## History

- 11
^{th} June, 2019: Initial version