From 9dde8ddba4a2bc3342ed9a81c2d6d68269511f4e Mon Sep 17 00:00:00 2001 From: Valerian Wintner <v.d.w@hotmail.com> Date: Sat, 27 Nov 2021 19:56:31 +0100 Subject: [PATCH] Query: Option to only get properties belonging to this class, to the superclass, or everything. --- .../src/main/java/com/kgshape/app/Query.java | 109 +++++++++++++++--- 1 file changed, 94 insertions(+), 15 deletions(-) diff --git a/framework/src/main/java/com/kgshape/app/Query.java b/framework/src/main/java/com/kgshape/app/Query.java index 21932e3..39aadd3 100644 --- a/framework/src/main/java/com/kgshape/app/Query.java +++ b/framework/src/main/java/com/kgshape/app/Query.java @@ -1,15 +1,15 @@ 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) { -- GitLab