流口水的规则实施 [英] Drools rule implementation

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

问题描述

我有一条规则,我不知道是否可以在流口水中实施。



说明: A L P 都是已连接的事实/ POJO。我需要找出是否每个 A 都可以通过 L分配给免费的 P / code>。如果没有,那么将剩下多少 A 个元素未分配。





在一个 A

我想出了一种简单描述的算法:


  1. 查找边缘最少的 A




    • 如果 A 没有边,将结果计数器增加1,删除 A


  2. 从中选择随机 L -> P code> A 并删除 A L P 元素


  3. 重复直到没有 A 为止

我在流口水中很难描述这一点。我不是流口水规则的专家。在JAVA中,您必须对集合进行很多操作,包括排序,而流口水似乎不支持。

解决方案

下面是实现该算法的一组规则。对于大量的As和Ps,效率不是很高。纯粹的Java解决方案也不应该那么困难。请注意,从集合中删除一个A并清除所有悬空的L和P对象后,不需要完全排序。

 规则findMin 

$ mina:A($ edges:edge)
不是A(edge.size()< $ edges.size())
然后
System.out.println( retract + $ mina);
收起($ mina);
结束

规则计数当
eval($ edges.size()== 0)
然后
Main.counter ++时,失败扩展findMin
;
System.out.println(不匹配 + $ mina);
end

规则matchLP在
$ l:L(this memberOf $ edges,$ p:p)
然后
时扩展findMin
缩回($ p);
收起($ l);
System.out.println(缩回 + $ p +和 + $ l);
结束

规则清理
显着性10

$ l时:L($ p:p)
不是P(this == $ p)
$ a:A(边包含$ l)
然后
退回($ l);
Modify($ a){
remove($ l);
}
System.out.println( cleanup + $ l +, + $ a);
结束


I have a rule that I do not know if it is possible to implement in drools.

The description: A, L, P are all facts/POJOs which are connected. I need to find out if each A can be assigned to a free P through L. And if not, how many A elements are left unassigned.

In the case above one A will be left unassigned.

I thought up an algorithm that is simple to describe:

  1. Find A with least edges

    • If A has no edges, increase result counter by 1, remove A
  2. Choose random L->P from that A and remove the A, L, P elements

  3. Repeat until there are no A left

I'm having a hard time describing that in drools. I'm no expert in drools rules. In JAVA, you'd have to do a lot of manipulations with collections, including sorting, which drools does not seem to support. Is it possible to do this somehow in drools?

解决方案

Here is a set of rules that implements the algorithm. It will not be very efficient on large numbers of As and Ps. A pure Java solution shouldn't be all that difficult either. Note that a complete sort isn't necessary after removing one A from the set and clearing away all dangling L and P objects.

rule findMin
when
    $mina: A( $edges: edges )
    not A( edges.size() < $edges.size() )
then
    System.out.println( "retract " + $mina );
    retract( $mina );
end

rule countFail extends findMin
when
    eval( $edges.size() == 0 )
then
    Main.counter++;
    System.out.println( "fail to match " + $mina );
end

rule matchLP extends findMin
when
    $l: L( this memberOf $edges, $p: p )
then
    retract( $p );
    retract( $l );
    System.out.println( "retract " + $p + " and " + $l );
end

rule cleanup
salience 10
when
    $l: L( $p: p )
    not P( this == $p )
    $a: A( edges contains $l )
then
    retract( $l );
    modify( $a ){
        remove( $l );
    }
    System.out.println( "cleanup " + $l + ", " + $a );
end

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

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