📄 operator.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.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.OperatorDescription;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Tools;
import edu.udo.cs.yale.tools.XMLException;
import edu.udo.cs.yale.tools.OperatorService;
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;
import org.w3c.dom.*;
/** 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.
*
* <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.37 2004/09/14 08:39:04 ingomierswa Exp $
*/
public abstract class Operator {
/** Indicates a breakpoint before the operator. */
public static final int BREAKPOINT_BEFORE = 0;
/** Indicates a breakpoint after the operator. */
public static final int BREAKPOINT_AFTER = 1;
/** Indicates if before / after this operator a breakpoint is set. */
private boolean breakPoint[] ={false,false};
/** Indicates if this operator is enabled. */
private boolean enabled = true;
/** Name of the operators (for logging).*/
private String name;
/** A user defined description for this operator instance. Will be filled from comments in the XML files. */
private String userDescription;
/** 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;
}
public void setUserDescription(String description) {
this.userDescription = description;
}
public String getUserDescription() {
return userDescription;
}
/** 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);
}
/** Sets the activation mode. Inactive operators do not perform their action. */
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/** Returns true if this operator is enabled. */
public boolean isEnabled() {
return enabled;
}
//--------------------------------------------------------------------------------
/** 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -