1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2005, University of Innsbruck, Austria
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18  package test.wsmo4j.logicalexpression.io;
19  
20  import org.omwg.logicalexpression.*;
21  
22  import test.wsmo4j.logicalexpression.*;
23  
24  /**
25   * Testing serializer for correct presedence order and namespace handling
26   *
27   * <pre>
28   * Created on Jul 29, 2005
29   * Committed by $Author$
30   * </pre>
31   *
32   * @author Holger Lausen (holger.lausen@deri.org)
33   *
34   * @version $Revision$ $Date$
35   */
36  public class SerializingTest extends LogicalExpressionTestCase {
37      /**
38       * serializer Bug reported by Stephan Grimm.
39       * @throws Exception
40       */
41      public void testParseSerialize() throws Exception{
42          LogicalExpression le = leFactory.createLogicalExpression(
43                  "?x memberOf C impliedBy (?x memberOf A and ?x memberOf B). ",
44                  topEntity);
45          
46          String str = LogicalExpressionTestCase.serialize(le,topEntity);
47          //System.out.println(str);
48          
49          le = leFactory.createLogicalExpression(str, topEntity);
50          
51          Binary implBy = (Binary)le;
52          Molecule xmofC = (Molecule)implBy.getLeftOperand();
53          Binary mofAandmofB = (Binary)implBy.getRightOperand();
54          Molecule xmofA = (Molecule)mofAandmofB.getLeftOperand();
55          Molecule xmofB = (Molecule)mofAandmofB.getRightOperand();
56          
57          assertEquals(xmofC.getRightParameter().toString(),
58                  "http://ex.org#C");
59          assertEquals(xmofA.getRightParameter().toString(),
60                  "http://ex.org#A");
61          assertEquals(xmofB.getRightParameter().toString(),
62                  "http://ex.org#B");
63      }
64      
65      
66      public void testParseSerializeValues() throws Exception{
67          LogicalExpression le = leFactory.createLogicalExpression(
68                  "i[a1 hasValue \"a1\", " +
69                  "  a2 hasValue 2, " +
70                  "  a3 hasValue 3.3," +
71                  "  a4 hasValue _date(2005,12,12)]. ",
72                  topEntity);
73          
74          String str = LogicalExpressionTestCase.serialize(le,topEntity);
75          
76          System.out.println(str);
77          LogicalExpression le2 = leFactory.createLogicalExpression(str, topEntity);
78          
79          assertEquals(le, le2);
80      }
81      
82      public void testParseBugJan() throws Exception{
83          String test = "forall ?x ( \n" +
84                  " ?x memberOf Human and ageOfHuman(?x,?age) \n" +
85                  " and ?age =< 14 implies ?x memberOf Child). ";
86          LogicalExpression le1 = leFactory.createLogicalExpression(test,topEntity);
87          LogicalExpression le2 = leFactory.createLogicalExpression(test,topEntity);
88          assertEquals(le1, le2);
89          
90          String str = LogicalExpressionTestCase.serialize(le1,topEntity);
91          
92          //System.out.println(str);
93          
94          LogicalExpression le3 = leFactory.createLogicalExpression(str, topEntity);
95          assertEquals(le1,le3);
96      }
97      
98      public void testParseSerializeComplex() throws Exception{
99          String test = "(?x[a hasValue {_#1,c}, a1 ofType b2, a3 impliesType a4]memberOf {q,a(1,f(1,2.0,\"jj\"))} and (true or false) and ?x memberOf B \n" +
100                 " impliedBy ?x memberOf C) \n" +
101                 " impliedBy (?x memberOf D) and \n" +
102                 "((?x memberOf E impliedBy ?x subConceptOf {F,Z}) \n" +
103                 "impliedBy (?x memberOf G or ?x memberOf H)) \n" +
104                 "and naf a(1) and neg a(2,a(1)) .";
105         //test = "?x[a hasValue a, b ofType c] ";
106         LogicalExpression le1 = leFactory.createLogicalExpression(test,topEntity);
107         LogicalExpression le2 = leFactory.createLogicalExpression(test,topEntity);
108         assertEquals(le1, le2);
109         
110         String str = LogicalExpressionTestCase.serialize(le1,topEntity);
111         //System.out.println(str);
112         LogicalExpression le3 = leFactory.createLogicalExpression(str, topEntity);
113         assertEquals(le1,le3);
114     }
115     
116     public void testParseSerializePresedenceImpliedByAnd() throws Exception{
117         String test = "((?x memberOf A and ?x memberOf B impliedBy C)) and b. \n";
118         LogicalExpression le1 = leFactory.createLogicalExpression(test,topEntity);
119         LogicalExpression le2 = leFactory.createLogicalExpression(test,topEntity);
120         assertEquals(le1, le2);
121         
122         String str = LogicalExpressionTestCase.serialize(le1,topEntity);
123         //System.out.println(str);
124         LogicalExpression le3 = leFactory.createLogicalExpression(
125                 str, topEntity);
126         assertEquals(le1,le3);
127     }
128 
129     public void testPresendence() throws Exception{
130         String str="(e equivalent f) :- g. ";
131         LogicalExpression le = leFactory.createLogicalExpression(str, topEntity);     
132         String str2 = LogicalExpressionTestCase.serialize(le,topEntity);
133         LogicalExpression le2 = leFactory.createLogicalExpression(str2, topEntity);      
134         assertEquals(le, le2);
135     }
136 
137     public void testNestedQuantified() throws Exception{
138         String str="a and b and (forall ?c (?c memberOf b) or reto). ";
139         LogicalExpression le = leFactory.createLogicalExpression(str, topEntity);     
140         String str2 = LogicalExpressionTestCase.serialize(le,topEntity);
141         LogicalExpression le2 = leFactory.createLogicalExpression(str2, topEntity);      
142         //System.out.println(str2);
143         assertEquals(le, le2);
144     }
145     
146     public void testSerializeNullPointer() throws Exception{
147         String test = "?x memberOf c equivalent ?x memberOf d and ?x memberOf e or ?x memberOf f.";
148         LogicalExpression le = leFactory.createLogicalExpression(test,topEntity);
149         String str = LogicalExpressionTestCase.serialize(le,topEntity);
150     }
151     
152     public void testPrefixAsFullIRI() throws Exception{
153         String test = "_\""+Constants.NUMERIC_ADD+"\"(?x,1,2)";
154         LogicalExpression le = leFactory.createLogicalExpression(test,topEntity);
155         String str = LogicalExpressionTestCase.serialize(le,topEntity);
156         assertTrue(str.indexOf("numericAdd")>0);
157         leFactory.createLogicalExpression(str,topEntity);
158 
159     }
160 
161 }