Advanced Rule Examples

Pierre Kil edited this page Nov 15, 2016 · 2 revisions
Clone this wiki locally

The following examples show rules that are using more technical code constructs. You should have some minimal programming knowledge to fully understand them. They are collected from use case examples from the community and may lack useful documentation for now.

Execute Rule with Function Arithmetic

package org.openremote.controller.model.event

import java.awt.Color

global org.openremote.controller.statuscache.CommandFacade execute

function int getRedComponent(int rgb)
{
  Color c = new Color(rgb);
  return c.getRed();
}

rule "Color Manipulation Functions"

when

  Range ( source == "hue", $hue : value )
  Level ( source == "saturation", $sat : value )
  Level ( source == "brightness", $bri : value )

then

  execute.command(
      "count", getRedComponent( Color.HSBtoRGB(
          new Float($hue) / 100,
          new Float($sat) / 100,
          new Float($bri) / 100 )
      )
  );

end

Fact Definition and Inserting New Facts into Rule Engine

package org.openremote.controller.model.event

global org.openremote.controller.statuscache.CommandFacade execute;

import org.openremote.controller.protocol.*;


/*
 * This example demonstrates creation of new facts from within the rules, in this
 * case a vacation object that can be used to determine differing behavior on event
 * actions.
 */
declare Vacation

  onVacation : boolean

end



/* Insert a new vacation fact */

rule "I'm going on vacation"

when

  Event ( source == "vacation start" )

then

  Vacation vacation = new Vacation();

  insert ( vacation );

end



/* Retract the vacation fact */

rule "I'm back from vacation"

when

  Event ( source == "vacation end" )

  $vacation : Vacation()

then

  retract ( $vacation );

end




/* Set night temperatures differently depending on presence of vacation fact */

rule "Set Night Temperature"

when

  CustomState ( source == "time of day", value == "night" ) and not Vacation()

then

  execute.command("temp", 18);

end


rule "Set Night Temperature when on Vacation"

when

  CustomState ( source == "time of day", value == "night" ) and exists Vacation()

then

  execute.command("temp", 15);

end



/* Set day temperatures differently depending on the presence of vacation fact */

rule "Set Day Temperature"

when

  CustomState ( source == "time of day", value == "day" ) and not Vacation()

then

  execute.command("temp", 21);

end


rule "Set Day Temperature when on Vacation"

when

  Event ( source == "time of day", value == "day" ) and exists Vacation()

then

  execute.command("temp", 15);

end

See also