use of org.drools.modelcompiler.fireandalarm.model.Sprinkler 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());
}
use of org.drools.modelcompiler.fireandalarm.model.Sprinkler 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.Sprinkler 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