问题描述
我有一条规则,我不知道是否可以在流口水中实施。
说明: A
, L
, P
都是已连接的事实/ POJO。我需要找出是否每个 A
都可以通过 L分配给免费的
P
/ code>。如果没有,那么将剩下多少 A
个元素未分配。
在一个 我想出了一种简单描述的算法: 查找边缘最少的 从中选择随机 A $ c以上的情况下$ p>
并删除
A
A
没有边,将结果计数器增加1,删除 A
L
-> P
code> A A
, L
, P
元素
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:
Find
A
with least edges- If
A
has no edges, increase result counter by 1, removeA
- If
Choose random
L
->P
from thatA
and remove theA
,L
,P
elements- 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
这篇关于流口水的规则实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!