Rule "if then else "

80 views
Skip to first unread message

Pierre Pierrot

unread,
Dec 3, 2017, 6:15:23 AM12/3/17
to OpenRemote
I tried to add a condition else to my rule as below (I used this tutorial: https://github.com/openremote/Documentation/wiki/OpenRemote-Rules-examples ) but I have an error that appears.


Rule Compilation error Syntax error on token "else", delete this token


rule "status_volet_salon" when


   
Event( source == "ZWay_2_0_4", value <= "10" )


 
then


   execute
.command( "virtual_volet_salon_status", 0 );


 
else


   execute
.command( "virtual_volet_salon_status", 1 );


end

Stuart Hanlon

unread,
Dec 3, 2017, 8:25:49 AM12/3/17
to openremot...@googlegroups.com
That should work, as I've got a number of rules like that in some configurations.


An alternative way of writing the if, then else rule might look like this :-


rule "02-10VMB7IN_8_Status"
when
    $evt
:Event(source matches "02-10VMB7IN_8_Status", $source : source) // NOTE SENSOR NAME IS WHAT IS BEING COMPARED HERE
then
   
String buttonStatus = $evt.getValue().toString();
   
boolean isActive = buttonStatus != null && buttonStatus.equalsIgnoreCase("Pressed"); // Replace "Pressed" with your march


   
System.out.println("Event Status Change '" + $source + "': " + buttonStatus); // Optional




   
if (isActive)
   
{


execute
.command("Relay 01 Virtual", "ON");




   
}
   
else
   
{
        execute
.command("Relay 01 Virtual", "OFF");
   
}
end



You'll need to ask someone else to confirm the following is acceptable...

But you could try adding a Boolean section to this...

rule "status_volet_salon" when


   
$evt:Event( source == "ZWay_2_0_4",  $source : source )


 
then

    String event = $evt.getValue().toString(); // Should the source be converted to a string? Should it be a Int if you want to calculate a result?


    boolean isActive = event != null && event.equalsIgnoreCase("Pressed"); // Replace all this with your match or condition. (I'm not sure it works this way)



   if (isActive)
   
{

   execute
.command( "virtual_volet_salon_status", 0 );

}

 
else

{
   execute
.command( "virtual_volet_salon_status", 1 );


}


end

If that works for you, I'll update the examples page ;-)


Just for reference, I use Java 1.6 in every controller which might make a difference to the required syntax.

Pierre Pierrot

unread,
Dec 3, 2017, 8:38:31 AM12/3/17
to OpenRemote
isActive is problematic.

Rule Compilation error isActive cannot be resolved to a variable

Stuart Hanlon

unread,
Dec 3, 2017, 8:54:24 AM12/3/17
to OpenRemote

Oh I see.


Is there a way to phrase your query to get a Boolean result ?

Pierre Pierrot

unread,
Dec 3, 2017, 9:25:52 AM12/3/17
to OpenRemote
yes it is possible to use a boolean result.

Stuart Hanlon

unread,
Dec 3, 2017, 10:09:46 AM12/3/17
to OpenRemote
Excellent....

When you get a good result, would you be kind enough to post your working code here so that I can copy and paste it into the examples?

Pierre Pierrot

unread,
Dec 9, 2017, 7:29:15 AM12/9/17
to openremot...@googlegroups.com
I tried this rule:

//Package, globals, imports


package org.openremote.controller.protocol

global org.openremote.controller.statuscache.CommandFacade execute;
global org.openremote.controller.statuscache.SwitchFacade switches;
global org.openremote.controller.statuscache.LevelFacade levels;

import org.openremote.controller.utils.Logger;
import org.openremote.controller.Constants;
import org.openremote.controller.model.event.*
import java.util.Date;
import java.text.SimpleDateFormat;

import java.lang.Float;
import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;
import java.io.*;
import java.util.*;
import java.util.regex.*;


rule "status_volet_salon" when

   Event( source == "ZWay_2_0_4" )
   if ( value > "10 ") break[on]
then
   execute.command( "virtual_volet_salon_status", 0 );
then[on]

   execute.command( "virtual_volet_salon_status", 1 );
end


But I have this error that appears: Rule Compilation error The operator > is undefined for the argument type(s) Object, String




And I tried this rule:

//Package, globals, imports


package org.openremote.controller.protocol

global org.openremote.controller.statuscache.CommandFacade execute;
global org.openremote.controller.statuscache.SwitchFacade switches;
global org.openremote.controller.statuscache.LevelFacade levels;

import org.openremote.controller.utils.Logger;
import org.openremote.controller.Constants;
import org.openremote.controller.model.event.*
import java.util.Date;
import java.text.SimpleDateFormat;

import java.lang.Float;
import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;
import java.io.*;
import java.util.*;
import java.util.regex.*;


rule "status_volet_salon" when

   Event( source == "ZWay_2_0_4" )
   if ( eval(Integer.parseInt(value.toString()) > 10 ) break[on]
then
   execute.command( "virtual_volet_salon_status", 0 );
then[on]

   execute.command( "virtual_volet_salon_status", 1 );
end


Rule definition 'modeler_rules.drl' could not be deployed. See errors below.
ERROR 2017-12-09 13:43:34,132 : [ERR 102] Line 580:55 mismatched input 'break' in rule "status_volet_salon"
ERROR 2017-12-09 13:43:34,132 : Parser returned a null Package

It may be missing a package !??
Reply all
Reply to author
Forward
0 new messages