📄 capabilities.java
字号:
if (!result) { m_FailReason = cap.m_FailReason; return false; } } } // passed all tests! return true; } /** * tests the given attribute by calling the test(Attribute,boolean) method * and throws an exception if the test fails. The method assumes that the * specified attribute is not the class attribute. * * @param att the attribute to test * @throws Exception in case the attribute doesn't pass the tests * @see #test(Attribute,boolean) */ public void testWithFail(Attribute att) throws Exception { test(att, false); } /** * tests the given attribute by calling the test(Attribute,boolean) method * and throws an exception if the test fails. * * @param att the attribute to test * @param isClass whether this attribute is the class attribute * @throws Exception in case the attribute doesn't pass the tests * @see #test(Attribute,boolean) */ public void testWithFail(Attribute att, boolean isClass) throws Exception { if (!test(att, isClass)) throw m_FailReason; } /** * tests the given data by calling the test(Instances,int,int) method and * throws an exception if the test fails. * * @param data the data to test * @param fromIndex the range of attributes - start (incl.) * @param toIndex the range of attributes - end (incl.) * @throws Exception in case the data doesn't pass the tests * @see #test(Instances,int,int) */ public void testWithFail(Instances data, int fromIndex, int toIndex) throws Exception { if (!test(data, fromIndex, toIndex)) throw m_FailReason; } /** * tests the given data by calling the test(Instances) method and throws * an exception if the test fails. * * @param data the data to test * @throws Exception in case the data doesn't pass the tests * @see #test(Instances) */ public void testWithFail(Instances data) throws Exception { if (!test(data)) throw m_FailReason; } /** * returns a string representation of the capabilities * * @return a string representation of this object */ public String toString() { Vector sorted; StringBuffer result; result = new StringBuffer(); // capabilities sorted = new Vector(m_Capabilities); Collections.sort(sorted); result.append("Capabilities: " + sorted.toString() + "\n"); // dependencies sorted = new Vector(m_Dependencies); Collections.sort(sorted); result.append("Dependencies: " + sorted.toString() + "\n"); // other stuff result.append("min # Instance: " + getMinimumNumberInstances() + "\n"); return result.toString(); } /** * turns the capabilities object into source code. The returned source code * is a block that creates a Capabilities object named 'objectname' and * enables all the capabilities of this Capabilities object. * * @param objectname the name of the Capabilities object being instantiated * @return the generated source code */ public String toSource(String objectname) { return toSource(objectname, 0); } /** * turns the capabilities object into source code. The returned source code * is a block that creates a Capabilities object named 'objectname' and * enables all the capabilities of this Capabilities object. * * @param objectname the name of the Capabilities object being instantiated * @param indent the number of blanks to indent * @return the generated source code */ public String toSource(String objectname, int indent) { StringBuffer result; String capsName; String capName; String indentStr; int i; result = new StringBuffer(); capsName = Capabilities.class.getName(); capName = Capabilities.Capability.class.getName().replaceAll("\\$", "."); indentStr = ""; for (i = 0; i < indent; i++) indentStr += " "; // object name result.append(indentStr + capsName + " " + objectname + " = new " + capsName + "(this);\n"); // capabilities result.append("\n"); for (Capability cap: Capability.values()) { // capability if (handles(cap)) result.append( indentStr + objectname + ".enable(" + capName + "." + cap.name() + ");\n"); // dependency if (hasDependency(cap)) result.append( indentStr + objectname + ".enableDependency(" + capName + "." + cap.name() + ");\n"); } // other result.append("\n"); result.append( indentStr + objectname + ".setMinimumNumberInstances(" + getMinimumNumberInstances() + ");\n"); result.append("\n"); return result.toString(); } /** * returns a Capabilities object specific for this data. The multi-instance * capability is not checked as well as the minimum number of instances * is not set. * * @param data the data to base the capabilities on * @return a data-specific capabilities object * @throws Exception in case an error occurrs, e.g., an unknown attribute * type */ public static Capabilities forInstances(Instances data) throws Exception { return forInstances(data, false); } /** * returns a Capabilities object specific for this data. The minimum number * of instances is not set, the check for multi-instance data is optional. * * @param data the data to base the capabilities on * @param multi if true then the structure is checked, too * @return a data-specific capabilities object * @throws Exception in case an error occurrs, e.g., an unknown attribute * type */ public static Capabilities forInstances(Instances data, boolean multi) throws Exception { Capabilities result; Capabilities multiInstance; int i; int n; int m; Instance inst; boolean missing; result = new Capabilities(null); // class if (data.classIndex() == -1) { result.enable(Capability.NO_CLASS); } else { switch (data.classAttribute().type()) { case Attribute.NOMINAL: if (data.classAttribute().numValues() == 1) result.enable(Capability.UNARY_CLASS); else if (data.classAttribute().numValues() == 2) result.enable(Capability.BINARY_CLASS); else result.enable(Capability.NOMINAL_CLASS); break; case Attribute.NUMERIC: result.enable(Capability.NUMERIC_CLASS); break; case Attribute.STRING: result.enable(Capability.STRING_CLASS); break; case Attribute.DATE: result.enable(Capability.DATE_CLASS); break; case Attribute.RELATIONAL: result.enable(Capability.RELATIONAL_CLASS); break; default: throw new UnsupportedAttributeTypeException( "Unknown class attribute type '" + data.classAttribute() + "'!"); } // missing class values for (i = 0; i < data.numInstances(); i++) { if (data.instance(i).classIsMissing()) { result.enable(Capability.MISSING_CLASS_VALUES); break; } } } // attributes for (i = 0; i < data.numAttributes(); i++) { // skip class if (i == data.classIndex()) continue; switch (data.attribute(i).type()) { case Attribute.NOMINAL: result.enable(Capability.UNARY_ATTRIBUTES); if (data.attribute(i).numValues() == 2) result.enable(Capability.BINARY_ATTRIBUTES); else if (data.attribute(i).numValues() > 2) result.enable(Capability.NOMINAL_ATTRIBUTES); break; case Attribute.NUMERIC: result.enable(Capability.NUMERIC_ATTRIBUTES); break; case Attribute.DATE: result.enable(Capability.DATE_ATTRIBUTES); break; case Attribute.STRING: result.enable(Capability.STRING_ATTRIBUTES); break; case Attribute.RELATIONAL: result.enable(Capability.RELATIONAL_ATTRIBUTES); break; default: throw new UnsupportedAttributeTypeException( "Unknown attribute type '" + data.attribute(i).type() + "'!"); } } // missing values missing = false; for (i = 0; i < data.numInstances(); i++) { inst = data.instance(i); if (inst instanceof SparseInstance) { for (m = 0; m < inst.numValues(); m++) { n = inst.index(m); // skip class if (n == inst.classIndex()) continue; if (inst.isMissing(n)) { missing = true; break; } } } else { for (n = 0; n < data.numAttributes(); n++) { // skip class if (n == inst.classIndex()) continue; if (inst.isMissing(n)) { missing = true; break; } } } if (missing) { result.enable(Capability.MISSING_VALUES); break; } } // multi-instance data? if (multi) { if ( (data.numAttributes() == 3) && (data.attribute(0).isNominal()) // bag-id && (data.attribute(1).isRelationValued()) // bag && (data.classIndex() == data.numAttributes() - 1) ) { multiInstance = new Capabilities(null); multiInstance.or(result.getClassCapabilities()); multiInstance.enable(Capability.NOMINAL_ATTRIBUTES); multiInstance.enable(Capability.RELATIONAL_ATTRIBUTES); multiInstance.enable(Capability.ONLY_MULTIINSTANCE); result.assign(multiInstance); } } return result; } /** * loads the given dataset and prints the Capabilities necessary to * process it. <p/> * * Valid parameters: <p/> * * -file filename <br/> * the file to load * * -c index * the explicit index of the class attribute (default: none) * * @param args the commandline arguments * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { String tmpStr; String filename; DataSource source; Instances data; int classIndex; Capabilities cap; Iterator iter; if (args.length == 0) { System.out.println( "\nUsage: " + Capabilities.class.getName() + " -file <dataset> [-c <class index>]\n"); return; } // get parameters tmpStr = Utils.getOption("file", args); if (tmpStr.length() == 0) throw new Exception("No file provided with option '-file'!"); else filename = tmpStr; tmpStr = Utils.getOption("c", args); if (tmpStr.length() != 0) { if (tmpStr.equals("first")) classIndex = 0; else if (tmpStr.equals("last")) classIndex = -2; // last else classIndex = Integer.parseInt(tmpStr) - 1; } else { classIndex = -3; // not set } // load data source = new DataSource(filename); if (classIndex == -3) data = source.getDataSet(); else if (classIndex == -2) data = source.getDataSet(source.getStructure().numAttributes() - 1); else data = source.getDataSet(classIndex); // determine and print capabilities cap = forInstances(data); System.out.println("File: " + filename); System.out.println("Class index: " + ((data.classIndex() == -1) ? "not set" : "" + (data.classIndex() + 1))); System.out.println("Capabilities:"); iter = cap.capabilities(); while (iter.hasNext()) System.out.println("- " + iter.next()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -