Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 357   Methods: 17
NCLOC: 145   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ConstantTransformer.java 0% 0% 0% 0%
coverage
 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  0 public static ConstantTransformer getInstance() {
 61  0 if (inst == null) {
 62  0 inst = new ConstantTransformer();
 63    }
 64  0 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  0 public String findIri(String notation) {
 73  0 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  0 public String findNotation(String iri) {
 81  0 BuiltInInfoSource nh = iri2notation.get(iri);
 82  0 if (nh != null) {
 83  0 return nh.getNotation();
 84    }
 85  0 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  0 public boolean isInfix(String iri) {
 97  0 BuiltInInfoSource nh = iri2notation.get(iri);
 98  0 if (nh != null) {
 99  0 return nh.isInfix();
 100    }
 101  0 return false;
 102    }
 103   
 104  0 public boolean isSimpleDataType(String iriOrNotation) {
 105  0 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  0 public boolean isDataType(String iriOrNotion) {
 114    //sanity check if _"" is still included -> remove
 115  0 iriOrNotion = iriOrNotion.trim();
 116    //FIXME
 117  0 if (iriOrNotion.indexOf("_\"") == 0) {
 118  0 iriOrNotion = iriOrNotion.substring(2, iriOrNotion.length() - 1);
 119    }
 120  0 BuiltInInfoSource nh = iri2notation.get(iriOrNotion);
 121  0 if (nh == null && notation2iri.get(iriOrNotion) != null) {
 122  0 nh = iri2notation.get(notation2iri.get(iriOrNotion));
 123    }
 124  0 if (nh != null) {
 125  0 return nh.isDataType();
 126    }
 127  0 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  0 public boolean isBuiltInFunctionSymbol(String iri) {
 138  0 BuiltInInfoSource nh = iri2notation.get(iri);
 139  0 if (nh != null) {
 140  0 return nh.isFSymbol();
 141    }
 142  0 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  0 public boolean isBuiltInAtom(String iri) {
 151  0 BuiltInInfoSource nh = iri2notation.get(iri);
 152  0 if (nh != null) {
 153  0 return nh.isBuiltInAtom();
 154    }
 155  0 return false;
 156    }
 157   
 158    /**
 159    * The private constructor of the ConstantTransformer
 160    */
 161  0 private ConstantTransformer() {
 162    //IRI => [notation, infix]
 163  0 iri2notation = new Hashtable <String, BuiltInInfoSource>();
 164    //notation => IRI
 165  0 notation2iri = new Hashtable <String, String>();
 166   
 167  0 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  0 private void fillHashTables() {
 175  0 add(Constants.UNIV_TRUE, new BuiltInInfoSource(Constants.TRUE, false, false, false, false));
 176  0 add(Constants.UNIV_FALSE, new BuiltInInfoSource(Constants.FALSE, false, false, false, false));
 177   
 178  0 add(Constants.NUMERIC_ADD, new BuiltInInfoSource(Constants.NUMERIC_ADD_NOTATION, true, false, false, false));
 179  0 add(Constants.NUMERIC_SUB, new BuiltInInfoSource(Constants.NUMERIC_SUB_NOTATION, true, false, false, false));
 180  0 add(Constants.NUMERIC_MUL, new BuiltInInfoSource(Constants.NUMERIC_MUL_NOTATION, true, false, false, false));
 181  0 add(Constants.NUMERIC_DIV, new BuiltInInfoSource(Constants.NUMERIC_DIV_NOTATION, true, false, false, false));
 182   
 183  0 add(Constants.EQUAL, new BuiltInInfoSource(Constants.EQUAL_NOTATION, true, false, true, false));
 184  0 add(Constants.STRONG_EQUAL, new BuiltInInfoSource(Constants.STRONG_EQUAL_NOTATION, true, false, true, false));
 185  0 add(Constants.INEQUAL, new BuiltInInfoSource(Constants.INEQUAL_NOTATION, true, false, true, false));
 186  0 add(Constants.STRING_EQUAL, new BuiltInInfoSource(Constants.STRING_EQUAL_NOTATION, true, false, true, false));
 187  0 add(Constants.STRING_INEQUAL, new BuiltInInfoSource(Constants.STRING_INEQUAL_NOTATION, true, false, true, false));
 188  0 add(Constants.NUMERIC_EQUAL, new BuiltInInfoSource(Constants.NUMERIC_EQUAL_NOTATION, true, false, true, false));
 189  0 add(Constants.NUMERIC_INEQUAL, new BuiltInInfoSource(Constants.NUMERIC_INEQUAL_NOTATION, true, false, true, false));
 190  0 add(Constants.LESS_THAN, new BuiltInInfoSource(Constants.LESS_THAN_NOTATION, true, false, true, false));
 191  0 add(Constants.LESS_EQUAL, new BuiltInInfoSource(Constants.LESS_EQUAL_NOTATION, true, false, true, false));
 192  0 add(Constants.GREATER_THAN, new BuiltInInfoSource(Constants.GREATER_THAN_NOTATION, true, false, true, false));
 193  0 add(Constants.GREATER_EQUAL, new BuiltInInfoSource(Constants.GREATER_EQUAL_NOTATION, true, false, true, false));
 194   
 195  0 add(WsmlDataType.WSML_STRING, new BuiltInInfoSource(WsmlDataType.WSML_STRING_NOTATION, false, true, false, true));
 196  0 add(WsmlDataType.WSML_INTEGER, new BuiltInInfoSource(WsmlDataType.WSML_INTEGER_NOTATION, false, true, false, true));
 197  0 add(WsmlDataType.WSML_DECIMAL, new BuiltInInfoSource(WsmlDataType.WSML_DECIMAL_NOTATION, false, true, false, true));
 198  0 add(WsmlDataType.WSML_IRI, new BuiltInInfoSource(WsmlDataType.WSML_IRI_NOTATION, false, true, false, true));
 199  0 add(WsmlDataType.WSML_SQNAME, new BuiltInInfoSource(WsmlDataType.WSML_SQNAME_NOTATION, false, true, false, true));
 200  0 add(WsmlDataType.WSML_FLOAT, new BuiltInInfoSource(WsmlDataType.WSML_FLOAT_NOTATION, false, true, false, true));
 201  0 add(WsmlDataType.WSML_DOUBLE, new BuiltInInfoSource(WsmlDataType.WSML_DOUBLE_NOTATION, false, true, false, true));
 202  0 add(WsmlDataType.WSML_BOOLEAN, new BuiltInInfoSource(WsmlDataType.WSML_BOOLEAN_NOTATION, false, true, false, true));
 203  0 add(WsmlDataType.WSML_DURATION, new BuiltInInfoSource(WsmlDataType.WSML_DURATION_NOTATION, false, true, false, true));
 204  0 add(WsmlDataType.WSML_DATETIME, new BuiltInInfoSource(WsmlDataType.WSML_DATETIME_NOTATION, false, true, false, true));
 205  0 add(WsmlDataType.WSML_TIME, new BuiltInInfoSource(WsmlDataType.WSML_TIME_NOTATION, false, true, false, true));
 206  0 add(WsmlDataType.WSML_DATE, new BuiltInInfoSource(WsmlDataType.WSML_DATE_NOTATION, false, true, false, true));
 207  0 add(WsmlDataType.WSML_GYEARMONTH, new BuiltInInfoSource(WsmlDataType.WSML_GYEARMONTH_NOTATION, false, true, false, true));
 208  0 add(WsmlDataType.WSML_GYEAR, new BuiltInInfoSource(WsmlDataType.WSML_GYEAR_NOTATION, false, true, false, true));
 209  0 add(WsmlDataType.WSML_GMONTHDAY, new BuiltInInfoSource(WsmlDataType.WSML_GMONTHDAY_NOTATION, false, true, false, true));
 210  0 add(WsmlDataType.WSML_GDAY, new BuiltInInfoSource(WsmlDataType.WSML_GDAY_NOTATION, false, true, false, true));
 211  0 add(WsmlDataType.WSML_GMONTH, new BuiltInInfoSource(WsmlDataType.WSML_GMONTH_NOTATION, false, true, false, true));
 212  0 add(WsmlDataType.WSML_HEXBINARY, new BuiltInInfoSource(WsmlDataType.WSML_HEXBINARY_NOTATION, false, true, false, true));
 213  0 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  0 private void add(String iri, BuiltInInfoSource bi) {
 222  0 iri2notation.put(iri, bi);
 223  0 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  0 protected BuiltInInfoSource(String notation, boolean infix,
 259    boolean fsymb, boolean builtInAtom, boolean isDataType) {
 260  0 this.notation = notation;
 261  0 this.infix = infix;
 262  0 this.fsymb = fsymb;
 263  0 this.builtInAtom = builtInAtom;
 264  0 this.isDataType = isDataType;
 265    }
 266   
 267    /**
 268    * @return true if the construct is a function symbol
 269    */
 270  0 protected boolean isFSymbol() {
 271  0 return fsymb;
 272    }
 273   
 274    /**
 275    * @return true if the construct is a built in atom
 276    */
 277  0 protected boolean isBuiltInAtom() {
 278  0 return builtInAtom;
 279    }
 280   
 281    /**
 282    * @return true if there exists an infix notation
 283    */
 284  0 protected boolean isInfix() {
 285  0 return infix;
 286    }
 287   
 288    /**
 289    * @return the short notation of a built-in element
 290    */
 291  0 protected String getNotation() {
 292  0 return notation;
 293    }
 294   
 295  0 protected boolean isDataType() {
 296  0 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    */