📄 operatorparams.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.tools.param;import edu.udo.cs.yale.Yale;import edu.udo.cs.yale.operator.parameter.Parameters;import edu.udo.cs.yale.tools.ParameterService;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.op.OperatorDescription;import edu.udo.cs.yale.tools.xml.TagObject;import edu.udo.cs.yale.tools.xml.XMLException;import edu.udo.cs.yale.operator.Operator;import edu.udo.cs.yale.operator.OperatorChain;import edu.udo.cs.yale.Experiment;import java.awt.Image;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.HashMap;import java.util.ListIterator;import java.util.Iterator;import java.util.Set;/** Represents an operator tag and its parameters stored within in a configuration file. * @author Simon, Ingo * @version 20.02.2001 */public class OperatorParams extends TagObject { /** Map for class names <-> classes. */ private static Map classnameMap = new HashMap(); /** Map for classes <-> names. */ private static Map nameMap = new HashMap(); /** Map for names <-> descriptions. */ private static Map operatorDescriptions = new HashMap(); /** Map for group name <-> group (list). */ //private static Map groupMap = new HashMap(); private static GroupTree groupTree = new GroupTree(""); /** This operator block's name. */ private String name; /** Nam of the operator type. */ private String className; /** This operator block's inner operators. */ private List innerOperators; /** This operator block's inner parameters. */ private ArrayList parameters; private OperatorParams parent; public OperatorParams() { parameters = new ArrayList(); innerOperators = new ArrayList(); } /** Inner tags allowed: <parameter> */ public void addInnerTag(TagObject tagObject) throws XMLException { if (tagObject instanceof Parameter) { Parameter p = (Parameter)tagObject; if (p.getKey() == null) { throw new XMLException("Key not set for <parameter>."); } else { parameters.add(p); } } else if (tagObject instanceof OperatorParams) { innerOperators.add(tagObject); ((OperatorParams)tagObject).setParent(this); } else { throw new XMLException("Illegal inner tag for <operator>."); } } /** Allowed attributes: name, class*/ public void setAttribute(String key, String val) throws XMLException { if (key.equals("name")) { name = val; } else if (key.equals("class")) { className = val; } else if (key.equals("parentlookup")) { LogService.logMessage("The attribute parentlookup is obsolete.", LogService.WARNING); } else { throw new XMLException("Illegal attribute for tag <operator>: " + key+"."); } } public void appendText(String text) {} public void setName(String name) { this.name = name; } public String getName() { return name; } private void setParent(OperatorParams parent) { this.parent = parent; } /** Instantiates this operator and all its inner operators. */ public Operator instantiateOperator(Experiment experiment) throws InstantiationException, IllegalAccessException { if (className == null) throw new InstantiationException("Classname not set in operator '" + name+"'!"); Class opClass = (Class)classnameMap.get(className); if (opClass == null) throw new InstantiationException("Unknown classname: '" + className + "'!"); Operator operator = (Operator)opClass.newInstance();// Operator operator = null;// try {// operator = (Operator)Class.forName("edu.udo.cs.yale.operator.ExperimentOperator").newInstance();// } catch (Exception e) {// e.printStackTrace();// } operator.register(experiment, name); Parameters opParameters = operator.getParameters(); Iterator i = parameters.iterator(); while (i.hasNext()) { Parameter parameter = (Parameter)i.next(); opParameters.setParameter(parameter.getKey(), parameter.getValue()); } if (operator instanceof OperatorChain) { OperatorChain chain = (OperatorChain)operator; i = innerOperators.iterator(); while (i.hasNext()) { OperatorParams op = (OperatorParams)i.next(); Operator innerOp = op.instantiateOperator(experiment); chain.addOperator(innerOp); } if (innerOperators.size() < chain.getMinNumberOfInnerOperators()) { LogService.logMessage("Less than " + chain.getMinNumberOfInnerOperators() + " inner operators for " + chain.getName() + "!", LogService.WARNING); } } return operator; } public static void registerOperators(List operators, ClassLoader classLoader) { Iterator i = operators.iterator(); while (i.hasNext()) { OperatorDescription op = (OperatorDescription)i.next(); try { Class opClass = Class.forName(op.getClassName(), true, classLoader); classnameMap.put(op.getName(), opClass); nameMap.put(opClass, op.getName()); operatorDescriptions.put(op.getName(), op); // todo: change to group tree String groupString = op.getGroup(); String[] groupNames = groupString.split("\\."); GroupTree currentGroup = groupTree; for (int j = 0; j < groupNames.length; j++) { GroupTree subGroup = currentGroup.getSubGroup(groupNames[j]); if (subGroup == null) { subGroup = new GroupTree(groupNames[j]); currentGroup.addSubGroup(subGroup); } currentGroup = subGroup; } currentGroup.addOperator(op.getName()); } catch (ClassNotFoundException e) { LogService.logMessage("Class '" + op.getClassName() + "' not found!", LogService.ERROR); //Yale.quit(1); } catch (Throwable t) { LogService.logException("Cannot register '" + op.getClassName() + "'!", t); } } } public static OperatorDescription getOperatorDescription(String name) { return (OperatorDescription)operatorDescriptions.get(name); } public static OperatorDescription getOperatorDescription(Class clazz) { return (OperatorDescription)operatorDescriptions.get(getOperatorClassName(clazz)); } public static Class getOperatorClass(String name) { return (Class)classnameMap.get(name); } public static String getOperatorClassName(Class operatorClass) { return (String)nameMap.get(operatorClass); } public static Set getOperatorNames() { return classnameMap.keySet(); } public static Set getOperatorClasses() { return nameMap.keySet(); } public static GroupTree getGroups() { return groupTree; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -