telefonicaid / perseo-fe

Front End process for the Perseo CEP
GNU Affero General Public License v3.0
15 stars 29 forks source link

Casting to float: document how to do it #345

Open manucarrace opened 5 years ago

manucarrace commented 5 years ago

In order to make a casting to float it is needed to make the following (cast(cast(battery?, String), float)

If we make (cast(battery?, float) the rule does not work as expected.

AlvaroVega commented 5 years ago

In (cast(battery?, float) is there a missed ) ?

fgalan commented 5 years ago

Is this actually a problem with Perseo? Or just a consequence of how EPL works?

AlvaroVega commented 5 years ago

Could you provide a full example (rule, action, data, expected data) ?

In general if you use version 2 of NGSI for updateAction then float cast is done automatically: https://github.com/telefonicaid/perseo-fe/blob/master/lib/models/updateAction.js#L280

AlvaroVega commented 5 years ago

if there is a temperature attribute of type Number, or inserted in ContextBroker as a real numerical type then you don't need to cast to string, just cast to float:

select *, *, ev.temperature? as Temperature,  ev.id? as Meter from pattern [every ev=iotEvent(cast(temperature?,float)>1.5)]
mrutid commented 1 year ago

Two nested cast expression are needed if you expect to receive Strings representing Numbers is your context stream.

Examples and explanation:

Every Dinamic Property (fillingLevel?) has Object type in Esper, therefore casting is needed to use fillingLevel in float expressions.

image

If we do the float cast it works when fillingLevel is a number (int or float). But fails if it's a String containing a number (in fact an Object containing a String "containing" a number):

image

If we resolve the Object to String (which works almost every time) then we can safety cast to float:

image

In this case if filling level contains a value uncasteable to float ("PATATA") an exception occurs, so rule doesn't get fired.

AlvaroVega commented 1 year ago

It would be nice document that explanation https://github.com/telefonicaid/perseo-fe/issues/345#issuecomment-1625123336 into doc about EPL usage https://github.com/telefonicaid/perseo-fe/blob/master/docs/API/plain_rules.md#epl-text