如何使用Python中的规则引擎生成流程图?

huangapple go评论61阅读模式
英文:

How to generate a flow chart using the rules engine in python?

问题

以下是您提供的代码的部分翻译:

我想使用我在规则引擎中定义的规则生成一个流程以下是代码

    from pyknow import *
    import pydot
    
    class Wine(Fact):
        """葡萄酒特性"""
        pass
    
    class Quality(Fact):
        """葡萄酒质量"""
        pass
    
    class WineExpert(KnowledgeEngine):
        @Rule(Wine(color='red'))
        def rule1(self):
            self.declare(Quality(color='red', quality='rich'))
    
        @Rule(Wine(color='white'))
        def rule2(self):
            self.declare(Quality(color='white', quality='crisp'))
    
        @Rule(AND(
            Wine(color='red'),
            Wine(body='light'),
            Wine(tannin='low')
        ))
        def rule3(self):
            self.declare(Quality(color='red', quality='smooth'))
    
        @Rule(AND(
            Wine(color='red'),
            Wine(body='full'),
            Wine(tannin='high')
        ))
        def rule4(self):
            self.declare(Quality(color='red', quality='bold'))
    
    # 创建新的知识引擎实例
    engine = WineExpert()
    
    # 创建Pydot图
    graph = pydot.Dot(graph_type='digraph', comment='葡萄酒专家规则')
    
    # 为每个规则添加节点
    for rule in engine.get_rules():
        node = pydot.Node(rule.__name__)
        graph.add_node(node)
    
        # 检查AND条件
        if isinstance(rule.new_conditions, AND):
            for condition in rule.new_conditions.args:
                label = str(condition).replace("<", "\\&lt;").replace(">", "\\&gt;")
                edge = pydot.Edge(label, rule.__name__)
                graph.add_edge(edge)
        else:
            label = str(rule.new_conditions).replace("<", "\\&lt;").replace(">", "\\&gt;")
            edge = pydot.Edge(label, rule.__name__)
            graph.add_edge(edge)
    
    # 将图保存到文件
    graph.write_png('wine_expert_rules.png')

我得到了一个我无法解释的图形有人可以告诉我如何获得一个良好的流程图吗

请注意,这只是您提供代码的部分翻译。如果您需要有关代码的更多详细信息或解释,请随时提出。

英文:

I would like to generate a flow using the rules that I defined in the rules engine. Below is the code

