日期:2014-05-18  浏览次数:20424 次

请教关于规则匹配的问题,谢谢了
我现在想做一个规则库,举例如下:
现有两个规则(存在数据库中):
1.(custno= 'A001 ')   and   (productype   like   '% ')   and   (processno= 'A1 ')
2.(custno= 'B001 ')   and   (productype   = 'BOX ')   and   (type= 'G ')   and   (itemtype= 'PM ')
现在我有一个条件:   processno= 'A1 '   and   custno= 'A001 '   and   productype= 'BOOK '  
按理我应该匹配到第一条规则.   可是我要怎么样才能匹配得出来?用正则表达式吗?   还是自己写分析器?  
更复杂的情况是:逻辑符不仅有and   ,或许还会有or   ,   NOT等,比较符除了=号外,或许还有> =, <=,between,in   等(当然,前提是能解决上面的问题).  
谢谢大家.
----   顺便抱怨一下,这个发贴页面做得不好,刚才不小心想给200分,结果系统说我不能给这么多分,结果再退回去时,刚才打的字全丢了,又得重新打一次,郁闷   ---

------解决方案--------------------
貌似很困难的说

------解决方案--------------------
--用触发器约束
create trigger tr_tablename_i on tablename
for insert,update
as
begin
if not exists(
select 1 from inserted
where (custno= 'A001 ') and (productype like '% ') and (processno= 'A1 ')
or (custno= 'B001 ') and (productype = 'BOX ') and (type= 'G ') and (itemtype= 'PM '))
begin
select @message= '规则不匹配! '
raiserror(@message,16,1)
rollback tran
return
end
end
------解决方案--------------------
感觉这东西挺麻烦,不是一句话2句话能说明白的
------解决方案--------------------
可不可以这样
create table 规则表(规则 varchar(8000))
两条记录
(custno= 'A001 ') and (productype like '% ') and (processno= 'A1 ')
(custno= 'B001 ') and (productype = 'BOX ') and (type= 'G ') and (itemtype= 'PM ')

现在有一条记录主键是A001,循环规则表,用动态语句进行匹配,拼成的语句就是这样的:
select 1 from t where 主键= 'A001 ' and (custno= 'A001 ') and (productype like '% ')
and (processno= 'A1 ')



------解决方案--------------------
up
------解决方案--------------------
你就把用户条件组成SQL,查询就行了吧
------解决方案--------------------
好像蠻難實現,提出個設想,將規則的條件分散
比如
1.(custno= 'A001 ') and (productype like '% ') and (processno= 'A1 ')
2.(custno= 'B001 ') and (productype = 'BOX ') and (type= 'G ') and (itemtype= 'PM ')
拆成2個表
t1
id p_id description
-----------------------------------
1 1 custno= 'A001 '
2 1 productype like '% '
3 1 processno= 'A1 '
4 2 custno= 'B001 '
5 2 productype= 'BOX '
6 2 type= 'G '
7 2 itemtype= 'PM '

t2
p_id id_d
-----------------------------------
1 1 and 2 and 3
2 4 and 5 and 6 and 7


/*********/
有一個條件processno= 'A1 ' and custno= 'A001 ' and productype= 'BOOK '
按and split,可以得出
processno= 'A1 '
custno= 'A001 '
productype= 'BOOK '
去匹配t1,可以得出 : 3 ,1 ,2 (這個2匹配productype需要處理),
order by 後可以得出條件變為 1 and 2 and 3,再去匹配t2,可以得出是第一條規則...

------------------------------------------------------