All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.datagenerators.Test Maven / Gradle / Ivy

/*
 *   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 3 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, see .
 */

/*
 * Test.java
 * Copyright (C) 2000-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.datagenerators;

import java.io.Serializable;

import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
import weka.core.Utils;

/**
 * Class to represent a test. 
*
* The string representation of the test can be supplied in standard notation or * for a subset of types of attributes in Prolog notation.
* * Following examples for all possible tests that can be represented by this * class, given in standard notation.
*
* Examples of tests for numeric attributes:
* B >= 2.333
* B < 4.56
*
* Examples of tests for nominal attributes with more then 2 values:
* A = rain
* A != rain
*
* Examples of tests for nominal attribute with exactly 2 values:
* A = false
* A = true
*
*
* The Prolog notation is only supplied for numeric attributes and for nominal * attributes that have the values "true" and "false".
*
* Following examples for the Prolog notation provided.
*
* Examples of tests for numeric attributes:
* The same as for standard notation above.
*
* Examples of tests for nominal attributes with values "true"and "false":
* A
* not(A)
*
* (Other nominal attributes are not supported by the Prolog notation.)
*
* * @author Gabi Schmidberger ([email protected]) * @version $Revision: 10203 $ **/ public class Test implements Serializable, RevisionHandler { /** for serialization */ static final long serialVersionUID = -8890645875887157782L; /** the attribute index */ int m_AttIndex; /** the split */ double m_Split; /** whether to negate the test */ boolean m_Not; /** the dataset */ Instances m_Dataset; /** * Constructor * * @param i the attribute index * @param s the split * @param dataset the dataset */ public Test(int i, double s, Instances dataset) { m_AttIndex = i; m_Split = s; m_Dataset = dataset; m_Not = false; } /** * Constructor * * @param i the attribute index * @param s the split * @param dataset the dataset * @param n whether to negate the test */ public Test(int i, double s, Instances dataset, boolean n) { m_AttIndex = i; m_Split = s; m_Dataset = dataset; m_Not = n; } /** * Negates the test. * * @return the test itself negated */ public Test getNot() { // returns a modified copy return new Test(m_AttIndex, m_Split, m_Dataset, m_Not ? false : true); } /** * Determines whether an instance passes the test. * * @param inst the instance * @return true if the instance satisfies the test, false otherwise * @throws Exception if something goes wrong */ public boolean passesTest(Instance inst) throws Exception { if (inst.isMissing(m_AttIndex)) { return false; // missing values fail } boolean isNominal = inst.attribute(m_AttIndex).isNominal(); double attribVal = inst.value(m_AttIndex); if (!m_Not) { if (isNominal) { if (((int) attribVal) != ((int) m_Split)) { return false; } } else if (attribVal >= m_Split) { return false; } } else { if (isNominal) { if (((int) attribVal) == ((int) m_Split)) { return false; } } else if (attribVal < m_Split) { return false; } } return true; } /** * Returns the test represented by a string. * * @return a string representing the test */ @Override public String toString() { return (m_Dataset.attribute(m_AttIndex).name() + " " + testComparisonString()); } /** * Returns the test represented by a string in Prolog notation. * * @return a string representing the test in Prolog notation */ public String toPrologString() { Attribute att = m_Dataset.attribute(m_AttIndex); StringBuffer str = new StringBuffer(); String attName = m_Dataset.attribute(m_AttIndex).name(); if (att.isNumeric()) { str = str.append(attName + " "); if (m_Not) { str = str.append(">= " + Utils.doubleToString(m_Split, 3)); } else { str = str.append("< " + Utils.doubleToString(m_Split, 3)); } } else { String value = att.value((int) m_Split); if (value == "false") { str = str.append("not(" + attName + ")"); } else { str = str.append(attName); } } return str.toString(); } /** * Gives a string representation of the test, starting from the comparison * symbol. * * @return a string representing the test */ private String testComparisonString() { Attribute att = m_Dataset.attribute(m_AttIndex); if (att.isNumeric()) { return ((m_Not ? ">= " : "< ") + Utils.doubleToString(m_Split, 3)); } else { if (att.numValues() != 2) { return ((m_Not ? "!= " : "= ") + att.value((int) m_Split)); } else { return ("= " + (m_Not ? att.value((int) m_Split == 0 ? 1 : 0) : att .value((int) m_Split))); } } } /** * Compares the test with the test that is given as parameter. * * @param t the test the object is compared to * @return true if the two Tests are equal */ public boolean equalTo(Test t) { return (m_AttIndex == t.m_AttIndex && m_Split == t.m_Split && m_Not == t.m_Not); } /** * Returns the revision string. * * @return the revision */ @Override public String getRevision() { return RevisionUtils.extract("$Revision: 10203 $"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy