source: branches/MetisMQI/src/main/java/weka/datagenerators/Test.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

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