0

I have a need to implement a special logical operations algorithm. The task has been assigned to store the logical conditions in a database and have C# code read the information and select the correct result.

Here is the scenario: This company uses what they call a config code for all their products. The configuration code is typically 50 digits but in rare cases can be longer.
Based on certain digit values within the config code, I will need to select a specific software version.

Lets say Digits 5-7 in the config code must be ‘LLP’, AND digit 9 must be X OR Y. This would lead to a software version of v01.1.1. The logical operation would look something like this [(D5-7=’LLP’ && D9=’X’) || (D5-7=’LLP’ && D9=’Y’)], where D = The digit value in the configuration code.

The C# code must be able to read the Expression and determine the version code. I could write this in native C# code but the customer does not want to change the C# application every time there is a new version. The database itself would not be large, probably less than 100 records. How do I design and implement an algorithm that will read logical expressions stored in the database? I know how to select tables in a database, what I’m struggling with most is the evaluation expression.

  • This sounds like a rules engine. But does the rules have to be stored in the database, or could they be stored in json/xml? – Bent Jul 31 '18 at 21:18
  • 1
    Could you use Regular Expressions? – Robert Harvey Jul 31 '18 at 22:13
  • "Rules engine" I have to look that up. Yes, it can be in XML but would not reside on the local machines. – MTH Aug 1 '18 at 0:17
1

Sounds like you need to write a mini parser for the logical expressions. This is not as hard as it sounds. There are plenty of examples on line of such parsers. The simplest arithmetic expression parsers can be built using shunt-yard. I believe will work for boolean also.

Check this out.

https://stackoverflow.com/questions/16762057/algorithm-to-evaluate-value-of-boolean-expression

1

I had the same thought as Robert Harvey: why not use regular expressions? So you'd have a table of regexes and the corresponding version numbers. The example you give could certainly be done as a regex.

The regex strings become the "logical conditions" for picking out version numbers. Here is sample code. In this example the "regexToVersion" dictionary is hardwired, but in production code this would be the list of "logical conditions" that are read from a database.

public class RegexTest
{
    private static Dictionary<string, string> regexToVersion =
        new Dictionary<string, string> {
                { "^.{4}LLP.[XY]", "v01.1.1"},
                { "^.{4}ABC.[XY]", "v01.1.2"},
            };
    public static void Test()
    {
        string[] tests = new[] { "abc", "1234LLP-Xaaaa", "1234LLP-Y", "1234LLP-Z",  };
        foreach (string test in tests)
        {
            Console.WriteLine($"Test {test}: {GetVersion(test)}");
        }
    }

    public static string GetVersion(string configCode)
    {
        foreach (var regex in regexToVersion)
        {
            if (Regex.IsMatch(configCode, regex.Key))
            {
                return regex.Value;
            }
        }
        return null;
    }
}

Output is:
Test abc:
Test 1234LLP-Xaaaa: v01.1.1
Test 1234LLP-Y: v01.1.1
Test 1234LLP-Z:
  • I'm not an expert on regex and I may be thinking about it wrong, but I believe they require pattern matching. This would not work because the pattern(s) could change depending on the version(s) requirement(s). – MTH Aug 4 '18 at 2:26
  • 1
    @MTH You are correct that the patterns can change, but that just means you will have different regex strings depending on the changing requirements. Those regex strings are the "logical conditions" that you are going to store and use. So no new code needs to be written, just add or edit the regex strings. – DaveG Aug 4 '18 at 14:10
0

This sounds like good job for

http://www.castleproject.org/projects/windsor/

Intro to castle

If you dig into xml configuration you can define classes in xml with your rules. You could do whole meta programming with xml config of castle. Funny thing you could also interact with system via console and create classes, change stuff on fly. Unfortunately I cannot share any of my code examples because that was on the job.

  • Castle Windsor is an IoC Container. Are you perhaps thinking of DynamicProxy? – Robert Harvey Aug 1 '18 at 1:09
  • @RobertHarvey yes and IoC container lets you modify code at runtime. You configure your object graph usually at start but then you have tons of options to change it while system is running (not that you should be doing that, but you can). – Mateusz Aug 1 '18 at 7:24

protected by gnat Aug 4 '18 at 1:33

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.