1
votes

I have a very simple test case to reward the player if he registers the first time. When I am running my test case getting an error.

Message [id=1, kieBase=defaultKieBase, level=ERROR, path=player.drl, line=10, column=0
text=Unable to Analyse Expression isNew == true:
[Error: no such identifier: isNew]
[Near : {... isNew == true ....}]
^ [Line: 10, Column: 8]]

I checked that the model class has the variable declared correctly and the same is referred to .drl file.

Player.drl

import com.xyz.model.business.objects.Player;
import com.xyz.rules.domain.Points;
import java.util.*;

global com.xyz.rules.domain.Points points;
dialect  "mvel"

rule "Reward Point if User registered"
    when
        playerInstance:Player(isNew == true);
    then
        points.setPoints(1000);
end

Model Class

package com.xyz.model.business.objects;    
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder(toBuilder = true)
public class Player {        
        private String userId;
        private String name;
        private int age;
        private boolean isNew;
}
2
Does this answer your question? drools - Unable to Analyse ExpressionRLD
@RLD given thread did not answer to my question. As you see it was case of getter/setter for the boolean property.Vijay Kumar Rajput

2 Answers

0
votes

You should should implement getters for the bean / model properties. As new is reserved word in Java I would replace it with new_. Because the property type is boolean getter name is isNew_ not getNew_.

Rule

Replace Player(isNew == true) with Player(new_ == true).

Model

Replace

private boolean isNew;

with

private boolean new_;
public boolean isNew_() {
    return new_;
}
0
votes

I see that methods are being generated by lombok and boolean method is coming like below

public boolean isNew() {
    return isNew;
}

public void setNew(boolean aNew) {
    isNew = aNew;
}

but when I explicitly added get/set then drools understood it.

getXXX() & setXXX()

So instead of auto-generated. I put get & set explicitly for boolean and voila

   public boolean getIsNew() {
        return isNew;
    }

    public void setNew(boolean aNew) {
        isNew = aNew;
    }