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.aquisitors;
021    
022    import java.net.URI;
023    import java.util.SortedSet;
024    import java.util.TreeSet;
025    
026    import org.apache.log4j.Logger;
027    import org.dllearner.utilities.datastructures.RDFNodeTuple;
028    
029    /**
030     * 
031     * Typed SPARQL query interface. The typing means that they all have the same
032     * input and the same output: They are fn: resource -> ( a | b ) where a
033     * normally is a predicate and b an object
034     * 
035     * @author Sebastian Hellmann
036     * 
037     */
038    public abstract class TupleAquisitor {
039            
040    
041            private static Logger logger = Logger.getLogger(TupleAquisitor.class);
042            protected final int NORMAL = 0;
043            protected final int CLASSES_FOR_INSTANCES = 1;
044            protected final int CLASS_INFORMATION = 2;
045            
046            protected int mode = 0;
047            protected boolean uriDebugCheck = true;
048            
049            public boolean dissolveBlankNodes = false;
050    
051            
052            /**
053             * TODO: this function is still used somewhere, but should be replaced
054             * @return
055             */
056            public boolean isDissolveBlankNodes(){
057                    return dissolveBlankNodes;
058            }
059            
060            public final SortedSet<RDFNodeTuple> getTupelForResource(String uri){
061                    checkURIforValidity(uri);
062                            
063                    try{
064                            if (mode == NORMAL) {
065                                    return retrieveTupel(uri);
066                            } else if(mode == CLASSES_FOR_INSTANCES){
067                                    return retrieveClassesForInstances(uri);
068                            }else if(mode == CLASS_INFORMATION){
069                                    return retrieveTuplesForClassesOnly(uri);
070                            }else{
071                                  throw new RuntimeException("undefined mode in aquisitor");
072                            }
073                    }catch(Exception e){
074                            logger.warn("Caught exception in tupleaquisitor, ignoring it: "+e.toString());
075                            return new TreeSet<RDFNodeTuple>();
076                            
077                    }
078            }
079            public abstract SortedSet<RDFNodeTuple> retrieveTupel(String uri);
080            public abstract SortedSet<RDFNodeTuple> retrieveClassesForInstances(String uri);
081            public abstract SortedSet<RDFNodeTuple> retrieveTuplesForClassesOnly(String uri);
082            protected abstract void disambiguateBlankNodes(String uri, SortedSet<RDFNodeTuple> resultSet);
083            public abstract  SortedSet<RDFNodeTuple> getBlankNode(int id);
084            
085            /*private void setMode(int mode) {
086                    this.mode = mode;
087            }*/
088    
089            public int getMode() {
090                    return mode;
091            }
092            
093            public void setNextTaskToNormal(){mode = NORMAL;}
094            public void setNextTaskToClassesForInstances(){mode = CLASSES_FOR_INSTANCES;}
095            public void setNextTaskToClassInformation(){mode = CLASS_INFORMATION;}
096            
097            protected boolean checkURIforValidity(String uri){
098                    if(uriDebugCheck) return true;
099                    try{
100                            new URI(uri);
101                    }catch (Exception e) {
102                            logger.warn("Exception while validating uri: "+uri);
103                            return false;
104                    }
105                    return true;
106            }
107    }
108    
109