📄 findwithcapabilities.java
字号:
String[] options; int i; result = new Vector(); result.add("-num-instances"); result.add("" + m_Capabilities.getMinimumNumberInstances()); if (isEnabled(Capability.NO_CLASS)) { result.add("-no-class"); } else { if (isEnabled(Capability.UNARY_CLASS)) result.add("-unary-class"); if (isEnabled(Capability.BINARY_CLASS)) result.add("-binary-class"); if (isEnabled(Capability.NOMINAL_CLASS)) result.add("-nominal-class"); if (isEnabled(Capability.NUMERIC_CLASS)) result.add("-numeric-class"); if (isEnabled(Capability.STRING_CLASS)) result.add("-string-class"); if (isEnabled(Capability.DATE_CLASS)) result.add("-date-class"); if (isEnabled(Capability.RELATIONAL_CLASS)) result.add("-relational-class"); } if (isEnabled(Capability.UNARY_ATTRIBUTES)) result.add("-unary-atts"); if (isEnabled(Capability.BINARY_ATTRIBUTES)) result.add("-binary-atts"); if (isEnabled(Capability.NOMINAL_ATTRIBUTES)) result.add("-nominal-atts"); if (isEnabled(Capability.NUMERIC_ATTRIBUTES)) result.add("-numeric-atts"); if (isEnabled(Capability.STRING_ATTRIBUTES)) result.add("-string-atts"); if (isEnabled(Capability.DATE_ATTRIBUTES)) result.add("-date-atts"); if (isEnabled(Capability.RELATIONAL_ATTRIBUTES)) result.add("-relational-atts"); // not allowed if (isEnabledNot(Capability.NO_CLASS)) result.add("-not-no-class"); if (isEnabledNot(Capability.UNARY_CLASS)) result.add("-not-unary-class"); if (isEnabledNot(Capability.BINARY_CLASS)) result.add("-not-binary-class"); if (isEnabledNot(Capability.NOMINAL_CLASS)) result.add("-not-nominal-class"); if (isEnabledNot(Capability.NUMERIC_CLASS)) result.add("-not-numeric-class"); if (isEnabledNot(Capability.STRING_CLASS)) result.add("-not-string-class"); if (isEnabledNot(Capability.DATE_CLASS)) result.add("-not-date-class"); if (isEnabledNot(Capability.RELATIONAL_CLASS)) result.add("-not-relational-class"); if (isEnabledNot(Capability.UNARY_ATTRIBUTES)) result.add("-not-unary-atts"); if (isEnabledNot(Capability.BINARY_ATTRIBUTES)) result.add("-not-binary-atts"); if (isEnabledNot(Capability.NOMINAL_ATTRIBUTES)) result.add("-not-nominal-atts"); if (isEnabledNot(Capability.NUMERIC_ATTRIBUTES)) result.add("-not-numeric-atts"); if (isEnabledNot(Capability.STRING_ATTRIBUTES)) result.add("-not-string-atts"); if (isEnabledNot(Capability.DATE_ATTRIBUTES)) result.add("-not-date-atts"); if (isEnabledNot(Capability.RELATIONAL_ATTRIBUTES)) result.add("-not-relational-atts"); if (isEnabled(Capability.ONLY_MULTIINSTANCE)) result.add("-only-multi-instance"); if (getHandler() != null) { result.add("-W"); result.add(getHandler().getClass().getName()); if (getHandler() instanceof OptionHandler) { result.add("--"); options = ((OptionHandler) getHandler()).getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); } } else if (getFilename().length() != 0) { result.add("-t"); result.add(getFilename()); result.add("-c"); result.add(m_ClassIndex.getSingleIndex()); } if (m_Superclass.length() != 0) { result.add("-superclass"); result.add(m_Superclass); } else { result.add("-packages"); result.add(m_Packages.toString().replaceAll("\\[", "").replaceAll("\\]", "")); } return (String[]) result.toArray(new String[result.size()]); } /** * sets the Capabilities handler to generate the data for */ public void setHandler(CapabilitiesHandler value) { m_Handler = value; setCapabilities(m_Handler.getCapabilities()); } /** * returns the current set CapabilitiesHandler to generate the dataset * for, can be null */ public CapabilitiesHandler getHandler() { return m_Handler; } /** * Sets the dataset filename to base the capabilities on. It immediately * loads the dataset and retrieves the capabilities from it. */ public void setFilename(String value) { Instances insts; m_Filename = value; if (m_Filename.length() != 0) { try { insts = new Instances(new BufferedReader(new FileReader(m_Filename))); m_ClassIndex.setUpper(insts.numAttributes()); insts.setClassIndex(Integer.parseInt(getClassIndex()) - 1); setCapabilities(Capabilities.forInstances(insts)); } catch (Exception e) { e.printStackTrace(); } } } /** * returns the current filename for the dataset to base the capabilities on */ public String getFilename() { return m_Filename; } /** * sets the class index, -1 for none, first and last are also valid */ public void setClassIndex(String value) { if (value.equals("-1")) m_ClassIndex = null; else m_ClassIndex = new SingleIndex(value); } /** * returns the current current class index, -1 if no class attribute */ public String getClassIndex() { if (m_ClassIndex == null) return "-1"; else return "" + m_ClassIndex.getIndex(); } /** * enables the given capability * * @param c the capability to enable */ public void enable(Capability c) { m_Capabilities.enable(c); } /** * whether the given capability is enabled * * @param c the capability to enable * @return true if the capability is enabled */ public boolean isEnabled(Capability c) { return m_Capabilities.handles(c); } /** * disables the given capability * * @param c the capability to disable */ public void disable(Capability c) { m_Capabilities.disable(c); } /** * enables the given "not to have" capability * * @param c the capability to enable */ public void enableNot(Capability c) { m_NotCapabilities.enable(c); } /** * whether the given "not to have" capability is enabled * * @param c the capability to enable * @return true if the capability is enabled */ public boolean isEnabledNot(Capability c) { return m_NotCapabilities.handles(c); } /** * disables the given "not to have" capability * * @param c the capability to disable */ public void disableNot(Capability c) { m_NotCapabilities.disable(c); } /** * returns true if the given capability can be handled * * @param c the capability to check * @return true if the capability can be handled */ public boolean handles(Capability c) { return m_Capabilities.handles(c); } /** * The capabilities to search for. * * @return the capabilities to search for * @see Capabilities */ public Capabilities getCapabilities() { return m_Capabilities; } /** * Uses the given Capabilities for the search. * * @param c the capabilities to use for the search */ public void setCapabilities(Capabilities c) { m_Capabilities = (Capabilities) c.clone(); } /** * The "not to have" capabilities to search for. * * @return the capabilities to search for * @see Capabilities */ public Capabilities getNotCapabilities() { return m_NotCapabilities; } /** * Uses the given "not to have" Capabilities for the search. * * @param c the capabilities to use for the search */ public void setNotCapabilities(Capabilities c) { m_NotCapabilities = (Capabilities) c.clone(); } /** * returns the matches from the last find call * * @return the matching classname from the last find run */ public Vector getMatches() { return m_Matches; } /** * returns the misses from the last find call * * @return the classnames that didn't match from the last find run */ public Vector getMisses() { return m_Misses; } /** * returns a list with all the classnames that fit the criteria * * @return contains all classnames that fit the criteria */ public Vector find() { Vector list; int i; Class cls; Object obj; CapabilitiesHandler handler; boolean fits; Capabilities caps; m_Matches = new Vector(); m_Misses = new Vector(); list = ClassDiscovery.find(m_Superclass, (String[]) m_Packages.toArray(new String[m_Packages.size()])); for (i = 0; i < list.size(); i++) { try { cls = Class.forName((String) list.get(i)); obj = cls.newInstance(); // exclude itself if (cls == this.getClass()) continue; // really a CapabilitiesHandler? if (!(obj instanceof CapabilitiesHandler)) continue; // check capabilities enumeration handler = (CapabilitiesHandler) obj; caps = handler.getCapabilities(); fits = true; for (Capability cap: Capability.values()) { if (m_Capabilities.handles(cap)) { if (!(caps.handles(cap))) { fits = false; break; } } } if (!fits) { m_Misses.add(list.get(i)); continue; } // check "not" list for (Capability cap: Capability.values()) { if (m_NotCapabilities.handles(cap)) { if ((caps.handles(cap))) { fits = false; break; } } } if (!fits) { m_Misses.add(list.get(i)); continue; } // other stuff if (caps.getMinimumNumberInstances() > m_Capabilities.getMinimumNumberInstances()) { m_Misses.add(list.get(i)); continue; } // matches all criteria! m_Matches.add(list.get(i)); } catch (Exception e) { // ignore } } return m_Matches; } /** * Executes the location of classes with parameters from the commandline. * * @param args the commandline parameters */ public static void main(String[] args) { FindWithCapabilities find; Vector list; String result; int i; boolean printMisses; Iterator iter; boolean first; printMisses = false; try { find = new FindWithCapabilities(); try { printMisses = Utils.getFlag("misses", args); find.setOptions(args); Utils.checkForRemainingOptions(args); } catch (Exception ex) { result = ex.getMessage() + "\n\n" + find.getClass().getName().replaceAll(".*\\.", "") + " Options:\n\n"; Enumeration enm = find.listOptions(); while (enm.hasMoreElements()) { Option option = (Option) enm.nextElement(); result += option.synopsis() + "\n" + option.description() + "\n"; } throw new Exception(result); } System.out.println("\nSearching for the following Capabilities:"); // allowed System.out.print("- allowed: "); iter = find.getCapabilities().capabilities(); first = true; while (iter.hasNext()) { if (!first) System.out.print(", "); first = false; System.out.print(iter.next()); } System.out.println(); // not allowed System.out.print("- not allowed: "); iter = find.getNotCapabilities().capabilities(); first = true; if (iter.hasNext()) { while (iter.hasNext()) { if (!first) System.out.print(", "); first = false; System.out.print(iter.next()); } System.out.println(); } else { System.out.println("-"); } find.find(); // matches list = find.getMatches(); if (list.size() == 1) System.out.println("\nFound " + list.size() + " class that matched the criteria:\n"); else System.out.println("\nFound " + list.size() + " classes that matched the criteria:\n"); for (i = 0; i < list.size(); i++) System.out.println(list.get(i)); // misses if (printMisses) { list = find.getMisses(); if (list.size() == 1) System.out.println("\nFound " + list.size() + " class that didn't match the criteria:\n"); else System.out.println("\nFound " + list.size() + " classes that didn't match the criteria:\n"); for (i = 0; i < list.size(); i++) System.out.println(list.get(i)); } System.out.println(); } catch (Exception ex) { System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -