Search in sources :

Example 1 with Alarm

use of org.drools.modelcompiler.fireandalarm.model.Alarm in project drools by kiegroup.

the class FireAndAlarmTest method testFireAndAlarm.

@Test
public void testFireAndAlarm() {
    Variable<Room> room = any(Room.class);
    Variable<Fire> fire = any(Fire.class);
    Variable<Sprinkler> sprinkler = any(Sprinkler.class);
    Variable<Alarm> alarm = any(Alarm.class);
    Rule r1 = rule("When there is a fire turn on the sprinkler").build(expr(sprinkler, s -> !s.isOn()), expr(sprinkler, fire, (s, f) -> s.getRoom().equals(f.getRoom())), on(sprinkler).execute(s -> {
        System.out.println("Turn on the sprinkler for room " + s.getRoom().getName());
        s.setOn(true);
    }).update(sprinkler, "on"));
    Rule r2 = rule("When the fire is gone turn off the sprinkler").build(expr(sprinkler, Sprinkler::isOn), not(fire, sprinkler, (f, s) -> f.getRoom().equals(s.getRoom())), on(sprinkler).execute(s -> {
        System.out.println("Turn off the sprinkler for room " + s.getRoom().getName());
        s.setOn(false);
    }).update(sprinkler, "on"));
    Rule r3 = rule("Raise the alarm when we have one or more fires").build(exists(fire), execute(() -> System.out.println("Raise the alarm")).insert(() -> new Alarm()));
    Rule r4 = rule("Lower the alarm when all the fires have gone").build(not(fire), input(alarm), execute(() -> System.out.println("Lower the alarm")).delete(alarm));
    Rule r5 = rule("Status output when things are ok").build(not(alarm), not(sprinkler, Sprinkler::isOn), execute(() -> System.out.println("Everything is ok")));
    Model model = new ModelImpl().withRules(asList(r1, r2, r3, r4, r5));
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    // phase 1
    Room room1 = new Room("Room 1");
    ksession.insert(room1);
    FactHandle fireFact1 = ksession.insert(new Fire(room1));
    ksession.fireAllRules();
    // phase 2
    Sprinkler sprinkler1 = new Sprinkler(room1);
    ksession.insert(sprinkler1);
    ksession.fireAllRules();
    assertTrue(sprinkler1.isOn());
    // phase 3
    ksession.delete(fireFact1);
    ksession.fireAllRules();
}
Also used : Fire(org.drools.modelcompiler.fireandalarm.model.Fire) Room(org.drools.modelcompiler.fireandalarm.model.Room) Variable(org.drools.model.Variable) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) FlowDSL(org.drools.model.FlowDSL) Sprinkler(org.drools.modelcompiler.fireandalarm.model.Sprinkler) Alarm(org.drools.modelcompiler.fireandalarm.model.Alarm) Arrays.asList(java.util.Arrays.asList) Rule(org.drools.model.Rule) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) ModelImpl(org.drools.model.impl.ModelImpl) FactHandle(org.kie.api.runtime.rule.FactHandle) Sprinkler(org.drools.modelcompiler.fireandalarm.model.Sprinkler) KieBase(org.kie.api.KieBase) Fire(org.drools.modelcompiler.fireandalarm.model.Fire) Alarm(org.drools.modelcompiler.fireandalarm.model.Alarm) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Room(org.drools.modelcompiler.fireandalarm.model.Room) Test(org.junit.Test)

Example 2 with Alarm

use of org.drools.modelcompiler.fireandalarm.model.Alarm in project drools by kiegroup.

the class FireAndAlarmUsingDroolsTest method testFireAndAlarmUsingDroolsInConsequences.

