1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004, 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 java.util.*;
21  
22  import org.omwg.logicalexpression.*;
23  import org.omwg.ontology.*;
24  import org.wsmo.wsml.*;
25  
26  import test.wsmo4j.logicalexpression.*;
27  
28  /**
29   * Testing the implementation of parse(String) that converts the string
30   * representation of a logical expression to a ObjectModel
31   * 
32   * @author Holger.lausen@deri.org
33   * @version $Revision$
34   */
35  public class ParsingTest extends LogicalExpressionTestCase {
36      
37      public void testNewQuantifier()throws Exception{
38          //parse builtin in in prefix correctly
39          String test = "forall ?x,?y (?x memberOf x and ?y memberOf y)." ;
40          UniversalQuantification c = (UniversalQuantification) leFactory.createLogicalExpression(test,topEntity);
41          assertEquals(2, c.listVariables().size());
42      }
43      
44      public void testDataTypePredicateInParsing()throws Exception{
45          //parse builtin in in prefix correctly
46          String test = "a[b hasValue ?y] and _\""+Constants.NUMERIC_ADD+"\"(?y,1,2)." ;
47          Conjunction c = (Conjunction) leFactory.createLogicalExpression(test,topEntity);
48          Atom a = (Atom)c.getRightOperand();
49          assertEquals(a.getIdentifier().toString(),Constants.NUMERIC_ADD);
50      }
51    
52      public void testDataTypePredicateRewrite()throws Exception{
53          String test;
54          LogicalExpression le;
55          
56          test = "a[b hasValue _\""+Constants.NUMERIC_EQUAL+"\"(1,2) ]. ";
57          System.out.println(test+"\n");
58          le = leFactory.createLogicalExpression(test,topEntity);
59          System.out.println(le);
60  
61          
62          test = "a[b hasValue (1+2*3+10/5+?y+?x*?z)] \n" +
63                  "and c((3+4-?x+f(c(a)))). ";
64          le = leFactory.createLogicalExpression(test,topEntity);
65          String le1 = le.toString();
66          le = leFactory.createLogicalExpression(le1,topEntity);
67          assertEquals(le1,le.toString());
68          assertTrue(le1.indexOf("2*3")>0);
69          test = "?x[intAttr1 hasValue ?a1, intAttr2 hasValue ?a2, " +
70                  "intAttr3 hasValue ?a3] and " +
71                  "?v=(?a1 + ?a2 + ?a3) implies ?x[sumAttr hasValue ?v].";
72          le = leFactory.createLogicalExpression(test,topEntity);
73          System.out.print(serialize(le,topEntity));
74          
75          test = "holger and (reto or jos[age hasValue (20+3)]) .";
76          le = leFactory.createLogicalExpression(test,topEntity);
77          System.out.print(serialize(le,topEntity));
78          
79  //        IRI plus = factory.createIRI(Constants.NUMERIC_ADD);
80  //        IRI mal = factory.createIRI(Constants.NUMERIC_MUL);
81  //        
82  //        DataFactory df = new DataFactoryImpl();
83  //        
84  //        List l1 = new LinkedList();
85  //        l1.add(df.createWsmlInteger("1"));
86  //        l1.add(df.createWsmlInteger("2"));
87  //        
88  //        List l2 = new LinkedList();
89  //        l2.add(df.createWsmlInteger("3"));
90  //        l2.add(leFactory.createConstructedTerm(mal,l1));
91  //        
92  //        List l3 = new LinkedList();
93  //        l3.add(leFactory.createConstructedTerm(plus,l2));
94  //        le = leFactory.createAtom(plus, l3) ;
95  //        System.out.print("\n\n\n"+le);
96              
97      }
98      
99      public void testBuiltInAtom() throws Exception{
100         //check numeric equal
101         Atom equal = (Atom) leFactory.createLogicalExpression("(?x = 2). ", topEntity);
102         //System.out.println(equal);
103         assertTrue(equal instanceof BuiltInAtom);
104 
105         equal = (Atom) leFactory.createLogicalExpression(" (4 = 3). ", topEntity);
106         assertEquals(Constants.NUMERIC_EQUAL, 
107                 equal.getIdentifier().toString());
108 
109         equal = (Atom) leFactory.createLogicalExpression(" (4.0 != 3). ", topEntity);
110         assertEquals(Constants.NUMERIC_INEQUAL, 
111                 equal.getIdentifier().toString());
112 
113         equal = (Atom) leFactory.createLogicalExpression(" (4.0 = \"3\"). ", topEntity);
114         assertEquals(Constants.EQUAL, 
115                 equal.getIdentifier().toString());
116 
117         equal = (Atom) leFactory.createLogicalExpression(" (\"4.0\" = \"3\"). ", topEntity);
118         assertEquals(Constants.STRING_EQUAL, 
119                 equal.getIdentifier().toString());
120     }
121     
122     public void testBuiltInStrongEqual() throws Exception{
123         Atom strongEqual = (Atom) leFactory.createLogicalExpression("b :=: a. ", topEntity);
124         assertTrue(strongEqual instanceof BuiltInAtom);
125         assertEquals(Constants.STRONG_EQUAL,
126                 strongEqual.getIdentifier().toString());
127         String str = strongEqual.toString();
128         Atom strongEqual2 = (Atom) leFactory.createLogicalExpression(str);
129         assertEquals(strongEqual,strongEqual2);
130         //System.out
131     }
132     
133     public void testBuiltInPredicate() throws Exception{
134         LogicalExpression mol = leFactory.createLogicalExpression("a[b hasValue (3+4)]", topEntity);
135         System.out.println(mol);
136     }
137     
138     /*
139      * testing presedence with brackets
140      */
141     public void testAndBrackets() throws Exception {
142         Binary b = (Binary) leFactory.createLogicalExpression(
143                 "a and (b or c).  ",topEntity);
144         assertTrue(b instanceof Conjunction);
145     }
146 
147     /**
148      * presedence is currently: 
149      * -NEG,NAF (strongest) 
150      * -AND 
151      * -OR 
152      * -IMPLIES | - impliedBy | equivalent | LP_IMPLICATION (the latest occurence binds stronger) 
153      * -Constraint (lowest)
154      */
155     public void testPresendence() throws Exception {
156         String str1 = "((((((neg a) and (naf b)) or c) implies d) impliedBy e) equivalent f) :- g. ";
157         LogicalExpression le1 = leFactory.createLogicalExpression(str1, topEntity);
158         String str2 = "neg a and naf b or c implies d impliedBy e equivalent f :- g. ";
159         LogicalExpression le2 = leFactory.createLogicalExpression(str2, topEntity);
160         assertEquals(le1, le2);
161     }
162     
163     /**
164      * checking latest element binds strongest behaviour
165      */
166     public void testLatestImplicationBindsStrongest() throws Exception {
167         String str1 = "a implies b impliedBy c.";
168         LogicalExpression le1 = leFactory.createLogicalExpression(str1, topEntity);
169         assertTrue(Arrays.asList(le1.getClass().getInterfaces()).contains(InverseImplication.class));
170         String str2 = "a impliedBy b implies c.";
171         LogicalExpression le2 = leFactory.createLogicalExpression(str2, topEntity);
172         assertTrue(le2 instanceof Implication);
173     }
174     
175     public void testDot() throws Exception{
176         leFactory.createLogicalExpression("a and\n b", topEntity);
177         leFactory.createLogicalExpression("a and\n b.", topEntity);
178         leFactory.createLogicalExpression("a and\n b.   ", topEntity);
179     }
180     
181     public void testException() throws Exception{
182              
183         try{
184             leFactory.createLogicalExpression(" a and\n ?b.  ", topEntity);
185         }catch (ParserException pe){
186             assertEquals(pe.getErrorLine(),2);
187             assertEquals(pe.getErrorPos(),2);
188         }
189 
190         try{
191             leFactory.createLogicalExpression(
192                     " c and "+Constants.IRI_DEL_START+WsmlDataType.WSML_DECIMAL+Constants.IRI_DEL_END+
193                     "(20,12,2005) .  ", topEntity);
194         }catch (ParserException pe){
195             assertEquals(1,pe.getErrorLine());
196             assertEquals(55,pe.getErrorPos());
197         }
198         
199         try{
200             leFactory.createLogicalExpression(
201                     " c and 1 .  ", topEntity);
202         }catch (ParserException pe){
203             assertEquals(pe.getErrorLine(),1);
204             assertEquals(pe.getErrorPos(),8);
205         }
206 
207         try{
208             leFactory.createLogicalExpression(
209                     " _date(20,20,2005) and (b or c).  ",
210                     topEntity);
211         }catch (ParserException pe){
212             assertEquals(1,pe.getErrorLine());
213             assertEquals(7,pe.getErrorPos());
214         }
215 // can not easily check since node is deleted during parsing        
216 //        try{
217 //            leFactory.createLogicalExpression(
218 //                    " _# and (b or c).  ",
219 //                    topEntity);
220 //        }catch (ParserException pe){
221 //            assertEquals(pe.getErrorLine(),1);
222 //            assertEquals(pe.getErrorPos(),2);
223 //        }
224         try{
225             leFactory.createLogicalExpression(
226                     " _#1 and (b or c).  ",
227                     topEntity);
228         }catch (ParserException pe){
229             assertEquals(pe.getErrorLine(),1);
230             assertEquals(pe.getErrorPos(),2);
231         }
232         
233         try{
234             leFactory.createLogicalExpression(
235                     " 2 and (b or c).  ",
236                     topEntity);
237         }catch (ParserException pe){
238             assertEquals(pe.getErrorLine(),1);
239             assertEquals(pe.getErrorPos(),2);
240         }
241             
242         try{
243             leFactory.createLogicalExpression(
244                     " \"dd\" and (b or c).  ",
245                     topEntity);
246         }catch (ParserException pe){
247             assertEquals(pe.getErrorLine(),1);
248             assertEquals(pe.getErrorPos(),2);
249         }
250 		
251         try{
252             leFactory.createLogicalExpression(
253                     "ff andee.  ",
254                     topEntity);
255         }catch (ParserException pe){
256             assertEquals(1,pe.getErrorLine());
257             assertEquals(4,pe.getErrorPos());
258 			assertEquals("andee",pe.getFoundToken());
259         }		
260 
261 
262     }
263 
264     public void testDataTypes() throws ParserException {
265     
266         Object o = leFactory.createLogicalExpression(
267                 "c[int ofType _integer, " +
268                 " dbl ofType _\""+WsmlDataType.WSML_DOUBLE+"\"].", topEntity);
269         CompoundMolecule m = (CompoundMolecule)o;
270         
271         AttributeMolecule dbl = m.listAttributeConstraintMolecules(factory.createIRI(ns+"dbl")).iterator().next();
272         
273         assertEquals(WsmlDataType.WSML_DOUBLE.toString(),
274                 dbl.getRightParameter().toString());
275 
276         AttributeMolecule intMol = m.listAttributeConstraintMolecules(factory.createIRI(ns+"int")).iterator().next();
277 
278         assertEquals(WsmlDataType.WSML_INTEGER.toString(),
279                 intMol.getRightParameter().toString());
280 
281     }
282     
283     public void testNegativeDecimalWithLeadingZero() throws Exception{
284     	//no asserts, we just test wether we can parse and instantiate this expression
285         String test = "?x[y hasValue -0.1] memberOf x." ;
286         leFactory.createLogicalExpression(test,topEntity);
287     }
288     
289 }