Skip to content

C++ logical rule trees 🇰🇷 C++ 논리적 규칙 트리

Notifications You must be signed in to change notification settings

JayTwoLab/cond3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cond3_rule_tree

Korean README

A C++17 library for building and evaluating complex rule trees using conditions and subjects. This project provides a flexible framework for defining conditions, grouping them into logical rule trees (AND, OR, NOT), and evaluating them against input data.

Features

  • Condition Expressions: Define conditions using operands, comparison operators, and expected values (single or list).
  • Rule Trees: Compose conditions into rule trees using logical operations (AND, OR, NOT).
  • Evaluation Engine: Evaluate rules against input subjects and get detailed results, including error reasons.
  • Extensible Types: Supports numbers, real numbers, and strings as value types.
  • String-Operator Overloads: Simplify condition definitions using string representations of operators.
  • Rule Parsing: Parse rule trees from boolean expression strings for easier rule definition.

Example Usage

Using Condition Expressions

rule_engine engine;

// Define conditions

// condition 11: LATITUDE < 42.0
engine.set_condition(11, "<", "LATITUDE", value{ 42.0 }); // double comparison

// condition 21: TEST INDICATOR == 0
engine.set_condition(21, "=", "TEST INDICATOR", value{ std::int64_t{0} }); // integer comparison

// condition 31: HELLO == "hello"
engine.set_condition(31, "=", "HELLO", value{ "hello" }); // string comparison

// condition 41: TEST INDICATOR IN [2,3,5]
engine.set_condition<std::int64_t>(41, "IN", "TEST INDICATOR", { 2, 3, 5 }); // integer IN list

// Rule tree: parse from string
// RULE = (11 AND (41 OR 31) AND NOT 21)
rule_node rule = cond3::parse_rule("(11 AND (41 OR 31) AND NOT 21)");

// Input (subjects)
rule_engine::subject_map subjects;

// Use helper from subject_utils.hpp so the key string is written only once
cond3::add_subject(subjects, "LATITUDE", value{ 38.5 }); // double match
cond3::add_subject(subjects, "TEST INDICATOR", value{ std::int64_t{3} }); // integer match
cond3::add_subject(subjects, "HELLO", value{ "hello" }); // string match

// Evaluate rule
auto r = engine.evaluate_rule(rule, subjects);

if (!r.ok) {
// error can happen e.g. when a subject key is missing.
    std::cout << "rule => error: " << to_string(r.error) << "\n";
    return 1;
}

std::cout << "rule => " << (r.value ? "true" : "false") << "\n";

Build Instructions

This project uses CMake (minimum version 3.16).

mkdir build
cd build
cmake ..
cmake --build .

Requirements

  • C++17 compatible compiler
  • CMake 3.16 or higher

License

MIT License

About

C++ logical rule trees 🇰🇷 C++ 논리적 규칙 트리

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published