~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/associations/tertius/Head.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

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