📄 operator.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.Main;import edu.udo.cs.yale.Experiment;import edu.udo.cs.yale.operator.parameter.Parameters;import edu.udo.cs.yale.operator.parameter.ParameterType;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.TexObject;import edu.udo.cs.yale.tools.Tools;import edu.udo.cs.yale.tools.param.OperatorParams;import edu.udo.cs.yale.tools.op.OperatorDescription;import java.util.List;import java.util.LinkedList;import java.util.ArrayList;import java.util.Iterator;import java.util.Map;import java.util.TreeMap;import java.util.Collection;import java.io.IOException;import java.io.PrintWriter;/** An operator accepts an array of input objects and generates an array of output objects that can be * processed by other operators. Both must implement the IOObject interface.<br> * * <h4>Parameters:</h4> * none * * <h4>Values:</h4> * <ul> * <li><tt>applycount</tt> number of times the operator was applied * <li><tt>time</tt> execution time until now (since <tt>apply()</tt> was called) * <li><tt>looptime</tt> this loop's execution time (since <tt>inApplyLoop()</tt> was called) * </ul> * * @see edu.udo.cs.yale.operator.OperatorChain * * @author ralf,oliver,ingo,simon * @version $Id: Operator.java,v 2.18 2003/09/10 12:21:39 fischer Exp $ */public abstract class Operator { public static final int BREAKPOINT_BEFORE = 0; public static final int BREAKPOINT_AFTER = 1; private boolean breakPoint[] ={false,false}; /** Name of the operators (for logging).*/ private String name; /** The experiment the operator belongs to. */ private Experiment experiment; /** Its parent, if part of an operator chain. */ private OperatorChain parent; /** Its input as set by the apply method. */ private IOContainer inputContainer; /** The output after apply has been called.*/ private IOContainer outputContainer; /** Number of times the operator was applied. */ private int applyCount; /** System time when execution started. */ private long startTime; /** System time when the current loop of execution started. */ private long loopStartTime; /** Parameters for this Operator. */ private Parameters parameters = new Parameters(getParameterTypes()); /** The values for this operator. The current value of a Value can be asked by the ExperimentLogOperator. */ private Map valueMap = new TreeMap(); /** The list which stores the errors of this operator (parameter not set, wrong children number, wrong IO). */ private List errorList = new LinkedList(); // -------------------- INITIALISATION -------------------- /** Creates an unnamed operator. */ public Operator () { addValue(new Value("applycount", "The number of times the operator was applied.", false) { public double getValue() { return applyCount; } }); addValue(new Value("time", "The time elapsed since this operator started.", false) { public double getValue() { return System.currentTimeMillis() - startTime; } }); addValue(new Value("looptime", "The time elapsed since the current loop started.", false) { public double getValue() { return System.currentTimeMillis() - loopStartTime; } }); } public OperatorChain getParent() { return parent; } /** Returns the name of the operator. */ public String getName() { return name; } public Experiment getExperiment() { return experiment; } /** Sets the enclosing operator chain. */ public void setParent(OperatorChain parent) { this.parent = parent; if (parent != null) { setExperiment(parent.getExperiment()); } } protected void setExperiment(Experiment experiment) { this.experiment = experiment; } public void register(Experiment experiment, String name) { this.experiment = experiment; setName(name); } /** Sets the name of the operator.*/ private void setName(String name) { this.name = experiment.getFirstFreeName(name); experiment.registerOperator(this.name, this); } /** Removes this operator from its parent. */ public void remove() { if (parent != null) { parent.removeOperator(this); } delete(); } /** Deletes this operator removing it from the name map. */ protected void delete() { experiment.unRegisterOperator(this.name); } /** Renames this operator updating the name map. */ public void rename(String name) { if (this.name != null) { experiment.unRegisterOperator(this.name); } setName(name); } //-------------------------------------------------------------------------------- /** Performs a deep clone on the most parts of this operator. The experiment, the in- and output containers and * the error list are only cloned by reference copying. Use this method only if you are sure what you are doing. */ public Operator cloneOperator(String name) { Operator clone = null; try { clone = (Operator)getClass().newInstance(); } catch (Exception e) { LogService.logMessage("Can not create clone of operator '" + getName() + "': " + e.getMessage(), LogService.ERROR); return null; } clone.breakPoint = new boolean[] { breakPoint[0], breakPoint[1] }; clone.experiment = experiment; // reference clone.register(getExperiment(), name); getExperiment().registerOperator(name, clone); clone.inputContainer = inputContainer; // reference clone.outputContainer = outputContainer; // reference clone.applyCount = applyCount; clone.startTime = startTime; clone.loopStartTime = loopStartTime; clone.parameters = (Parameters)parameters.clone(); Iterator i = valueMap.keySet().iterator(); while (i.hasNext()) { Object key = i.next(); clone.valueMap.put(key, valueMap.get(key)); } clone.errorList = errorList; // reference return clone; } /** Returns human readable status information. */ public String getStatus() { return name + " ["+applyCount+"]"; } public int getApplyCount() { return applyCount; } /** Returns the name. */ public String toString() { String type = null; if (getOperatorDescription() != null) type = getOperatorDescription().getName(); else type = getClass().getName(); return ((breakPoint[0] || breakPoint[1]) ? "* ":"") + name + " ("+type+")"; } /** Returns this operator's name and class. */ public String createExperimentTree(int indent) { return createExperimentTree(indent(indent), "", "", null, null); } /** Returns this operator's name and class. */ public String createMarkedExperimentTree(int indent, String mark, Operator operator) { mark += " "; indent = Math.max(indent, mark.length()); return createExperimentTree(indent(indent), "", "", operator, indent(indent-mark.length())+mark); } /** Returns this operator's name and class. */ public String createExperimentTree(String indent, String selfPrefix, String childPrefix, Operator markOperator, String mark) { if ((markOperator != null) && (getName().equals(markOperator.getName()))) return mark + selfPrefix + getName() + "[" + applyCount + "]" +" ("+getOperatorDescription().getName()+")"; else return indent + selfPrefix + getName() + "[" + applyCount + "]" + " ("+getOperatorDescription().getName()+")"; } /** Returns a whitespace length indent. */ public String indent(int indent) { String s; for (s = ""; s.length() < indent; s += " "); return s; } public TexObject getTexObject() { TexObject to = new TexObject(); to.addRectUL(0,0,10,6, "\\parbox{0mm}{"+ TexObject.format(OperatorParams.getOperatorClassName(getClass()), TexObject.CLASS_FORMAT) + "\\\\" + TexObject.format(getName(), TexObject.OPERATOR_FORMAT)+ "}", 2); return to; } public OperatorDescription getOperatorDescription() { return OperatorParams.getOperatorDescription(this.getClass()); } // -------------------- Stoepselei -------------------- /** Returns the classes that are needed as input. May be null. */ public abstract Class[] getInputClasses(); /** Returns the classes that are guaranteed to be returned by <tt>apply()</tt>. May be null. */ public abstract Class[] getOutputClasses(); /** Implement this method in subclasses. */ public abstract IOObject[] apply() throws OperatorException; /** Returns the number of steps (e.g. loops) the operator performs, e.g. 10 for * a tenfold cross validation. */ public int getNumberOfSteps() { return 1; } /** If you find the <tt>getInputClasses()</tt> and <tt>getOuputClasses()</tt> methods for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -