Skip to content

Commit

Permalink
CBD size estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzBuehmann committed Sep 23, 2016
1 parent 33a4cf9 commit fc46a4f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
Expand Up @@ -29,10 +29,7 @@

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.*;
import org.apache.jena.sparql.core.Var;
import org.dllearner.utilities.QueryUtils;

Expand Down Expand Up @@ -209,10 +206,14 @@ public void generateBenchmarkDescription(File benchmarkQueriesFile, File htmlOut
// 4. column: depth
int depth = org.dllearner.utilities.QueryUtils.getSubjectObjectJoinDepth(query, var) + 1;
html += "<td class='number'>" + depth + "</td>\n";


List<String> result = SPARQLUtils.getResult(qef, query);

// 5. column: #instances
int nrOfInstances = SPARQLUtils.getResult(qef, query).size();
int nrOfInstances = result.size();
html += "<td class='number'>" + nrOfInstances + "</td>\n";

analyzeResult(result);

html += "</tr>\n";
break;
Expand All @@ -229,6 +230,16 @@ public void generateBenchmarkDescription(File benchmarkQueriesFile, File htmlOut
}
}

private void analyzeResult(List<String> resources) {
resources.forEach(r -> {
ParameterizedSparqlString template = SPARQLUtils.CBD_TEMPLATE_DEPTH3.copy();
template.setIri("uri", r);
ResultSet rs = qef.createQueryExecution(template.toString()).execSelect();
System.out.println(rs.next().getLiteral("cnt").getInt());

});
}

public static void main(String[] args) throws Exception{
if(args.length < 3) {
System.out.println("Usage: BenchmarkDescriptionGeneratorHTML <source> <target> <endpointURL> <defaultGraphURI>");
Expand All @@ -240,6 +251,8 @@ public static void main(String[] args) throws Exception{
String defaultGraph = null;
if(args.length == 4)
defaultGraph = args[3];
endpointURL = "http://dbpedia.org/sparql";
defaultGraph = "http://dbpedia.org";

QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpointURL, defaultGraph);
qef = new QueryExecutionFactoryPaginated(qef);
Expand Down
Expand Up @@ -23,7 +23,9 @@
import com.jamonapi.MonitorFactory;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.shared.PrefixMapping;
Expand Down Expand Up @@ -119,7 +121,6 @@ public void analyze() {
SPARQLUtils.QueryType queryType = SPARQLUtils.getQueryType(q);
sb.append("\t").append(queryType.name());


// // check CBD sizes and time
// Monitor mon = MonitorFactory.getTimeMonitor("CBD");
// mon.reset();
Expand Down
@@ -1,14 +1,16 @@
package org.dllearner.algorithms.qtl.experiments;

import com.google.common.base.Joiner;
import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.*;
import org.apache.jena.sparql.core.Var;
import org.dllearner.utilities.QueryUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -21,10 +23,26 @@ public class SPARQLUtils {

private static QueryUtils utils = new QueryUtils();

public static ParameterizedSparqlString CBD_TEMPLATE_DEPTH3;

static {
try {
String query = Joiner.on("\n").join(Files.readAllLines(Paths.get(
SPARQLUtils.class.getClassLoader().getResource("org/dllearner/algorithms/qtl/cbd-depth3.query").toURI())));
CBD_TEMPLATE_DEPTH3 = new ParameterizedSparqlString(query);
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}

}

enum QueryType {
IN, OUT, MISC
}


public static List<String> getResult(QueryExecutionFactory qef, Query query) throws Exception{
return getResult(qef, query, query.getProjectVars().get(0));
}
Expand Down
Expand Up @@ -35,9 +35,9 @@ public class Question {
@JsonProperty("aggregation")
private boolean aggregation;
@JsonProperty("onlydbo")
private String onlydbo;
private boolean onlydbo;
@JsonProperty("hybrid")
private String hybrid;
private boolean hybrid;
@JsonProperty("question")
private List<Question_> question = new ArrayList<Question_>();
@JsonProperty("query")
Expand Down Expand Up @@ -113,7 +113,7 @@ public void setAggregation(boolean aggregation) {
* The onlydbo
*/
@JsonProperty("onlydbo")
public String getOnlydbo() {
public boolean isOnlyDBO() {
return onlydbo;
}

Expand All @@ -123,7 +123,7 @@ public String getOnlydbo() {
* The onlydbo
*/
@JsonProperty("onlydbo")
public void setOnlydbo(String onlydbo) {
public void setOnlydbo(boolean onlydbo) {
this.onlydbo = onlydbo;
}

Expand All @@ -133,7 +133,7 @@ public void setOnlydbo(String onlydbo) {
* The hybrid
*/
@JsonProperty("hybrid")
public String getHybrid() {
public boolean isHybrid() {
return hybrid;
}

Expand All @@ -143,7 +143,7 @@ public String getHybrid() {
* The hybrid
*/
@JsonProperty("hybrid")
public void setHybrid(String hybrid) {
public void setHybrid(boolean hybrid) {
this.hybrid = hybrid;
}

Expand Down
@@ -0,0 +1,33 @@
SELECT ?cnt_1 ?cnt_2 ?cnt_3 (( ( ?cnt_1 + ?cnt_2 ) + ?cnt_3 ) AS ?cnt)
WHERE
{ { SELECT (count(*) AS ?cnt_1)
WHERE
{ ?uri ?p0 ?o0}
}
{ SELECT (count(*) AS ?cnt_2)
WHERE
{ { SELECT DISTINCT ?o0 ?p1 ?o1
WHERE
{ ?uri ?p0 ?o0 .
?o0 ?p1 ?o1
FILTER ( ?o0 != ?uri )
}
}
}
}
{ SELECT (count(*) AS ?cnt_3)
WHERE
{ { SELECT DISTINCT ?o1 ?p1 ?o2
WHERE
{ ?uri ?p0 ?o0 .
?o0 ?p1 ?o1 .
?o1 ?p2 ?o2
FILTER ( ( ?o1 != ?uri ) && ( ?o0 != ?uri ) )
FILTER NOT EXISTS {?uri ?p0x ?o1 .
?o1 ?p1x ?ox
}
}
}
}
}
}

0 comments on commit fc46a4f

Please sign in to comment.