from pyknow import *
import pydot
class Wine(Fact):
&quot;&quot;&quot;Wine characteristics&quot;&quot;&quot;
pass
class Quality(Fact):
&quot;&quot;&quot;Wine quality&quot;&quot;&quot;
pass
class WineExpert(KnowledgeEngine):
@Rule(Wine(color=&#39;red&#39;))
def rule1(self):
self.declare(Quality(color=&#39;red&#39;, quality=&#39;rich&#39;))
@Rule(Wine(color=&#39;white&#39;))
def rule2(self):
self.declare(Quality(color=&#39;white&#39;, quality=&#39;crisp&#39;))
@Rule(AND(
Wine(color=&#39;red&#39;),
Wine(body=&#39;light&#39;),
Wine(tannin=&#39;low&#39;)
))
def rule3(self):
self.declare(Quality(color=&#39;red&#39;, quality=&#39;smooth&#39;))
@Rule(AND(
Wine(color=&#39;red&#39;),
Wine(body=&#39;full&#39;),
Wine(tannin=&#39;high&#39;)
))
def rule4(self):
self.declare(Quality(color=&#39;red&#39;, quality=&#39;bold&#39;))
# Create a new knowledge engine instance
engine = WineExpert()
# Create a Pydot graph
graph = pydot.Dot(graph_type=&#39;digraph&#39;, comment=&#39;Wine Expert Rules&#39;)
# Add nodes for each rule
for rule in engine.get_rules():
node = pydot.Node(rule.__name__)
graph.add_node(node)
# Check for AND conditions
if isinstance(rule.new_conditions, AND):
for condition in rule.new_conditions.args:
label = str(condition).replace(&quot;&lt;&quot;, &quot;\\&lt;&quot;).replace(&quot;&gt;&quot;, &quot;\\&gt;&quot;)
edge = pydot.Edge(label, rule.__name__)
graph.add_edge(edge)
else:
label = str(rule.new_conditions).replace(&quot;&lt;&quot;, &quot;\\&lt;&quot;).replace(&quot;&gt;&quot;, &quot;\\&gt;&quot;)
edge = pydot.Edge(label, rule.__name__)
graph.add_edge(edge)
# Save the graph to a file
graph.write_png(&#39;wine_expert_rules.png&#39;)

I am getting a graph which I can't interpret it. Can anyone tell me how to get a good flowchart out of it?

答案1

得分: 1

以下是您要翻译的内容:

There seems to be some confusion with decorators and classes. Also, you never execute the rule. Do you expect quality to appear on your flow chart? If so, you should run the rules in your loop. Note: I'm also returning Quality to avoid accessing the engine's agenda and drawing AND nodes (also using experta, which is a fork and available at pypi):

from experta import *
import pydot

class Wine(Fact):
    """Wine characteristics"""
    pass

class Quality(Fact):
    """Wine quality"""
    pass

class WineExpert(KnowledgeEngine):
    @Rule(Wine(color='red'))
    def rule1(self):
        return self.declare(Quality(color='red', quality='rich'))

    @Rule(Wine(color='white'))
    def rule2(self):
        return self.declare(Quality(color='white', quality='crisp'))

    @Rule(AND(
        Wine(color='red'),
        Wine(body='light'),
        Wine(tannin='low')
    ))
    def rule3(self):
        return self.declare(Quality(color='red', quality='smooth'))

    @Rule(AND(
        Wine(color='red'),
        Wine(body='full'),
        Wine(tannin='high')
    ))
    def rule4(self):
        return self.declare(Quality(color='red', quality='bold'))

# Create a new knowledge engine instance
engine = WineExpert()
engine.reset()
# Create a Pydot graph
graph = pydot.Dot(graph_type='digraph', comment='Wine Expert Rules')

and_counter = 0
# Add nodes for each rule
for rule_instance in engine.get_rules():
    quality = rule_instance()
    qual_node = pydot.Node(repr(quality))
    graph.add_node(qual_node)
    for rule in rule_instance:
        # Check for AND conditions
        if isinstance(rule, AND):
            and_node = pydot.Node(name=and_counter, label='AND')
            graph.add_node(and_node)
            and_counter += 1
            for condition in rule:
                cond_node = pydot.Node(repr(condition))
                graph.add_node(cond_node)
                edge = pydot.Edge(cond_node, and_node)
                graph.add_edge(edge)
            edge = pydot.Edge(and_node, qual_node)
            graph.add_edge(edge)
        else:
            cond_node = pydot.Node(repr(rule))
            graph.add_node(cond_node)
            edge = pydot.Edge(cond_node, qual_node)
            graph.add_edge(edge)

# Save the graph to a file
graph.write_png('wine_expert_rules.png')

输出: 如何使用Python中的规则引擎生成流程图?

英文:

There seems to be a some confusion with decorators and classes. Also you never execute the rule. Do you expect quality to appear on your flow chart? If so, you should run the rules in your loop.
Note: I'm also returning Quality to avoid accessing the engine's agenda and drawing AND nodes (also using experta which is a fork and available at pypi):

from experta import *
import pydot

class Wine(Fact):
    &quot;&quot;&quot;Wine characteristics&quot;&quot;&quot;
    pass

class Quality(Fact):
    &quot;&quot;&quot;Wine quality&quot;&quot;&quot;
    pass

class WineExpert(KnowledgeEngine):
    @Rule(Wine(color=&#39;red&#39;))
    def rule1(self):
        return self.declare(Quality(color=&#39;red&#39;, quality=&#39;rich&#39;))

    @Rule(Wine(color=&#39;white&#39;))
    def rule2(self):
        return self.declare(Quality(color=&#39;white&#39;, quality=&#39;crisp&#39;))

    @Rule(AND(
        Wine(color=&#39;red&#39;),
        Wine(body=&#39;light&#39;),
        Wine(tannin=&#39;low&#39;)
    ))
    def rule3(self):
        return self.declare(Quality(color=&#39;red&#39;, quality=&#39;smooth&#39;))

    @Rule(AND(
        Wine(color=&#39;red&#39;),
        Wine(body=&#39;full&#39;),
        Wine(tannin=&#39;high&#39;)
    ))
    def rule4(self):
        return self.declare(Quality(color=&#39;red&#39;, quality=&#39;bold&#39;))

# Create a new knowledge engine instance
engine = WineExpert()
engine.reset()
# Create a Pydot graph
graph = pydot.Dot(graph_type=&#39;digraph&#39;, comment=&#39;Wine Expert Rules&#39;)

and_counter = 0
# Add nodes for each rule
for rule_instance in engine.get_rules():
    quality = rule_instance()
    qual_node = pydot.Node(repr(quality))
    graph.add_node(qual_node)
    for rule in rule_instance:
        # Check for AND conditions
        if isinstance(rule, AND):
            and_node = pydot.Node(name=and_counter, label=&#39;AND&#39;)
            graph.add_node(and_node)
            and_counter += 1
            for condition in rule:
                cond_node = pydot.Node(repr(condition))
                graph.add_node(cond_node)
                edge = pydot.Edge(cond_node, and_node)
                graph.add_edge(edge)
            edge = pydot.Edge(and_node, qual_node)
            graph.add_edge(edge)
        else:
            cond_node = pydot.Node(repr(rule))
            graph.add_node(cond_node)
            edge = pydot.Edge(cond_node, qual_node)
            graph.add_edge(edge)

# Save the graph to a file
graph.write_png(&#39;wine_expert_rules.png&#39;)

Output:

如何使用Python中的规则引擎生成流程图?

huangapple
  • 本文由 发表于 2023年3月9日 22:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定