简体   繁体   English

基于层次匹配的规则引擎

[英]Rule Engine based on hierarchical matching

Say, I have a dataset (cities) with this structure:说,我有一个具有这种结构的数据集(城市):

  • ID ID
  • City城市
  • State状态
  • Country国家
  • Continent大陆

And I have a configuration table (key, value) where the key can be a combination of above parameters我有一个配置表(键,值),其中键可以是上述参数的组合

For eg:例如:

{
    "continent": "asia"
}

or或者

{
    "continent": "asia",
    "country" : "india"
}

or或者

{
    "continent": "asia",
    "country" : "india",
    "state" : "maharashtra",
    "city" : "mumbai"
}

Now, I want to do a closest match of cities from the 1st dataset to the entries in configuration.现在,我想对第一个数据集中的城市与配置中的条目进行最接近的匹配。

For example, if I have例如,如果我有

city: mumbai, state: maharashtra, country: india, continent: asia

it should match, 3rd entry in the config above.它应该匹配,上面配置中的第三个条目。

If I have如果我有

city: tokyo, state: Kantō, country: japan, continent: asia

it should match 1st entry in the config above.它应该匹配上面配置中的第一个条目。

I am looking for suggestions if something is readily available for this kind of a scenario.我正在寻找建议,如果有什么可以用于这种场景。

I am open to storing the configurations in some different way if there is any.如果有的话,我愿意以不同的方式存储配置。

Ideally I would like a solution in which I can pass multiple entries (inputs) and it should return the closest matching configuration for each of the inputs.理想情况下,我想要一个可以传递多个条目(输入)的解决方案,并且它应该为每个输入返回最接近的匹配配置。

With the risk that I misunderstand the question, I would:冒着误解这个问题的风险,我会:

  • collect configuration entries in a tree-like data structure, for faster retrieval以树状数据结构收集配置条目,以便更快地检索
  • find a match for a single data entry at a time, by traversing through that tree通过遍历该树,一次找到单个数据条目的匹配项

Here is how that would look:这是它的外观:

class Config:
    def __init__(self):
        self.continents = {}

    def add(self, config):
        collection = self.continents
        for prop in ["continent", "country", "state", "city"]:
            if prop not in config:
                break
            key = config[prop]
            if key not in collection:
                collection[key] = {}
            collection = collection[key]
        collection["config"] = config

    def get(self, data):
        collection = self.continents
        config = None
        for prop in ["continent", "country", "state", "city"]:
            if prop not in data or data[prop] not in collection:
                break
            collection = collection[data[prop]]
            if "config" in collection:
                config = collection["config"]
        return config

The above class can be used as follows.上面的类可以如下使用。

First create an instance, and populate it with configuration entries:首先创建一个实例,并用配置条目填充它:

config = Config()

config.add({ "continent": "asia"})
config.add({ "continent": "asia", "country": "india" })
config.add({ "continent": "asia", "country": "india", 
             "state": "maharashtra", "city": "mumbai" })

Then iterate your data entries, and for each call config.get to get the best matching configuration.然后迭代您的数据条目,并为每次调用config.get以获得最佳匹配配置。 For example:例如:

print(config.get({ "city": "mumbai", "state": "maharashtra", 
                   "country": "india", "continent": "asia"}))

print(config.get({"city": "tokyo", "state": "kantō", 
                   "country": "japan", "continent": "asia"}))

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

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