基于Python规则的引擎 [英] Python Rule Based Engine

查看:1704
本文介绍了基于Python规则的引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求设计一个基本上需要根据输入做出决策的系统.输入将是一个人.

I am looking to design a system that will essentially need to make decisions based on input. The input will be a person.

class Person:
    def __init__(self, name, age, sex, weight, height, nationality):
        self.name = name
        self.age = age
        self.sex = sex
        self.weight = weight
        self.height = height
        self.nationality = nationality

我们希望根据某些规则将每个人分配到一个学校班级.

We want to assign each person to a school class based on certain rules.

例如:

22至25岁之间的英国妇女应参加B级课程. 75岁以上的男性应该参加A级. 6英尺以上的女性应参加C级课程.

Women from the UK between 22-25 should go to class B. Men over 75 should go to class A. Women over 6ft should go to class C.

我们将有大约400个不同的规则,并且应该应用第一个满足的规则-我们需要保持规则的顺序.

We will have approximately 400 different rules and the first rule that is met should be applied - we need to maintain the order of the rules.

我正在考虑如何在此处存储/表示规则.显然,您可能只有一条很长的if, elif, elif语句,但这并不有效.另一种选择是将规则存储在数据库中,并可能在内存表中.

I am thinking about how to store/represent the rules here. Obviously, you could just have a veeeery long if, elif, elif statement but this isn't efficient. Another option would be storing the rules in a database and maybe having an in memory table.

我希望能够在不发布规则的情况下编辑规则-可能具有前端,以允许非技术人员添加,删除和重新排序规则.

I would like to be able to edit the rules without doing a release - possibly having a front end to allow non tech people to add, remove and reorder rules.

这里所有内容都在表上-唯一确定的要求是实际的编程语言必须是Python.

Everything is on the table here - the only certain requirement is the actually programming language must be Python.

添加了更多背景信息

我想我的问题是如何存储规则.目前,这是一条很大的长if elif elif语句,因此,只要业务逻辑发生变化,PM就会制定新规则,然后将它们转换为if语句.

I suppose my question is how to store the rules. At the moment it is one huge long if elif elif statement so anytime there is a change to the business logic the PM does up the new rules and I then convert them to the if statement.

所有输入到系统的信息将通过相同的规则列表发送,并且将应用第一个匹配的规则.多个规则可以应用于每个输入,但始终是第一个应用的规则.

All inputs to the system will be sent through the same list of rules and the first rule that matches will be applied. Multiple rules can apply to each input but it's always the first that is applied.

例如

25岁以上的女性进入B级课程
女人上A级.

Women over 25 go to Class B
Women go to Class A.

即使第二条规则也适用,所有25岁以上的女性都将被送进B级.

Any women over 25 will be sent to class B even though the second rule also applies.

输入将始终包含相同格式的输入-尚未确定它将是对象还是字典的位置,但是某些值可能是None.有些人可能没有与之相关的体重.

Input will always contain the same format input - haven't decided where it will be an object or a dict but some of the values may be None. Some Persons may not have a weight associated with them.

推荐答案

我建议您使用一些易于使用的解决方案,而不是重新发明轮子.有几种专家系统,我将重点介绍那些可以使用Python或可以通过Python使用的系统.

Rather than re-inventing the wheel, I'd suggest you to use some readily available solution. There are several expert systems out there and I'll focus on those which are either in Python or can be used via Python.

CLIPS 是最初由NASA开发的专家系统.它被认为是最先进的,并且在教授AI基础知识时用于大学课程.由于其出色的文档记录,这是一个很好的起点.

CLIPS is an expert system originally developed by NASA. It's considered state of the art and used in university courses when teaching basics of AI. It's a great starting point due to its excellent documentation.

它的语法绝对不是Python,而是让人想起Lisp. CLIPS的优点是它是一个可靠的C引擎,可以通过其绑定与任何其他Python系统完全集成:较旧的 clipspy .绑定允许将Python代码嵌入CLIPS语言中,从而使其易于扩展.

