I'm working on a Spring Boot application where I created some REST APIs. There is a Readings entity, Alert entity. I'm sending readings via POST method and in the postReadings
method of the ReadingsService
class I'm checking whether the readings match certain criteria using KIE
and wrote the rules in a rules.drl
file. I'm creating an Alert object and setting it as a global using session.setGlobal
method. Then I'm checking if the Alert object is null and saving it to the Alert Repository. In the drools file I added a print statement to check whether all the alerts are correctly created. All the alerts are correctly printed via the print statement, however, only some of them are being saved to the alert repository. Can anyone please help?
This is the ReadingsService
@Service
public class ReadingsServiceImpl implements ReadingsService{
@Autowired
ReadingsRepository repository;
@Autowired
VehicleRepository vehicleRepo;
@Autowired
private KieSession session;
@Autowired
AlertRepository alertRepo;
@Override
public List<Readings> findAll() {
return repository.findAll();
}
@Override
public Readings postReadings(Readings readings) {
Alert alert = new Alert();
session.setGlobal("alert", alert);
session.insert(readings);
session.fireAllRules();
if(!Optional.ofNullable(alert).map(o -> o.getVin()).orElse("").isBlank()) {
alertRepo.save(alert);
}
return repository.save(readings);
}
This is the drools file
import com.shiva.truckerapi.entity.Readings;
global com.shiva.truckerapi.entity.Alert alert;
rule "EngineCoolantOrCheckEngineLight"
when
readingsObject: Readings(engineCoolantLow || checkEngineLightOn);
then
alert.setVin(readingsObject.getVin());
alert.setPriotity("LOW");
alert.setDescription("Engine Coolant LOW Or Check Engine Light ON");
alert.setTimestamp(readingsObject.getTimestamp());
alert.setLatitude(readingsObject.getLatitude());
alert.setLongitude(readingsObject.getLongitude());
System.out.println(alert.toString());
end;