I have a complex Drools (8.33) DMN. I enrich an Object (datatype: MyType) with add. fields with 'context put' => a function call with MyType fails - sorting

I have an input as list of Datatype 'MyType' with some string fields (defined as itemComponent).
MyType: {"partner": "xyz", "start": "2020-01-01", ...}
A DMN sort() tries to sort the list. After that, I add an info object to each object of MyType.
context put(myObject, {"position": 1, "name": "x"})
Now, I try to call function (BKF) with parameter MyType, giving the result of 'context put'. If the function tries to access a field like:
Literal Expression: if myObject.partner="X" then true else false
the call fails. The call is done inside of another sort() function and I get the error message:
FEEL Error: ...The parameter 'list', in function sort(), raised an exception while sorting by natural order.
Is the Object of MyType not a MyType after using the result of 'context put'?
And is it possible to get a specific error message inside a Drools DMN sort() function?
I removed the access to the field, then the sort() function ran without error.
I tried to access the field with 'get value(myObject, "partner") -> didn't work

Related

Adding a number in a tuple

I'm trying to add a number to a tuple but i get this error:
"TypeError: 'int' object is not iterable"
here's my code:
v = tuple(int(input('enter a value: ')))
print(v)
what does this error mean? and how can I solve it?
There are two parts to this error message: TypeError and the error message.
A TypeError is raised when a function is applied to an object of the wrong data type.They occur when you try to apply a function on a value of the wrong type.For instance, if you try to apply a mathematical function to a string, or call a value like a function which is not a function, a TypeError is raised.
The error message tells us that you have tried to iterate over an object that is not iterable. Iterable objects are items whose values you can access using a “for loop”(for example). An “‘int’ object is not iterable” error is raised when you try to iterate over an integer value.
To solve this error, make sure that you are iterating over an iterable rather than a number.
You have to convert tuple to list to add an element('number' in your case) and then append the number to the list.
tuple1 = ('q','w','35','70.00')
list1 = list(tuple1)
list1.append(input('enter a value : '))
tuple1= tuple(list1)
I have stated an example illustrating how we can append to list and then change it to tuple.

Golang error control on parameters when calling a function with reflection

I'm trying to call a function using reflection based on it's name.
I first recover the function with
f := reflect.ValueOf(s).MethodByName(name)
I do error control on the function name by checking if "f" is valid with
if !f.IsValid() {
return errors.New("There is no function with that name")
}
And finally I perform the call with
f.Call(inputs)
My challenge is that inputs depends on the user, and sometimes they can put too many parameters, too little or invalid types.
IE:
2019/01/04 16:47:54 http: panic serving [::1]:53662: reflect: Call using string as type int
How can I control that the inputs are valid before performing the call?
Maybe there is a way to recover the expected inputs in my method and check them against the provided ones?

How to cast a promise error to a custom type?

Hej, I have this piece of code
BsFirebase.Auth.signInWithEmailAndPassword(
Firebase.auth,
~email=self.state.email,
~password=self.state.password,
)
|> Js.Promise.then_(_user => {
// ...
})
|> Js.Promise.catch((error) => {
// ...
})
|> ignore;
In the catch, the error field includes a field code that I can use to have more details about the error.
I'm trying to write a wrapper function to cast the error to a "custom" type but I have no idea how to do it.
So far I have (thanks Jared)
type firebaseError = {"code": int, "message": string}; /* etc. */
external unsafeCeorceToFirebaseError: Js.Promise.error => firebaseError = "%identity";
How can I add extra check to be sure the code property exists in the error ?
One way would be to treat the data structure as JSON, if that's appropriate, and use a JSON decoding library like bs-json since those are designed to deal with unknown or unreliable data structures.
Alternatively, you can type each field as a Js.Nullable.t and test them individually upon use.
Although if you can avoid using promise errors for error handling that would be much preferred, as it's not a type safe way of doing it. Use the result type instead and treat promise errors like exceptions.

Why does graphql-ruby function definition work via kwarg, but not via the function method in a block?

Where
class FindRecord < GraphQL::Function
...
argument :id, !types.ID
...
end
this variation of a field definition within a GraphQL::ObjectType.define block works without error:
field 'd1', DirectoryType, function: FindRecord.new(:directory)
But for the same query this fails with the error message "Field 'd2' doesn't accept argument 'id'":
field 'd2', DirectoryType do
function FindRecord.new(:directory)
end
After those fields are defined, for both d1 and d2 the value of target.fields['d?'].function is the same (with different object ids). But it looks like in the block version the function doesn't get applied to the field.
It also doesn't work when I skip the field helper and define the field like this:
target.fields[name] = GraphQL::Field.define {
...
function FindRecord.new(:directory)
...
}
Am I calling the function method wrong? Any suggestions appreciated.
You probably want to use resolve in the block instead of function (which is an option to the field method, and not a helper available on the block):
field 'd2', DirectoryType do
resolve FindRecord.new(:directory)
end
because Functions usually implement the same call(obj, args, ctx) method, then they can be considered Callables and you should be able to use it with the resolve helper instead of the usual way:
field :students, types[StudentType] do
argument :grade, types.Int
resolve ->(obj, args, ctx) {
Student.where(grade: args[:grade])
}
end

Returning empty UDT From a function

I have written a function that returns a User Defined Type.
How can i return a empty UDT in case of any error from the function?
I tried setting function to 'Nothing',but it is throwing 'Object Required' error.
Thanks in advance.
If at all possible, use a Class/Object instead. Even doing something as simple as turning this type:
Public Type EmpRecord
FName As String
LName As String
HiredDate As Date
End Type
into a class can be done by adding a Class to your project called EmpRecord and including just this in it:
Public FName As String
Public LName As String
Public HiredDate As Date
Then you can return Nothing from a function that gets an error while retrieving the record.
In VB6, a user-defined type is a "value type", while a class is a "reference type". Value types are typically stored on the stack (unless they're a member of a class). Reference types are stored as a pointer on the stack pointing to a place in the heap where the actual instance data is stored.
That means a reference to a class can be Nothing (the pointer is zero), whereas a value type can't.
There are multiple ways to solve your problem:
Add a Boolean member to your user-defined type to indicate success or failure.
Create another "wrapper" UDT like (see below) and return it from your function.
Change your UDT into a class, which can be Nothing (as tcarvin said).
Write the function to return a Boolean and take a ByRef parameter. The results are written to the parameter passed in if the function result is True. (A lot of people don't like this, but it's a common solution you should be aware of.)
Wrapper:
Public Type Wrapper
Success As Boolean
Inner As YourOriginalUDT
End Type
Function with ByRef:
Function Foo(ByRef Result As YourOriginalUDT) As Boolean
...
If Success Then
Foo = True
Result.A = A
Result.B = B
Result.C = C
... etc. ...
End If
End Function
A UDT can't be empty.
You can either use a "dummy" unintialised UDT, or just set all it's members back to the default values.

Resources