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 | * Body.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 body 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 Body |
---|
41 | extends LiteralSet { |
---|
42 | |
---|
43 | /** for serialization */ |
---|
44 | private static final long serialVersionUID = 4870689270432218016L; |
---|
45 | |
---|
46 | /** |
---|
47 | * Constructor without storing the counter-instances. |
---|
48 | */ |
---|
49 | public Body() { |
---|
50 | |
---|
51 | super(); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Constructor storing the counter-instances. |
---|
56 | * |
---|
57 | * @param instances The dataset. |
---|
58 | */ |
---|
59 | public Body(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 body. |
---|
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 new literal satisfies the instance). |
---|
72 | */ |
---|
73 | public boolean canKeep(Instance instance, Literal newLit) { |
---|
74 | |
---|
75 | return newLit.satisfies(instance); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Test if this Body is included in a rule. |
---|
80 | * It is the literals of this Body are contained in the body of the other rule, |
---|
81 | * or if their negation is included in the head of the other rule. |
---|
82 | */ |
---|
83 | public boolean isIncludedIn(Rule otherRule) { |
---|
84 | |
---|
85 | Iterator iter = this.enumerateLiterals(); |
---|
86 | while (iter.hasNext()) { |
---|
87 | Literal current = (Literal) iter.next(); |
---|
88 | if (!otherRule.bodyContains(current) |
---|
89 | && !otherRule.headContains(current.getNegation())) { |
---|
90 | return false; |
---|
91 | } |
---|
92 | } |
---|
93 | return true; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Gives a String representation of this set of literals as a conjunction. |
---|
98 | */ |
---|
99 | public String toString() { |
---|
100 | Iterator iter = this.enumerateLiterals(); |
---|
101 | |
---|
102 | if (!iter.hasNext()) { |
---|
103 | return "TRUE"; |
---|
104 | } |
---|
105 | StringBuffer text = new StringBuffer(); |
---|
106 | while (iter.hasNext()) { |
---|
107 | text.append(iter.next().toString()); |
---|
108 | if (iter.hasNext()) { |
---|
109 | text.append(" and "); |
---|
110 | } |
---|
111 | } |
---|
112 | return text.toString(); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Returns the revision string. |
---|
117 | * |
---|
118 | * @return the revision |
---|
119 | */ |
---|
120 | public String getRevision() { |
---|
121 | return RevisionUtils.extract("$Revision: 1.6 $"); |
---|
122 | } |
---|
123 | } |
---|