Click here to Skip to main content
15,893,668 members
Articles / Web Development / HTML

Logician: A Table-based Rules Engine Suite in C++/.NET/JavaScript using XML

Rate me:
Please Sign up or sign in to vote.
4.90/5 (52 votes)
8 Mar 2017GPL314 min read 104.4K   6.8K   173  
Overview of a cross-platform spreadsheet-based rules enigne with algorithms implemented in C++ and JavaScript, and wrappers to C#/.NET/WinRT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EDSNET;

namespace ColorMixConsoleCSharp
{
    class Program
    {
        static EDSEngine m_TableEvaluator = null;
        static Dictionary<string, string> mAppData = new Dictionary<string, string>();
        static void Main(string[] args)
        {
            Console.WriteLine("Loading the rules file ColorRules.xml...");
            m_TableEvaluator = new EDSEngine("ColorRules.xml");
            m_TableEvaluator.InputGetterDelegate = delegate(string attrName, object context)
            {
                if (mAppData.ContainsKey(attrName))
                    return mAppData[attrName];
                else
                    return "";
            };
            Console.WriteLine("done\n");

            Console.WriteLine("Enter red or blue for first paint color:");
            mAppData["PaintColor1"] = Console.ReadLine();
            Console.WriteLine("Enter yellow or blue for second paint color:");
            mAppData["PaintColor2"] = Console.ReadLine();

            Console.WriteLine("The result is: " + GetResultingColor() + "\n");

            pause();
        }

        static void pause()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }

        static string GetSingleSolution(string tableToEvaluate, string nameOfOutput) //could reuse this function for all similar aplication events
        {            
            string[] results = m_TableEvaluator.EvaluateTable(tableToEvaluate, nameOfOutput);
            //EDSEngine supports returning multiple true results on a sigle line, but in this case we just want a single result (the first one it finds)

            if (results.Length > 0)
                return results[0];
            else
                return ""; //shouldn't happen since we have a "fallout" rule at the end of the table for unspecified combos
        }

        static string GetResultingColor()
        {
            return GetSingleSolution("ColorMixingTable", "ResultColor");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
I have extensive experience developing software on both Linux and Windows in C++ and Python. I have also done a lot of work in the C#/.NET ecosystem. I currently work in the fields of robotics and machine learning, and also have a strong background in business automation/rules engines.

Comments and Discussions