001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.core.owl;
021    
022    import java.util.Map;
023    
024    /**
025     * A constant which has an explicitly assigned datatype.
026     * 
027     * @author Jens Lehmann
028     *
029     */
030    public class TypedConstant extends Constant {
031    
032            /**
033             * 
034             */
035            private static final long serialVersionUID = -9135242138291085300L;
036            private Datatype datatype;
037            
038            public TypedConstant(String literal, Datatype datatype) {
039                    super(literal);
040                    this.datatype = datatype;
041            }
042            
043            /* (non-Javadoc)
044             * @see org.dllearner.core.owl.KBElement#getLength()
045             */
046            public int getLength() {
047                    return 1;
048            }
049    
050            /* (non-Javadoc)
051             * @see org.dllearner.core.owl.KBElement#toString(java.lang.String, java.util.Map)
052             */
053            public String toString(String baseURI, Map<String, String> prefixes) {
054                    return literal;
055    //              return literal + "^^" + datatype;
056            }
057            
058            public String toKBSyntaxString(String baseURI, Map<String, String> prefixes) {
059                    return "\"" + literal + "\"";
060    //              return literal + "^^" + datatype;
061            }
062            
063            /* (non-Javadoc)
064             * @see org.dllearner.core.owl.KBElement#toManchesterSyntaxString(java.lang.String, java.util.Map)
065             */
066            @Override
067            public String toManchesterSyntaxString(String baseURI, Map<String, String> prefixes) {
068                    // implemented according to http://www.w3.org/TR/owl2-manchester-syntax/
069                    // (not completely clear because "typedLiteral" and "integerLiteral" definitions there overlap, but hopefully correct)
070                    if(datatype.equals(OWL2Datatype.INT.getDatatype()) || datatype.equals(OWL2Datatype.DOUBLE.getDatatype())) {
071                            if(Double.valueOf(literal) >= 0) {
072                                    return "+" + literal;
073                            } else {
074                                    return "-" + literal;
075                            }
076                    } else if(datatype.equals(OWL2Datatype.STRING.getDatatype())) {
077                            return "\"" + literal + "\"";
078                    } else {
079                            return "\"" + literal + "\"^^" + datatype.toManchesterSyntaxString(baseURI, prefixes);
080                    }
081            }
082    
083            /**
084             * @return the datatype
085             */
086            public Datatype getDatatype() {
087                    return datatype;
088            }
089            
090            /* (non-Javadoc)
091             * @see org.dllearner.core.owl.KBElement#accept(org.dllearner.core.owl.KBElementVisitor)
092             */
093            public void accept(KBElementVisitor visitor) {
094                    visitor.visit(this);
095            }
096    
097            /* (non-Javadoc)
098             * @see java.lang.Comparable#compareTo(java.lang.Object)
099             */
100            public int compareTo(TypedConstant o) {
101                    // the first criteria is the datatype
102                    int datatypeComparision = datatype.getURI().compareTo(datatype.getURI());
103                    if(datatypeComparision == 0) {
104                            // the second criterion is the literal value
105                            return literal.compareTo(o.literal);
106                    } else
107                            return datatypeComparision;
108            }       
109            
110            @Override
111            public String toString() {
112                    return literal + "^^" + datatype;
113            }
114    
115            @Override
116            public int compareTo(Constant o) {
117                    if(o instanceof UntypedConstant) {
118                            return 1;
119                    }
120                    String str = literal + datatype;
121                    String str2 = o.literal + ((TypedConstant)o).datatype;
122                    return str.compareTo(str2);
123            }       
124    
125    }