复合值限制被用于超过一个可能的值匹配的地方。目前只支持 in 和 no in 运算符。 这个运算符的第二个操作数必须是一个逗号分开的值列表,用括号括起来的。值可以是用变量、字面文字、返回值或限定标识符提供的。这两个运算符实际是“语法糖(syntactic sugar)”,内部使用'!=' 和'=='运算符改写为多限制的一个列表。
rule "All Buses are Red"
when
forall( Bus( color == 'red' ) )
then
# all asserted Bus facts are red
end
另外一个例子,显示了 forall 内部的多个模式:
rule "all employees have health and dental care programs"
when
forall( $emp : Employee()
HealthCare( employee == $emp )
DentalCare( employee == $emp )
)
then
# all employees have health and dental care
end
为了完善的表现, forall 可以被嵌套在其他条件元素同部。 例如, forall 可以被用在 not 条件元素内部。
注意,单个模式时扩号是可选的,所以,使用嵌套 forall,必须使用扩号:
forall 与 not 条件元素的组合
rule "not all employees have health and dental care"
when
not ( forall( $emp : Employee()
HealthCare( employee == $emp )
DentalCare( employee == $emp ) )
)
then
# not all employees have health and dental care
end
rule "apply 10% discount to all items over US$ 100,00 in an order"
when
$order : Order()
$item : OrderItem( value > 100 ) from $order.items
then
# apply discount to $item
end
在上面的例子中,对每个给定的定单,每个项目的值大于 100 时,将使规则引发一次。
然而,在使用 form 时,你必须小心,特别是与 lock-on-active 规则属性联合使用时,因为它可能产生 不希望的结果。
考虑前面提供的例子,现在可略加修改如下:
rule "Assign people in North Carolina (NC) to sales region 1"
ruleflow-group "test"
lock-on-active true
when
$p : Person( )
$a : Address( state == "NC") from $p.address
then
modify ($p) {} #Assign person to sales region 1 in a modify block
end
rule "Apply a discount to people in the city of Raleigh"
ruleflow-group "test"
lock-on-active true
when
$p : Person( )
$a : Address( city == "Raleigh") from $p.address
then
modify ($p) {} #Apply discount to person in a modify block
end
当你可以直接断言所有事实到工作内存时, 首选解决方案是尽量减少 from 的使用。 在上面的例子中, Person 和 Address 实例都可以被断言到工作内存中。在这种情况下,因为该图相当简单,更容易的解决方案是如下这样修改你的规则
rule "Assign people in North Carolina (NC) to sales region 1"
ruleflow-group "test"
lock-on-active true
when
$p : Person(address.state == "NC" )
then
modify ($p) {} #Assign person to sales region 1 in a modify block
end
rule "Apply a discount to people in the city of Raleigh"
ruleflow-group "test"
lock-on-active true
when
$p : Person(address.city == "Raleigh" )
then
modify ($p) {} #Apply discount to person in a modify block
end
现在,你会发现两条规则如希望的一样被引发。然而,它并不总是可以访问上述的嵌套事实。考虑一 个例子,Person 持有一个或多个 Address,并且你希望使用一个存在的量词匹配至少有一个符合一定条件的地址的人。在这种情况下,你将不得不求助于 from 的使用,在集合上进行推断。有几种 from 的用法完成这种情况, 并且不是所有它们与 lock-on-active 一起使用会表现出问题。
例如,下面的 from 用法,使两条规则如希望的一样被引发
rule "Assign people in North Carolina (NC) to sales region 1"
ruleflow-group "test"
lock-on-active true
when
$p : Person($addresses : addresses)
exists (Address(state == "NC") from $addresses)
then
modify ($p) {} #Assign person to sales region 1 in a modify block
end
rule "Apply a discount to people in the city of Raleigh"
ruleflow-group "test"
lock-on-active true
when
$p : Person($addresses : addresses)
exists (Address(city == "Raleigh") from $addresses)
then
modify ($p) {} #Apply discount to person in a modify block
end
import java.util.ArrayList
rule "Raise priority if system has more than 3 pending alarms"
when
$system : System()
$alarms : ArrayList( size >= 3 )
from collect( Alarm( system == $system, status == 'pending' ) )
then
# Raise priority, because system $system has
# 3 or more alarms pending. The pending alarms
# are $alarms.
end
import java.util.LinkedList;
rule "Send a message to all mothers"
when
$town : Town( name == 'Paris' )
$mothers : LinkedList()
from collect( Person( gender == 'F', children > 0 ) from $town.getPeople() )
then
# send a message to all mothers
end
rule "取对象中的最大值,和最小值"
dialect "mvel"
when
accumulate(Person($value:age),
$min:min($value),
$max:max($value);
$max<=5
)
then
System.out.println($min+">>>>>>>>>>>"+$max);
end
Drools附带几个内置的accumulate功能,包括:
average 平均值
min 最小值
max 最大值
count 统计
sum 求和
collectList 返回List
collectSet 返回HastSet
所有的传入的值的Person属性age都减1
rule "sun求和后减1"
dialect "mvel"
when
//$n:Number() from
accumulate(Person($value:age),
$sum:sum($value-1)
)
then
System.out.println("求和减1>>>>>>>>>>>>"+$sum);
end
rule "测试accumulatefrom用法1"
dialect "mvel"
when
$total : String() from
accumulate(Person($value:dous),
init( Double total = 0.1; Person p = new Person();),
action( total += $value; ),
result( p )
)
then
System.out.println($total+"accumulate from 用法 求和");
end
rule "取对象中的最大值,和最小值"
dialect "mvel"
when
accumulate(Person($value:age),
$min:min($value),
$max:max($value);
$max<=5
)
then
System.out.println($min+">>>>>>>>>>>"+$max);
end
import accumulate com.drools.TestAccunmulateneeded factorial
rule "自定义函数阶乘"
dialect "mvel"
when
//$n:Number() from
accumulate(Person($value:age !=null ) ,
$factorial:factorial($value)
)
then
System.out.println("自定义函数>>>>>>>>>>>>"+$factorial);
end
如果在使用JAXB把xml文件unmarshal成vo(XSD自动生成的vo)时碰到如下错误:
org.xml.sax.saxparseexception : premature end of file
很有可能时你直接读取文件为inputstream,然后将inputstream作为构建unmarshal需要的source参数。InputSource inputSource = new In
servlet 搞java web开发的人一定不会陌生,而且大家还会时常用到它。
下面是java官方网站上对servlet的介绍: java官网对于servlet的解释 写道
Java Servlet Technology Overview Servlets are the Java platform technology of choice for extending and enha
这两天学到事务管理这一块,结合到之前的terasoluna框架,觉得书本上讲的还是简单阿。我就把我从书本上学到的再结合实际的项目以及网上看到的一些内容,对声明式事务管理做个整理吧。我看得Spring in Action第二版中只提到了用TransactionProxyFactoryBean和<tx:advice/>,定义注释驱动这三种,我承认后两种的内容很好,很强大。但是实际的项目当中
1)nosql数据库主要由以下特点:非关系型的、分布式的、开源的、水平可扩展的。
1,处理超大量的数据
2,运行在便宜的PC服务器集群上,
3,击碎了性能瓶颈。
1)对数据高并发读写。
2)对海量数据的高效率存储和访问。
3)对数据的高扩展性和高可用性。
redis支持的类型:
Sring 类型
set name lijie
get name lijie
set na
在多节点的系统中,如何实现分布式锁机制,其中用redis来实现是很好的方法之一,我们先来看一下jedis包中,有个类名BinaryJedis,它有个方法如下:
public Long setnx(final byte[] key, final byte[] value) {
checkIsInMulti();
client.setnx(key, value);
ret