Drools Runtime does not recognize Custom Operator Parameter - operators

Problem Statement:
We have few custom operators for Date and Strings, which are implemented looking at Str operator like shown here: https://github.com/kiegroup/drools/blob/f7ab4ee3c88d767a8cbb372c2db1afa84b20a51d/drools-core/src/main/java/org/drools/core/base/evaluators/StrEvaluatorDefinition.java
with below configuration in kie.properties.conf:
drools.evaluator.str = com.company.drools.operators.StrEvaluatorDefinition
For Custom Str operator we created, Drools runtime is not able to make distinction between Str[begins] and Str[ends].
What this means is when we are running below tests, we expect Rule-1 to fail and Rule-2 to get activated. But what we noticed is, Rule-2 will not be activated.
// Facts input into drools :
// str1 = drools
// str2 = dr
rule "Rule-1"
when message : Customer( str1 str[endsWith] str2 )
then System.out.println("Rule - Str 1 - fired");
end
rule "Rule-2"
when message : Customer( str1 str[startsWith] str2 )
then System.out.println("Rule - Str 2 - fired");
end
Other Observations:
If we change RHS to a a string literal like shown below, then Rule-2 will be Activated.
// Facts input into drools :
// str1 = drools
// str2 = dr
rule "Rule-1"
when message : Customer( str1 str[endsWith] "abc" )
then System.out.println("Rule - Str 1 - fired");
end
rule "Rule-2"
when message : Customer( str1 str[startsWith] str2 )
then System.out.println("Rule - Str 2 - fired");
end
Finally
We removed Str custom operator, and then used the out of the box Str operator provided by drools. We ran below mentioned tests again, this time Rule-2 gets activated.
// Facts input into drools :
// str1 = drools
// str2 = dr
rule "Rule-1"
when message : Customer( str1 str[endsWith] str2 )
then System.out.println("Rule - Str 1 - fired");
end
rule "Rule-2"
when message : Customer( str1 str[startsWith] str2 )
then System.out.println("Rule - Str 2 - fired");
end
Need help on :
Is there an issue with the way we are registering custom operator? because we literally copied entire code from Drools Repo and created our own operator. But notice different behavior in executions.
We have other operators like date[before/after/equals] we are noticing same behavior for them too. We are using latest version of the drools JARs.

Related

Building string from list of list of strings

I rather have this ugly way of building a string from a list as:
val input = listOf("[A,B]", "[C,D]")
val builder = StringBuilder()
builder.append("Serialized('IDs((")
for (pt in input) {
builder.append(pt[0] + " " + pt[1])
builder.append(", ")
}
builder.append("))')")
The problem is that it adds a comma after the last element and if I want to avoid that I need to add another if check in the loop for the last element.
I wonder if there is a more concise way of doing this in kotlin?
EDIT
End result should be something like:
Serialized('IDs((A B,C D))')
In Kotlin you can use joinToString for this kind of use case (it deals with inserting the separator only between elements).
It is very versatile because it allows to specify a transform function for each element (in addition to the more classic separator, prefix, postfix). This makes it equivalent to mapping all elements to strings and then joining them together, but in one single call.
If input really is a List<List<String>> like you mention in the title and you assume in your loop, you can use:
input.joinToString(
prefix = "Serialized('IDs((",
postfix = "))')",
separator = ", ",
) { (x, y) -> "$x $y" }
Note that the syntax with (x, y) is a destructuring syntax that automatically gets the first and second element of the lists inside your list (parentheses are important).
If your input is in fact a List<String> as in listOf("[A,B]", "[C,D]") that you wrote at the top of your code, you can instead use:
input.joinToString(
prefix = "Serialized('IDs((",
postfix = "))')",
separator = ", ",
) { it.removeSurrounding("[", "]").replace(",", " ") }
val input = listOf("[A,B]", "[C,D]")
val result =
"Serialized('IDs((" +
input.joinToString(",") { it.removeSurrounding("[", "]").replace(",", " ") } +
"))')"
println(result) // Output: Serialized('IDs((A B,C D))')
Kotlin provides an extension function [joinToString][1] (in Iterable) for this type of purpose.
input.joinToString(",", "Serialized('IDs((", "))')")
This will correctly add the separator.

Make string starts with specified prefix in Kotlin

I am trying to find out the native way to concatenate prefix to string, but only in case, it was not.
This code checks the text variable and makes it start with "#".
val text = "123456"
val prefix = "#"
val textFormatted = (if (text.startsWith(prefix)) "" else prefix ) + text
I hope there are clean solutions somewhere in Kotlin
An alternative would be to use removePrefix:
val textFormatted = prefix + text.removePrefix(prefix)
Otherwise you could also keep the if but write it the following way to avoid extra parentheses and extra concatenation, and also make the code closer to the semantics:
val textFormatted = if (text.startsWith(prefix)) text else "$prefix$text"
But your solution works too.
You can use the string interpolation from the kotlin, example:
val text:String = "123456#123456"
val prefix:String = "#"
val interpolation:String = "#${text.removePrefix(prefix)}"

