0

I have a list of strings and a string value in my bean file.

Here is my bean file

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);  
       }  

I want to check whether the story consists of any of the strings from the list combinations If yes then print true and if no then print false.

I want to create this rule in my DRL file file but I couldn't understand the syntax. I am totally new at drools kindly help me with this.

I could create and execute simple rules but I am not able to figure out rules of such nature.

Yatin
  • 727
  • 1
  • 9
  • 40
  • This may help: http://stackoverflow.com/questions/28326953/drools-check-if-exists-contains-string-in-a-liststring – akhil_mittal Aug 24 '15 at 11:01
  • Thanks @akhil.. But My problem is just the opposite of the question you have added. In the link posted by you,It is basically checking for a string in a list of strings and I want to check a list of words(string : combinations list) in a paragraph(string: story). I hope I am able to clearly state my problem. Thanks – Yatin Aug 24 '15 at 11:07
  • The specification of your problem is somewhat inaccurate. Do you mean that the **story** string must be made up from one or more strings from the string list **combinations**, with or without any additional strings? In any permutation? And with spaces and/or interpunctuation in between the strings from the **combinations**? – laune Aug 24 '15 at 12:44

1 Answers1

2

Unless you have grossly mis-stated the nature of your problem, there is no simple way of writing a rule telling you whether the story is made up from the strings in combinations. I suggest that you write a

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

in class DataBean implementing the algorithm (see my comment) and use this on the RHS to check whether a fact of type DataBean contains data according to this condition.

Edit According to the comment below my answer (but not in agreement with the phrase "whether the story consists of any of the strings from the list) you can write the following rule:

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

with classes

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

Note that the contains in the rule is java.lang.String.contains, which simply tests whether the argument is a substring of the String object. The rule will fire once for each word from the list words that occurs in text.

Also, this will produce misleading results. If, for instance, the story contains the word "portmanteau" and the list is made up from "port", "or", "man" and "ant", you'll get four incorrect firings. You can "cook" the preceding rule by using

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

I add this to emphasize that a question must be stated precisely in order to elicit useful answers. Maybe neither of my proposals is what you are looking for.

laune
  • 31,114
  • 3
  • 29
  • 42
  • I think I am not able to state my problem here clearly. I have created a rule that could check whether a list contains a particular string :- rule "Rule List Iteration" salience 100 when listBeanIteration : MyBean($listOfString : relatedEntity, $listOfString contains "yatin") value : String() from $listOfString $listOfString then System.out.println("data present--->"+value); end – Yatin Aug 25 '15 at 03:00
  • Now what I want to do is the exact opposite. I have different stories(paragraphs : type string) and I want to check whether these stories contain any of the words from a list of words. One way could be is to use the keyword "contains" and then specify the word. eg story contains "yatin" or story contains "xyz". this way i could match story with a list of words with an OR condition. So is there any way I could avoid specifying all these words explicitly and probably make the rule iterate over these words from a list. – Yatin Aug 25 '15 at 03:06
  • Thanks again.. This is exactly what I required. I will try to be more clear next time..:) – Yatin Aug 25 '15 at 09:38