Test.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 6k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    Test.java
  3.  *    Copyright (C) 2000 Gabi Schmidberger.
  4.  *
  5.  *    This program is free software; you can redistribute it and/or modify
  6.  *    it under the terms of the GNU General Public License as published by
  7.  *    the Free Software Foundation; either version 2 of the License, or
  8.  *    (at your option) any later version.
  9.  *
  10.  *    This program is distributed in the hope that it will be useful,
  11.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *    GNU General Public License for more details.
  14.  *
  15.  *    You should have received a copy of the GNU General Public License
  16.  *    along with this program; if not, write to the Free Software
  17.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19. package weka.datagenerators;
  20. import weka.core.Instance;
  21. import weka.core.Instances;
  22. import weka.core.Attribute;
  23. import weka.core.OptionHandler;
  24. import weka.core.Option;
  25. import weka.core.Utils;
  26. import java.io.Serializable;
  27. import java.util.Random;
  28. import java.util.Enumeration;
  29. import java.util.Vector;
  30. /** 
  31.  * Class to represent a test.<br>
  32.  *<br>
  33.  * The string representation of the test can be supplied in standard notation
  34.  * or for a subset of types of attributes  in Prolog notation.<br>
  35.  *
  36.  * Following examples for all possible tests that can be represented by
  37.  * this class, given in standard notation.<br>
  38.  *<br>
  39.  * Examples of tests for numeric attributes:<br>
  40.  * B >= 2.333<br>        B < 4.56<br>
  41.  *<br>
  42.  * Examples of tests for nominal attributes with more then 2 values:<br>
  43.  * A = rain <br>            A != rain<br>
  44.  *<br>
  45.  * Examples of tests for nominal attribute with exactly 2 values:<br>
  46.  * A = false <br>            A = true<br>
  47.  *<br>
  48.  *<br>
  49.  * The Prolog notation is only supplied for numeric attributes and
  50.  * for nominal attributes that have the values "true" and "false".<br>
  51.  * <br>
  52.  * Following examples for the Prolog notation provided.<br>
  53.  *<br>
  54.  * Examples of tests for numeric attributes:<br>
  55.  * The same as for standard notation above.<br>
  56.  *<br>
  57.  * Examples of tests for nominal attributes with values "true"and "false":<br>
  58.  * A<br>
  59.  * not(A)<br>
  60.  *<br>
  61.  * (Other nominal attributes are not supported by the Prolog notation.)<br>
  62.  *<br>
  63.  *
  64.  * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz)
  65.  * @version $Revision: 1.1 $ 
  66.  **/
  67. public class Test implements Serializable {
  68.   int m_AttIndex;
  69.   double m_Split;
  70.   boolean m_Not;
  71.   Instances m_Dataset;
  72.   /** 
  73.    * Constructor 
  74.    */
  75.   Test(int i, double s, Instances dataset) { 
  76.     m_AttIndex = i; 
  77.     m_Split = s;
  78.     m_Dataset = dataset;
  79.    
  80.     m_Not = false;
  81.   }
  82.   /** 
  83.    * Constructor 
  84.    */
  85.   Test(int i, double s, Instances dataset, boolean n) {
  86.     m_AttIndex = i;
  87.     m_Split = s;
  88.     m_Dataset = dataset;
  89.     m_Not = n;
  90.   }
  91.   /** 
  92.    * Negates the test.
  93.    *
  94.    * @return the test itself negated
  95.    */
  96.   public Test getNot() { // returns a modified copy
  97.     return new Test(m_AttIndex, m_Split, m_Dataset, m_Not ? false : true);
  98.     }
  99.   /**
  100.    * Determines whether an instance passes the test.
  101.    *
  102.    * @param inst the instance
  103.    * @return true if the instance satisfies the test, false otherwise
  104.    * @exception Exception if something goes wrong
  105.    */   
  106.   public boolean passesTest(Instance inst) throws Exception {
  107.     if (inst.isMissing(m_AttIndex)) return false; // missing values fail
  108.     boolean isNominal = inst.attribute(m_AttIndex).isNominal();
  109.     double attribVal = inst.value(m_AttIndex);
  110.     if (!m_Not) {
  111.       if (isNominal) {
  112.         if (((int) attribVal) != ((int) m_Split)) return false;
  113.       }
  114.       else if (attribVal >= m_Split) return false;
  115.     } else {
  116.       if (isNominal) {
  117. if (((int) attribVal) == ((int) m_Split)) return false;
  118.       }
  119.       else if (attribVal < m_Split) return false;
  120.     }
  121.     return true;
  122.   }
  123.   /**
  124.    * Returns the test represented by a string.
  125.    *
  126.    * @return a string representing the test
  127.    */   
  128.   public String toString() {
  129.     return (m_Dataset.attribute(m_AttIndex).name() + " " +
  130.     testComparisonString());
  131.   }
  132.   /**
  133.    * Returns the test represented by a string in Prolog notation.
  134.    *
  135.    * @return a string representing the test in Prolog notation
  136.    */   
  137.   public String toPrologString() {
  138.     Attribute att = m_Dataset.attribute(m_AttIndex);
  139.     StringBuffer str = new StringBuffer();
  140.     String attName = m_Dataset.attribute(m_AttIndex).name();
  141.     if (att.isNumeric()) {
  142.       str = str.append(attName + " ");
  143.       if (m_Not) str = str.append(">= " + Utils.doubleToString(m_Split, 3));
  144.       else str = str.append("< " + Utils.doubleToString(m_Split, 3));
  145.     } else {
  146.       String value = att.value((int)m_Split);
  147.     
  148.       if (value == "false") { str = str.append("not(" + attName + ")"); }      
  149.       else { str = str.append(attName); }
  150.     }
  151.   return str.toString();
  152.   }
  153.   /**
  154.    * Gives a string representation of the test, starting from the comparison
  155.    * symbol.
  156.    *
  157.    * @return a string representing the test
  158.    */   
  159.   private String testComparisonString() {
  160.     Attribute att = m_Dataset.attribute(m_AttIndex);
  161.     if (att.isNumeric()) {
  162.       return ((m_Not ? ">= " : "< ") + Utils.doubleToString(m_Split,3));
  163.     }
  164.     else {
  165.       if (att.numValues() != 2) 
  166.         return ((m_Not ? "!= " : "= ") + att.value((int)m_Split));
  167.       else return ("= " 
  168.                    + (m_Not ?
  169.       att.value((int)m_Split == 0 ? 1 : 0) : att.value((int)m_Split)));
  170.     }
  171.   }
  172.   /**
  173.    * Gives a string representation of the test in Prolog notation, starting
  174.    * from the comparison symbol.
  175.    *
  176.    * @return a string representing the test in Prolog notation
  177.    */   
  178.   private String testPrologComparisonString() {
  179.     Attribute att = m_Dataset.attribute(m_AttIndex);
  180.     if (att.isNumeric()) {
  181.       return ((m_Not ? ">= " : "< ") + Utils.doubleToString(m_Split,3));
  182.     }
  183.     else {
  184.       if (att.numValues() != 2) 
  185.         return ((m_Not ? "!= " : "= ") + att.value((int)m_Split));
  186.       else return ("= " 
  187.                    + (m_Not ? att.value((int)m_Split == 0 ? 1 : 0) 
  188.                           : att.value((int)m_Split)));
  189.     }
  190.   }
  191.   /**
  192.    * Compares the test with the test that is given as parameter.
  193.    *
  194.    * @param t the test the object is compared to
  195.    * @return true if the two Tests are equal 
  196.    */   
  197.   public boolean equalTo(Test t) {
  198.     return (m_AttIndex == t.m_AttIndex && m_Split == t.m_Split && m_Not == t.m_Not);
  199.   }
  200.     
  201. }