| [4] | 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 | * AttributeValueLiteral.java |
|---|
| 19 | * Copyright (C) 2003 Peter A. Flach, Nicolas Lachiche |
|---|
| 20 | * |
|---|
| 21 | * Thanks to Amelie Deltour for porting the original C code to Java |
|---|
| 22 | * and integrating it into Weka. |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | package weka.associations.tertius; |
|---|
| 26 | |
|---|
| 27 | import weka.core.Instance; |
|---|
| 28 | import weka.core.RevisionUtils; |
|---|
| 29 | import weka.associations.Tertius; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * @author Peter A. Flach |
|---|
| 33 | * @author Nicolas Lachiche |
|---|
| 34 | * @version $Revision: 1.6 $ |
|---|
| 35 | */ |
|---|
| 36 | public class AttributeValueLiteral |
|---|
| 37 | extends Literal { |
|---|
| 38 | |
|---|
| 39 | /** for serialization */ |
|---|
| 40 | private static final long serialVersionUID = 4077436297281456239L; |
|---|
| 41 | |
|---|
| 42 | private String m_value; |
|---|
| 43 | |
|---|
| 44 | private int m_index; |
|---|
| 45 | |
|---|
| 46 | public AttributeValueLiteral(Predicate predicate, String value, |
|---|
| 47 | int index, int sign, int missing) { |
|---|
| 48 | |
|---|
| 49 | super(predicate, sign, missing); |
|---|
| 50 | m_value = value; |
|---|
| 51 | m_index = index; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public boolean satisfies(Instance instance) { |
|---|
| 55 | |
|---|
| 56 | if (m_index == -1) { |
|---|
| 57 | if (positive()) { |
|---|
| 58 | return instance.isMissing(getPredicate().getIndex()); |
|---|
| 59 | } else { |
|---|
| 60 | return !instance.isMissing(getPredicate().getIndex()); |
|---|
| 61 | } |
|---|
| 62 | } else if (instance.isMissing(getPredicate().getIndex())) { |
|---|
| 63 | if (positive()) { |
|---|
| 64 | return false; |
|---|
| 65 | } else { |
|---|
| 66 | return m_missing != Tertius.EXPLICIT; |
|---|
| 67 | } |
|---|
| 68 | } else { |
|---|
| 69 | if (positive()) { |
|---|
| 70 | return (instance.value(getPredicate().getIndex()) == m_index); |
|---|
| 71 | } else { |
|---|
| 72 | return (instance.value(getPredicate().getIndex()) != m_index); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public boolean negationSatisfies(Instance instance) { |
|---|
| 78 | |
|---|
| 79 | if (m_index == -1) { |
|---|
| 80 | if (positive()) { |
|---|
| 81 | return !instance.isMissing(getPredicate().getIndex()); |
|---|
| 82 | } else { |
|---|
| 83 | return instance.isMissing(getPredicate().getIndex()); |
|---|
| 84 | } |
|---|
| 85 | } else if (instance.isMissing(getPredicate().getIndex())) { |
|---|
| 86 | if (positive()) { |
|---|
| 87 | return m_missing != Tertius.EXPLICIT; |
|---|
| 88 | } else { |
|---|
| 89 | return false; |
|---|
| 90 | } |
|---|
| 91 | } else { |
|---|
| 92 | if (positive()) { |
|---|
| 93 | return (instance.value(getPredicate().getIndex()) != m_index); |
|---|
| 94 | } else { |
|---|
| 95 | return (instance.value(getPredicate().getIndex()) == m_index); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public String toString() { |
|---|
| 101 | |
|---|
| 102 | StringBuffer text = new StringBuffer(); |
|---|
| 103 | if (negative()) { |
|---|
| 104 | text.append("not "); |
|---|
| 105 | } |
|---|
| 106 | text.append(getPredicate().toString() + " = " + m_value); |
|---|
| 107 | return text.toString(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Returns the revision string. |
|---|
| 112 | * |
|---|
| 113 | * @return the revision |
|---|
| 114 | */ |
|---|
| 115 | public String getRevision() { |
|---|
| 116 | return RevisionUtils.extract("$Revision: 1.6 $"); |
|---|
| 117 | } |
|---|
| 118 | } |
|---|