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.kb.extraction;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.util.SortedSet;
025    import java.util.TreeSet;
026    
027    import org.apache.log4j.Logger;
028    import org.dllearner.kb.aquisitors.TupleAquisitor;
029    import org.dllearner.kb.manipulator.Manipulator;
030    
031    import com.hp.hpl.jena.rdf.model.Literal;
032    import com.hp.hpl.jena.rdf.model.RDFNode;
033    
034    /**
035     * A node in the graph that is a Literal.
036     * 
037     * @author Sebastian Hellmann
038     * 
039     */
040    public class LiteralNode extends Node {
041            
042            private Literal l;
043            
044            @SuppressWarnings("unused")
045            private static Logger logger = Logger
046                    .getLogger(LiteralNode.class);
047    
048    
049            public LiteralNode(String uri) {
050                    super(uri);
051                    
052            }
053            
054            public LiteralNode(RDFNode node) {
055                    super(node.toString());
056                    l = (Literal) node;
057            }
058            
059            public Literal getLiteral(){
060                    return l;
061            }
062    
063            // expands all directly connected nodes
064            @Override
065            public List<Node> expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) {
066                    return new ArrayList<Node>();
067            }
068            
069            
070    
071            // gets the types for properties recursively
072            @Override
073            public List<BlankNode>  expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) {
074                    return new ArrayList<BlankNode>();
075            }
076    
077            @Override
078            public SortedSet<String> toNTriple() {
079                    return new TreeSet<String>();
080            }
081    
082            
083            
084            @Override
085            public String getNTripleForm() {
086                    String quote = "\\\"";
087                    quote = "&quot;";
088                    String retVal = l.getLexicalForm();
089                    retVal = retVal.replaceAll("\n", "\\n");
090                    retVal = retVal.replaceAll("\"", quote);
091                    retVal = "\""+retVal+"\"";
092                    if(l.getDatatypeURI()!=null) {
093                            return retVal +"^^<"+l.getDatatypeURI()+">";
094                    }else {
095                            return retVal+((l.getLanguage().length()==0)?"":"@"+l.getLanguage());
096                    }
097                    
098            }
099            
100            @Override
101            public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){
102                    
103            }
104            
105            public boolean isDouble(){
106                    try{
107                            if(l.getDatatypeURI().contains("double")){return true;}
108                            else{return false;}
109            
110                            //l.getFloat();
111                            
112                            //l.getDouble();
113                            //return true;
114                    }catch (Exception e) {
115                            return false;
116                    }/*
117                    try{
118                            l.getDouble();
119                            return true;
120                    }catch (Exception e) {
121                            return false;
122                    }*/
123            }
124            
125            public boolean isFloat(){
126                    try{
127                            if(l.getDatatypeURI().contains("float")){return true;}
128                            else{return false;}
129            
130                            //l.getFloat();
131                            
132                            //l.getDouble();
133                            //return true;
134                    }catch (Exception e) {
135                            return false;
136                    }
137            }
138            
139            public boolean isInt(){
140                    try{
141                            if(l.getDatatypeURI().contains("int")){return true;}
142                            else{return false;}
143            
144                            //l.getFloat();
145                            
146                            //l.getDouble();
147                            //return true;
148                    }catch (Exception e) {
149                            return false;
150                    }
151                    
152                    
153            /*try{
154                            l.getInt();
155                            return true;
156                    }catch (Exception e) {
157                            return false;
158                    }*/
159            }
160            
161            public boolean isBoolean(){
162                    try{
163                            if(l.getDatatypeURI().contains("boolean")){return true;}
164                            else{return false;}
165            
166                            //l.getFloat();
167                            
168                            //l.getDouble();
169                            //return true;
170                    }catch (Exception e) {
171                            return false;
172                    }
173                    
174                    /*try{
175                            l.getBoolean();
176                            return true;
177                    }catch (Exception e) {
178                            return false;
179                    }*/
180            }
181            
182            public boolean isString(){
183                    try{
184                            l.getString();
185                            return true;
186                    }catch (Exception e) {
187                            return false;
188                    }
189            }
190            public boolean hasLanguageTag(){
191                    return (!(l.getLanguage().length()==0));
192            }
193            
194            
195            
196            
197    
198    }