简体   繁体   English

简单的Python规则匹配

[英]Simple Python rules matching

I'm attempting to create a simple rule matching engine where if any rules match from a dictionary:我正在尝试创建一个简单的规则匹配引擎,如果有任何规则与字典匹配:

{'id': [{'status1' : 'v1'}  , {'status3' : 'v2'} , {'status1' : 'v2'}] }

and the rules are:规则是:

{'rule1': {'status1': ['v1'], 'status2': ['v2' , 'v3']},
              'rule2': {'status1': ['v2'], 'status2': ['v4']}}

The rule matching should produce the output:规则匹配应生成 output:

[('status1', 'v1', ['v1'], 'rule1'), ('status1', 'v2', ['v2'], 'rule2')]

('status1', 'v1', ['v1'], 'rule1') corresponds to status1 matching on rule1 as rule1 contains the value v1 for status1 ('status1', 'v1', ['v1'], 'rule1')对应于 rule1 上的 status1 匹配,因为 rule1 包含 status1 的值 v1

('status1', 'v2', ['v2'], 'rule2') corresponds to status1 matching on rule2 as rule2 contains the value v2 for status1 ('status1', 'v2', ['v2'], 'rule2')对应于在 rule2 上匹配的 status1,因为 rule2 包含 status1 的值 v2

Here is the code I've written:这是我写的代码:

values_to_validate = {'id': [{'status1' : 'v1'}  , {'status3' : 'v2'} , {'status1' : 'v2'}] }

rule_set_1 = {'rule1': {'status1': ['v1'], 'status2': ['v2' , 'v3']},
              'rule2': {'status1': ['v2'], 'status2': ['v4']}}

class RulesConfig() :
    
    def __init__(self , rules):
        self.rules = rules

    def get_rule_names(self):
        return self.rules.keys()
    
    def get_conditions_for_rule(self, rule_name):
        return self.rules[rule_name]
    
    def get_matched_rules(self, values_to_validate):
        is_matched = False
        matched_rules = []
        for v in values_to_validate.values() : 
            for rk,rv in self.rules.items() :
                for a in v : 
                    for d,f in a.items() : 
                        if d in rv : 
                            matched_rules.append((d,f , rv[d] , rk))

        return matched_rules

    
v = RulesConfig(rule_set_1)
print(v.get_matched_rules(values_to_validate))

which returns:返回:

[('status1', 'v1', ['v1'], 'rule1'), ('status1', 'v2', ['v1'], 'rule1'),
 ('status1', 'v1', ['v2'], 'rule2'), ('status1', 'v2', ['v2'], 'rule2')]

To return the expected output I could remove duplicated entries in the above list of tuples but I think there is a mistake in my algorithm.要返回预期的 output 我可以删除上述元组列表中的重复条目,但我认为我的算法有错误。

The code I use to find the matched rules:我用来查找匹配规则的代码:

def get_matched_rules(self, values_to_validate):
    is_matched = False
    matched_rules = []
    for v in values_to_validate.values() : 
        for rk,rv in self.rules.items() :
            for a in v : 
                for d,f in a.items() : 
                    if d in rv : 
                        matched_rules.append((d,f , rv[d] , rk))

seems unnecessary complex, is there an idiomatic preferred Python method of solving this type of problem?似乎不必要的复杂,是否有解决此类问题的惯用首选 Python 方法? I'm using Python 3.6 and do not want to include libraries that are not included as part of standard Python interpreter.我正在使用 Python 3.6 并且不想包含未包含在标准 Python 解释器中的库。

This test is insufficient:这个测试是不够的:

if d in rv : 
    matched_rules.append((d,f , rv[d] , rk))

This will check only if 'status1' for example is in the rule dictionary, you also need to check whether it has the right value:这将仅检查“status1”例如是否在规则字典中,您还需要检查它是否具有正确的值:

if d in rv and f in rv[d]: 
    matched_rules.append((d,f , rv[d] , rk))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM