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

📄 operatorchain.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version. 
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.operator;

import edu.udo.cs.yale.Experiment;
import edu.udo.cs.yale.tools.LogService;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Iterator;
import java.io.IOException;
import java.io.PrintWriter;

import edu.udo.cs.yale.operator.OperatorException;

/** A chain of operators that is subsequently applied. As an OperatorChain is an Operator itself
 *  it can be arbitrarily nested.<br>
 *  As the inner operators are instantiated by the ParameterService, inheritants can
 *  access them by <code>getOperator(int)</code>. They should override 
 *  <code>getMaxNumberOfInnerOps()</code> and <code>getMinNumberInnerOps()</code> which are 
 *  used for some checks. <br>
 *
 *  <h4>Operator-Input</h4>
 *  <ol>
 *    <li>The first operator's input
 *  </ol>
 *  <h4>Operator-Output</h4>
 *  <ol>
 *    <li>The last operator's output
 *  </ol>
 *
 *  @author Simon
 *  @version $Id: OperatorChain.java,v 2.18 2004/09/14 08:39:04 ingomierswa Exp $
 */
public abstract class OperatorChain extends Operator {

    /** The inner operators. They are applied in their ordering in the list. */
    private List operators;

    private int stepCounter = 0;

    // --------------------------------------------------------------------------------

    /** Creates an empty operator chain. */
    public OperatorChain() {
	operators = new ArrayList();
    }

    /** Performs a deep clone of this operator chain. Use this method only if you are sure what you are doing. */
    public Operator cloneOperator(String name) {
	OperatorChain clone = (OperatorChain)super.cloneOperator(name);
	clone.stepCounter = stepCounter;
	clone.operators = new ArrayList();
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Operator originalChild = (Operator)i.next();
	    Operator clonedChild = originalChild.cloneOperator(originalChild.getName());
	    clonedChild.setParent(clone);
	    clone.addOperator(clonedChild);
	}
	return clone;
    }

    public void setExperiment(Experiment experiment) {
	super.setExperiment(experiment);
	for (int i = 0; i < getNumberOfOperators(); i++) {
	    getOperator(i).setExperiment(experiment);
	}
    }

    public abstract Class[] checkIO(Class[] input) throws IllegalInputException;

    /** Returns the maximum number of innner operators. */
    public abstract int getMaxNumberOfInnerOperators();
    /** Returns the minimum number of innner operators. */
    public abstract int getMinNumberOfInnerOperators();

    /** Adds a new inner operator at the last position. */
    public int addOperator(Operator o) {
	if (o == null) return -1;
	o.setParent(this);
	operators.add(o);
	if (getNumberOfOperators() == getMaxNumberOfInnerOperators()+1) {
	    LogService.logMessage("More than "+getMaxNumberOfInnerOperators()+" inner operators in "+getName()+"!", LogService.WARNING);
	}
	return getNumberOfOperators() - 1;
    }

    public void addOperator(Operator operator, int index) {
	operators.add(index, operator);
	operator.setParent(this);
	if (getNumberOfOperators() == getMaxNumberOfInnerOperators()+1) {
	    LogService.logMessage("More than "+getMaxNumberOfInnerOperators()+" inner operators in "+getName()+"!", LogService.WARNING);
	}
    }

    public void removeOperator(Operator operator) {
	operators.remove(operator);
    }

    /** Returns the i-th inner operator. */
    public Operator getOperator(int i) {
	if ((i < 0) || (i >= getNumberOfOperators())) {
	    throw new RuntimeException("Illegal operator index in getOperator() ("+getName()+"): " + i);
	}
	return (Operator)operators.get(i);
    }

    /** Returns an iterator over all Operators. */
    public Iterator getOperators() {
	return operators.iterator();
    }

    /** Returns recursively all child operators. */
    public List getAllInnerOperators() {
	List children = new LinkedList();
	for (int i = 0; i < operators.size(); i++) {
	    Operator innerOp = getOperator(i);
	    children.add(innerOp);
	    if (innerOp instanceof OperatorChain)
		children.addAll(((OperatorChain)innerOp).getAllInnerOperators());
	}
	return children;
    }

    /** Returns the number of all operators enclosed in the operator chain (only top level counted). */
    public int  getNumberOfOperators() {
	return operators.size();
    }

    public int getIndexOfOperator(Operator operator) {
	return operators.indexOf(operator);
    }

    /** Returns the inner operator named <tt>name</tt> or null if no such operator exists. */
    public Operator getInnerOperatorForName(String name) {
	if (name == null) return null;
	if (name.equals(this.getName())) return this;
	ListIterator i = operators.listIterator();
	while (i.hasNext()) {
	    Operator inner = (Operator)i.next();
	    if (name.equals(inner.getName())) return inner;
	    if (inner instanceof OperatorChain) {
		Operator innerinner = ((OperatorChain)inner).getInnerOperatorForName(name);
		if (innerinner != null) return innerinner;
	    }
	}
	return null;
    }

    public void setEnabled(boolean enabled) {
	super.setEnabled(enabled);
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Operator child = (Operator)i.next();
	    child.setEnabled(enabled);
	}
    }

    public boolean isEnabled() {
	if (getParent() == null) {
	    return super.isEnabled();
	} else {
	    return (super.isEnabled() && getParent().isEnabled());
	}
    }

    public void experimentStarts() {
	super.experimentStarts();
	for (int i = 0; i < getNumberOfOperators(); i++)
	    getOperator(i).experimentStarts();
    }

    public void experimentFinished() {
	super.experimentFinished();
	for (int i = 0; i < getNumberOfOperators(); i++)
	    getOperator(i).experimentFinished();
    }

    // -------------------- implementierte abstrakte Methoden --------------------

    /** Applies all inner operators. The input to this operator becomes the input of the first inner operator.
     *  The latter's output is passed to the second inner operator and so on.
     *  Note to subclassers: If subclasses (for example wrappers) want to make use of this method remember to call exactly
     *  this method <tt>(super.apply())</tt> and do not call <tt>super.apply(IOContainer)</tt> erroneously 
     *  which will result in an infinite loop.
     *  @return the last inner operator's output or the input itself if the chain is empty. */
    public IOObject[] apply() throws OperatorException {
	stepCounter = 0;
	IOContainer input = getInput();
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    input = ((Operator)i.next()).apply(input);
	}
	return input.getIOObjects();
    }


    /** Since the apply methods of the inner operators already add additional output, this method simply return a new
     *  container which is build from the additional output objects. */
    protected IOContainer handleAdditionalOutput(IOContainer container, IOObject[] additional) {
	//return new IOContainer(additional);
	return container.prepend(additional);
    }

    // --------------------------------------------------------------------------------

    /** Will throw an exception if a non optional property has no default value and is not defined by user. */
    public int checkProperties() {
	int errorCount = super.checkProperties();
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Operator o = (Operator)i.next();
	    errorCount += o.checkProperties();
	}
	return errorCount;
    }

    /** Checks if the number of inner operators lies between MinInnerOps and MaxInnerOps. 
     *  Performs the check for all operator chains which are children of this operator chain. */
    public int checkNumberOfInnerOperators() {
	int errorCount = 0;
	if ((getNumberOfEnabledOperators() < getMinNumberOfInnerOperators()) || 
	    (getNumberOfEnabledOperators() > getMaxNumberOfInnerOperators())) {
	    String message = "Operator has " + getNumberOfEnabledOperators() + " " +
		((getNumberOfEnabledOperators() == 1) ? "child" : "children") + 
		", should be " + 
		((getMinNumberOfInnerOperators() == getMaxNumberOfInnerOperators()) ? 
		 getMinNumberOfInnerOperators() + "" : 
		 " between " + getMinNumberOfInnerOperators() + 
		 " and " + getMaxNumberOfInnerOperators());
	    addError(message);
	    errorCount++;
	}
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Object o = i.next();
	    if (o instanceof OperatorChain) 
		errorCount += ((OperatorChain)o).checkNumberOfInnerOperators();
	}
	return errorCount;
    }

    /** Used only for validation checks. In all other cases disabled operators are handled like dummy operators. */
    private int getNumberOfEnabledOperators() {
	int number = 0;
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Operator op = (Operator)i.next();
	    if (op.isEnabled()) number++;
	}
	return number;
    }

    /** Returns this OperatorChain's name and class and the ExperimentTrees of
     *  the inner operators. */
    public String createExperimentTree(String indent, String selfPrefix, String childPrefix, Operator markOperator, String mark) {
	String tree = super.createExperimentTree(indent, selfPrefix, childPrefix, markOperator, mark);
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Operator o = (Operator)i.next();
	    tree += "\n" + o.createExperimentTree(indent, 
						  childPrefix + "+- ", 
						  childPrefix + (i.hasNext() ? "|  " : "   "),
						  markOperator,
						  mark);
	}
	return tree;
    }

    protected final String getInnerOperatorsXML(String indent) {
	StringBuffer result = new StringBuffer();
	Iterator i = operators.iterator();
	while ((i.hasNext())) {
	    result.append(((Operator)i.next()).getXML(indent));
	} 
	return result.toString();
    }

    public void delete() {
	Iterator i = operators.iterator();
	while (i.hasNext()) {
	    Operator operator = (Operator)i.next();
	    operator.delete();
	    //operator.setParent(null);
	}
	super.delete();
    }

    public void clearErrorList() {
	Iterator i = operators.iterator();
	while ((i.hasNext())) {
	    ((Operator)i.next()).clearErrorList();
	} 
	super.clearErrorList();
    }

    public abstract int getNumberOfSteps();

    public int getNumberOfChildrensSteps() {
	int sum = 0;
	for (int i = 0; i < getNumberOfOperators(); i++) {
	    sum += getOperator(i).getNumberOfSteps();
	}
	return sum;
    }

    public void countStep() {
	int max = getNumberOfSteps();
	if (stepCounter < max) {
	    stepCounter++;	    
	}
	if (getParent() != null) {
	    getParent().countStep();
	}
    }

    public int getCurrentStep() {
	return stepCounter;
    }

    public boolean configurationEquals(Object o) {
	if (!super.configurationEquals(o)) return false;
	OperatorChain op = (OperatorChain)o;
	if (op.getNumberOfOperators() != this.getNumberOfOperators()) return false;
	for (int i = 0; i < getNumberOfOperators(); i++) {
	    if (!getOperator(i).configurationEquals(op.getOperator(i))) return false;
	}
	return true;
    }

}

⌨️ 快捷键说明

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