Skip to content
Snippets Groups Projects
Commit 9dde8ddb authored by Valerian Wintner's avatar Valerian Wintner Committed by Gritsch Philipp - Studierendenaccount
Browse files

Query: Option to only get properties belonging to this class, to the superclass, or everything.

parent 640e9e55
No related branches found
No related tags found
1 merge request!5Fixes RDF2Graph further.
package com.kgshape.app;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;
/**
* Queries a remote endpoint.
*/
......@@ -22,20 +22,81 @@ public class Query {
PREFIX o: <http://dbpedia.org/ontology/>
select distinct ?subj where {?subj a o:Language} LIMIT 20
""";
// Get 100 different actors
String construct_query = """
String format_construct_all = """
PREFIX o: <http://dbpedia.org/ontology/>
CONSTRUCT {?person ?pred ?obj}
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?subj ?pred ?obj.
}
WHERE {
?person ?pred ?obj
{
SELECT DISTINCT(?person)
WHERE {
?person a o:Actor
} LIMIT 100
}
?subj ?pred ?obj.
{
SELECT DISTINCT(?subj)
WHERE {
?subj a o:%1$s
} LIMIT 100
}
}
""";
String format_construct = """
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?subj ?pred ?obj.
?subj a o:%1$s.
}
WHERE {
?subj ?pred ?obj.
{
SELECT DISTINCT(?subj)
WHERE {
?subj a o:%1$s
} LIMIT 100
}
{
SELECT DISTINCT ?pred WHERE {
?pred rdfs:domain o:%1$s.
}
}
}
""";
String format_construct_super = """
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?subj ?pred ?obj.
?subj a ?class.
}
WHERE {
?subj ?pred ?obj.
?subj a ?class.
{
SELECT DISTINCT(?subj)
WHERE {
?subj a o:%1$s
} LIMIT 100
}
{
SELECT DISTINCT ?pred WHERE {
{
?pred rdfs:domain ?class.
}
}
}
{
SELECT ?class WHERE {
o:%1$s rdfs:subClassOf* ?class.
}
}
}
""";
// System.out.println(construct_query);
String construct_query2 = """
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
......@@ -47,8 +108,26 @@ public class Query {
// You can also open this endpoint in the browser and test the above query
String endpoint = "https://dbpedia.org/sparql";
// Pick the name of the class we want to extract from the arguments
String className;
if (args.length < 1) {
className = "Artist";
} else {
className = args[0];
}
String construct_query = String.format(format_construct, className);
Model fetchedModel = fetchConstruct(endpoint, construct_query);
writeModel(fetchedModel, "rdf_file.ttl");
writeModel(fetchedModel, "rdf_" + className + ".ttl");
String construct_query_super = String.format(format_construct_super, className);
fetchedModel = fetchConstruct(endpoint, construct_query_super);
writeModel(fetchedModel, "rdf_" + className + "_super.ttl");
String construct_query_all = String.format(format_construct_all, className);
fetchedModel = fetchConstruct(endpoint, construct_query_all);
writeModel(fetchedModel, "rdf_" + className + "_all.ttl");
}
public static Model fetchConstruct(String endpoint, String query) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment