Coincidencias de drools con anticipación negativa

I want one of my conditions to be that an asserted string is not in a list of strings.

I'm using a decision table and in my condition cell I have

firstName matches("?!($param)")

and in the param cell I have

Billy|Joe|Bob

For some reason when I have a name like Sally it doesn't match that rule. If I remove "Billy|Joe|Bob" from the param cell it matches.

I should probably mention that I'm using drools 3.0.6 I saw mention of a not matches that was added in drools 4 but for reasons I won't get into I can't upgrade.

preguntado el 24 de agosto de 12 a las 23:08

3 Respuestas

Negative look ahead is something completely different than negating an expression, that I think is what you are looking for.

I don't remember much of Drools 3, but you probably will have to use an eval expression to negate that. So your decision table cell would look like

eval( ! getFirstName().matches("($param)") )

Basically you are calling a pure java expression instead of using the provided matches operator.

Respondido 25 ago 12, 01:08

I don't think eval is allowed in decision tables in drools 3. It says unexpected token on eval. - AmishDave

Decision tables are just text templates to generate rules, so they do accept evals, just a matter of finding the right syntax. Take a look at the generated rule and see what is wrong... it might help you fix it. - edson tirelli

You have the leading bracket out of place.

The syntax for a negative look ahead is:

(?!$param)

However, this change alone isn't enough. You need to anchor the regex:

^(?!.*$param)

This regex says "match any input that no se que no contengo $param".

Entonces, usando tu ejemplo, ^(?!.*Billy|Joe|Bob) would match any input that doesn't contain Bill, Joe or Bob

Respondido 25 ago 12, 02:08

I tried both of those and they didn't work. I believe your regex is correct but I have a hunch that drools 3.0 doesn't support the full regex grammar. - AmishDave

The best solution I've found is to split this into multiple conditions. I wish I could place these all in one condition but to do that I'm pretty sure I'd need to go up to at least drools 4.

| firstName!="$param" | firstName!="$param" | firstName!="$param" |
|Billy                |Joe                  |Bob                  |

Note: The use of pipes in the original question was to be treated as an "or" whereas here I'm using pipes to denote cells in the table.

Respondido 27 ago 12, 21:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.