Strange drools syntax - drools

I am having difficulty understanding the part value : value == 0? How does this code work?
rule "My rule"
when
m : MyClass( value : value == 0)
then
end

Assuming you are using Drools 5.4 or a newer snapshot, you can write any boolean expression as a constraint, so value == 0 is a constraint where "value" is a field name in MyClass.
Drools also allows you do use ":" to bind an attribute to a variable name, like this:
<variable_name> : <fieldName>
So, you can write:
MyClass( $var : value == 0)
Finally, since Drools uses a "context-aware" parser, you can have a variable with the same name as the attribute name, because Drools knows what comes before the : is a variable name, not a field. So, in your example: the variable "value" will be bound to the attribute "value" and the constraint "value == 0" will be true if the value attribute is equal to 0.
Hope it helps.

Related

Can I get a field from a list of maps using a variable name instead of a specific field name

I have a list of maps (bText) and the map of field names include (kjvtext, nasbtext, esvtext) string fields.
I have another preferences variable: String bVersion = "esvtext";
I want to access the map field bText[index].esvtext when the bVersion field contains "esvtext" and field bText[index].nasbtext when the bVersion field contains "nasbtext".
I've tried several combinations of code such as:
Text(bText[index].bVersion)
Text(bText[index].[bVersion])
Text(bText[index]."$bVersion")...
I've finally resorted to:
xText = (bVersion == "kjvtext")
? verselist[index].kjvtext
: (bVersion == "nasbtext")
? verselist[index].nasbtext
: (bVersion == "esvtext")
? verselist[index].esvtext
: "bVersion version error\n",
Seems like there would be a simpler way to code this?
Any thoughts? Thanks in advance!

Add jsonb property of type boolean

I'm trying to add a property to an existing jsonb column (column "data").
I want have my jsonb document to log like this
{
// ... existing properties
"Filed": false // new property
}
I tried
UPDATE "doc" SET "data" = jsonb_set("data"::jsonb, 'Filed', false, true)
I get this error:
[42883] ERROR: function jsonb_set(jsonb, unknown, boolean, boolean) does not exist
Hint: No function matches the given name and argument types.
You might need to add explicit type casts. Position: 46
It should be
jsonb_set("data"::jsonb, '{Filed}', 'false', TRUE)
The second parameter is an array denoting the path to the appropriate key, and 'false' is the string representation of a JSON boolean.
Better use the || operator.
UPDATE "doc" SET "data" = "data" || '{"Filed": false}';
This one is equivalent but more suitable for parameterization:
UPDATE "doc" SET "data" = "data" || jsonb_build_object('Filed', false);

How to define and access local variable in Typoscript 2 (Neos)?

I have created a custom NodeType "Events" with a custom TS2 file in Neos, but I guess it is more a general question about Typoscript 2.
prototype(Some.Namespace:Events) < prototype(TYPO3.Neos:Document) {
...
sortOrder = ${request.arguments.sortOrder == 'asc' ? 'asc' : 'desc'}
otherVariable = ${sortOrder}
...
}
Of course this is simplified to focus on the issue:
I want to assign the value of the variable sortOrder (which is "asc" or "desc") to another variable named otherVariable.
How can I do that? I cannot access the value using ${sortOrder}, which returns always NULL.
All you need to do is add this as below and {otherVariable} in your fluid template will work. Flush cache in case you sill have NULL.
sortOrder = ${request.arguments.sortOrder == 'asc' ? 'asc' : 'desc'}
otherVariable = ${this.sortOrder}

Default value for null fields in a Jasper Report

Background
A ResultSet has many Double value fields (with patterns like "###0.000"). Some values can be null.
Problem
I want to replace null values with "N/A", which is a String and cannot print to a Double field. Printing "0.00" for null values is unacceptable.
Using an PrintWhenExpression value of ($F{value} != null) ? $F{value} : "N/A" does not work; it is not possible to use patterns in that way.
Idea
Add hidden fields that write "N/A". These fields will be printed only if value is null.
Question
Is there a better solution, and if so, what is it?
Thank you.
Solution #1
Your solution:
Use a regular Double field (doubleField) for the column value.
Add a static String text field at the same location.
Change the Double field to Blank When Null.
Set the PrintWhenExpression value for the String text field to: $F{doubleField} == null.
Solution #2
The problem is, as you pointed out, that a Double and a String are two different data types. You can assign a String variable to the value of the Double using an appropriate expression. Then use the String variable as the field. The expression might resemble:
($F{doubleField} == null) ?
"N/A" : new java.text.DecimalFormat("#.##").format($F{doubleField})
(Note: My preference is to use == instead of !=. Think positive.)
Solution #3
Change the SQL statement to pre-format the Double as a text string, and use the "N/A" in the string (by using a CASE or DECODE statement in the query).
Avoid this solution, though, as it is not maintainable.
Recommendation
Do not hard-code the "N/A" string throughout the report(s); put the "N/A" text in a constant, or a parameter with a default value of "N/A".
You can try something like this in your field expression:
("Your static text "+(($F{field}!=null)?$F{field}:""))
Or, if you don't want your static text to be visible when the field is null, try putting $F{field}!=null in your PrintWhenExpression.

EntLib Way to Bind "Null" Value to Parameter

I wish to pass Null Value to the parameter as follow:
_db.AddInParameter(dbCommand, "Id", DBNull.Value, myContactPerson.Id);
I am receiving the following error :
"can not convert "System.DBNull to System.Data.DbType".
I know the meaning of this error.
But i need to supply null value to myContactPerson.Id
How can i achieve this ?
If myContactPerson.Id isn't an auto-number, then why not just pass 0.
DBType should be passed in that parameter and should match your the dbtype (string, int, etc.) for the table that you are comparing with in your database. You would replace your value field "myContactPerson.Id" with DBNull.Value to always pass the null value.