package org.drools.sensors.alarms import org.sensors.configuration.trailerinfo.domain.SensorDataIdem; import org.mule.MessageExchangePattern; import org.sensors.rules.util.AlarmVehicleRegistry; import org.sensors.rules.util.AlarmVehicle; import org.sensors.alarms.persistence.Alarm; import org.sensors.rules.util.AlarmConditionUtil; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; import java.util.Iterator; import org.sensors.rules.util.AlarmConstants; dialect "java" global org.mule.module.bpm.MessageService mule; # tells the engine that a SensorDataIdem instance will assume the # role (semantics) of events and that the default retention # policy will be 2 minute declare SensorDataIdem @role( event ) @expires( 2m ) end # check data from the current sensor data against values set in the alarm rule "Update Sensor Data to Alarm Vehicle facts" lock-on-active when // Set a references for later on the initial facts $av : AlarmVehicle($vid : vehicleId) // "Sensor Data Idem Stream" is where Mule sends the Sensordata facts // for each Alarm / Sensordata combination that is identified via the vehicle id $sd : SensorDataIdem (vehicleId == $vid && sensorAvailable == true && (valueVariable!=null || valueDiscrete!=null) ) from entry-point "Sensor Data Idem Stream" //The Condition to be added to get the active Alarms from the List of Alarms $activeAlarms : ArrayList() from collect ( Alarm( (activation == AlarmConstants.ALWAYS_ACTIVE) || (activation == AlarmConstants.ACTIVE_ONLY_ONTOUR && $sd.onTour == true) || (activation == AlarmConstants.ACTIVE_ONLY_NOT_ONTOUR && $sd.onTour == false) || (activation == AlarmConstants.ACTIVE_ONLY_ONMOVING && $sd.onMove == true) || (activation == AlarmConstants.ACTIVE_ON_TIME_WINDOW ) ) from $av.getAlarms() ) $alarms : ArrayList() from collect ( Alarm( ( (geofenceCustomer == 0) || (geofenceCustomer == 1 && $sd.inCustomerGeoFence == true) || (geofenceCustomer == 2 && $sd.inCustomerGeoFence == false) ) && ( (geofenceDepot == 0) || (geofenceDepot == 2 && $sd.inDepotGeoFence == true) || (geofenceDepot == 3 && $sd.inDepotGeoFence == false) ) ) from $activeAlarms ) // Need to replace the below alarms array with the Alarms after satisfying the Condition. //$alarms : ArrayList() from collect ( Alarm() from $av.getAlarms() ) eval($alarms.size()>0 && $sd != null) then List sensorRelatedAlarms = new ArrayList(); int factUpdates = 0; for(int i=0;i<$alarms.size();i++) { Alarm a = (Alarm)$alarms.get(i); String evalCondBeforeReplace = null; String evalCondition = null; boolean isConditionChanged = false; System.out.println("The Alarm Condition Just Arrived for Alarm ID '"+a.getId()+"' is : '"+a.getConditionToEvaluate()+"'"); if(!a.getConditionToEvaluate().contains("s") && a.getConditionToEvaluate().indexOf('s')<0) { evalCondBeforeReplace = a.getCondition(); } else { evalCondBeforeReplace = a.getConditionToEvaluate(); } System.out.println("The Alarm Condition for Alarm ID '"+a.getId()+"' is : '"+evalCondBeforeReplace+"'"); System.out.println("The Sensor ID is '"+$sd.getSensorId()+"'"); if($sd.getValueVariable()!=null) { evalCondition = AlarmConditionUtil.replaceSensor(evalCondBeforeReplace, "s"+$sd.getSensorId(), $sd.getValueVariable().toString()); } else if($sd.getValueDiscrete()!=null) { evalCondition = AlarmConditionUtil.replaceSensor(evalCondBeforeReplace, "s"+$sd.getSensorId(), $sd.getValueDiscrete()); } if(evalCondition!=null && a.getConditionToEvaluate()!=null && !a.getConditionToEvaluate().trim().equals(evalCondition.trim()) ) { a.setConditionToEvaluate(evalCondition); factUpdates++; isConditionChanged = true; } else if(evalCondBeforeReplace!=null && a.getConditionToEvaluate()!=null && !a.getConditionToEvaluate().trim().equals(evalCondBeforeReplace.trim()) ) { a.setConditionToEvaluate(evalCondBeforeReplace); factUpdates++; isConditionChanged = true; } if(isConditionChanged) { // Replacing the Alarm in the AlarmVehicle Facts by Updating the ConditionToEvaluate Field with Values $av.getAlarms().remove(a); $av.getAlarms().add(a); } if(!a.getConditionToEvaluate().contains("s") && a.getConditionToEvaluate().indexOf('s')<0) { sensorRelatedAlarms.add(a); } } if(factUpdates>0) { update($av); factUpdates = 0; } if(sensorRelatedAlarms.size()>0) { mule.generateMessage("alertsEndPoint", sensorRelatedAlarms, null, MessageExchangePattern.ONE_WAY); } end