Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sample
- import com.sample.DroolsTest.Message;
- import com.sample.DroolsTest.Pomiar;
- /*
- rule "Hello World"
- when
- m : Message( status == Message.HELLO, myMessage : message )
- then
- m.setMessage( "Goodbye cruel world" );
- m.setStatus( Message.GOODBYE );
- update( m );
- end
- rule "SI"
- when
- m : Message( status == Message.SI, myMessage : message )
- then
- m.setMessage( "SI LAB TEST MSG" );
- m.setStatus( Message.GOODBYE );
- update( m );
- end
- rule "GoodBye"
- when
- Message( status == Message.GOODBYE, myMessage : message )
- then
- System.out.println( myMessage );
- end
- */
- rule "PALI"
- when
- p : Pomiar( getDym() == Pomiar.PALI, pom = pomiar)
- then
- System.out.println( "PALI SIĘ\nProszę zadzwonić na numer 112."+ pom.getTemperatura() );
- end
- rule "NIEPALI"
- when
- p : Pomiar( getDym() == Pomiar.NIEPALI, pom = pomiar)
- then
- System.out.println( "NIE PALI SIĘ, CZEKAMY NA POZAR ELO" + pom.getTemperatura());
- end
- ///////////////////////////////////////////////////////////////////////
- package com.sample;
- import org.drools.KnowledgeBase;
- import org.drools.KnowledgeBaseFactory;
- import org.drools.builder.KnowledgeBuilder;
- import org.drools.builder.KnowledgeBuilderError;
- import org.drools.builder.KnowledgeBuilderErrors;
- import org.drools.builder.KnowledgeBuilderFactory;
- import org.drools.builder.ResourceType;
- import org.drools.io.ResourceFactory;
- import org.kie.api.KieServices;
- import org.kie.api.logger.KieRuntimeLogger;
- import org.kie.api.runtime.KieContainer;
- import org.kie.api.runtime.KieSession;
- /**
- * This is a sample class to launch a rule.
- */
- public class DroolsTest {
- static KieSession kSession;
- static KieServices ks;
- static KieContainer kContainer;
- static KieRuntimeLogger kLogger;
- private static void init(){
- try{
- ks = KieServices.Factory.get();
- kContainer = ks.getKieClasspathContainer();
- kSession = kContainer.newKieSession("ksession-rules");
- kLogger = ks.getLoggers().newFileLogger(kSession, "test");
- }catch(Throwable t){
- t.printStackTrace();
- }
- }
- private static void insert(Object o){
- kSession.insert(o);
- }
- private static void loadNGo(){
- kSession.fireAllRules();
- kLogger.close();
- }
- public static final void main(String[] args) {
- init();
- insert(new Pomiar(151));
- insert(new Pomiar(150));
- loadNGo();
- }
- private static KnowledgeBase readKnowledgeBase() throws Exception {
- // Utwórz obiekt KnowledgeBuilder, który potrafi wczytywać reguły z różnych źródeł
- KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
- // Dodaj reguły zawarte w Sample.drl w zasobach programu, zapisane w formacie DRL
- kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
- // Wyszukaj błędy
- KnowledgeBuilderErrors errors = kbuilder.getErrors();
- // Jeżeli są błędy wyświetl je i rzuć wyjątek
- if (errors.size() > 0) {
- for (KnowledgeBuilderError error: errors) {
- System.err.println(error);
- }
- throw new IllegalArgumentException("Could not parse knowledge.");
- }
- // Stwórz pustą bazę wiedzy
- KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
- // Dodaj do niej wczytane reguły
- kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
- return kbase;
- }
- public static class Pomiar {
- public static final int PALI = 0;
- public static final int NIEPALI = 1;
- private int dym;
- private int temperatura;
- public Pomiar(int temperatura){
- this.temperatura = temperatura;
- this.dym = (temperatura > 150)?PALI:NIEPALI;
- }
- public int getTemperatura(){
- return this.temperatura;
- }
- public void setTemperatura(int temperatura){
- this.temperatura = temperatura;
- }
- public int getDym() {
- return this.dym;
- }
- public void setDym(int dym) {
- this.dym = dym;
- }
- }
- public static class Message {
- public static final int HELLO = 0;
- public static final int GOODBYE = 1;
- public static final int SI = 2;
- private String message;
- private int status;
- public String getMessage() {
- return this.message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public int getStatus() {
- return this.status;
- }
- public void setStatus(int status) {
- this.status = status;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement