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();
}
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();
}
Aggregations