📄 operator.java
字号:
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 OperatorDescription getOperatorDescription() {
return OperatorService.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
* some reason not useful, you may override this method. Otherwise it returns a default IODescription
* containing the classes returned by the first. */
private IODescription getIODescription() {
return new IODescription(getInputClasses(), getOutputClasses());
}
/** Subclasses will throw an exception if something isn't ok. Returns the output
* that this operator returns when provided with the given input. */
public Class[] checkIO(Class[] input) throws IllegalInputException {
return getIODescription().getOutputClasses(input, this);
}
/** Will throw an exception if a non optional property has no default value and is not defined by user.
* Returns the number of errors. */
public int checkProperties() {
int errorCount = 0;
Iterator i = getParameterTypes().iterator();
while (i.hasNext()) {
ParameterType type = (ParameterType)i.next();
Object value = getParameters().getParameter(type.getKey());
if (!type.isOptional() && (type.getDefaultValue() == null) && (value == null)) {
addError(getName()+": "+type.getKey() + " is not defined!");
errorCount++;
}
}
return errorCount;
}
// -------------------- Apply und Input-Versorgung --------------------
private void stop() throws ExperimentStoppedException {
LogService.logMessage("$bExperiment stopped.^b",LogService.MAXIMUM);
throw new ExperimentStoppedException(this);
}
/** Applies the operator. Don't override this method, but <tt>apply()</tt>.
* @return An IOContainer containing all IOObjects returned by apply() plus
* the unused IOObjects of input, i.e. those IOObjects that were not returned
* by one of <tt>input.getInput(Class)</tt> or <tt>input.getInput(Class, int)</tt>. */
public synchronized final IOContainer apply(IOContainer input) throws OperatorException {
if (experiment == null) {
throw new RuntimeException("Experiment of operator " + this + " is null, probably not registered!");
}
if (isEnabled()) {
if (experiment.shouldStop()) {
stop();
}
experiment.setCurrentOperator(this);
applyCount++;
startTime = loopStartTime = System.currentTimeMillis();
if (input == null) throw new IllegalArgumentException("Input is null!");
this.inputContainer = input;
if (parent != null)
parent.countStep();
if (breakPoint[BREAKPOINT_BEFORE]) {
LogService.logMessage("$b" + getName() + "^b: Breakpoint reached", LogService.MAXIMUM);
Thread currentThread = Thread.currentThread();
try {
experiment.fireBreakpointEvent(this, inputContainer);
synchronized (currentThread) {
currentThread.wait();
}
if (experiment.shouldStop()) stop();
} catch (InterruptedException e) {
LogService.logException("While waiting after breakpoint", e);
}
}
IOObject[] ioo = inputContainer.getIOObjects();
if (ioo.length == 0) {
LogService.logMessage("$b"+getName() + " called^b "+Tools.ordinalNumber(applyCount)+" time without input",
LogService.OPERATOR);
} else {
String iLog = "$b"+getName() + " called^b "+Tools.ordinalNumber(applyCount)+" time with input:";
for (int i = 0; i < ioo.length; i++) iLog += "\n "+(i+1)+". " + ioo[i];
LogService.logMessage(iLog, LogService.OPERATOR);
}
initApply();
IOObject[] output = apply();
if (output.length == 0) {
LogService.logMessage(getName() + " returned no additional output", LogService.OPERATOR);
} else {
String oLog = getName() + " returned additional output:";
for (int i = 0; i < output.length; i++) oLog += "\n "+(i+1)+". " + output[i];
LogService.logMessage(oLog, LogService.OPERATOR);
}
LogService.logMessage(getName()+": execution time was "+(System.currentTimeMillis()-startTime)+" ms", LogService.OPERATOR);
outputContainer = handleAdditionalOutput(inputContainer, output);
if (breakPoint[BREAKPOINT_AFTER]) {
LogService.logMessage("$b" + getName() + "^b: Breakpoint reached", LogService.MAXIMUM);
Thread currentThread = Thread.currentThread();
try {
experiment.fireBreakpointEvent(this, outputContainer);
synchronized (currentThread) {
currentThread.wait();
}
if (experiment.shouldStop()) stop();
} catch (InterruptedException e) {
LogService.logException("While waiting after breakpoint", e);
}
}
return outputContainer;
} else {
return input;
}
}
/** The default implementation appends the given additional IOObjects to the container. */
protected IOContainer handleAdditionalOutput(IOContainer container, IOObject[] additional) {
return inputContainer.prepend(additional);
}
/** Returns an IOObject of class cls. Program terminates if not available. */
protected IOObject getInput(Class cls) throws MissingIOObjectException {
return getInput(cls, 0, true);
}
/** Returns the nr-th IOObject of class cls. Program terminates if not available. */
protected IOObject getInput(Class cls, int nr, boolean remove) throws MissingIOObjectException {
return inputContainer.getInput(cls, nr, remove);
}
/** Returns input for the given class. */
protected IOObject getInput(Class cls, boolean remove) throws MissingIOObjectException {
return getInput(cls, 0, remove);
}
/** Returns the complete input. */
public IOContainer getInput() {
return inputContainer;
}
/** ATTENTION: Use this method only if you are ABSOLUTELY sure what you are doing! */
protected void setInput(IOContainer input) {
if (input == null) throw new IllegalArgumentException("Input is null!");
this.inputContainer = input;
}
/** Called immediately before apply is called. The default implementation does nothing.
* @deprecated Formerly this method was invoked once at the beginning of each experiment. Now it is invoked each time in apply(). Please initiate your operator in {@link #apply()}. */
public void initApply() throws OperatorException { }
/** Called when the experiment starts. Resets all counters. */
public void experimentStarts() { applyCount = 0; }
/** Called at the end of the experiment. The default implementation does nothing. */
public void experimentFinished() { }
// -------------------- --------------------
/** Should be called if this operator performs a loop (for statistics) .*/
public void inApplyLoop() {
loopStartTime = System.currentTimeMillis();
}
/** Adds an implementation of Value. */
public void addValue(Value value) {
valueMap.put(value.getKey(), value);
}
/** Returns the value of the Value with the given key. */
public final double getValue(String key) {
Value value = (Value)valueMap.get(key);
if (value != null) {
return value.getValue();
} else {
LogService.logMessage(getName()+": Illegal value requested: "+key, LogService.WARNING);
return Double.NaN;
}
}
/** Returns all Values sorted by key. */
public Collection getValues() {
return valueMap.values();
}
// -------------------- parameter wrapper --------------------
/** Returns a collection of all parameters of this operator. */
public Parameters getParameters() { return parameters; }
/** Returns a single parameter retrieved from the {@link Parameters} of this Operator. */
public Object getParameter(String key) { return parameters.getParameter(key); }
/** Returns true iff the parameter with the given name is set. */
public boolean isParameterSet(String key) { return getParameter(key) != null; }
/** Returns a single named parameter and casts it to String. */
public String getParameterAsString(String key) { return expandString((String)getParameter(key)); }
/** Returns a single named parameter and casts it to int. */
public int getParameterAsInt(String key) { return ((Integer)getParameter(key)).intValue(); }
/** Returns a single named parameter and casts it to double. */
public double getParameterAsDouble(String key) { return ((Double)getParameter(key)).doubleValue(); }
/** Returns a single named parameter and casts it to boolean. */
public boolean getParameterAsBoolean(String key) { return ((Boolean)getParameter(key)).booleanValue(); }
/** Returns a single named parameter and casts it to List. */
public List getParameterList(String key) { return (List)getParameter(key); }
/** Returns a single named parameter and casts it to File. This file is already resolved against the experiment. */
public java.io.File getParameterAsFile(String key) {
String fileName = getParameterAsString(key);
if (getExperiment() != null) return getExperiment().resolveFileName(fileName);
else return new java.io.File(fileName);
}
/**
* Replaces
* <ul>
* <li><b>$n</b> with the name of this operator</li>
* <li><b>$c</b> with the class of this operator</li>
* <li><b>$a</b> with the number of times the operator was applied</li>
* <li><b>$b</b> with the number of times the operator was applied plus one</li>
* <li><b>$t</b> with the system time
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -