📄 operatorchain.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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 edu.udo.cs.yale.tools.TexObject;import edu.udo.cs.yale.tools.param.OperatorParams;import java.util.List;import java.util.ArrayList;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.6 2003/08/13 15:09:07 fischer 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 (operators.size() == getMaxNumberOfInnerOperators()+1) { LogService.logMessage("More than "+getMaxNumberOfInnerOperators()+" inner operators in "+getName()+"!", LogService.WARNING); } return operators.size() - 1; } public void addOperator(Operator operator, int index) { operators.add(index, operator); operator.setParent(this); if (operators.size() == 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 >= operators.size())) { 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 the number of 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 initApply() throws OperatorException { super.initApply();// for (int i = 0; i < getNumberOfOperators(); i++)// getOperator(i).initApply(); stepCounter = 0; } 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 { IOContainer input = getInput(); ListIterator i = operators.listIterator() ; while (i.hasNext()) { input = ((Operator)i.next()).apply(input); } return input.getIOObjects(); } // -------------------------------------------------------------------------------- /** 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 ((operators.size() < getMinNumberOfInnerOperators()) || (operators.size() > getMaxNumberOfInnerOperators())) { String message = "Operator has " + operators.size() + " " + ((operators.size() == 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; } /** 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; } public TexObject getTexObject() { TexObject to = new TexObject(); TexObject[] texO = new TexObject[getNumberOfOperators()]; int hMax = 0; int w = 1; for (int i = 0; i < getNumberOfOperators(); i++) { texO[i] = getOperator(i).getTexObject(); hMax = Math.max(hMax, texO[i].getHeight()); w += 12 + texO[i].getWidth(); } int height = hMax+4; to.addRectUL(0,0,w, height, TexObject.format(OperatorParams.getOperatorClassName(getClass()), TexObject.CLASS_FORMAT) + " " + TexObject.format(getName(), TexObject.OPERATOR_FORMAT)); int cw = 1; for (int i = 0; i < texO.length; i++) { int y = height - texO[i].getHeight() - 3; to.add(texO[i], cw, y); cw += texO[i].getWidth(); Operator op = getOperator(i); Class[] out = op.getOutputClasses(); if (out != null) { for (int k = 0; k < out.length; k++) { to.addVector(cw, y+texO[i].getHeight()-1-2*k, 12, TexObject.format(TexObject.shortClassName(out[k].getName()), TexObject.IO_FORMAT));; } } cw += 12; } return to; } 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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -