1   /*
2     wsmo4j - a WSMO API and Reference Implementation
3   
4   
5    Copyright (c) 2005, University of Innsbruck, Austria
6   
7    This library is free software; you can redistribute it and/or modify it under
8    the terms of the GNU Lesser General Public License as published by the Free
9    Software Foundation; either version 2.1 of the License, or (at your option)
10   any later version.
11   This library is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14   details.
15   You should have received a copy of the GNU Lesser General Public License along
16   with this library; if not, write to the Free Software Foundation, Inc.,
17   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package test.wsmo4j.logicalexpression;
20  
21  import org.omwg.logicalexpression.*;
22  import org.omwg.logicalexpression.terms.ConstructedTerm;
23  import org.omwg.logicalexpression.terms.NumberedAnonymousID;
24  import org.omwg.ontology.Axiom;
25  import org.omwg.ontology.Variable;
26  import org.wsmo.common.IRI;
27  import org.wsmo.common.UnnumberedAnonymousID;
28  
29  /**
30   * Checks Equality Method of terms and different logical expressions
31   * 
32   * <pre>
33   * 
34   *  
35   *   Created on Jul 24, 2005
36   *   Committed by $Author$
37   *   $Source$,
38   *   
39   *  
40   * </pre>
41   * 
42   * @author Holger Lausen (holger.lausen@deri.org)
43   * @version $Revision$ $Date$
44   */
45  public class EqualityTest extends LogicalExpressionTestCase {
46  
47      public void testAnonymous() throws Exception {
48          // numbered are same
49          NumberedAnonymousID n2 = leFactory.createAnonymousID((byte)2);
50          NumberedAnonymousID n2_ = leFactory.createAnonymousID((byte)2);
51          assertEquals(n2, n2_);
52          assertEquals(n2.hashCode(), n2_.hashCode());
53  
54          // different numbered are different
55          NumberedAnonymousID n3 = leFactory.createAnonymousID((byte)3);
56          assertFalse(n2.equals(n3));
57  
58          
59          // unnumbered are different
60          UnnumberedAnonymousID aid1 = factory.createAnonymousID();
61          UnnumberedAnonymousID aid2 = factory.createAnonymousID();
62          assertFalse(aid1.equals(aid2));
63          
64          //testing anonids in more complex expressions
65          CompoundMolecule molecule1 = (CompoundMolecule) leFactory.createLogicalExpression(
66                  "i[a hasValue _#, b hasValue _#]. ", topEntity);
67          CompoundMolecule molecule2 = (CompoundMolecule) leFactory.createLogicalExpression(
68                  "i[a hasValue _#, b hasValue _#]. ", topEntity);
69          assertFalse(molecule1.equals(molecule2));
70          
71          //testing reusing refs to one id in multiple places
72          //FIXME! anon refs in more then one object
73          UnnumberedAnonymousID aid = factory.createAnonymousID();
74          Axiom axiom1 = factory.createAxiom(aid);
75          axiom1.addDefinition(molecule1);
76          Axiom axiom2 = factory.createAxiom(aid);
77          axiom1.addDefinition(molecule2);
78          //assertFalse(axiom1.equals(axiom2));
79      }
80  
81      public void testVariable() throws Exception {
82          Binary b = (Binary) leFactory.createLogicalExpression(
83                  "?i memberOf ?i and _int(?n). ", topEntity);
84          Molecule m = (Molecule) b.getLeftOperand();
85          Variable n = (Variable) ((Atom) b.getRightOperand()).getParameter(0);
86          Variable i1 = (Variable) m.getRightParameter();
87          Variable i2 = (Variable) m.getLeftParameter();
88          assertEquals(i1, i2);
89          assertTrue(!n.equals(i1));
90          
91          assertEquals(i1.hashCode(), i2.hashCode());
92      }
93  
94      public void testIRI() throws Exception {
95          Binary or = (Binary) leFactory.createLogicalExpression("(a and a) or c. ",
96                  topEntity);
97          Binary and = (Binary) or.getLeftOperand();
98          IRI c = (IRI) ((Atom) or.getRightOperand()).getIdentifier();
99          IRI a1 = (IRI) ((Atom) and.getLeftOperand()).getIdentifier();
100         IRI a2 = (IRI) ((Atom) and.getRightOperand()).getIdentifier();
101         assertEquals(a1, a2);
102         assertTrue(!a1.equals(c));
103         assertEquals(a1.hashCode(), a2.hashCode());
104     }
105 
106     public void testConstructedTerm() throws Exception {
107         Binary or = (Binary) leFactory.createLogicalExpression(
108                 "a(b(1,2.0,\"jj\"),e) or a(b(1,2.0,\"jj\"),e). ", topEntity);
109         ConstructedTerm ct1 = (ConstructedTerm) ((Atom) or.getLeftOperand())
110                 .getParameter(0);
111         ConstructedTerm ct2 = (ConstructedTerm) ((Atom) or.getRightOperand())
112                 .getParameter(0);
113         assertEquals(ct1, ct2);
114         assertEquals(ct1.hashCode(),ct2.hashCode());
115 
116         or = (Binary) leFactory.createLogicalExpression(
117                 "a(b(1,2.00,\"jj\"),e) or a(b(1,2.0,\"jj\"),e). ", topEntity);
118         ct1 = (ConstructedTerm) ((Atom) or.getLeftOperand()).getParameter(0);
119         ct2 = (ConstructedTerm) ((Atom) or.getRightOperand()).getParameter(0);
120         assertFalse(ct1.equals(ct2));
121         
122     }
123     
124     public void testSimpleMolecule() throws Exception{
125         Molecule m1 = (Molecule) leFactory.createLogicalExpression(
126                 "a memberOf c. ", topEntity);
127         Molecule m2 = (Molecule) leFactory.createLogicalExpression(
128                 "a memberOf c. ", topEntity);
129         assertEquals(m1, m2);
130         assertEquals(m1.hashCode(), m2.hashCode());
131 
132         m1 = (Molecule) leFactory.createLogicalExpression(
133                 "a subConceptOf c. ", topEntity);
134         m2 = (Molecule) leFactory.createLogicalExpression(
135                 "a subConceptOf c. ", topEntity);
136         assertEquals(m1, m2);
137         assertEquals(m1.hashCode(), m2.hashCode());
138         
139         m1 = (Molecule) leFactory.createLogicalExpression(
140                 "a[d hasValue c]. ", topEntity);
141         m2 = (Molecule) leFactory.createLogicalExpression(
142                 "a[d hasValue c]. ", topEntity);
143         assertEquals(m1, m2);
144         assertEquals(m1.hashCode(), m2.hashCode());
145 
146         m1 = (Molecule) leFactory.createLogicalExpression(
147                 "a[d ofType c]. ", topEntity);
148         m2 = (Molecule) leFactory.createLogicalExpression(
149                 "a[d ofType c]. ", topEntity);
150         assertEquals(m1, m2);
151         assertEquals(m1.hashCode(), m2.hashCode());
152 
153         m1 = (Molecule) leFactory.createLogicalExpression(
154                 "a[d impliesType c]. ", topEntity);
155         m2 = (Molecule) leFactory.createLogicalExpression(
156                 "a[d impliesType c]. ", topEntity);
157         assertEquals(m1, m2);
158         assertEquals(m1.hashCode(), m2.hashCode());
159 }
160 
161     public void testMolecule() throws Exception {
162         CompoundMolecule m1 = (CompoundMolecule) leFactory.createLogicalExpression(
163                 "a[f hasValue x(bc), b hasValue {c,f}] memberOf {b,c}. ", topEntity);
164         CompoundMolecule m2 = (CompoundMolecule) leFactory.createLogicalExpression(
165                 "a[b hasValue {f,c}, f hasValue x(bc)] memberOf {b,c}. ", topEntity);
166         assertEquals(m1, m2);
167         assertEquals(m1.hashCode(), m2.hashCode());
168 
169         m1 = (CompoundMolecule) leFactory.createLogicalExpression(
170                 "b[b hasValue {c,f}] memberOf {b,c}. ", topEntity);
171         m2 = (CompoundMolecule) leFactory.createLogicalExpression(
172                 "b[b hasValue {c,f}] memberOf {c}. ", topEntity);
173         assertFalse(m1.equals(m2));
174     }
175 
176     public void testAtom() throws Exception {
177         Atom a1 = (Atom) leFactory.createLogicalExpression("a(a,b(a)).", topEntity);
178         Atom a2 = (Atom) leFactory.createLogicalExpression("a(a,b(a)).", topEntity);
179         assertEquals(a1, a2);
180         assertEquals(a1.hashCode(), a2.hashCode());
181 
182         a2 = (Atom) leFactory.createLogicalExpression("b(a,b(a)).", topEntity);
183         assertFalse(a1.equals(a2));
184     }
185 
186     public void testBinary() throws Exception {
187         Binary b1 = (Binary) leFactory.createLogicalExpression("a and b.", topEntity);
188         Binary b2 = (Binary) leFactory.createLogicalExpression("b and a.", topEntity);
189         assertEquals(b1, b2);
190         assertEquals(b1.hashCode(), b2.hashCode());
191 
192         b1 = (Binary) leFactory.createLogicalExpression("a implies b.", topEntity);
193         b2 = (Binary) leFactory.createLogicalExpression("b implies a.", topEntity);
194         assertFalse(b1.equals(b2));
195         
196         b1 = (Binary) leFactory.createLogicalExpression("a and b.", topEntity);
197         b2 = (Binary) leFactory.createLogicalExpression("a or b.", topEntity);
198         assertFalse(b1.equals(b2));
199     }
200 
201     public void testUnary() throws Exception {
202         Unary u1 = (Unary) leFactory.createLogicalExpression("neg b.", topEntity);
203         Unary u2 = (Unary) leFactory.createLogicalExpression("neg (b).", topEntity);
204         assertEquals(u1, u2);
205         assertEquals(u1.hashCode(), u2.hashCode());
206 
207         u1 = (Unary) leFactory.createLogicalExpression("neg b.", topEntity);
208         u2 = (Unary) leFactory.createLogicalExpression("naf b.", topEntity);
209         assertFalse(u1.equals(u2));
210     }
211 
212     public void testQuantified() throws Exception {
213 
214         Quantified q1 = (Quantified) leFactory.createLogicalExpression(
215                 "forall {?a,?b} (neg ?a memberOf b).", topEntity);
216         Quantified q2 = (Quantified) leFactory.createLogicalExpression(
217                 "forall {?b,?a} (neg ?a memberOf b).", topEntity);
218         assertEquals(q1, q2);
219         assertEquals(q1.hashCode(), q2.hashCode());
220 
221         q1 = (Quantified) leFactory.createLogicalExpression(
222                 "forall {?a,?b} (neg ?a memberOf b).", topEntity);
223         q2 = (Quantified) leFactory.createLogicalExpression(
224                 "exists {?b,?a} (neg ?a memberOf b).", topEntity);
225         assertFalse(q1.equals(q2));
226 
227         q1 = (Quantified) leFactory.createLogicalExpression(
228                 "forall {?a,?b} (neg ?a memberOf b).", topEntity);
229         q2 = (Quantified) leFactory.createLogicalExpression(
230                 "forall {?b,?c} (neg ?a memberOf b).", topEntity);
231         assertFalse(q1.equals(q2));
232     }
233 
234     public void testComplex() throws Exception {
235         LogicalExpression l1 = (LogicalExpression) leFactory.createLogicalExpression(
236                 "  a and b or a[b hasValue c] and a[b ofType c] memberOf d and test(?x) :- t implies z impliedBy c(f(a)).", topEntity);
237         LogicalExpression l2 = (LogicalExpression) leFactory.createLogicalExpression(
238                 "  a and b or a[b hasValue c] and a[b ofType c] memberOf d and test(?x) :- t implies z impliedBy c(f(a)).", topEntity);
239         assertEquals(l1, l2);
240         assertEquals(l1.hashCode(), l2.hashCode());
241     }
242 }