Its syntax is definitely not Python, it rather reminds of Lisp. The advantage of CLIPS is that is a solid C engine which can be fully integrated with any other Python system via its bindings: the older pyclips and the newer clipspy. The bindings allow to embed Python code within the CLIPS language making it very easy to extend.

可以在运行时加载规则,而无需重新启动引擎,这将更适合您的需求.

Rules can be loaded at runtime without the need of restarting the engine which should better suit your need.

Python Knowledge Engine 这是一个非常强大的逻辑编程框架.对于CLIPSPyKE带有自己的语法来表达规则,并依靠Python来实现机制.

The Python Knowledge Engine it's a fairly powerful logic programming framework. As for CLIPS, PyKE comes with its own syntax to express rules and relies on Python for implementing the mechanics.

换句话说,您用Python编写了做什么,并通过规则表达了时间和方式.

In other words, you write what to do in Python and you express when and how via rules.

可以根据需要激活和禁用规则.这应该使您支持无版本更新.

Rules can be activated and deactivated on demand. This should allow you to support releaseless updates.

Durable Rules 是一个相当新的项目,旨在支持多种编程语言(Python,Node .js和Ruby).

Durable Rules is a fairly new project with the ambition of supporting multiple programming languages (Python, Node.js and Ruby so far).

Durable Rules允许您用Python编写整个知识库(事实和规则).语法可能看起来有些怪异,但在此之后请注意这方面的内容.

Durable Rules allow you to write the whole knowledge base (facts and rules) in Python. The syntax might look a bit weird though, a note in this regards at the end of the post.

我不确定在系统在线时是否可以更新规则集.

I am not sure if you can update the rule-set while the system is online.

除了对多种语法的支持之外,我对这个项目感兴趣的是核心是 RETE 建立在Redis DB之上.从长远来看,这可能会导致一些有趣的发展.

Apart from the multiple syntax support, what interest me of this project is the fact the core is a C based implementation of RETE built on top of Redis DB. On a long run, this might lead to some interesting development.

PyKnow

智力

业务规则

这些项目允许大多数情况下使用Python表达知识库.我从未见过他们的实际应用,也不确定他们的性能和功能支持.

These projects allow to express knowledge bases mostly in Python. I have never seen them in action and I am not sure about their performance and feature support.

我建议不要将自己的规则引擎用于生产的主要原因是,尽管起初看似很容易,但很快就会发现问题域比预期的要大.

The main reason I recommend against brewing your own rule engine to use in production is that, despite it seems an easy task at first, it quickly becomes evident that the problem domain is way bigger than predicted.

Python OOP本质最初似乎很适合表达知识,因为Rule和Facts都可以是简单的类. 但是,一旦模式匹配变得更加复杂(Employee必须在Company中工作了3年以上,而Stock在最近3年中的价值<10 $),就会发现两件事.

Python OOP nature seems initially a good fit for expressing knowledge as both Rules and Facts could be simple classes. Nevertheless, as soon as the pattern matching becomes a little more complex (Employee must have worked > 3 years in Company which Stock value is < 10$ in the last 3 years) two things become evident.

  1. 仅使用andornotis,...的局限性使得事情真的很难阅读
  2. 该问题突然显示出它的指数性质,性能成为主要问题
  1. The limitation of simply using and, or, not, is, ... make things really hard to read
  2. The problem suddenly reveals its exponential nature and performance become a major concern

此外,强迫组织的员工使用另一种内部构建的语言通常是一个坏主意.它阻止他们学习在更广泛的上下文中使用的内容,例如CLIPS Drools 长时间处于维护/文档循环中.

Moreover, forcing the employees of an organization to use yet-another-in-house-built-language is usually a bad idea. It prevents them from learning something used in broader contexts such as CLIPS or Drools and will get you stuck in a maintenance/documentation loop for a long time.

这篇关于基于Python规则的引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