/
TreeBasedConciseBoundedDescriptionGenerator.java
187 lines (163 loc) · 6.85 KB
/
TreeBasedConciseBoundedDescriptionGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Copyright (C) 2007 - 2016, Jens Lehmann
*
* This file is part of DL-Learner.
*
* DL-Learner is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* DL-Learner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dllearner.kb.sparql;
import com.google.common.base.Joiner;
import org.aksw.jena_sparql_api.cache.core.QueryExecutionFactoryCacheEx;
import org.aksw.jena_sparql_api.cache.extra.CacheFrontend;
import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2;
import org.aksw.jena_sparql_api.core.FluentQueryExecutionFactory;
import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
import org.aksw.jena_sparql_api.http.QueryExecutionHttpWrapper;
import org.aksw.jena_sparql_api.model.QueryExecutionFactoryModel;
import org.aksw.jena_sparql_api.pagination.core.QueryExecutionFactoryPaginated;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.WebContent;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.engine.http.QueryEngineHTTP;
import org.dllearner.algorithms.qtl.datastructures.GenericTree;
import org.dllearner.core.ComponentInitException;
import org.dllearner.kb.SparqlEndpointKS;
import org.dllearner.utilities.QueryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* {@inheritDoc}
* @author Lorenz Buehmann
*
*/
public class TreeBasedConciseBoundedDescriptionGenerator implements ConciseBoundedDescriptionGenerator{
private static final Logger logger = LoggerFactory.getLogger(TreeBasedConciseBoundedDescriptionGenerator.class);
private Set<String> allowedPropertyNamespaces = new TreeSet<>();
private Set<String> allowedObjectNamespaces = new TreeSet<>();
private QueryExecutionFactory qef;
AtomicInteger inIndex = new AtomicInteger(0);
AtomicInteger outIndex = new AtomicInteger(0);
AtomicInteger predIndex = new AtomicInteger(0);
public TreeBasedConciseBoundedDescriptionGenerator(QueryExecutionFactory qef) {
this.qef = qef;
}
/* (non-Javadoc)
* @see org.dllearner.kb.sparql.ConciseBoundedDescriptionGenerator#getConciseBoundedDescription(java.lang.String, int, boolean)
*/
public Model getConciseBoundedDescription(String resourceURI, CBDStructureTree structureTree) throws Exception {
logger.trace("Computing CBD for {} ...", resourceURI);
long start = System.currentTimeMillis();
String query = generateQuery(resourceURI, structureTree);
try(QueryExecution qe = qef.createQueryExecution(query)) {
Model model = qe.execConstruct();
long end = System.currentTimeMillis();
logger.trace("Got {} triples in {} ms.", model.size(), (end - start));
return model;
} catch(Exception e) {
throw new Exception("CBD retrieval failed when using query\n" + query, e);
}
}
@Override
public Model getConciseBoundedDescription(String resourceURI, int depth, boolean withTypesForLeafs) {
throw new NotImplementedException("please use getConciseBoundedDescription(String resourceURI, CBDStructureTree structureTree) instead.");
}
@Override
public void addAllowedPropertyNamespaces(Set<String> namespaces) {
this.allowedPropertyNamespaces.addAll(namespaces);
}
@Override
public void addAllowedObjectNamespaces(Set<String> namespaces) {
this.allowedObjectNamespaces.addAll(namespaces);
}
/**
* A SPARQL CONSTRUCT query is created, to get a RDF graph for the given example with a specific recursion depth.
* @param resource The example resource for which a CONSTRUCT query is created.
* @return the SPARQL query
*/
private String generateQuery(String resource, CBDStructureTree structureTree){
reset();
StringBuilder sb = new StringBuilder();
sb.append("CONSTRUCT {\n");
append(sb, structureTree, "<" + resource + ">");
sb.append("} WHERE {\n");
reset();
append(sb, structureTree, "<" + resource + ">");
sb.append("}");
return sb.toString();
}
private void append(StringBuilder query, CBDStructureTree tree, String rootVar) {
List<CBDStructureTree> children = tree.getChildren();
for (CBDStructureTree child : children) {
String var;
if(child.isInNode()) {
var = "?x_in" + inIndex.getAndIncrement();
String predVar = "?p" + predIndex.getAndIncrement();
query.append(String.format("%s %s %s .\n", var, predVar, rootVar));
} else {
var = "?x_out" + outIndex.getAndIncrement();
String predVar = "?p" + predIndex.getAndIncrement();
query.append(String.format("%s %s %s .\n", rootVar, predVar, var));
}
append(query, child, var);
}
}
private void reset() {
inIndex = new AtomicInteger(0);
outIndex = new AtomicInteger(0);
predIndex = new AtomicInteger(0);
}
@Override
public void addPropertiesToIgnore(Set<String> properties) {
}
public static void main(String[] args) throws Exception {
String query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"\n" +
"SELECT DISTINCT ?uri\n" +
"WHERE\n" +
" { ?x <http://dbpedia.org/ontology/director> <http://dbpedia.org/resource/William_Shatner> ;\n" +
" <http://dbpedia.org/ontology/starring> ?uri\n" +
" }";
query = "PREFIX dbo: <http://dbpedia.org/ontology/>\n" +
"PREFIX : <http://dbpedia.org/resource/>\n" +
"\n" +
"SELECT DISTINCT ?uri\n" +
"WHERE\n" +
" { :The_Three_Dancers\n" +
" dbo:author ?person .\n" +
" ?person dbo:movement ?uri\n" +
" }";
CBDStructureTree cbdTree = QueryUtils.getOptimalCBDStructure(QueryFactory.create(query));
System.out.println(cbdTree.toStringVerbose());
SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpedia();
SparqlEndpointKS ks = new SparqlEndpointKS(endpoint);
ks.init();
TreeBasedConciseBoundedDescriptionGenerator cbdGen = new TreeBasedConciseBoundedDescriptionGenerator(ks.getQueryExecutionFactory());
Model cbd = cbdGen.getConciseBoundedDescription("http://dbpedia.org/resource/Dan_Gauthier", cbdTree);
System.out.println(cbd.size());
// cbd.write(System.out, "NTRIPLES");
}
}