How to check for null value in accumulate Drools - drools

Iam first time user of Drools. In accumulate function I have a scenario to check for the (!= null) feature of java. But in Drools I find that != doesn't work .
when
$addr : String(length > 0) from accumulate(
$person : Person(),
action(
$addresses : $person.getAddresses();
// i want to check whether $addresses is not
null.
if($addresses !=null) is what i want to achieve
)
Any help is really appreciated.

Edit the rule as Below :
Person(adresses !=null) && $addr : String(length > 0) from accumulate(
$person : Person(),
action(
$addresses : $person.getAddresses();
)

Related

PowerShell DataTable delete column containing value "description"

I would like to delete all empty rows from the DataTable below as well as delete the column containing the value "description". I am able to delete all empty rows; however, I can't figure out how to also explicitly delete description from the DataTable.
Original DataTable:
dev_id : 4721
office_id : 355
name : Bobby
ip : 10.10.10.1
ipv6 : 2001:1930:ff28:ff00::1
mac :
serial_num : XX234555
platform : Supercode
:
description : I;need;to;delete;this;
:
:
max_speed : 100000
Code:
$myreader = $mycommand.ExecuteReader()
$DevicesDataTable.Load($myreader)
$myconnection.Close()
$Columns = $DevicesDataTable.Columns.Count
$Rows = $DevicesDataTable.Rows.Count
for ($r = 0; $r -lt $Rows; $r++) {
$Empty = 0
for ($c = 0; $c -lt $Columns; $c++) {
if (($DevicesDataTable.Rows[$r].IsNull($c)) -or ($DevicesDataTable.Rows[$r].Equals("description"))) {
$Empty++
}
}
if ($Empty -eq $Columns) {
$DevicesDataTable.Rows[$r].Delete()
}
}
# Delete
$DevicesDataTable.AcceptChanges()
Write-Output $DevicesDataTable
Result:
Note DataTable below still shows description:
dev_id : 4721
office_id : 355
name : Bobby
ip : 10.10.10.1
ipv6 : 2001:1930:ff28:ff00::1
serial_num : XX234555
platform : Supercode
description : I;need;to;delete;this;
max_speed : 100000
Note: I am able to identify the column containing "description", however, I'm not sure how to delete the respective row via the correct method:
if ($DevicesDataTable.Columns[$c].ColumnName.Equals("description"))
You can drop a column from the data table by calling Remove() on the Columns collection:
$DevicesDataTable.Columns.Remove('description')

Drools indecate variable as unexpected

I wan't return from query Company object. But IDE indecate company as unexpected
enter image description here
What is my error:
dialect "java"
query getBalance(World world, Company company)
$company := Company(companyName == "Intel") from world.getCompanies()
end
rule "BMWCarsToPerson"
when
$world : World()
$company : Company()
getBalance($world, $company)
/*$company : Company() from $world.getCompanies()*/
$manager : Manager() from $company.getManagers()
exists Car(name == BrandOfCar.BMW) from $manager.getCars()
$bmwCarsFromManager : Car(
name == BrandOfCar.BMW
) from $manager.getCars()
$person : Person(
name == "Elvis"
) from $world.getPersons()
/*$listMageners : List() from $manager*/
then
end
You shouldn't (and cannot) use a query for this simple task. Replace
$company : Company()
getBalance($world, $company)
by
$company: Company( companyName == "Intel" ) from $world.getCompanies()

Inference not working

I'm relatively new to Drools. I have those rules :
import models.Demarche;
declare IsMarque
demarche : Demarche
end
rule "Add type Marque taxe"
salience 2
no-loop true
lock-on-active true
when
$d : Demarche( typeDemarche == "Marque" )
then
modify($d){
listTaxes.put(14, 1)
}
insert( new IsMarque( $d ) );
System.out.println("infer marque");
end
And :
rule "Add Marque Polynesie extention taxe"
no-loop true
lock-on-active true
when
$d : Demarche( extPolynesie == true)
IsMarque(demarche == $d)
then
$d.listTaxes.put(17, 1);
System.out.println("marque");
end
rule "Add Not Marque Polynesie extention taxe"
no-loop true
lock-on-active true
when
$d : Demarche( extPolynesie == true)
not(IsMarque(demarche == $d))
then
System.out.println("not marque");
end
For the rules 2 or 3, nothing happens. One of them should be true, but nothing is printed, as if the infered IsMarque cannot be evaluated. If I comment the IsMaqrue evaluation this is working, i can see the messages printed in the console.
Any idea?
This rule needs to be rewritten as
rule "Add type Marque taxe"
when
$d : Demarche( typeDemarche == "Marque", $t: listTaxes) // I AM GUESSING HERE
not IsMarque(demarche == $d)
then
modify($t){
put(14, 1)
}
insert( new IsMarque( $d ) );
System.out.println("infer marque");
end
And no no-loop true and lock-on-active true if you have shown everything there is.
Note that you could write the first rule also as
rule "Add type Marque taxe"
when
$d : Demarche( typeDemarche == "Marque")
then
$d.getListTaxes().put(14, 1);
insert( new IsMarque( $d ) );
System.out.println("infer marque");
end
if (and only if) no other rule as CEs referring to the listTaxes property. Since you have used this "dirty update" in rule "Add Marque Polynesie extention taxe", it seems to be OK.
Please post complete rules - #1 doesn't compile.

Drools get facts from database at runtime

I need a little help with Drools eval and variable assigning.
rule "check that no previously submitted requests exist"
when
$user : UserFormField( name == 'employeeId', value != null )
$repository : Repository( )
$activeRequests : List( ) from $repository.findActiveRequestsByEmployee( $user.getValue() ) # call to repository
eval( $activeRequests.size() > 0 )
then
System.err.println(' You have active requests: ' + ((Request)$activeRequests.get(0)).getTitle);
insert(Boolean.TRUE);
end
In this rule I try to access repository and get active requests for current user. Rule compiles and executes without any exceptions or warnings. In debug mode it can be seen that repository returns non empty list and I expect to see console message 'You have active requests' but this doesn't happen. I think the problem is in this line
$activeRequests : List( ) from $repository.findActiveRequestsByEmployee( $user.getValue() )
because this rule works fine
rule "check that no previously submitted requests exist"
when
$user : UserFormField( name == 'employeeId', value != null )
$repository : Repository( )
eval( $repository.findActiveRequestsByEmployee($user.getValue()).size() > 0 )
then
System.err.println(' You have active requests !' );
insert(Boolean.TRUE);
end
So could someone point me how to solve this problem?
Thanks!
I was helped to find a solution. I should use from collect expression instead simple from to bundle facts into collection :
$activeRequests : ArrayList() from collect ( Request() from $repository.findActiveRequestsByEmployee( $user.getValue() ) )
You have to distinguish (i.e., read the fine print in the
documentation) between "from" and "from collect". If you want the rule
to fire individually for each collection element produced by the
expression after "from", then use "from". If, however, you want to
have it all bundled into a collection you must used "from collect".
$activeRequests : ArrayList() from collect ( Request() from
$repository.findActiveRequestsByEmployee( $user.getValue() ) )
Note that the separate eval isn't necessary. You can put this
constraint into the ArrayList pattern:
ArrayList( size > 0 )

How do I bind DBI parameters at runtime in Perl?

I have the following code :
sub run_query {
my $name = shift || undef;
my $sql = (defined $name ) ? "select * from table where name = ?" :
"select * from table";
my $sth = $dbh->prepare("$sql");
$sth->execute($name);
}
The above subroutine need to work as follows: if $name is provided, then run the first query, else fetch all the data from the table. How can I bind the name field? I'd like it bound dynamically if it is provided.
From the DBI documentation on cpan:
A common issue is to have a code fragment handle a value that could be
either defined or undef (non-NULL or NULL) at runtime. A simple
technique is to prepare the appropriate statement as needed, and
substitute the placeholder for non-NULL cases:
$sql_clause = defined $age ? "age = ?" : "age IS NULL";
$sth = $dbh->prepare(qq{
SELECT fullname FROM people WHERE $sql_clause
});
$sth->execute(defined $age ? $age : ());
It does not exactly apply to your question, which I assume is that your execute fails if you add an argument where one is not expected. So, the last line here would apply:
$sth->execute(defined $name ? $name : ());
You should probably have two different subs, but you could use
sub run_query {
my $sql = #_
? "select * from table where name = ?"
: "select * from table";
my $sth = $dbh->prepare($sql);
$sth->execute(#_);
}
You can conditionally omit parameters if $name is not defined:
$sth->execute(defined $name ? $name : ());