View Javadoc

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 org.deri.wsmo4j.logicalexpression;
19  
20  
21  import java.util.*;
22  
23  import org.omwg.logicalexpression.*;
24  import org.omwg.ontology.*;
25  
26  
27  /**
28   * <p>This singleton class is used to transform forth and back the built-in predicate names and
29   * datatypes from IRI to short notation.</p>
30   * <p>The Singleton design pattern allows to ensure that only one instance of a class
31   * is created, to provide a global point of access to the object and to allow multiple
32   * instances in the future without affecting a singleton class's clients</p>
33   * @author DERI Innsbruck, reto.krummenacher@deri.org
34   * @version $Revision$ $Date$
35   * @see org.omwg.logicalexpression.Constants
36   * @see <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton Pattern</a>
37   */
38  public class ConstantTransformer {
39  
40      /**
41       * The singleton instance of the ConstantTransformer
42       */
43      private static ConstantTransformer inst;
44  
45      /**
46       * A hashtable containing the transformation from IRI to short notations
47       */
48      private Hashtable <String, BuiltInInfoSource> iri2notation;
49  
50      /**
51       * A hashtable containing the transformation from the short notation to IRI
52       */
53      private Hashtable <String, String>notation2iri;
54  
55      /**
56       * Singletons maintain a static reference to the sole singleton instance
57       * and return a reference to that instance from a static instance() method.
58       * @return the singleton instance of the ConstantTransformer
59       */
60      public static ConstantTransformer getInstance() {
61          if (inst == null) {
62              inst = new ConstantTransformer();
63          }
64          return inst;
65      }
66  
67      /**
68       * @param notation the short notation available
69       * @return the full IRI corresponding to the short notation
70       * @see java.util.Hashtable#get(java.lang.Object)
71       */
72      public String findIri(String notation) {
73          return notation2iri.get(notation);
74      }
75  
76      /**
77       * @param iri the full IRI available
78       * @return the short notation of the given IRI
79       */
80      public String findNotation(String iri) {
81          BuiltInInfoSource nh = iri2notation.get(iri);
82          if (nh != null) {
83              return nh.getNotation();
84          }
85          return null;
86      }
87  
88      /**
89       * Some of the built-in predicates and function symbols like e.g.
90       * comparisons or mathematical functions have an infix notation in WSML
91       * human-readable syntax. This method allows you to find out if the predicate corresponding
92       * to a given IRI has an infix notation.
93       * @param iri the IRI for which one needs to know if there is an infix notation
94       * @return a boolean that is true if there is an infix notation
95       */
96      public boolean isInfix(String iri) {
97          BuiltInInfoSource nh = iri2notation.get(iri);
98          if (nh != null) {
99              return nh.isInfix();
100         }
101         return false;
102     }
103     
104     public boolean isSimpleDataType(String iriOrNotation) {
105         return iriOrNotation.equals(WsmlDataType.WSML_STRING_NOTATION) ||
106                 iriOrNotation.equals(WsmlDataType.WSML_INTEGER_NOTATION) ||
107                 iriOrNotation.equals(WsmlDataType.WSML_DECIMAL_NOTATION) ||
108                 iriOrNotation.equals(WsmlDataType.WSML_STRING) ||
109                 iriOrNotation.equals(WsmlDataType.WSML_INTEGER) ||
110                 iriOrNotation.equals(WsmlDataType.WSML_DECIMAL);
111     }
112 
113     public boolean isDataType(String iriOrNotion) {
114         //sanity check if _"" is still included -> remove
115         iriOrNotion = iriOrNotion.trim();
116         //FIXME
117         if (iriOrNotion.indexOf("_\"") == 0) {
118             iriOrNotion = iriOrNotion.substring(2, iriOrNotion.length() - 1);
119         }
120         BuiltInInfoSource nh = iri2notation.get(iriOrNotion);
121         if (nh == null && notation2iri.get(iriOrNotion) != null) {
122             nh = iri2notation.get(notation2iri.get(iriOrNotion));
123         }
124         if (nh != null) {
125             return nh.isDataType();
126         }
127         return false;
128     }
129 
130     /**
131      * There are built-in predicates (atoms) and built-in function symbols in a logical expression.
132      * This method allows one to find out if a built-in construction is actually an atom or a function
133      * symbol.
134      * @param iri the IRI for which the information is required
135      * @return a boolean that is true if the construction is a function symbol
136      */
137     public boolean isBuiltInFunctionSymbol(String iri) {
138         BuiltInInfoSource nh = iri2notation.get(iri);
139         if (nh != null) {
140             return nh.isFSymbol();
141         }
142         return false;
143     }
144 
145     /**
146      * Check if iri stands for a built-in predicate.
147      * @param iri
148      * @return true if iri is a built in predicate
149      */
150     public boolean isBuiltInAtom(String iri) {
151         BuiltInInfoSource nh = iri2notation.get(iri);
152         if (nh != null) {
153             return nh.isBuiltInAtom();
154         }
155         return false;
156     }
157 
158     /**
159      * The private constructor of the ConstantTransformer
160      */
161     private ConstantTransformer() {
162         //IRI => [notation, infix]
163         iri2notation = new Hashtable <String, BuiltInInfoSource>();
164         //notation => IRI
165         notation2iri = new Hashtable <String, String>();
166 
167         fillHashTables();
168     }
169 
170     /**
171      * This method fills the two hashtables, which contain the transformations. The method is called
172      * only once in the singleton constructor of the ConstantTransformer
173      */
174     private void fillHashTables() {
175         add(Constants.UNIV_TRUE, new BuiltInInfoSource(Constants.TRUE, false, false, false, false));
176         add(Constants.UNIV_FALSE, new BuiltInInfoSource(Constants.FALSE, false, false, false, false));
177 
178         add(Constants.NUMERIC_ADD, new BuiltInInfoSource(Constants.NUMERIC_ADD_NOTATION, true, false, false, false));
179         add(Constants.NUMERIC_SUB, new BuiltInInfoSource(Constants.NUMERIC_SUB_NOTATION, true, false, false, false));
180         add(Constants.NUMERIC_MUL, new BuiltInInfoSource(Constants.NUMERIC_MUL_NOTATION, true, false, false, false));
181         add(Constants.NUMERIC_DIV, new BuiltInInfoSource(Constants.NUMERIC_DIV_NOTATION, true, false, false, false));
182 
183         add(Constants.EQUAL, new BuiltInInfoSource(Constants.EQUAL_NOTATION, true, false, true, false));
184         add(Constants.STRONG_EQUAL, new BuiltInInfoSource(Constants.STRONG_EQUAL_NOTATION, true, false, true, false));
185         add(Constants.INEQUAL, new BuiltInInfoSource(Constants.INEQUAL_NOTATION, true, false, true, false));
186         add(Constants.STRING_EQUAL, new BuiltInInfoSource(Constants.STRING_EQUAL_NOTATION, true, false, true, false));
187         add(Constants.STRING_INEQUAL, new BuiltInInfoSource(Constants.STRING_INEQUAL_NOTATION, true, false, true, false));
188         add(Constants.NUMERIC_EQUAL, new BuiltInInfoSource(Constants.NUMERIC_EQUAL_NOTATION, true, false, true, false));
189         add(Constants.NUMERIC_INEQUAL, new BuiltInInfoSource(Constants.NUMERIC_INEQUAL_NOTATION, true, false, true, false));
190         add(Constants.LESS_THAN, new BuiltInInfoSource(Constants.LESS_THAN_NOTATION, true, false, true, false));
191         add(Constants.LESS_EQUAL, new BuiltInInfoSource(Constants.LESS_EQUAL_NOTATION, true, false, true, false));
192         add(Constants.GREATER_THAN, new BuiltInInfoSource(Constants.GREATER_THAN_NOTATION, true, false, true, false));
193         add(Constants.GREATER_EQUAL, new BuiltInInfoSource(Constants.GREATER_EQUAL_NOTATION, true, false, true, false));
194 
195         add(WsmlDataType.WSML_STRING, new BuiltInInfoSource(WsmlDataType.WSML_STRING_NOTATION, false, true, false, true));
196         add(WsmlDataType.WSML_INTEGER, new BuiltInInfoSource(WsmlDataType.WSML_INTEGER_NOTATION, false, true, false, true));
197         add(WsmlDataType.WSML_DECIMAL, new BuiltInInfoSource(WsmlDataType.WSML_DECIMAL_NOTATION, false, true, false, true));
198         add(WsmlDataType.WSML_IRI, new BuiltInInfoSource(WsmlDataType.WSML_IRI_NOTATION, false, true, false, true));
199         add(WsmlDataType.WSML_SQNAME, new BuiltInInfoSource(WsmlDataType.WSML_SQNAME_NOTATION, false, true, false, true));
200         add(WsmlDataType.WSML_FLOAT, new BuiltInInfoSource(WsmlDataType.WSML_FLOAT_NOTATION, false, true, false, true));
201         add(WsmlDataType.WSML_DOUBLE, new BuiltInInfoSource(WsmlDataType.WSML_DOUBLE_NOTATION, false, true, false, true));
202         add(WsmlDataType.WSML_BOOLEAN, new BuiltInInfoSource(WsmlDataType.WSML_BOOLEAN_NOTATION, false, true, false, true));
203         add(WsmlDataType.WSML_DURATION, new BuiltInInfoSource(WsmlDataType.WSML_DURATION_NOTATION, false, true, false, true));
204         add(WsmlDataType.WSML_DATETIME, new BuiltInInfoSource(WsmlDataType.WSML_DATETIME_NOTATION, false, true, false, true));
205         add(WsmlDataType.WSML_TIME, new BuiltInInfoSource(WsmlDataType.WSML_TIME_NOTATION, false, true, false, true));
206         add(WsmlDataType.WSML_DATE, new BuiltInInfoSource(WsmlDataType.WSML_DATE_NOTATION, false, true, false, true));
207         add(WsmlDataType.WSML_GYEARMONTH, new BuiltInInfoSource(WsmlDataType.WSML_GYEARMONTH_NOTATION, false, true, false, true));
208         add(WsmlDataType.WSML_GYEAR, new BuiltInInfoSource(WsmlDataType.WSML_GYEAR_NOTATION, false, true, false, true));
209         add(WsmlDataType.WSML_GMONTHDAY, new BuiltInInfoSource(WsmlDataType.WSML_GMONTHDAY_NOTATION, false, true, false, true));
210         add(WsmlDataType.WSML_GDAY, new BuiltInInfoSource(WsmlDataType.WSML_GDAY_NOTATION, false, true, false, true));
211         add(WsmlDataType.WSML_GMONTH, new BuiltInInfoSource(WsmlDataType.WSML_GMONTH_NOTATION, false, true, false, true));
212         add(WsmlDataType.WSML_HEXBINARY, new BuiltInInfoSource(WsmlDataType.WSML_HEXBINARY_NOTATION, false, true, false, true));
213         add(WsmlDataType.WSML_BASE64BINARY, new BuiltInInfoSource(WsmlDataType.WSML_BASE64BINARY_NOTATION, false, true, false, true));
214     }
215 
216     /**
217      * Adds the transformation information to the hashtables
218      * @param iri the IRI on the one side of the transformations
219      * @param bi an instance of BuiltInInfoSource containing the notation, infix and function symbol information
220      */
221     private void add(String iri, BuiltInInfoSource bi) {
222         iri2notation.put(iri, bi);
223         notation2iri.put(bi.getNotation(), iri);
224     }
225 
226     /**
227      * This internal class reunites the information needed about built-in datatypes, predicates
228      * and function symbols. In more detail that includes the short notation and the booleans infix
229      * and fsymbol that indicate if there exists an infix notation, resp. if an construct is a predicate
230      * or a function symbol
231      * @author DERI Innsbruck, reto.krummenacher@deri.org
232      *
233      */
234     private class BuiltInInfoSource {
235 
236         /**
237          * The short notation
238          */
239         private String notation;
240 
241         private boolean isDataType;
242 
243         /**
244          * builtInAtom
245          */
246         private boolean builtInAtom;
247 
248         /**
249          * Indication if there is an infix notation
250          */
251         private boolean infix;
252 
253         /**
254          * Indication if the construct is a function symbol
255          */
256         private boolean fsymb;
257 
258         protected BuiltInInfoSource(String notation, boolean infix,
259                                     boolean fsymb, boolean builtInAtom, boolean isDataType) {
260             this.notation = notation;
261             this.infix = infix;
262             this.fsymb = fsymb;
263             this.builtInAtom = builtInAtom;
264             this.isDataType = isDataType;
265         }
266 
267         /**
268          * @return true if the construct is a function symbol
269          */
270         protected boolean isFSymbol() {
271             return fsymb;
272         }
273 
274         /**
275          * @return true if the construct is a built in atom
276          */
277         protected boolean isBuiltInAtom() {
278             return builtInAtom;
279         }
280 
281         /**
282          * @return true if there exists an infix notation
283          */
284         protected boolean isInfix() {
285             return infix;
286         }
287 
288         /**
289          * @return the short notation of a built-in element
290          */
291         protected String getNotation() {
292             return notation;
293         }
294 
295         protected boolean isDataType() {
296             return isDataType;
297         }
298     }
299 }
300 /*
301  * $Log$
302  * Revision 1.5  2007/04/02 12:13:20  morcen
303  * Generics support added to wsmo-api, wsmo4j and wsmo-test
304  *
305  * Revision 1.4  2005/11/09 06:58:38  holgerlausen
306  * fixed bug parsing wsml#add(?x,1,2) was converted to a constructed term instead of atom
307  *
308  * Revision 1.3  2005/10/17 08:36:17  holgerlausen
309  * fixed null pointer in unit tests
310  *
311  * Revision 1.2  2005/10/12 13:32:07  ohamano
312  * add isSimpleDataType
313  *
314  * Revision 1.1  2005/09/23 07:09:51  holgerlausen
315  * moved constanttransformer from API to implementation, removed dublicated constants in logicalexpression.constants
316  *
317  * Revision 1.7  2005/09/15 13:52:11  holgerlausen
318  * allow for strong equality in parsing serializing
319  *
320  * Revision 1.6  2005/09/09 11:58:19  holgerlausen
321  * fixed header logexp no longer extension
322  *
323  * Revision 1.5  2005/09/09 11:12:12  marin_dimitrov
324  * formatting
325  *
326  * Revision 1.4  2005/09/08 15:16:26  holgerlausen
327  * fixes for parsing and serializing datatypes
328  *
329  * Revision 1.3  2005/09/07 18:45:56  holgerlausen
330  * add isDataType() support
331  *
332  * Revision 1.2  2005/09/07 14:43:50  holgerlausen
333  * Added Explicit Support for BuiltIn Atoms and Constructed Terms
334  *
335  * Revision 1.1  2005/09/02 13:32:43  ohamano
336  * move logicalexpression packages from ext to core
337  * move tests from logicalexpression.test to test module
338  *
339  * Revision 1.6  2005/08/30 23:41:34  haselwanter
340  * Making the compiler happy: removing unneccesarily nested clauses.
341  *
342  * Revision 1.5  2005/08/18 16:15:40  nathaliest
343  * JavaDoc added
344  *
345  * Revision 1.4  2005/06/22 13:32:01  ohamano
346  * change header
347  *
348  * Revision 1.3  2005/06/20 08:30:03  holgerlausen
349  * formating
350  *
351  * Revision 1.2  2005/06/18 14:06:10  holgerlausen
352  * added local LEFactory, updated javadoc, refactored LEVariable > Variable etc. parse(String) for LEFactory is running now
353  *
354  * Revision 1.1  2005/06/16 13:55:23  ohamano
355  * first import
356  *
357  */