0

I am using Optaplanner 6.2Final version and everything is working fine except the matches operator. To explain more, this is my rule.

rule "TEST REG EXP OPERATOR"
when
    $cheese:Cheese($cheese.type matches "(Buffulo)?\\S*Mozarella")
then
    scoreHolder.addSoftConstraintMatch(kcontext, 1);
end

The following is the exception thrown:

Exception in thread "main" [Error: unable to resolve method: com.app.test.domain.Cheese.$cheese() [arglength=0]]
[Near : {... $cheese.type ~= "(Buffulo)?\\S*Mozarella" ....}]

Note : if I removed $cheese. declaration in front of type, the rule has no issue. This is working fine :

$cheese:Cheese($cheese.type == "Buffulo")

Any suggestions would be appreciated.

UPDATE: It turns out that the following throws exception ONLY on the drools core version 6.2.0.Final and 6.3.0.Final after I have done checking on all the version. It is working in the rest of versions.

$cheese:Cheese($cheese.type matches "(Buffulo)?\\S*Mozarella")
sky.flyer
  • 91
  • 8
  • Even if there is nothing evidently wrong with your code, using a variable on the pattern you are currently in to access one of its attributes is not a common pattern (and I don't see any advantage). Have you try this? `$cheese: Cheese(type matches "(Buffulo)?\\S*Mozarella")` – Esteban Aliverti Aug 16 '16 at 05:52
  • Yes, it is working if I use $cheese: Cheese(type matches "(Buffulo)?\\S*Mozare‌​lla"). However, i do not understand why it is not able to allow using variable. How can we achieve the following condition? $cheese1:Cheese(type matches "(Buffulo)?\\S*Mozarella") $cheese2:Cheese($cheese1.name matches "A.*", id>$cheese1.id) – sky.flyer Aug 16 '16 at 06:24
  • If the `name` condition if about the first `Cheese`, then move it to the first pattern: `$cheese1:Cheese(type matches "(Buffulo)?\\SMozare‌​lla", name matches "A.*") $cheese2:Cheese(id>$cheese1.id)` – Esteban Aliverti Aug 16 '16 at 08:40

1 Answers1

0

If you want to have this to work:

  $cheese1:Cheese(type matches "(Buffulo)?\\SMozare‌​lla")
  $cheese2:Cheese($che‌​ese1.name matches "A.*", id>$cheese1.id)

you'll have to use method calls:

  $cheese1:Cheese(type matches "(Buffulo)?\\SMozare‌​lla")
  $cheese2:Cheese($che‌​ese1.getName() matches "A.*", id>$cheese1.getId())

This assumes you have been following JavaBeans conventions (as you should). But much better would have been to use

  $cheese1:Cheese(type matches "(Buffulo)?\\SMozare‌​lla",
                  name matches "A.*", $id1: id )
  $cheese2:Cheese( id > $id1 )
laune
  • 31,114
  • 3
  • 29
  • 42
  • `$cheese1:Cheese(type matches "(Buffulo)?\\SMozare‌​lla") $cheese2:Cheese($che‌​ese1.getName() matches "A.*", id>$cheese1.getId())` throws the same exception actually. Perhaps my question was not clear. The question was why `$cheese1:Cheese($cheese1.type matches "(Buffulo)?\\SMozare‌​lla")` throws exception of using variable and why the following is working fine? `$cheese1:Cheese($cheese1.type == "Buffulo")` – sky.flyer Aug 17 '16 at 10:18
  • To answer that, we should know the Drools version and have accurate (!) code for class Cheese. Perhaps you add this to your question. -- But the usual paradigm is to use field names without preceding binding variable and without parentheses, in contrast to a binding variable followed by a dot, the getter name (!) and a pair of parentheses. – laune Aug 17 '16 at 16:47
  • thanks for answer. After i have done checking on other drools versions it seems working. So, it is because I am using 6.2.0.Final version. I updated the post. – sky.flyer Aug 19 '16 at 03:25