001/** 002 * Copyright (C) 2007 - 2016, 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 */ 019package org.dllearner.kb.aquisitors; 020 021import java.net.URI; 022import java.util.SortedSet; 023import java.util.TreeSet; 024 025import org.apache.log4j.Logger; 026import org.dllearner.utilities.datastructures.RDFNodeTuple; 027 028/** 029 * 030 * Typed SPARQL query interface. The typing means that they all have the same 031 * input and the same output: They are fn: resource -> ( a | b ) where a 032 * normally is a predicate and b an object 033 * 034 * @author Sebastian Hellmann 035 * 036 */ 037public abstract class TupleAquisitor { 038 039 040 private static Logger logger = Logger.getLogger(TupleAquisitor.class); 041 protected final int NORMAL = 0; 042 protected final int CLASSES_FOR_INSTANCES = 1; 043 protected final int CLASS_INFORMATION = 2; 044 045 protected int mode = 0; 046 protected boolean uriDebugCheck = true; 047 048 public boolean dissolveBlankNodes = false; 049 050 051 /** 052 * TODO: this function is still used somewhere, but should be replaced 053 * @return whether blank nodes are dissolved 054 */ 055 public boolean isDissolveBlankNodes(){ 056 return dissolveBlankNodes; 057 } 058 059 public final SortedSet<RDFNodeTuple> getTupelForResource(String uri){ 060 checkURIforValidity(uri); 061 062 try{ 063 if (mode == NORMAL) { 064 return retrieveTupel(uri); 065 } else if(mode == CLASSES_FOR_INSTANCES){ 066 return retrieveClassesForInstances(uri); 067 }else if(mode == CLASS_INFORMATION){ 068 return retrieveTuplesForClassesOnly(uri); 069 }else{ 070 throw new RuntimeException("undefined mode in aquisitor"); 071 } 072 }catch(Exception e){ 073 logger.warn("Caught exception in tupleaquisitor, ignoring it: "+e.toString()); 074 e.printStackTrace(); 075 return new TreeSet<>(); 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