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 org.apache.jena.query.QuerySolution; 022import org.apache.jena.query.ResultSetRewindable; 023import org.apache.jena.rdf.model.RDFNode; 024import org.dllearner.utilities.datastructures.RDFNodeTuple; 025 026import java.util.*; 027 028public class BlankNodeCollector { 029 030 private static int globalBNodeId = 0; 031 public static synchronized int getNextGlobalBNodeId(){ 032 int ret = globalBNodeId; 033 globalBNodeId++; 034 return ret; 035 } 036 037 private static Map<Integer, SortedSet<RDFNodeTuple>> blankNodes = new HashMap<>(); 038 039 public static void addBlankNode(int id, RDFNodeTuple t){ 040 if(blankNodes.get(id)==null){ 041 blankNodes.put(id, new TreeSet<>()); 042 } 043 blankNodes.get(id).add(t); 044 //System.out.println("added: "+id+" "+t); 045 //System.out.println(); 046 } 047 048 public static SortedSet<RDFNodeTuple> getBlankNode(int id){ 049 return blankNodes.get(id); 050 } 051 052 public static Map<Integer, SortedSet<RDFNodeTuple>> getBlankNodeMap(){ 053 return blankNodes; 054 } 055 056 057 /** 058 * @param rsw 059 * @param depth 060 * @return true if there are more blanknodes 061 */ 062 public static boolean testResultSet(ResultSetRewindable rsw, int depth){ 063 List<String> vars = new ArrayList<>(); 064 vars.add("o0"); 065 for (int i = 1; i <= depth; i++) { 066 vars.add("o"+i); 067 } 068 069 while (rsw.hasNext()){ 070 QuerySolution q = rsw.nextSolution(); 071 if(!q.get("o0").isAnon()){ 072 continue; 073 }else{ 074 if(!testOneQuerySolution(vars, q)){ 075 rsw.reset(); 076 return false; 077 } 078 079 } 080 } 081 rsw.reset(); 082 return true; 083 } 084 085 //true to stop expansion 086 private static boolean testOneQuerySolution(List<String> vars, QuerySolution q){ 087 088// System.out.println(q); 089// System.out.println(vars); 090// for (String v : vars) { 091// RDFNode n = q.get(v); 092// if(n==null){ 093// System.out.println("returning true"); 094// return true; 095// } 096// 097// } 098 099 for (String v : vars) { 100 RDFNode n = q.get(v); 101 if(!n.isAnon()){ 102// System.out.println(n); 103 return true; 104 } 105 } 106 return false; 107 } 108 109 public static String makeQuery(String uri, String predicate, int maxDepth){ 110 int currentDepth = 0; 111 StringBuffer sq = new StringBuffer(); 112 String init = "SELECT * WHERE { "+ 113 "<"+uri+"> <"+predicate+"> ?o"+currentDepth+". "; 114 115 sq.append(init); 116 117 for (; currentDepth < maxDepth; currentDepth++) { 118 String currentO = "?o"+currentDepth; 119 String nextP = "?p"+(currentDepth+1); 120 String nextO = "?o"+(currentDepth+1); 121 sq.append(" OPTIONAL { ").append(currentO).append(" ").append(nextP).append(" ").append(nextO).append(". }"); 122 } 123 124 125 sq.append(" }"); 126 return sq.toString(); 127 } 128 129}