Search in sources :

Example 1 with Fire

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

the class CompilerTest method testFireAndAlarm.

@Test
public void testFireAndAlarm() {
    String str = "import " + StringBuilder.class.getCanonicalName() + ";\n" + "import " + Alarm.class.getCanonicalName() + ";\n" + "import " + Fire.class.getCanonicalName() + ";\n" + "import " + Room.class.getCanonicalName() + ";\n" + "import " + Sprinkler.class.getCanonicalName() + ";\n" + "global StringBuilder sb;" + "rule \"When there is a fire turn on the sprinkler\"" + "when\n" + "   Fire( $room : room )\n" + "   $sprinkler : Sprinkler( room == $room, !on )\n" + "then\n" + "   modify( $sprinkler ) { setOn( true ) };\n" + "   sb.append( \"Turn on the sprinkler for room \" + $room.getName() + \"\\n\");\n" + "end\n" + "\n" + "rule \"When the fire is gone turn off the sprinkler\"" + "when\n" + "   $sprinkler : Sprinkler( $room : room, on == true )\n" + "   not Fire( room == $room )\n" + "then\n" + "   modify( $sprinkler ) { setOn( false ) };\n" + "   sb.append( \"Turn off the sprinkler for room \" + $room.getName() + \"\\n\" );\n" + "end\n" + "\n" + "rule \"Raise the alarm when we have one or more fires\"" + "when\n" + "   exists Fire()\n" + "then\n" + "   insert( new Alarm() );\n" + "   sb.append( \"Raise the alarm\\n\" );\n" + "end\n" + "\n" + "rule \"Lower the alarm when all the fires have gone\"" + "when\n" + "   not Fire()\n" + "   $alarm : Alarm()\n" + "then\n" + "   retract( $alarm );\n" + "   sb.append( \"Lower the alarm\\n\" );\n" + "end\n" + "\n" + "rule \"Status output when things are ok\"" + "when\n" + "   not Alarm()\n" + "   not Sprinkler( on )\n" + "then\n" + "   sb.append( \"Everything is ok\\n\" );\n" + "end\n";
    KieSession ksession = getKieSession(str);
    StringBuilder sb = new StringBuilder();
    ksession.setGlobal("sb", sb);
    // 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();
    // phase 3
    ksession.delete(fireFact1);
    ksession.fireAllRules();
    String result = "Raise the alarm\n" + "Turn on the sprinkler for room Room 1\n" + "Turn off the sprinkler for room Room 1\n" + "Lower the alarm\n" + "Everything is ok\n";
    assertEquals(result, sb.toString());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Fire(org.drools.modelcompiler.fireandalarm.model.Fire) KieSession(org.kie.api.runtime.KieSession) Room(org.drools.modelcompiler.fireandalarm.model.Room) Sprinkler(org.drools.modelcompiler.fireandalarm.model.Sprinkler) BaseModelTest(org.drools.modelcompiler.BaseModelTest) Test(org.junit.Test)

Example 2 with Fire

use of org.drools.modelcompiler.fireandalarm.model.Fire 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 3 with Fire

use of org.drools.modelcompiler.fireandalarm.model.Fire 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

Fire (org.drools.modelcompiler.fireandalarm.model.Fire)3 Room (org.drools.modelcompiler.fireandalarm.model.Room)3 Sprinkler (org.drools.modelcompiler.fireandalarm.model.Sprinkler)3 Test (org.junit.Test)3 KieSession (org.kie.api.runtime.KieSession)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 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 Assert.assertTrue (org.junit.Assert.assertTrue)2 KieBase (org.kie.api.KieBase)2 BitMask (org.drools.model.BitMask)1 BaseModelTest (org.drools.modelcompiler.BaseModelTest)1