public int compare(Object o1, Object o2) { if (!(o1 instanceof BeliefUpdateTrigger) || !(o2 instanceof BeliefUpdateTrigger)) throw new ClassCastException("Cannot compare objects. Not BeliefUpdateTriggers."); BeliefUpdateTrigger t1 = (BeliefUpdateTrigger) o1; BeliefUpdateTrigger t2 = (BeliefUpdateTrigger) o2; if (t1.getTriggerTimestamp() < t2.getTriggerTimestamp()) return -1; if (t1.getTriggerTimestamp() > t2.getTriggerTimestamp()) return 1; // If the timestamps are the same, then we define an action to // preceed a diagnosis. // if ((o1 instanceof BelievabilityAction) && (o2 instanceof DiagnosisTrigger)) return -1; // Equality case is zero return value. // return 0; } // method compare
/** * Add a trigger to this current history. * * @param trigger The new trigger to be added. * @return False if the trigger is not added. This happens if the timestamp of the trigger * precedes the last computed belief state timestamp. */ boolean add(BeliefUpdateTrigger trigger) { // Method implementation comments go here ... // I am pretty sure having an inequality here is better. I // originally had "<=", which caused some problems for // simultaneous actions/diagnosis. Anyway, if you are // debugging a problem and happen to look here, this might be // useful information for you. // if ((_last_computed_belief != null) && (trigger.getTriggerTimestamp() < _last_computed_belief.getTimestamp())) { if (_logger.isDetailEnabled()) _logger.detail("Found late arriving trigger."); if (!shouldAddLateArrivingTrigger(trigger)) { return false; } if (_logger.isDetailEnabled()) _logger.detail("Decided to add late arriving trigger."); } // if late arriving trigger // This will simply append it, though we will sort it by // timestamp later. // _current_triggers.add(trigger); // BelievabilityDiagnosis objects require some special // handling as far as ensuring that our data structures stay // current. // if (trigger instanceof BelievabilityDiagnosis) { BelievabilityDiagnosis diag = (BelievabilityDiagnosis) trigger; // Must track the last explicit diagnosis for sensors in // case we find that we need to add implicit diagnoses for // them. // _last_explicit_diagnosis.put(diag.getSensorName(), trigger); // An explict diagnosis should clear out the last implict // diagosis if there was any. // _last_implicit_diagnosis_time.remove(diag.getSensorName()); // Also need to track all the sensor names, so that we can // handle the case of having received diagnoses from all // sensors before the sensor latency window alarm expires. // _current_sensors.add(diag.getSensorName()); } // If this is a diagnosis trigger return true; } // method add
/** * Looks in the current set of triggers for the trigger with the latest (most recent) trigger * time. Assumes that the trigger collection has been sorted and are currently stored in ascending * trigger timestamp sorted order. Latest time will be the last one in the list. */ long getLatestCurrentTriggerTime() { // Method implementation comments go here ... BeliefUpdateTrigger last_trigger; try { last_trigger = (BeliefUpdateTrigger) _current_triggers.get(_current_triggers.size() - 1); } catch (IndexOutOfBoundsException ioobe) { if (_logger.isInfoEnabled()) _logger.info("Cannot get latest trigger time from empty list."); return System.currentTimeMillis(); } return last_trigger.getTriggerTimestamp(); } // method getLatestCurrentTriggerTime