Using Drools 6.5.0.Final
I want to use abbreviated combined relation condition (e.g. Person( age > 30 && < 40 )) in combination with additional conditions.
I tried it, but the resulting rules are executed more than once.
I have a small example, where temperature deviation from a setpoint is checked and the allowed deviations depend on the setpoint. The allowed deviations are configured with Param facts, see below.
If I execute the example (fire-all-rules):
rule 1 fires two times (bug?)
rule 2 fires once as expected (without abbreviated notation).
Is my usage of the abbreviated notation wrong or is this a bug?
Example rules:
declare Param
from : float
to : float
low : float
high : float
end
declare TemperatureEvent
#role( event )
id : String
setpoint : float
t : float
end
rule "Init abbreviated conditions test"
when
then
insert(new Param(0, 10, 1, 1));
insert(new Param(10,20, 2, 3));
insert(new TemperatureEvent("id1", 13.7f,11.5f));
// rule 1 and rule 2 should fire exactly once
end
rule "rule 1"
when
$p: Param()
$x : TemperatureEvent($p.from <= setpoint && < $p.to, (t < setpoint+$p.low || t > setpoint+$p.high))
then
System.out.println("rule 1: "+$x.getId()+" "+$x.getSetpoint()+" "+$x.getT());
end
rule "rule 2"
when
$p: Param()
$x : TemperatureEvent($p.from <= setpoint, setpoint < $p.to, (t < setpoint+$p.low || t > setpoint+$p.high))
then
System.out.println("rule 2: "+$x.getId()+" "+$x.getSetpoint()+" "+$x.getT());
end
The abbreviated restriction
$p.from <= setpoint && < $p.to
is equivalent to
$p.from <= setpoint && $p.from < $p.to
What you want is
setpoint >= $p.from && < $p.to
Related
import random
i went to add another function to calculate the number guesses
def guess(x):
randomNumb = random.randint(1, x)
guess = 0
while guess != randomNumb :
guess = int(input(f'entre number between 1 and {x} : '))
print(guess)
if guess < randomNumb :
print('guess is low')
elif guess > randomNumb :
print('guess is high')
print(f'guess is right {randomNumb}')
guess(100)
When i use below condition with 'not' I am getting an error.
not(Obj1(value == 0) && Obj2(value <= 3))
However if i replace above condition as below I am not getting any casting exception
Obj1(value != 0) or Obj2(value > 3)
The rule looks like this:
rule "test_6"
salience 10
when
not(Obj1(value == 0) && Obj2(value <= 3))
then
.....
end
And this is the error I'm getting:
throwing error Error Message: org.drools.core.rule.GroupElement cannot be cast to org.drools.core.rule.Pattern
The && and || operators can only be used inside a single pattern. For example: Obj1( value > 3 && value < 10 || value == 0). According to the documentation, to separate Patterns, you have to use the and and or operators.
So, in your case, your rule should be:
rule "test_6"
salience 10
when
not(Obj1(value == 0) and Obj2(value <= 3))
then
.....
end
Note that it was not failing when you were using or because that was the right operator to use instead of ||.
Hope it helps,
I need help in writing the accumulate logic for below requirement:
Requirement: Certain rules will provide the percentage to be applied for global value. Another set of rules should use the aggregate total percentage in determining the result.
For example: 0.75 is the global value passed as input(threshold).
Rule 1 might apply -10% of the fixed value ie. 0.75 - (0.75 * 0.10) = 0.675
Rule2 will apply + 20% off updated value. ie., 0.675 + (0.675 * 0.20) = 0.81
My global value is 0.75 (Threshold)
Using the below rules I am trying to applied the percentage applicable for a fixed value :
//class imports
global Double FIXED_THRESHOLD;
//Rules
rule "Prediction Rule_2"
lock-on-active true
no-loop true
salience (2)
when
LossInput (airBagDeployed == 'Y' , driveable == 'N')
result : RuleResult(predictedTotalsThreshold == 0)
then
insert(new ControlFact( -10.0 ) ); //Reduce -10% to global value 0.75 - 0.75* 0.10 = 0.675
System.err.println("New control fact added to working memory....");
end
rule "Prediction Rule_1"
lock-on-active true
no-loop true
salience (1)
when
LossInput (airBagDeployed == 'Y' , driveable == 'N', make == 'Honda' )
result : RuleResult(predictedTotalsThreshold == 0)
then
insert(new ControlFact( 20.0 ) ); // Add 20% to the updated aggregate (0.20 % of 0.675).
System.err.println("New control fact added to working memory....");
end
I tried the below accumulate logic but obviously it is wrong. It is applying only to fixed value always instead of the updated value.
rule "Aggregate All Threshold"
no-loop true
when
$aggregateTotalsThresholdPercentage : Number() from accumulate(
ControlFact( $totalsThreshold : totalsThresholdPercentage ),
sum( ( FIXED_THRESHOLD + ( FIXED_THRESHOLD * $totalsThreshold ) / 100 ) ) )
ruleResult: RuleResult(predictedTotalsThreshold == 0)
then
ruleResult.setPredictedTotalsThreshold($aggregateTotalsThresholdPercentage.doubleValue());
update(ruleResult);
end
POJO:
public class LossInput{
private String airBagDeployed;
private String driveable;
private String make;
}
public class ControlFact {
public double totalsThresholdPercentage;
}
public class RuleResult {
private double predictedTotalsThreshold;
}
//insert facts in working memory
kieSession.insert(lossInput);
kieSession.insert(ruleResult);
kieSession.setGlobal("FIXED_THRESHOLD", new Double(0.75));
kieSession.fireAllRules();
Please help on the accumulate logic to apply the updated value everytime when percentage threshold to be applied.
You cannot use accumulate/sum this way because you add the FIXED_THRESHOLD for each ControlFact.
Insert ControlFacts as you have in the "Prediction..." rules (without all the rule attributes). Use each ControlFact to update RuleResult's predictedTotalsThreshold. The "Aggregate" rule will fire repeatedly, and therefore you need to make sure to retract the used ControlFact.
rule "Aggregate All Threshold"
when
ControlFact( $ttp: totalsThresholdPercentage );
$res: RuleResult( $ptt: predictedTotalsThreshold)
then
double nt = $ptt + $ptt*$ttp/100;
modify( $res ){ setPredictedTotalsThreshold( $nt ) }
retract( $res );
end
I have to write a drool function on a list which has to do the following thing
create a summation
check whether the summation is greater than 100.
below is the drool rule I have created
rule "001"
when
$charge : MainClass(subList.size() > 0)
$item : SubListClass(number < 0) from $charge.subOrderROList
$total : Number() from accumulate(SubListClass( $p : number ),sum( $p )
then
int index = $charge.SubListClass.indexOf($item)+1;
violations.error(kcontext, "ad", "ad.message", new String[]{String.valueOf(index),$item.getNumber().toString()},index);
end`
I am not able to check whether the $total is greater than 100
thanks
This would be correct if it were possible to obtain a sum > 100 by adding negative numbers. I have kept the constraints as they were in the Q, so change this as appropriate. Maybe number > 0?
rule "001"
when
$charge: MainClass(subList.size() > 0)
$total: Number( intValue > 100 )
from accumulate( SubListClass($p: number < 0)
from $charge.subOrderROList,
sum( $p ) )
then
I have a question for my VHDL code. My entire code has to do the following:
input = Minutes + hours + clk
output = AFTER 1 minute: minuten = minuten + 1 (and if needed hours += 1)
So I built a counter, which counts (well for simulation easyness to 1/10th of a second instead of a minute) clock periods untill one minute is over. Then it raises the minute by 1, and send that to the output. I got one problem however: due to setup violation I can't simulate the synthesis well. I thought this violation was in the line: min_save = minutes. I think that it's impossible to save the input minutes immediately, so I thought: let's build another counter, which counts 1 clock period, and then saves it.
So my 2 questions:
Does this indeed solve the setup violation?
If so: why does the signal setup have no value. It has a value of undefined. What do i do wrong?
If not: why not, and how can I solve this then?
CODE:
architecture behaviour of internclk_u is
begin
process (minuten)
begin -- switching input minutes to binary.
minintern(6) <= minuten(0);
minintern(5) <= minuten(1);
minintern(4) <= minuten(2);
minintern(3) <= minuten(3);
minintern(2) <= minuten(4);
minintern(1) <= minuten(5);
minintern(0) <= minuten(6);
end process;
process (uren)
begin -- switching input hours to binary
uurintern(5) <= uren(0);
uurintern(4) <= uren(1);
uurintern(3) <= uren(2);
uurintern(2) <= uren(3);
uurintern(1) <= uren(4);
uurintern(0) <= uren(5);
end process;
process (clk)
begin
setup <= '0';
if(clk'event and clk = '1') then
if(setup < '1') then
setup <= '1';
else
setup <= setup;
if(min_save = minutes) then
count <= new_count;
else
min_save <= minutes;
count <= (others => '0');
end if;
end if;
end if;
end process;
process (count, minintern)
begin -- count one minute (now its 0.1 second)
if(count < F/10 ) then
new_count <= count + 1;
intern_out <= minintern;
uurintern_out <= uurintern;
else
case minintern is -- Calculate new value for output
when "0001001" => intern_out <= "0010000";
uurintern_out <= uurintern;
when "0011001" => intern_out <= "0100000";
uurintern_out <= uurintern;
when "0101001" => intern_out <= "0110000";
uurintern_out <= uurintern;
when "0111001" => intern_out <= "1000000";
uurintern_out <= uurintern;
when "1001001" => intern_out <= "1010000";
uurintern_out <= uurintern;
when "1011001" => intern_out <= "0000000";
case uurintern is
when "001001" => uurintern_out <= "010000";
when "011001" => uurintern_out <= "100000";
when others => uurintern_out <= uurintern + 1;
end case;
when others => intern_out <= minintern + 1;
uurintern_out <= uurintern;
end case;
new_count <= count;
setup <= '0';
end if;
end process;
process (intern_out) -- Reversing signals for next blocks in system
begin
interne_tijd_m(6) <= intern_out(0);
interne_tijd_m(5) <= intern_out(1);
interne_tijd_m(4) <= intern_out(2);
interne_tijd_m(3) <= intern_out(3);
interne_tijd_m(2) <= intern_out(4);
interne_tijd_m(1) <= intern_out(5);
interne_tijd_m(0) <= intern_out(6);
end process;
process (uurintern_out)
begin -- Reversing signals for next blocks in system
interne_tijd_u(5) <= uurintern_out(0);
interne_tijd_u(4) <= uurintern_out(1);
interne_tijd_u(3) <= uurintern_out(2);
interne_tijd_u(2) <= uurintern_out(3);
interne_tijd_u(1) <= uurintern_out(4);
interne_tijd_u(0) <= uurintern_out(5);
end process;
end behaviour;
Remarks:
ignore the 2 case statements; this is because the inputsignals are not binary coded; they are reversed. Besides that: it doesnt count 1,2,4,8,16 etc; it counts: 1,2,4,8,10,20 etc.
EDIT
I put in the whole architecture now.
The error message I received was something like: "Setup time violations occured(?)". So I believe I cant save the input minutes immediately when I receive minutes. Thats why I tried to program in the setup signal. Hope you guys can help. This error gave me a headache and lots of frustration already:(
EDIT 2
Forget about the whole "setup" signal. A student-assistent pointed out to me that the saving of the input doesnt make any sense. So my updated question is: How can I properly save the input signal/value into another signal?
Your code is really a mess, and it's very difficult to debug... :-(
I think that the problem is due to the fact that you have a multiple definition of the signal setup. Moreover the first time is defined inside a clocked process (even if the setup <= '0' is outside the if statement). The second time is defined inside another process that is not clocked. I think that the synthesis tool doesn't know how to proceed exactly. In the simulation of the RTL code you should normally see some glitches on the setup signal. I also think that the type of setup is set_logic. Try to change it to std_ulogic. The simulation tool will not compile your code since this type is unresolved. Fix the code and then go back to std_logic.
Another thing: you wrote if setup < '1' then: this sounds very strange to me (I didn't know that it was accepted). I prefer to write if setup /= '1' then: it is much more easier to read this kind of code...