📄 operator.java
字号:
* 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(type.getKey() + " is not defined!"); errorCount++; } } return errorCount; } // -------------------- Apply und Input-Versorgung -------------------- /** 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 FatalException("Experiment of operator " + this + " is null, probably not registered!"); } if (experiment.shouldStop()) { LogService.logMessage("$bExperiment stopped.^b",LogService.MAXIMUM); throw new ExperimentStoppedException(this); } 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(); } } 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 = inputContainer.append(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(); } } catch (InterruptedException e) { LogService.logException("While waiting after breakpoint", e); } } return outputContainer; } /** 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. */ 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 loops 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(); } // --------------------------------------------------------------------------------// /**// * 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>$t</b> with the system time// * <li><b>$$</b> with $</li>// * </ul>// * Returns null if str is null.// */// public String expandString(String str) {// if (str == null) return null;// StringBuffer result = new StringBuffer();// int start = 0;// int end = 0;// while ((end = str.indexOf('$', start)) >= 0) {// result.append(str.substring(start, end));// if (end+1 < str.length()) {// switch (str.charAt(end+1)) {// case 'n': result.append(getName()); break;// case 'c': result.append(getClass().getName()); break;// case 'a': result.append(applyCount); break;// case 't': result.append(System.currentTimeMillis()); break;// case '$': result.append('$'); break;// default: result.append(str.charAt(end+1)); break;// }// }// start = end+2;// }// result.append(str.substring(start));// return result.toString();// } // -------------------- 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 (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 list of <tt>ParameterTypes</tt> describing the parameters of this operator. * The default implementation returns an empty list. */ public List getParameterTypes() { return new ArrayList(); } /** Writes the XML representation of this operator. */ public void writeXML(PrintWriter out, String indent) throws IOException { out.println(getXML(indent)); } /** Returns the XML representation of this operator. */ public String getXML(String indent) { StringBuffer result = new StringBuffer(); result.append(indent + "<operator "+ "name=\""+name+"\" "+ "class=\""+OperatorParams.getOperatorClassName(this.getClass())+"\""+ ">\n"); result.append(parameters.getXML(indent+" ")); result.append(getInnerOperatorsXML(indent+" ")); result.append(indent + "</operator>\n"); return result.toString(); } /** Writes the XML representation of the inner operators. Since an Operator does not have any * inner operators, the default implementation does nothing. Implemented by <tt>OperatorChain</tt>. */ protected String getInnerOperatorsXML(String indent) { return ""; } /** Sets or clears a breakpoint at the given position. * @param position One out of BREAKPOINT_BEFORE and BREAKPOINT_AFTER */ public void setBreakpoint(int position, boolean on) { breakPoint[position] = on; } /** Returns true iff a breakpoint is set at the given position * @param position One out of BREAKPOINT_BEFORE and BREAKPOINT_AFTER */ public boolean hasBreakpoint(int position) { return breakPoint[position]; } /** Clears the list of errors. * @see #addError(String) */ public void clearErrorList() { errorList.clear(); } /** Adds an error message. * @see #getErrorList() */ public void addError(String message) { LogService.logMessage(this.getName()+": " + message, LogService.ERROR); errorList.add(message); } /** Returns a List of Strings containing error messages. * @see #addError(String) */ public List getErrorList() { return errorList; } /** Returns the system time when the operator was started. */ public long getStartTime() { return startTime; } /** Convenience method for logging a message prefixed by the operator name. * @see LogService */ public void logMessage(String message, int verbosityLevel) { LogService.logMessage(getName()+": "+message, verbosityLevel); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -