You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drools 7规则文件中如何检查插入的String类型事实内容?

How to Check if a String Fact Contains a Substring in Drools 7 DRL

Hey there! Sounds like you're already up and running with inserting String facts into Drools 7 and executing rules—great start! To modify your rule to check if the String contains the substring et (instead of matching an exact value), you can use either Drools' built-in syntax or directly call Java's String.contains() method in your DRL condition.

Here's a complete example of what your DRL rule would look like:

package com.your.package.name;

rule "Detect Substring 'et' in String Fact"
    when
        // Option 1: Use Drools' built-in 'contains' operator
        $inputStr: String(this contains "et")
        
        // Option 2: Call Java's native String.contains() method (equivalent to Option 1)
        // $inputStr: String(contains("et"))
    then
        System.out.println("Does contain 'et'");
end

Let's break this down:

  • Matching the String Fact: The $inputStr: String(...) part tells Drools to look for any inserted fact of type String.
  • Substring Check:
    • this contains "et" is Drools' native way to check if the String (referenced by this) includes the substring "et".
    • Alternatively, contains("et") directly invokes Java's String.contains(CharSequence) method—this works because Drools allows calling valid Java methods on objects in rule conditions.

Testing this rule:

  • If you insert a String fact with value like "test" or "eternal", the rule will trigger and print "Does contain 'et'".
  • If you insert your original value "asdf", the rule won't fire since "asdf" doesn't include the substring "et".

Just make sure you're inserting a pure String fact (not wrapped in another object) and that there are no conflicting rules that might interfere with this check. That's all there is to it!

内容的提问来源于stack exchange,提问作者mikeb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:53:27