Click here to Skip to main content
15,888,610 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.3K   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
// ColorMixConsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include "KnowledgeBase.h"

using namespace std;

#ifndef __GNUC__
void pause()
{
    system ("pause");
}
#endif

EDS::CKnowledgeBase m_TableEvaluator;
map<string, string> mAppData;

string GetSingleSolution(string tableToEvaluate, string nameOfOutput) //could reuse this function for all similar aplication events
{
  vector<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.size() > 0)
    return results[0];
  else
    return ""; //shouldn't happen since we have a "fallout" rule at the end of the table for unspecified combos
}

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

int main(int argc, char* argv[])
{
	cout<<"Loading the rules file ColorRules.xml...";
	m_TableEvaluator.CreateKnowledgeBase("ColorRules.xml");
	m_TableEvaluator.SetInputValueGetter([](const string& attrName, void* context)
	{
		string retval;
		if (mAppData.find(attrName) != end(mAppData))
		{
			retval = mAppData[attrName];
		}
		return retval;
	});
	cout<<"done\n";

	cout<<"Enter red or blue for first paint color:";
	cin>>mAppData["PaintColor1"];
	cout<<"Enter yellow or blue for second paint color:";
	cin>>mAppData["PaintColor2"];

	cout<<"The result is: " + GetResultingColor() + "\n";

	pause();
}

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