1
I’m using Drools 6 and I’m having a problem getting him to use the mode stream. I tried a few things and they didn’t work out. Here’s the code as it is, it works so far, it inserts the facts, but it doesn’t execute the drls, I don’t know why.
final KieServices ks = KieServices.Factory.get();
KieFileSystem kFileSystem = ks.newKieFileSystem();
//FileInputStream fis = new FileInputStream("src/main/resources/rules/Sample.drl");
KieBuilder kbuilder = ks.newKieBuilder( kFileSystem );
kbuilder.buildAll();
if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RuntimeException("Build time Errors: " + kbuilder.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
KieBaseConfiguration config = ks.newKieBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieModuleModel kmodule = ks.newKieModuleModel();
KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase")
.setDefault(true)
.setEventProcessingMode(EventProcessingOption.STREAM);
baseModel.newKieSessionModel("defaultKSession")
.setDefault(true)
.setClockType(ClockTypeOption.get("pseudo"));
KieBase kieBase = kContainer.newKieBase( config );
KieSession kSession = kContainer.newKieSession();
//go
Sensor sensorUm = new Sensor("SensorUm");
Sensor sensorDois = new Sensor("SensorDois");
Sensor sensorTres = new Sensor("SensorTres");
FactHandle fsensorUm = kSession.insert( sensorUm );
FactHandle fsensorDois = kSession.insert( sensorDois );
FactHandle fsensorTres = kSession.insert( sensorTres );
//test for see the facts is ok
System.out.println( "Number facts : "+kSession.getFactCount() );
Random gerador = new Random();
for(int i=0;i<20;i++){
sensorUm.estadoInterno = gerador.nextInt(100);
sensorDois.estadoInterno = gerador.nextInt(100);
sensorTres.estadoInterno = gerador.nextInt(100);
sensorUm.temperatura= gerador.nextInt(100);;
System.out.println( sensorUm.estadoInterno +" "+sensorDois.estadoInterno+" "+sensorTres.estadoInterno);
kSession.update(fsensorUm, sensorUm);
kSession.update(fsensorDois, sensorDois);
kSession.update(fsensorTres, sensorTres);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
kSession.fireAllRules();
//clock.advanceTime(4, TimeUnit.DAYS);
}
kSession.dispose();
} catch (Throwable t) {
t.printStackTrace();
}
}
And here are my rules:
rule "Sensor 1 e sensor 2 e sensor 3"
salience 10
when
Sensor( estadoInterno>20, nome=="SensorUm" )
Sensor( estadoInterno>20, nome=="SensorDois" )
Sensor( estadoInterno>20, nome=="SensorTres" )
then
System.out.println( "3 sensores > 20" );
end
rule "Sound the alarm in case temperature rises above threshold"
when
Number( doubleValue > 50 ) from accumulate(
Sensor( $temp : temperatura ) over window:time( 10m ),
average( $temp ) )
then
System.out.println( "explode" );
end
I do not understand why he does not call the rules drls, being that he puts the facts.