问题
I have a situation Where I need to ignore any attribute value at run time.
$applicant : Applicant ($age : age, $gender : gender, $income : income)
$person : Person( age == $age, gender == $gender, income == $income )
Say in income attribute, I am saying ANY value; it means if der is any value specified in application income attribute then it is ok otherwise also it should execute the rule because ANY value is acceptable.
Please help to implement this.
Thanks
回答1:
For a dynamic switch you need another object, PersonControl
with boolean attributes:
$applicant : Applicant ($age : age, $gender : gender, $income : income)
PersonControl( $igAge: igAge, $igGender: ifGender, $igIncome: igIncome)
$person : Person( age == $age || $igAge,
gender == $gender || $igGender,
income == $income || $igIncome )
You may have to insert/retract (or modify) PersonControl for each Applicant evaluation if these evaluations need to be done individually.
If you want to state a value as any, you'll have to think about a value you can use for that, e.g. -1 for numeric values:
$applicant : Applicant ($age : age, $gender : gender, $income : income)
$person : Person( age == $age ,
gender == $gender,
income == $income || == -1 )
回答2:
$applicant : Applicant ($age : age, $gender : gender, $income : income)
$age : age
is an assignment, such that $age
can be referred later on. and similarly with gender and income.
$person : Person( age == $age, gender == $gender, income == $income )
Here you are checking that age of every applicant to each of the person object and similarly with gender and income. So just to ignore any of them, just drop them from the equality checks.
i.e. So remove the conditions that you dont want to compare for equality, e.g. to ignore income:
rule "Compare Applicants"
salience 155
when
$applicant : Applicant ($age : age, $gender : gender)
$person : Person( age == $age, gender == $gender )
then
// do stuff
end
To ignore everything, i.e. to compare every applicant with every person and execute the then
clause
when
$applicant : Applicant ()
$person : Person()
then
// do your stuff
end
回答3:
Can you just assign the applicant to $applicant
, and then do the setting of $age
and $gender
in the then
clause:
rule "do application"
when
$applicant : Applicant()
$person : Person( age == $age, gender == $gender, income == $income )
then
$age = $applicant.getAge()
// do more stuff
end
来源:https://stackoverflow.com/questions/28803397/ignore-an-attribute-value-dynamically-in-drools-rules