001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * Body.java
019: * Copyright (C) 2003 Peter A. Flach, Nicolas Lachiche
020: *
021: * Thanks to Amelie Deltour for porting the original C code to Java
022: * and integrating it into Weka.
023: */
024:
025: package weka.associations.tertius;
026:
027: import weka.core.Instance;
028: import weka.core.Instances;
029: import java.util.Iterator;
030:
031: /**
032: * Class representing the body of a rule.
033: *
034: * @author <a href="mailto:adeltour@netcourrier.com">Amelie Deltour</a>
035: * @version $Revision: 1.5 $
036: */
037:
038: public class Body extends LiteralSet {
039:
040: /** for serialization */
041: private static final long serialVersionUID = 4870689270432218016L;
042:
043: /**
044: * Constructor without storing the counter-instances.
045: */
046: public Body() {
047:
048: super ();
049: }
050:
051: /**
052: * Constructor storing the counter-instances.
053: *
054: * @param instances The dataset.
055: */
056: public Body(Instances instances) {
057:
058: super (instances);
059: }
060:
061: /**
062: * Test if an instance can be kept as a counter-instance,
063: * if a new literal is added to this body.
064: *
065: * @param instance The instance to test.
066: * @param newLit The new literal.
067: * @return True if the instance is still a counter-instance
068: * (if the new literal satisfies the instance).
069: */
070: public boolean canKeep(Instance instance, Literal newLit) {
071:
072: return newLit.satisfies(instance);
073: }
074:
075: /**
076: * Test if this Body is included in a rule.
077: * It is the literals of this Body are contained in the body of the other rule,
078: * or if their negation is included in the head of the other rule.
079: */
080: public boolean isIncludedIn(Rule otherRule) {
081:
082: Iterator iter = this .enumerateLiterals();
083: while (iter.hasNext()) {
084: Literal current = (Literal) iter.next();
085: if (!otherRule.bodyContains(current)
086: && !otherRule.headContains(current.getNegation())) {
087: return false;
088: }
089: }
090: return true;
091: }
092:
093: /**
094: * Gives a String representation of this set of literals as a conjunction.
095: */
096: public String toString() {
097: Iterator iter = this .enumerateLiterals();
098:
099: if (!iter.hasNext()) {
100: return "TRUE";
101: }
102: StringBuffer text = new StringBuffer();
103: while (iter.hasNext()) {
104: text.append(iter.next().toString());
105: if (iter.hasNext()) {
106: text.append(" and ");
107: }
108: }
109: return text.toString();
110: }
111: }
|