user2893821
user2893821

Reputation: 73

Drools return list of rules

I'm new to drools and I succeded in creating a working application that uses the created rules. I have a simple class Message with two variables type and language.

public class Message {
  private String type;
  private String language;

  public String getType() {
      return type;
  }

  public void setType(String type) {
      this.type = type;
  }
  public String getLanguage() {
      return language;
  }

  public void setLanguage(String language) {
      this.language = language;
  }
}

My rules are implemented as

rule "test_auto"
when
  message:Message (( type == 'string1' ) && ( language == 'string2' ) )
then
  ...
end

If the user inserts some strange values for both type or language , I have a particular rule that returns an error. But on top of that I wanted to know if it is possible to return also the list of all the possible variables inserted in the rules: for example string1 and string2.

Upvotes: 1

Views: 976

Answers (1)

laune
laune

Reputation: 31300

I guess you mean "string literals" and not variables?

There's a class for representing a rule; with classes for patterns and constraints. But these are "not stable", and it is generally not advisable to base an application on them.

If you have several rules catching some bad combination of Message.type and Message.language, you might consider inserting Facts according to

class BadMessage {
    String type;
    String language;
}

with all those "bad" combinations, and a single rule

rule "catch bad messages"
when
    $m: Message'( $t: type, $l: language )
    BadMessage( type == $t, language == $l )
then
    // handle $m as a "bad" message

As an aside, note that you can write your pattern simply as

message: Message( type == "string1", language == "string2" )

Upvotes: 1

Related Questions