How to Build a Simple Rule Engine for Medical Recommendation Systems
This article explains how to design and implement a lightweight rule engine using condition‑based rules and a simple database schema, illustrated with a medical prescription recommendation example, and includes Java code that evaluates logical expressions to determine when to display results.
Rule Engine: Essential Component for Large Systems
Rule engines enable flexible rule and control functions. This article presents a simple and efficient implementation based on condition configuration, illustrated with a medical prescription recommendation scenario.
1. Requirement Overview
The engine can be used in many domains. Here we focus on a rule engine that matches patient symptoms to suitable medicines, e.g., a female patient diagnosed with "qi blood deficiency", "thin body", "soft waist and knees", and "irregular menstruation" should be recommended the herbal formula "Wu Ji Bai Feng Wan".
2. System Design and Implementation
2.1 Database Design
The design includes three tables: SymptomRule, SymptomCondition, and ResultDisplay. Each rule has an ID, name, expression, and description. Conditions store identifiers, codes, comparison operators, and values. Results link to rule IDs.
Rule example: ID 1001, name "Wu Ji Bai Feng Wan Rule", expression "(A1 && A2 && A3 && A4 && A5)".
Condition examples: A1 = sex = female, A2 = blood = weak, A3 = body = thin, A4 = waist = limp, A5 = menses = menoxenia.
Result example: ID 1, name "Wu Ji Bai Feng Wan: treats female qi blood deficiency, thin body, soft waist and knees, irregular menstruation".
2.2 Code Implementation
The engine evaluates the logical expression by loading condition values and using a script engine to compute the result.
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class TestExpress {
public static void main(String[] args) throws Exception {
String expr = "(A1 && A2 && A3 && A4 && A5)";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("A1", true);
engine.put("A2", true);
engine.put("A3", true);
engine.put("A4", true);
engine.put("A5", true);
Object result = engine.eval(expr);
System.out.println(result);
}
}If any condition evaluates to false, the overall expression is false and no recommendation is shown; otherwise the matching result is displayed.
In summary, by defining rules as combinations of simple conditions stored in a database and evaluating them with a script engine, a flexible rule engine can be built for scenarios such as medical prescription recommendation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
