규칙집합

2026년 3월 27일 09:30분

class Rule:
    def __init__(self, rule_id: str, command: str, guard_key: str):
        self.rule_id = rule_id
        self.command = command
        self.guard_key = guard_key

class RuleEngine:
    def __init__(self):
        self.rules = []

    def add(self, r: Rule):
        self.rules.append(r)

    def evaluate(self, actions: list) -> list:
        fired = []
        for a in actions:
            for r in self.rules:
                if a == r.guard_key:
                    fired.append(f"{r.rule_id} => {r.command}")
        return fired

engine = RuleEngine()

engine.add(Rule("L19-01", "거룩하라 / Be holy", "identity"))
engine.add(Rule("L19-02", "부모 공경 / Honor parents", "parents"))
engine.add(Rule("L19-03", "안식일 지킴 / Keep sabbath", "sabbath"))
engine.add(Rule("L19-04", "우상 금지 / No idols", "idols"))
engine.add(Rule("L19-05", "이웃 사랑 / Love neighbor", "neighbor"))

actions = ["parents", "idols", "neighbor"]

out = engine.evaluate(actions)

for x in out:
    print(x)

package com.jesusbornd.leviticus;

import java.util.ArrayList;
import java.util.List;

public class Leviticus_19_Chapter_Lv3 {

    public static class Rule {
        private final String id;
        private final String command;
        private final String guardKey;

        public Rule(String id, String command, String guardKey) {
            this.id = id;
            this.command = command;
            this.guardKey = guardKey;
        }

        public String id() { return id; }
        public String command() { return command; }
        public String guardKey() { return guardKey; }
    }

    public static class RuleEngine {
        private final List<Rule> rules = new ArrayList<Rule>();

        public void add(Rule r) {
            rules.add(r);
        }

        public List<String> evaluate(List<String> actions) {
            List<String> fired = new ArrayList<String>();

            for (int i = 0; i < actions.size(); i++) {
                String a = actions.get(i);

                for (int j = 0; j < rules.size(); j++) {
                    Rule r = rules.get(j);
                    if (a.equals(r.guardKey())) {
                        fired.add(r.id() + " => " + r.command());
                    }
                }
            }

            return fired;
        }
    }

    public static void main(String[] args) {
        RuleEngine engine = new RuleEngine();

        engine.add(new Rule("L19-01", "거룩하라 / Be holy", "identity"));
        engine.add(new Rule("L19-02", "부모 공경 / Honor parents", "parents"));
        engine.add(new Rule("L19-03", "안식일 지킴 / Keep sabbath", "sabbath"));
        engine.add(new Rule("L19-04", "우상 금지 / No idols", "idols"));
        engine.add(new Rule("L19-05", "이웃 사랑 / Love neighbor", "neighbor"));

        List<String> actions = new ArrayList<String>();
        actions.add("parents");
        actions.add("idols");
        actions.add("neighbor");

        List<String> out = engine.evaluate(actions);

        for (int i = 0; i < out.size(); i++) {
            System.out.println(out.get(i));
        }
    }
}

Comments

Avatar
 2026년 3월 27일 14:52분

여러 규칙이 동시에 적용되는 부분도 현실적인 판단 구조처럼 느껴집니다.



Search

← 목록으로