Search in sources :

Example 1 with SensorEvent

use of org.drools.compiler.oopath.model.SensorEvent in project drools by kiegroup.

the class OOPathQueriesTest method testReactiveQuery.

@Test
public void testReactiveQuery() {
    final String drl = "import org.drools.compiler.oopath.model.Room;\n" + "import org.drools.compiler.oopath.model.Sensor;\n" + "import org.drools.compiler.oopath.model.SensorEvent;\n" + "query temperature ( Room $r, double $t )\n" + "    $t := /$r/temperatureSensor/value\n" + "end\n" + "" + "rule \"Change sensor value\" when\n" + "    $e : SensorEvent( $s : sensor, $v : value)\n" + "then\n" + "    modify($s) { setValue($v) }\n" + "    retract($e)\n" + "end\n" + "" + "rule \"Turn heating on\" when\n" + "    $r : Room()\n" + "    temperature( $r, $t; )\n" + "    eval( $t < 20 )" + "then\n" + "    $r.getHeating().setOn(true);\n" + "end\n" + "rule \"Turn heating off\" when\n" + "    $r : Room()\n" + "    temperature( $r, $t; )\n" + "    eval( $t > 20 )" + "then\n" + "    $r.getHeating().setOn(false);\n" + "end\n";
    final Room room = new Room("Room");
    room.getTemperatureSensor().setValue(15);
    room.getHeating().setOn(false);
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    ksession.insert(room);
    ksession.insert(room.getTemperatureSensor());
    ksession.insert(room.getHeating());
    ksession.fireAllRules();
    assertThat(room.getHeating().isOn()).as("Temperature is bellow 20 degrees of Celsius. Heating should be turned on.").isTrue();
    ksession.insert(new SensorEvent(room.getTemperatureSensor(), 25));
    ksession.fireAllRules();
    assertThat(room.getHeating().isOn()).as("Temperature is higher than 20 degrees of Celsius. Heating should be turned off.").isFalse();
    ksession.dispose();
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Room(org.drools.compiler.oopath.model.Room) SensorEvent(org.drools.compiler.oopath.model.SensorEvent) Test(org.junit.Test)

Example 2 with SensorEvent

use of org.drools.compiler.oopath.model.SensorEvent in project drools by kiegroup.

the class OOPathQueriesTest method testNonReactiveOOPathInQuery.

@Test
public void testNonReactiveOOPathInQuery() {
    final String drl = "import org.drools.compiler.oopath.model.Room;\n" + "import org.drools.compiler.oopath.model.Sensor;\n" + "import org.drools.compiler.oopath.model.SensorEvent;\n" + "query temperature ( Room $r, double $t )\n" + "    $t := /$r?/temperatureSensor/value\n" + "end\n" + "" + "rule \"Change sensor value\" when\n" + "    $e : SensorEvent( $s : sensor, $v : value)\n" + "then\n" + "    modify($s) { setValue($v) }\n" + "    retract($e)\n" + "end\n" + "" + "rule \"Turn heating on\" when\n" + "    $r : Room()\n" + "    temperature( $r, $t; )\n" + "    eval( $t < 20 )" + "then\n" + "    $r.getHeating().setOn(true);\n" + "end\n" + "rule \"Turn heating off\" when\n" + "    $r : Room()\n" + "    temperature( $r, $t; )\n" + "    eval( $t > 20 )" + "then\n" + "    $r.getHeating().setOn(false);\n" + "end\n";
    final Room room = new Room("Room");
    room.getTemperatureSensor().setValue(15);
    room.getHeating().setOn(false);
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    ksession.insert(room);
    ksession.insert(room.getTemperatureSensor());
    ksession.insert(room.getHeating());
    ksession.fireAllRules();
    assertThat(room.getHeating().isOn()).as("Temperature is bellow 20 degrees of Celsius. Heating should be turned on.").isTrue();
    ksession.insert(new SensorEvent(room.getTemperatureSensor(), 25));
    ksession.fireAllRules();
    assertThat(room.getHeating().isOn()).as("Query is not reactive. Heating should still be turned on.").isTrue();
    ksession.dispose();
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Room(org.drools.compiler.oopath.model.Room) SensorEvent(org.drools.compiler.oopath.model.SensorEvent) Test(org.junit.Test)

Aggregations

Room (org.drools.compiler.oopath.model.Room)2 SensorEvent (org.drools.compiler.oopath.model.SensorEvent)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 KieHelper (org.kie.internal.utils.KieHelper)2