public void acceptInstance(InstanceEvent e) { if (e.getStatus() == InstanceEvent.FORMAT_AVAILABLE) { Instances structure = e.getStructure(); m_legendText = new Vector(); m_max = 1.0; m_min = 0; int i = 0; for (i = 0; i < structure.numAttributes(); i++) { if (i > 10) { i--; break; } m_legendText.addElement(structure.attribute(i).name()); m_legendPanel.repaint(); m_scalePanel.repaint(); } m_dataPoint = new double[i]; m_xCount = 0; return; } // process data point Instance inst = e.getInstance(); for (int i = 0; i < m_dataPoint.length; i++) { if (!inst.isMissing(i)) { m_dataPoint[i] = inst.value(i); } } acceptDataPoint(m_dataPoint); m_xCount++; }
@Override public void instanceProduced(InstanceEvent e) { Object source = e.getSource(); if (source instanceof InstanceProducer) { try { InstanceProducer a = (InstanceProducer) source; switch (e.getID()) { case InstanceEvent.FORMAT_AVAILABLE: inputFormat(a.outputFormat()); break; case InstanceEvent.INSTANCE_AVAILABLE: input(a.outputPeek()); break; case InstanceEvent.BATCH_FINISHED: if (m_Debug) { System.err.println("InstanceCounter::instanceProduced() - End of instance batch"); } break; default: System.err.println("InstanceCounter::instanceProduced() - unknown event type"); break; } } catch (Exception ex) { System.err.println(ex.getMessage()); } } else { System.err.println("InstanceCounter::instanceProduced() - Unknown source object type"); } }
public void notifyListeners(InstanceEvent event) { if (event.getType() == InstanceEventType.INSTANCE_ABOUT_TO_TRANSITION) { fireInstanceAboutToTransition(event); } else { eventsToFire.add(event); synchronized (eventsToFire) { eventsToFire.notify(); } } }
public void run() { while (InstanceEventManager.this.isListenerAttached()) { if (eventsToFire != null) { if (eventsToFire.isEmpty()) { synchronized (eventsToFire) { try { eventsToFire.wait(); } catch (InterruptedException e) { } } } if (!eventsToFire.isEmpty()) { InstanceEvent[] array = eventsToFire.toArray(new InstanceEvent[eventsToFire.size()]); InstanceEvent e = array[0]; switch (e.getType()) { case INSTANCE_CREATED: fireInstanceCreated(e); break; case INSTANCE_DELETED: fireInstanceDeleted(e); break; case INSTANCE_LOADED: fireInstanceLoaded(e); break; case INSTANCE_UNLOADED: fireInstanceUnloaded(e); break; case INSTANCE_UPDATED: fireInstanceUpdated(e); break; case INSTANCE_TRANSITIONED: fireInstanceTransitioned(e); break; } eventsToFire.remove(e); } } } }
@Override public void acceptInstance(InstanceEvent e) { m_busy = true; if (e.getStatus() == InstanceEvent.FORMAT_AVAILABLE) { Instances structure = e.getStructure(); init(structure); if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "Processing stream..."); } // notify listeners of structure m_ie.setStructure(structure); if (m_indexOfTrueStep >= 0) { ((InstanceListener) m_downstream[m_indexOfTrueStep]).acceptInstance(m_ie); } if (m_indexOfFalseStep >= 0) { ((InstanceListener) m_downstream[m_indexOfFalseStep]).acceptInstance(m_ie); } } else { Instance inst = e.getInstance(); m_ie.setStatus(e.getStatus()); if (inst == null || e.getStatus() == InstanceEvent.BATCH_FINISHED) { if (inst != null) { // evaluate and notify boolean result = true; if (m_root != null) { result = m_root.evaluate(inst, true); } if (result) { if (m_indexOfTrueStep >= 0) { m_ie.setInstance(inst); ((InstanceListener) m_downstream[m_indexOfTrueStep]).acceptInstance(m_ie); } if (m_indexOfFalseStep >= 0) { m_ie.setInstance(null); ((InstanceListener) m_downstream[m_indexOfFalseStep]).acceptInstance(m_ie); } } else { if (m_indexOfFalseStep >= 0) { m_ie.setInstance(inst); ((InstanceListener) m_downstream[m_indexOfFalseStep]).acceptInstance(m_ie); } if (m_indexOfTrueStep >= 0) { m_ie.setInstance(null); ((InstanceListener) m_downstream[m_indexOfTrueStep]).acceptInstance(m_ie); } } } else { // notify both of end of stream m_ie.setInstance(null); if (m_indexOfTrueStep >= 0) { ((InstanceListener) m_downstream[m_indexOfTrueStep]).acceptInstance(m_ie); } if (m_indexOfFalseStep >= 0) { ((InstanceListener) m_downstream[m_indexOfFalseStep]).acceptInstance(m_ie); } } if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "Finished"); } } else { boolean result = true; if (m_root != null) { result = m_root.evaluate(inst, true); } m_ie.setInstance(inst); if (result) { if (m_indexOfTrueStep >= 0) { ((InstanceListener) m_downstream[m_indexOfTrueStep]).acceptInstance(m_ie); } } else { if (m_indexOfFalseStep >= 0) { ((InstanceListener) m_downstream[m_indexOfFalseStep]).acceptInstance(m_ie); } } } } m_busy = false; }
/** * Accept an instance for processing by StreamableFilters only * * @param e an <code>InstanceEvent</code> value */ public void acceptInstance(InstanceEvent e) { // to do! if (m_filterThread != null) { String messg = Messages.getInstance().getString("Filter_AcceptInstance_Mess_Text_First") + statusMessagePrefix() + Messages.getInstance().getString("Filter_AcceptInstance_Mess_Text_Second"); if (m_log != null) { m_log.logMessage(messg); m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_First")); } else { System.err.println(messg); } return; } if (!(m_Filter instanceof StreamableFilter)) { stop(); // stop all processing if (m_log != null) { m_log.logMessage( Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_First") + statusMessagePrefix() + Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_Second") + m_Filter.getClass().getName() + Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_Third")); m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Second")); } return; } if (e.getStatus() == InstanceEvent.FORMAT_AVAILABLE) { try { m_instanceCount = 0; // notifyInstanceListeners(e); // Instances dataset = e.getInstance().dataset(); Instances dataset = e.getStructure(); if (m_Filter instanceof SupervisedFilter) { // defualt to last column if no class is set if (dataset.classIndex() < 0) { dataset.setClassIndex(dataset.numAttributes() - 1); } } // initialize filter m_Filter.setInputFormat(dataset); // attempt to determine post-filtering // structure. If successful this can be passed on to instance // listeners as a new FORMAT_AVAILABLE event. m_structurePassedOn = false; try { if (m_Filter.isOutputFormatDefined()) { // System.err.println("Filter - passing on output format..."); // System.err.println(m_Filter.getOutputFormat()); m_ie.setStructure(m_Filter.getOutputFormat()); notifyInstanceListeners(m_ie); m_structurePassedOn = true; } } catch (Exception ex) { stop(); // stop all processing if (m_log != null) { m_log.logMessage( Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_Fourth") + statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_LogMessage_Text_Fifth") + ex.getMessage()); m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Third")); } else { System.err.println( Messages.getInstance().getString("Filter_AcceptInstance_Error_Text_First") + statusMessagePrefix() + Messages.getInstance().getString("Filter_AcceptInstance_Error_Text_Second")); } } } catch (Exception ex) { ex.printStackTrace(); } return; } if (e.getStatus() == InstanceEvent.BATCH_FINISHED) { // get the last instance (if available) try { if (m_log != null) { m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Fourth")); } if (m_Filter.input(e.getInstance())) { Instance filteredInstance = m_Filter.output(); if (filteredInstance != null) { if (!m_structurePassedOn) { // pass on the new structure first m_ie.setStructure(new Instances(filteredInstance.dataset(), 0)); notifyInstanceListeners(m_ie); m_structurePassedOn = true; } m_ie.setInstance(filteredInstance); // if there are instances pending for output don't want to send // a batch finisehd at this point... // System.err.println("Filter - in batch finisehd..."); if (m_Filter.batchFinished() && m_Filter.numPendingOutput() > 0) { m_ie.setStatus(InstanceEvent.INSTANCE_AVAILABLE); } else { m_ie.setStatus(e.getStatus()); } notifyInstanceListeners(m_ie); } } if (m_log != null) { m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Fourth_Alpha")); } } catch (Exception ex) { stop(); // stop all processing if (m_log != null) { m_log.logMessage( Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_Sixth") + statusMessagePrefix() + ex.getMessage()); m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Fifth")); } ex.printStackTrace(); } // check for any pending instances that we might need to pass on try { if (m_Filter.batchFinished() && m_Filter.numPendingOutput() > 0) { if (m_log != null) { m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Sixth")); } Instance filteredInstance = m_Filter.output(); if (filteredInstance != null) { if (!m_structurePassedOn) { // pass on the new structure first m_ie.setStructure(new Instances(filteredInstance.dataset(), 0)); notifyInstanceListeners(m_ie); m_structurePassedOn = true; } m_ie.setInstance(filteredInstance); // TODO here is the problem I think m_ie.setStatus(InstanceEvent.INSTANCE_AVAILABLE); notifyInstanceListeners(m_ie); } while (m_Filter.numPendingOutput() > 0) { filteredInstance = m_Filter.output(); m_ie.setInstance(filteredInstance); // System.err.println("Filter - sending pending..."); if (m_Filter.numPendingOutput() == 0) { m_ie.setStatus(InstanceEvent.BATCH_FINISHED); } else { m_ie.setStatus(InstanceEvent.INSTANCE_AVAILABLE); } notifyInstanceListeners(m_ie); } if (m_log != null) { m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Seventh")); } } } catch (Exception ex) { stop(); // stop all processing if (m_log != null) { m_log.logMessage( Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_Seventh") + statusMessagePrefix() + ex.toString()); m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Eighth")); } ex.printStackTrace(); } } else { // pass instance through the filter try { if (!m_Filter.input(e.getInstance())) { // System.err.println("Filter - inputing instance into filter..."); /* if (m_log != null) { m_log.logMessage("ERROR : filter not ready to output instance"); } */ // quietly return. Filter might be able to output some instances // once the batch is finished. return; } // collect output instance. Instance filteredInstance = m_Filter.output(); if (filteredInstance == null) { return; } m_instanceCount++; if (!m_structurePassedOn) { // pass on the new structure first m_ie.setStructure(new Instances(filteredInstance.dataset(), 0)); notifyInstanceListeners(m_ie); m_structurePassedOn = true; } m_ie.setInstance(filteredInstance); m_ie.setStatus(e.getStatus()); if (m_log != null && (m_instanceCount % 10000 == 0)) { m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Nineth") + m_instanceCount + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Tenth")); } notifyInstanceListeners(m_ie); } catch (Exception ex) { stop(); // stop all processing if (m_log != null) { m_log.logMessage( Messages.getInstance().getString("Filter_AcceptInstance_LogMessage_Text_Eighth") + statusMessagePrefix() + ex.toString()); m_log.statusMessage( statusMessagePrefix() + Messages.getInstance() .getString("Filter_AcceptInstance_StatusMessage_Text_Eleventh")); } ex.printStackTrace(); } } }
/** * Accept and process an instance event * * @param e an <code>InstanceEvent</code> value */ @Override public void acceptInstance(InstanceEvent e) { if (e.getStatus() == InstanceEvent.FORMAT_AVAILABLE) { m_connectedFormat = e.getStructure(); m_stopRequested.set(false); try { init(new Instances(e.getStructure(), 0)); } catch (IllegalArgumentException ex) { if (m_log != null) { String message = "ERROR: There is a problem with the incoming instance structure"; // m_log.statusMessage(statusMessagePrefix() + message // + " - see log for details"); // m_log.logMessage(statusMessagePrefix() + message + " :" // + ex.getMessage()); stopWithErrorMessage(message, ex); // m_busy = false; return; } } String buffSize = m_bufferSize; try { buffSize = m_env.substitute(buffSize); m_bufferSizeI = Integer.parseInt(buffSize); } catch (Exception ex) { ex.printStackTrace(); } m_incrementalBuffer = new ArrayList<InstanceHolder>(m_bufferSizeI); m_bufferFiles = new ArrayList<File>(); m_streamCounter = 0; return; } m_busy = true; if (e.getInstance() != null) { if (m_streamCounter == 0) { if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "Starting streaming sort..."); m_log.logMessage( "[Sorter] " + statusMessagePrefix() + " Using streaming buffer size: " + m_bufferSizeI); } } InstanceHolder tempH = new InstanceHolder(); tempH.m_instance = e.getInstance(); tempH.m_fileNumber = -1; // unused here if (m_stringAttIndexes != null) { copyStringAttVals(tempH); } m_incrementalBuffer.add(tempH); m_streamCounter++; } if (e.getInstance() == null || e.getStatus() == InstanceEvent.BATCH_FINISHED) { emitBufferedInstances(); // thread will set busy to false and report done status when // complete return; } else if (m_incrementalBuffer.size() == m_bufferSizeI) { // time to sort and write this to a temp file try { sortBuffer(true); } catch (Exception ex) { String msg = statusMessagePrefix() + "ERROR: unable to write to temp file."; // if (m_log != null) { // m_log.statusMessage(msg); // m_log.logMessage("[" + getCustomName() + "] " + msg); // } stopWithErrorMessage(msg, ex); // ex.printStackTrace(); m_busy = false; return; } } m_busy = false; }