【问题标题】:Checking whether any of the string from a list of strings is present in a paragraph(string)?检查字符串列表中的任何字符串是否存在于段落(字符串)中?
【发布时间】:2015-08-25 15:49:18
【问题描述】:

我的 bean 文件中有一个字符串列表和一个字符串值。

这是我的 bean 文件

public class DataBean {    
List<String> combinations;
    private String story;

    public String getStory() {
            return story;
        }
        public void setStory(String story) {
            this.story = story;
        }

    public List<String> getCombinations() {
            return combinations;
        }
        public void setCombinations(List<String> combinations) {
            this.combinations = combinations;
        }

         public void addString(String value) {  
            if (combinations == null) {  
                combinations = new ArrayList<String>();  
            }  
            combinations.add(value);  
       }  

我想检查 story 是否包含列表 combinations 中的任何字符串 如果是,则打印 true,如果不是,则打印 false。

我想在我的 DRL 文件文件中创建此规则,但我无法理解语法。我是流口水的新手,请帮我解决这个问题。

我可以创建和执行简单的规则,但我无法弄清楚这种性质的规则。

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/28326953/…
  • 谢谢@akhil .. 但我的问题与您添加的问题正好相反。在您发布的链接中,它基本上是检查字符串列表中的字符串,我想检查段落(字符串:故事)中的单词列表(字符串:组合列表)。我希望我能够清楚地说明我的问题。谢谢
  • 您的问题说明有些不准确。您的意思是 story 字符串必须由字符串列表 combinations 中的一个或多个字符串组成,有或没有任何附加字符串?在任何排列? combinations? 中的字符串之间有空格和/或间断

标签: java drools rule-engine business-rules


【解决方案1】:

除非您严重错误地陈述了问题的性质,否则没有简单的方法可以编写规则来告诉您故事是否由组合中的字符串组成强>。我建议你写一个

public static boolean isComposed(){
    // ...
}

在类DataBean 中实现算法(请参阅我的评论)并在RHS 上使用它来检查DataBean 类型的事实是否包含符合此条件的数据。

编辑根据我的回答下面的评论(但不同意“故事是否列表中的任何字符串组成)这句话,你可以写以下规则:

rule "check for word"
when
    Wordlist( $words: words )
    $word: String() from $words
    $story: Story( $text: text, $text.contains( $word ) )
then
    // String $story.text has $word as a substring
end

class Wordlist { List<String> words; ... }
class Story { String text; ... }

注意,规则中的containsjava.lang.String.contains,它只是测试参数是否是String对象的子字符串。该规则将对列表words 中出现在text 中的每个单词触发一次。

此外,这会产生误导性的结果。例如,如果故事包含单词“portmanteau”,并且列表由“port”、“or”、“man”和“ant”组成,你会得到四个错误的触发。您可以使用“烹饪”上述规则

rule "check for word"
when
    Wordlist( $words: words )
    $word: String() from $words
    $story: Story( $text: text, 
                   $text.matches( ".*+\\b" + $word + "\\b.*" ) )
then
    // String $story.text contains the $word
end

我添加这个是为了强调必须准确地陈述一个问题才能得到有用的答案。也许我的建议都不是你想要的。

【讨论】:

  • 我想我无法在这里清楚地说明我的问题。我创建了一个规则,可以检查列表是否包含特定字符串:- 规则“规则列表迭代”显着性 100 当 listBeanIteration : MyBean($listOfString : relatedEntity, $listOfString contains "yatin") value : String() from $listOfString $listOfString 然后 System.out.println("数据存在--->"+value);结束
  • 现在我想做的恰恰相反。我有不同的故事(段落:类型字符串),我想检查这些故事是否包含单词列表中的任何单词。一种方法可能是使用关键字“包含”,然后指定该词。例如故事包含“yatin”或故事包含“xyz”。这样我可以将故事与带有 OR 条件的单词列表匹配。那么有什么方法可以避免明确指定所有这些词,并且可能使规则从列表中迭代这些词。
  • 再次感谢.. 这正是我所需要的。下次我会尝试更清楚..:)
猜你喜欢
  • 2013-01-09
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2016-12-11
相关资源
最近更新 更多