How to set large string as param without getting ORA-01460: unimplemented or unreasonable conversion error?

In spring-boot using namedParameterJdbcTemplate (Oracle db version 12 and odbc8 driver 12.2)
I am getting the following error while executing a SELECT query bound with a parameter larger than 4000 character whereas update queries working fine.
ORA-01460: unimplemented or unreasonable conversion requested
The unit test I am trying to execute;
#Test
public void testSqlSelectQueryLargeStringParameter() {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("param", theLargeStr);
// #formatter:off
String sql =
"SELECT NULL id, NULL text FROM DUAL WHERE 'X' != :param ";
// #formatter:on
namedParameterJdbcTemplate.queryForRowSet(sql, params);
}
Is there any way to set this large param via MapSqlParameterSource?
I am #ahmet-orhan 's colleague, we've found a solution.
Thanks #kfinity for your suggestion, It is working for insert and update but we are still getting this error when we set clob or blob as "paremeter" in select statements.
If using a driver that supports JDBC4.0, the right solution is create a DefaultLobHandler and set streamAsLob or createTemporaryLob to true.
MapSqlParameterSource params = new MapSqlParameterSource();
String myString = "";
for (int i = 0; i < MAX_CLOB_BLOB_SIZE_IN_SELECT; i++) {
myString = myString + "1";
}
DefaultLobHandler lobHandler = new DefaultLobHandler();
lobHandler.setStreamAsLob(true);
params.addValue("param", new SqlLobValue(myString, lobHandler), Types.CLOB);
// #formatter:off
String sql =
"SELECT 1 id FROM DUAL WHERE :param IS NOT NULL ";
// #formatter:on
Integer id = namedParameterJdbcTemplate.queryForObject(sql, params, Integer.class);
We prefer streamAsLob but to be honest we have no idea which one is better.
This comment points out that ORA-01460 in JDBC queries is the same as "ORA-01704: string literal too long". (You can't have string literals longer than 4000 characters.) Maybe try this solution?
params.addValue("param", theLargeStr, Types.CLOB);
Although also != won't work for clob comparison, so you'll also need to change your query to
SELECT NULL id, NULL text FROM DUAL WHERE dbms_lob.compare('X',:param) != 0

Elm : initialise set in the model

I am new to Elm. What I want to do is I am trying to initialise model with a set instead of a List, but given elm doesn't have any initialisers for sets (which is a shame, it'd be good if it was #{1,2,3}, as in Clojure), it is problematic.
With the code (Elm tutorial code, a little bit modified), I am trying to
main =
App.program
{ init = init "cats"
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ topic : String
, gifUrl : String
, error : String
, history : Set String
}
init : String -> (Model, Cmd Msg)
init topic =
( Model topic "waiting.gif" "" Set.fromList([topic])
, getRandomGif topic
)
Which throws me a compiler error of:
Function `Model` is expecting 4 arguments, but was given 5.
Which is strange because this doesn't throw an error in Elm repl and is a valid code in most cases.
How would I achieve this?
The problem originates from your use of parentheses.
Model topic "waiting.gif" "" Set.fromList([topic])
-- is the same as:
Model topic "waiting.gif" "" Set.fromList [topic]
You don't need to enclose arguments in parentheses in Elm, but you do need to enclose the entire fourth parameter of Set.fromList [topic] so that the compiler knows it's a single parameter. Change it to this and you should be all set:
Model topic "waiting.gif" "" (Set.fromList [topic])

how to make a function that prints a variable number of parameters in pascal?

I want to let the user give me a variable number of strings (as virables).
example :
begin
cout('Hello').(' ').('world')
end.
this will print: "Hello world"
I know I just can let him input a string but I want to this code to work...
I think a record will help nut I dont know how
thank you
Im not sure what is that code example you have written ... but i try to help you.
Program test;
uses crt;
var string1,string2,string3:string;
begin
write("Write to first string : "); readln(string1);
write("Write to second string: "); readln(string2);
write("Write to third string : "); readln(string3);
String2:= string1 +" "+ string3; // it will add first with third string to one with one space
String3:= string2 +" "+ string1;// also 2.+ 1.
clrscr; //clearscreen (in CRT)
writeln("1. = 1.");
writeln("2. = 1. + 3.");
writeln("3. = 2. + 1.");
writeln;
writeln(string1);
writelm(string2);
writeln(string3);
I havent tested this (i have written it now for you) i think you will read it and learn how to addict or how to do simple with strings.