📄 capabilities.java
字号:
/* * 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., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * Capabilities.java * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand */package weka.core;import weka.core.converters.ConverterUtils.DataSource;import java.io.Serializable;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.Properties;import java.util.Vector;/** * A class that describes the capabilites (e.g., handling certain types of * attributes, missing values, types of classes, etc.) of a specific * classifier. By default, the classifier is capable of nothing. This * ensures that new features have to be enabled explicitly. <p/> * * A common code fragment for making use of the capabilities in a classifier * would be this: * <pre> * public void <b>buildClassifier</b>(Instances instances) throws Exception { * // can the classifier handle the data? * getCapabilities().<b>testWithFail(instances)</b>; * ... * // possible deletion of instances with missing class labels, etc. * </pre> * For only testing a single attribute, use this: * <pre> * ... * Attribute att = instances.attribute(0); * getCapabilities().<b>testWithFail(att)</b>; * ... * </pre> * Or for testing the class attribute (uses the capabilities that are * especially for the class): * <pre> * ... * Attribute att = instances.classAttribute(); * getCapabilities().<b>testWithFail(att, <i>true</i>)</b>; * ... * </pre> * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.24 $ */public class Capabilities implements Cloneable, Serializable { /** serialversion UID */ static final long serialVersionUID = -5478590032325567849L; /** the properties file for managing the tests */ public final static String PROPERTIES_FILE = "weka/core/Capabilities.props"; /** the actual properties */ protected static Properties PROPERTIES; /** defines an attribute type */ private final static int ATTRIBUTE = 1; /** defines a class type */ private final static int CLASS = 2; /** defines an attribute capability */ private final static int ATTRIBUTE_CAPABILITY = 4; /** defines a class capability */ private final static int CLASS_CAPABILITY = 8; /** defines a other capability */ private final static int OTHER_CAPABILITY = 16; /** enumeration of all capabilities */ public enum Capability { // attributes /** can handle nominal attributes */ NOMINAL_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Nominal attributes"), /** can handle binary attributes */ BINARY_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Binary attributes"), /** can handle unary attributes */ UNARY_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Unary attributes"), /** can handle empty nominal attributes */ EMPTY_NOMINAL_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Empty nominal attributes"), /** can handle numeric attributes */ NUMERIC_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Numeric attributes"), /** can handle date attributes */ DATE_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Date attributes"), /** can handle string attributes */ STRING_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "String attributes"), /** can handle relational attributes */ RELATIONAL_ATTRIBUTES(ATTRIBUTE + ATTRIBUTE_CAPABILITY, "Relational attributes"), /** can handle missing values in attributes */ MISSING_VALUES(ATTRIBUTE_CAPABILITY, "Missing values"), // class /** can handle data without class attribute, eg clusterers */ NO_CLASS(CLASS_CAPABILITY, "No class"), /** can handle nominal classes */ NOMINAL_CLASS(CLASS + CLASS_CAPABILITY, "Nominal class"), /** can handle binary classes */ BINARY_CLASS(CLASS + CLASS_CAPABILITY, "Binary class"), /** can handle unary classes */ UNARY_CLASS(CLASS + CLASS_CAPABILITY, "Unary class"), /** can handle empty nominal classes */ EMPTY_NOMINAL_CLASS(CLASS + CLASS_CAPABILITY, "Empty nominal class"), /** can handle numeric classes */ NUMERIC_CLASS(CLASS + CLASS_CAPABILITY, "Numeric class"), /** can handle date classes */ DATE_CLASS(CLASS + CLASS_CAPABILITY, "Date class"), /** can handle string classes */ STRING_CLASS(CLASS + CLASS_CAPABILITY, "String class"), /** can handle relational classes */ RELATIONAL_CLASS(CLASS + CLASS_CAPABILITY, "Relational class"), /** can handle missing values in class attribute */ MISSING_CLASS_VALUES(CLASS_CAPABILITY, "Missing class values"), // other /** can handle multi-instance data */ ONLY_MULTIINSTANCE(OTHER_CAPABILITY, "Only multi-Instance data"); /** the flags for the capabilities */ private int m_Flags = 0; /** the display string */ private String m_Display; /** * initializes the capability with the given flags * * @param flags "meta-data" for the capability * @param display the display string (must be unique!) */ private Capability(int flags, String display) { m_Flags = flags; m_Display = display; } /** * returns true if the capability is an attribute * * @return true if the capability is an attribute */ public boolean isAttribute() { return ((m_Flags & ATTRIBUTE) == ATTRIBUTE); } /** * returns true if the capability is a class * * @return true if the capability is a class */ public boolean isClass() { return ((m_Flags & CLASS) == CLASS); } /** * returns true if the capability is an attribute capability * * @return true if the capability is an attribute capability */ public boolean isAttributeCapability() { return ((m_Flags & ATTRIBUTE_CAPABILITY) == ATTRIBUTE_CAPABILITY); } /** * returns true if the capability is a class capability * * @return true if the capability is a class capability */ public boolean isOtherCapability() { return ((m_Flags & OTHER_CAPABILITY) == OTHER_CAPABILITY); } /** * returns true if the capability is a other capability * * @return true if the capability is a other capability */ public boolean isClassCapability() { return ((m_Flags & CLASS_CAPABILITY) == CLASS_CAPABILITY); } /** * returns the display string of the capability * * @return the display string */ public String toString() { return m_Display; } }; /** the object that owns this capabilities instance */ protected CapabilitiesHandler m_Owner; /** the hashset for storing the active capabilities */ protected HashSet m_Capabilities; /** the hashset for storing dependent capabilities, eg for meta-classifiers */ protected HashSet m_Dependencies; /** the reason why the test failed, used to throw an exception */ protected Exception m_FailReason = null; /** the minimum number of instances in a dataset */ protected int m_MinimumNumberInstances = 1; /** whether to perform any tests at all */ protected boolean m_Test; /** whether to perform data based tests */ protected boolean m_InstancesTest; /** whether to perform attribute based tests */ protected boolean m_AttributeTest; /** whether to test for missing values */ protected boolean m_MissingValuesTest; /** whether to test for missing class values */ protected boolean m_MissingClassValuesTest; /** whether to test for minimum number of instances */ protected boolean m_MinimumNumberInstancesTest; /** * initializes the capabilities for the given owner * * @param owner the object that produced this Capabilities instance */ public Capabilities(CapabilitiesHandler owner) { super(); setOwner(owner); m_Capabilities = new HashSet(); m_Dependencies = new HashSet(); // load properties if (PROPERTIES == null) { try { PROPERTIES = Utils.readProperties(PROPERTIES_FILE); } catch (Exception e) { e.printStackTrace(); PROPERTIES = new Properties(); } } m_Test = Boolean.parseBoolean(PROPERTIES.getProperty("Test", "true")); m_InstancesTest = Boolean.parseBoolean(PROPERTIES.getProperty("InstancesTest", "true")) && m_Test; m_AttributeTest = Boolean.parseBoolean(PROPERTIES.getProperty("AttributeTest", "true")) && m_Test; m_MissingValuesTest = Boolean.parseBoolean(PROPERTIES.getProperty("MissingValuesTest", "true")) && m_Test; m_MissingClassValuesTest = Boolean.parseBoolean(PROPERTIES.getProperty("MissingClassValuesTest", "true")) && m_Test; m_MinimumNumberInstancesTest = Boolean.parseBoolean(PROPERTIES.getProperty("MinimumNumberInstancesTest", "true")) && m_Test; } /** * Creates and returns a copy of this object. * * @return a clone of this object */ public Object clone() { Capabilities result; result = new Capabilities(m_Owner); result.assign(this); return result; } /** * retrieves the data from the given Capabilities object * * @param c the capabilities object to initialize with */ public void assign(Capabilities c) { for (Capability cap: Capability.values()) { // capability if (c.handles(cap)) enable(cap); else disable(cap); // dependency if (c.hasDependency(cap)) enableDependency(cap); else disableDependency(cap); } setMinimumNumberInstances(c.getMinimumNumberInstances()); } /** * performs an AND conjunction with the capabilities of the given * Capabilities object and updates itself * * @param c the capabilities to AND with */ public void and(Capabilities c) { for (Capability cap: Capability.values()) { // capability if (handles(cap) && c.handles(cap)) m_Capabilities.add(cap); else m_Capabilities.remove(cap); // dependency if (hasDependency(cap) && c.hasDependency(cap)) m_Dependencies.add(cap); else m_Dependencies.remove(cap); } // minimum number of instances that both handlers need at least to work if (c.getMinimumNumberInstances() > getMinimumNumberInstances()) setMinimumNumberInstances(c.getMinimumNumberInstances()); } /** * performs an OR conjunction with the capabilities of the given * Capabilities object and updates itself * * @param c the capabilities to OR with */ public void or(Capabilities c) { for (Capability cap: Capability.values()) { // capability if (handles(cap) || c.handles(cap)) m_Capabilities.add(cap); else m_Capabilities.remove(cap); // dependency if (hasDependency(cap) || c.hasDependency(cap)) m_Dependencies.add(cap); else m_Dependencies.remove(cap); } if (c.getMinimumNumberInstances() < getMinimumNumberInstances()) setMinimumNumberInstances(c.getMinimumNumberInstances()); } /** * Returns true if the currently set capabilities support at least all of * the capabiliites of the given Capabilities object (checks only the enum!) * * @param c the capabilities to support at least * @return true if all the requested capabilities are supported */ public boolean supports(Capabilities c) { boolean result; result = true; for (Capability cap: Capability.values()) { if (c.handles(cap) && !handles(cap)) { result = false; break; } } return result; } /** * Returns true if the currently set capabilities support (or have a * dependency) at least all of the capabilities of the given Capabilities * object (checks only the enum!) * * @param c the capabilities (or dependencies) to support at least * @return true if all the requested capabilities are supported (or at * least have a dependency) */ public boolean supportsMaybe(Capabilities c) { boolean result; result = true; for (Capability cap: Capability.values()) { if (c.handles(cap) && !(handles(cap) || hasDependency(cap))) { result = false; break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -