⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classifier.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		      
		      GraphEvent ge = new GraphEvent(Classifier.this, 
						     grphString, 
						     grphTitle);
		      notifyGraphListeners(ge);
		    }

		    if (m_textListeners.size() > 0) {
		      String modelString = m_Classifier.toString();
		      String titleString = m_Classifier.getClass().getName();
		      titleString = titleString.
			substring(titleString.lastIndexOf('.') + 1,
				  titleString.length());
		      titleString = "Set "+e.getSetNumber() + " ("
			+ m_trainingSet.relationName() + ") " + titleString
			+ " model";
		      TextEvent nt = new TextEvent(Classifier.this,
						   modelString,
						   titleString);
		      notifyTextListeners(nt);
		    }
		  }
		} catch (Exception ex) {
		  ex.printStackTrace();
		} finally {
		  m_visual.setText(oldText);
		  m_visual.setStatic();
		  m_state = IDLE;
		  if (isInterrupted()) {
		    // prevent any classifier events from being fired
		    m_trainingSet = null;
		    if (m_log != null) {
		      m_log.logMessage("Build classifier interrupted!");
		      m_log.statusMessage("OK");
		    }
		  } else {
		    // save header
		    m_trainingSet = new Instances(m_trainingSet, 0);
		  }
		  if (m_log != null) {
		    m_log.statusMessage("OK");
		  }
		  block(false);
		}
	      }	
	    };
	  m_buildThread.setPriority(Thread.MIN_PRIORITY);
	  m_buildThread.start();
	  // make sure the thread is still running before we block
	  //	  if (m_buildThread.isAlive()) {
	  block(true);
	    //	  }
	  m_buildThread = null;
	  m_state = IDLE;
	}
      } catch (Exception ex) {
	ex.printStackTrace();
      }
    }
  }

  /**
   * Accepts a test set for a batch trained classifier
   *
   * @param e a <code>TestSetEvent</code> value
   */
  public void acceptTestSet(TestSetEvent e) {

    if (m_trainingSet != null) {
      try {
	if (m_state == IDLE) {
	  synchronized(this) {
	    m_state = CLASSIFYING;
	  }

	  m_testingSet = e.getTestSet();
	  if (m_testingSet != null) {
	    if (m_testingSet.classIndex() < 0) {
	      m_testingSet.setClassIndex(m_testingSet.numAttributes()-1);
	    }
	  }
	  if (m_trainingSet.equalHeaders(m_testingSet)) {
	    BatchClassifierEvent ce = 
	      new BatchClassifierEvent(this, m_Classifier, 
				       new DataSetEvent(this, e.getTestSet()),
				  e.getSetNumber(), e.getMaxSetNumber());

	    //	    System.err.println("Just before notify classifier listeners");
	    notifyBatchClassifierListeners(ce);
	    //	    System.err.println("Just after notify classifier listeners");
	  }
	  m_state = IDLE;
	}
      } catch (Exception ex) {
	ex.printStackTrace();
      }
    }
  }


  private void buildClassifier() throws Exception {
    m_Classifier.buildClassifier(m_trainingSet);
  }

  /**
   * Sets the visual appearance of this wrapper bean
   *
   * @param newVisual a <code>BeanVisual</code> value
   */
  public void setVisual(BeanVisual newVisual) {
    m_visual = newVisual;
  }

  /**
   * Gets the visual appearance of this wrapper bean
   */
  public BeanVisual getVisual() {
    return m_visual;
  }

  /**
   * Use the default visual appearance for this bean
   */
  public void useDefaultVisual() {
    m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultClassifier.gif",
		       BeanVisual.ICON_PATH+"DefaultClassifier_animated.gif");
  }

  /**
   * Add a batch classifier listener
   *
   * @param cl a <code>BatchClassifierListener</code> value
   */
  public synchronized void 
    addBatchClassifierListener(BatchClassifierListener cl) {
    m_batchClassifierListeners.addElement(cl);
  }

  /**
   * Remove a batch classifier listener
   *
   * @param cl a <code>BatchClassifierListener</code> value
   */
  public synchronized void 
    removeBatchClassifierListener(BatchClassifierListener cl) {
    m_batchClassifierListeners.remove(cl);
  }

  /**
   * Notify all batch classifier listeners of a batch classifier event
   *
   * @param ce a <code>BatchClassifierEvent</code> value
   */
  private void notifyBatchClassifierListeners(BatchClassifierEvent ce) {
    Vector l;
    synchronized (this) {
      l = (Vector)m_batchClassifierListeners.clone();
    }
    if (l.size() > 0) {
      for(int i = 0; i < l.size(); i++) {
	((BatchClassifierListener)l.elementAt(i)).acceptClassifier(ce);
      }
    }
  }

  /**
   * Add a graph listener
   *
   * @param cl a <code>GraphListener</code> value
   */
  public synchronized void addGraphListener(GraphListener cl) {
    m_graphListeners.addElement(cl);
  }

  /**
   * Remove a graph listener
   *
   * @param cl a <code>GraphListener</code> value
   */
  public synchronized void removeGraphListener(GraphListener cl) {
    m_graphListeners.remove(cl);
  }

  /**
   * Notify all graph listeners of a graph event
   *
   * @param ge a <code>GraphEvent</code> value
   */
  private void notifyGraphListeners(GraphEvent ge) {
    Vector l;
    synchronized (this) {
      l = (Vector)m_graphListeners.clone();
    }
    if (l.size() > 0) {
      for(int i = 0; i < l.size(); i++) {
	((GraphListener)l.elementAt(i)).acceptGraph(ge);
      }
    }
  }

  /**
   * Add a text listener
   *
   * @param cl a <code>TextListener</code> value
   */
  public synchronized void addTextListener(TextListener cl) {
    m_textListeners.addElement(cl);
  }

  /**
   * Remove a text listener
   *
   * @param cl a <code>TextListener</code> value
   */
  public synchronized void removeTextListener(TextListener cl) {
    m_textListeners.remove(cl);
  }

  /**
   * Notify all text listeners of a text event
   *
   * @param ge a <code>TextEvent</code> value
   */
  private void notifyTextListeners(TextEvent ge) {
    Vector l;
    synchronized (this) {
      l = (Vector)m_textListeners.clone();
    }
    if (l.size() > 0) {
      for(int i = 0; i < l.size(); i++) {
	((TextListener)l.elementAt(i)).acceptText(ge);
      }
    }
  }

  /**
   * Add an incremental classifier listener
   *
   * @param cl an <code>IncrementalClassifierListener</code> value
   */
  public synchronized void 
    addIncrementalClassifierListener(IncrementalClassifierListener cl) {
    m_incrementalClassifierListeners.add(cl);
  }

  /**
   * Remove an incremental classifier listener
   *
   * @param cl an <code>IncrementalClassifierListener</code> value
   */
  public synchronized void 
    removeIncrementalClassifierListener(IncrementalClassifierListener cl) {
    m_incrementalClassifierListeners.remove(cl);
  }
  
  /**
   * Notify all incremental classifier listeners of an incremental classifier
   * event
   *
   * @param ce an <code>IncrementalClassifierEvent</code> value
   */
  private void 
    notifyIncrementalClassifierListeners(IncrementalClassifierEvent ce) {
    Vector l;
    synchronized (this) {
      l = (Vector)m_incrementalClassifierListeners.clone();
    }
    if (l.size() > 0) {
      for(int i = 0; i < l.size(); i++) {
	((IncrementalClassifierListener)l.elementAt(i)).acceptClassifier(ce);
      }
    }
  }

  /**
   * Returns true if, at this time, 
   * the object will accept a connection with respect to the named event
   *
   * @param eventName the event
   * @return true if the object will accept a connection
   */
  public boolean connectionAllowed(String eventName) {
    /*    if (eventName.compareTo("instance") == 0) {
      if (!(m_Classifier instanceof weka.classifiers.UpdateableClassifier)) {
	return false;
      }
      } */
    if (m_listenees.containsKey(eventName)) {
      return false;
    }
    return true;
  }

  /**
   * Notify this object that it has been registered as a listener with
   * a source with respect to the named event
   *
   * @param eventName the event
   * @param source the source with which this object has been registered as
   * a listener
   */
  public synchronized void connectionNotification(String eventName,
						  Object source) {
    if (eventName.compareTo("instance") == 0) {
      if (!(m_Classifier instanceof weka.classifiers.UpdateableClassifier)) {
	if (m_log != null) {
	  m_log.logMessage("Warning : " + m_Classifier.getClass().getName()
			   + " is not an updateable classifier. This "
			   +"classifier will only be evaluated on incoming "
			   +"instance events and not trained on them.");
	}
      }
    }

    if (connectionAllowed(eventName)) {
      m_listenees.put(eventName, source);
      /*      if (eventName.compareTo("instance") == 0) {
	startIncrementalHandler();
	} */
    }
  }

  /**
   * Notify this object that it has been deregistered as a listener with
   * a source with respect to the supplied event name
   *
   * @param eventName the event
   * @param source the source with which this object has been registered as
   * a listener
   */
  public synchronized void disconnectionNotification(String eventName,
						     Object source) {
    m_listenees.remove(eventName);
    if (eventName.compareTo("instance") == 0) {
      stop(); // kill the incremental handler thread if it is running
    }
  }

  /**
   * Function used to stop code that calls acceptTrainingSet. This is 
   * needed as classifier construction is performed inside a separate
   * thread of execution.
   *
   * @param tf a <code>boolean</code> value
   */
  private synchronized void block(boolean tf) {

    if (tf) {
      try {
	  // only block if thread is still doing something useful!
	if (m_buildThread.isAlive() && m_state != IDLE) {
	  wait();
	  }
      } catch (InterruptedException ex) {
      }
    } else {
      notifyAll();
    }
  }


  /**
   * Stop any classifier action
   */
  public void stop() {
    // tell all listenees (upstream beans) to stop
    Enumeration en = m_listenees.keys();
    while (en.hasMoreElements()) {
      Object tempO = m_listenees.get(en.nextElement());
      if (tempO instanceof BeanCommon) {
	System.err.println("Listener is BeanCommon");
	((BeanCommon)tempO).stop();
      }
    }
    
    // stop the build thread
    if (m_buildThread != null) {
      m_buildThread.interrupt();
      m_buildThread.stop();
      m_buildThread = null;
      m_visual.setStatic();
    }
  }

  /**
   * Set a logger
   *
   * @param logger a <code>Logger</code> value
   */
  public void setLog(Logger logger) {
    m_log = logger;
  }

  /**
   * Return an enumeration of requests that can be made by the user
   *
   * @return an <code>Enumeration</code> value
   */
  public Enumeration emerateRequests() {
    Vector newVector = new Vector(0);
    if (m_buildThread != null) {
      newVector.addElement("Stop");
    }
    return newVector.elements();
  }

  /**
   * Perform a particular request
   *
   * @param request the request to perform
   * @exception IllegalArgumentException if an error occurs
   */
  public void performRequest(String request) {
    if (request.compareTo("Stop") == 0) {
      stop();
    } else {
      throw new IllegalArgumentException(request
					 + " not supported (Classifier)");
    }
  }

  /**
   * Returns true, if at the current time, the event described by the
   * supplied event descriptor could be generated.
   *
   * @param esd an <code>EventSetDescriptor</code> value
   * @return a <code>boolean</code> value
   */
  public boolean eventGeneratable(EventSetDescriptor esd) {
    String eventName = esd.getName();
    return eventGeneratable(eventName);
  }

  /**
   * Returns true, if at the current time, the named event could
   * be generated. Assumes that the supplied event name is
   * an event that could be generated by this bean
   *
   * @param eventName the name of the event in question
   * @return true if the named event could be generated at this point in
   * time
   */
  public boolean eventGeneratable(String eventName) {
    if (eventName.compareTo("graph") == 0) {
      // can't generate a GraphEvent if classifier is not drawable
      if (!(m_Classifier instanceof weka.core.Drawable)) {
	return false;
      }
      // need to have a training set before the classifier
      // can generate a graph!
      if (!m_listenees.containsKey("trainingSet")) {
	return false;
      }
    }

    if (eventName.compareTo("batchClassifier") == 0) {
      if (!m_listenees.containsKey("testSet") || 
	  !m_listenees.containsKey("trainingSet")) {
	return false;
      }
      Object source = m_listenees.get("testSet");
      if (source instanceof EventConstraints) {
	if (!((EventConstraints)source).eventGeneratable("testSet")) {
	  return false;
	}
      }
      source = m_listenees.get("trainingSet");
      if (source instanceof EventConstraints) {
	if (!((EventConstraints)source).eventGeneratable("trainingSet")) {
	  return false;
	}
      }
    }

    if (eventName.compareTo("text") == 0) {
      if (!m_listenees.containsKey("trainingSet") &&
	  !m_listenees.containsKey("instance")) {
	return false;
      }
      Object source = m_listenees.get("trainingSet");
      if (source != null && source instanceof EventConstraints) {
	if (!((EventConstraints)source).eventGeneratable("trainingSet")) {
	  return false;
	}
      }
      source = m_listenees.get("instance");
      if (source != null && source instanceof EventConstraints) {
	if (!((EventConstraints)source).eventGeneratable("instance")) {
	  return false;
	}
      }
    }

    if (eventName.compareTo("incrementalClassifier") == 0) {
      /*      if (!(m_Classifier instanceof weka.classifiers.UpdateableClassifier)) {
	return false;
	} */
      if (!m_listenees.containsKey("instance")) {
	return false;
      }
      Object source = m_listenees.get("instance");
      if (source instanceof EventConstraints) {
	if (!((EventConstraints)source).eventGeneratable("instance")) {
	  return false;
	}
      }
    }
    return true;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -