[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 | * Head.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.Instances; |
---|
| 29 | import weka.core.RevisionUtils; |
---|
| 30 | |
---|
| 31 | import java.util.Iterator; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Class representing the head of a rule. |
---|
| 35 | * |
---|
| 36 | * @author <a href="mailto:adeltour@netcourrier.com">Amelie Deltour</a> |
---|
| 37 | * @version $Revision: 1.6 $ |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | public class Head |
---|
| 41 | extends LiteralSet { |
---|
| 42 | |
---|
| 43 | /** for serialization */ |
---|
| 44 | private static final long serialVersionUID = 5068076274253706199L; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Constructor without storing the counter-instances. |
---|
| 48 | */ |
---|
| 49 | public Head() { |
---|
| 50 | |
---|
| 51 | super(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * Constructor storing the counter-instances. |
---|
| 56 | * |
---|
| 57 | * @param instances The dataset. |
---|
| 58 | */ |
---|
| 59 | public Head(Instances instances) { |
---|
| 60 | |
---|
| 61 | super(instances); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Test if an instance can be kept as a counter-instance, |
---|
| 66 | * if a new literal is added to this head. |
---|
| 67 | * |
---|
| 68 | * @param instance The instance to test. |
---|
| 69 | * @param newLit The new literal. |
---|
| 70 | * @return True if the instance is still a counter-instance |
---|
| 71 | * (if the negation of the new literal satisfies the instance). |
---|
| 72 | */ |
---|
| 73 | public boolean canKeep(Instance instance, Literal newLit) { |
---|
| 74 | return newLit.negationSatisfies(instance); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Test if this Head is included in a rule. |
---|
| 79 | * It is the literals of this Head are contained in the head of the other rule, |
---|
| 80 | * or if their negation is included in the body of the other rule. |
---|
| 81 | */ public boolean isIncludedIn(Rule otherRule) { |
---|
| 82 | Iterator iter = this.enumerateLiterals(); |
---|
| 83 | while (iter.hasNext()) { |
---|
| 84 | Literal current = (Literal) iter.next(); |
---|
| 85 | if (!otherRule.headContains(current) |
---|
| 86 | && !otherRule.bodyContains(current.getNegation())) { |
---|
| 87 | return false; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | return true; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Gives a String representation of this set of literals as a disjunction. |
---|
| 95 | */ |
---|
| 96 | public String toString() { |
---|
| 97 | Iterator iter = this.enumerateLiterals(); |
---|
| 98 | |
---|
| 99 | if (!iter.hasNext()) { |
---|
| 100 | return "FALSE"; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | StringBuffer text = new StringBuffer(); |
---|
| 104 | while (iter.hasNext()) { |
---|
| 105 | text.append(iter.next().toString()); |
---|
| 106 | if (iter.hasNext()) { |
---|
| 107 | text.append(" or "); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | return text.toString(); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Returns the revision string. |
---|
| 115 | * |
---|
| 116 | * @return the revision |
---|
| 117 | */ |
---|
| 118 | public String getRevision() { |
---|
| 119 | return RevisionUtils.extract("$Revision: 1.6 $"); |
---|
| 120 | } |
---|
| 121 | } |
---|