@Test
public void testFireAndAlarmUsingDroolsInConsequences() {
    Variable<Room> room = any(Room.class);
    Variable<Fire> fire = any(Fire.class);
    Variable<Sprinkler> sprinkler = any(Sprinkler.class);
    Variable<Alarm> alarm = any(Alarm.class);
    Rule r1 = rule("When there is a fire turn on the sprinkler").build(expr(sprinkler, s -> !s.isOn()), expr(sprinkler, fire, (s, f) -> s.getRoom().equals(f.getRoom())), on(sprinkler).execute((drools, s) -> {
        System.out.println("Turn on the sprinkler for room " + s.getRoom().getName());
        s.setOn(true);
        drools.update(s, "on");
    }));
    BitMask r2_mask1 = BitMask.getPatternMask(Sprinkler.class, "on");
    Rule r2 = rule("When the fire is gone turn off the sprinkler").build(expr(sprinkler, Sprinkler::isOn), not(fire, sprinkler, (f, s) -> f.getRoom().equals(s.getRoom())), on(sprinkler).execute((drools, s) -> {
        System.out.println("Turn off the sprinkler for room " + s.getRoom().getName());
        s.setOn(false);
        drools.update(s, r2_mask1);
    }));
    Rule r3 = rule("Raise the alarm when we have one or more fires").build(exists(fire), execute(drools -> {
        System.out.println("Raise the alarm");
        drools.insert(new Alarm());
    }));
    Rule r4 = rule("Lower the alarm when all the fires have gone").build(not(fire), on(alarm).execute((drools, a) -> {
        System.out.println("Lower the alarm");
        drools.delete(a);
    }));
    Rule r5 = rule("Status output when things are ok").build(not(alarm), not(sprinkler, Sprinkler::isOn), execute(() -> System.out.println("Everything is ok")));
    Model model = new ModelImpl().withRules(asList(r1, r2, r3, r4, r5));
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    // phase 1
    Room room1 = new Room("Room 1");
    ksession.insert(room1);
    FactHandle fireFact1 = ksession.insert(new Fire(room1));
    ksession.fireAllRules();
    // phase 2
    Sprinkler sprinkler1 = new Sprinkler(room1);
    ksession.insert(sprinkler1);
    ksession.fireAllRules();
    assertTrue(sprinkler1.isOn());
    // phase 3
    ksession.delete(fireFact1);
    ksession.fireAllRules();
}
Also used : BitMask(org.drools.model.BitMask) Fire(org.drools.modelcompiler.fireandalarm.model.Fire) Room(org.drools.modelcompiler.fireandalarm.model.Room) Variable(org.drools.model.Variable) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) FlowDSL(org.drools.model.FlowDSL) Sprinkler(org.drools.modelcompiler.fireandalarm.model.Sprinkler) Alarm(org.drools.modelcompiler.fireandalarm.model.Alarm) Arrays.asList(java.util.Arrays.asList) Rule(org.drools.model.Rule) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) ModelImpl(org.drools.model.impl.ModelImpl) FactHandle(org.kie.api.runtime.rule.FactHandle) Sprinkler(org.drools.modelcompiler.fireandalarm.model.Sprinkler) BitMask(org.drools.model.BitMask) KieBase(org.kie.api.KieBase) Fire(org.drools.modelcompiler.fireandalarm.model.Fire) Alarm(org.drools.modelcompiler.fireandalarm.model.Alarm) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Room(org.drools.modelcompiler.fireandalarm.model.Room) Test(org.junit.Test)

Aggregations

Arrays.asList (java.util.Arrays.asList)2 FlowDSL (org.drools.model.FlowDSL)2 Model (org.drools.model.Model)2 Rule (org.drools.model.Rule)2 Variable (org.drools.model.Variable)2 ModelImpl (org.drools.model.impl.ModelImpl)2 KieBaseBuilder (org.drools.modelcompiler.builder.KieBaseBuilder)2 Alarm (org.drools.modelcompiler.fireandalarm.model.Alarm)2 Fire (org.drools.modelcompiler.fireandalarm.model.Fire)2 Room (org.drools.modelcompiler.fireandalarm.model.Room)2 Sprinkler (org.drools.modelcompiler.fireandalarm.model.Sprinkler)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieSession (org.kie.api.runtime.KieSession)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 BitMask (org.drools.model.BitMask)1