SPARQL DELETE: not possible in jena/pellet? - sparql

I am working on ontology reconciliation between two ontologies. I apply the jena/pellet platform, locally, and apply sparql rules as much as possible. For inserts this is successful, for deletes it is not, no matter what I try. This raises the question whether sparql deletes are supported at all with jena/pellet. Please advise!
Please find the related code snippets below. First the code, then the sparql DELETE query.
public void executeDelete(String mySparqlFile, OntModel o ) {
UpdateRequest updateObj = null;
UpdateProcessor up = null;
GraphStore graphStore = GraphStoreFactory.create();
graphStore.setDefaultGraph( o.getGraph() );
updateObj = UpdateFactory.read( mySparqlFile );
up = UpdateExecutionFactory.create(updateObj, graphStore);
up.execute();
}
public static void main() {
static OntModel ontModel = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC, rawModel);
static OntModel stanfordModel = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
stanfordModel.read("path/to/modelA.owl");
ontModel.addSubModel(stanfordModel);
executeDelete("path/to/delQuery.sparql", ontModel);
}
the file "delQuery.sparql"
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX stfd: <http://www.semanticweb.org/brandtp/ontologies/2014/6/Goose-stanford-metamodel.owl#>
DELETE { ?c a stfd:Token . }
WHERE {
?c stfd:hasFeature stfd:Determiner .
}

Related

OWL query in JENA

part of ontologyi have an owl file and i want to query it in jena. i used of URL of my ontology and i want to use of object properties from my ontology for query. but give me nothing output.do i need to make html document from my ontology to can query it in jena?
// Create a new query
String queryString =
"PREFIX owl: <http://www.w3.org/2002/07/owl#>"+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
"PREFIX base: <http://www.owl-ontologies.com/RailwaysSemantic.owl#>"+
"PREFIX dp_ontology: <http://www.semanticweb.org/ameneh/ontologies/2020/1/dp_ontology#>"+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"SELECT ?design_patterns ?createObject WHERE{ " +
"?design_patterns dp_ontology:concerned ?createObject. "+"}";
Query query = QueryFactory.create(queryString);
// Execute the query and obtain results
QueryExecution qe = QueryExecutionFactory.create(query, m);
try {
ResultSet results = qe.execSelect();
while (results.hasNext()) {
ResultSetFormatter.out(System.out, results, query);
}
}finally {
qe.close();

How to stack SpinSail on top of GraphDB remote repository

I am using GraphDB to store my triples and need to stack SpinSail component upon the GraphDB to support Spin Rules along with all the rest of the features that GraphDB supports by default.
So far I have managed to create a SailRepository on the remote server supporting Spin Rules (more details below) but it seems that it only supports Spin and none of the other features that GraphDB supports (e.g. reviewing the Graph, adding triples through files, searching etc.)
The configuration file, once the repository is created, looks like below:
#prefix ms: <http://www.openrdf.org/config/sail/memory#> .
#prefix rep: <http://www.openrdf.org/config/repository#> .
#prefix sail: <http://www.openrdf.org/config/sail#> .
#prefix sr: <http://www.openrdf.org/config/repository/sail#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<#Test1> a rep:Repository;
rep:repositoryID "Test1";
rep:repositoryImpl [
rep:repositoryType "openrdf:SailRepository";
sr:sailImpl [
sail:delegate [
sail:sailType "openrdf:MemoryStore";
ms:persist true
];
sail:sailType "openrdf:SpinSail"
]
] .
Although a normal configuration file (i.e. if someone would create a repository through the workbench) would look like the one below:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix rep: <http://www.openrdf.org/config/repository#> .
#prefix sail: <http://www.openrdf.org/config/sail#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<#Test> a rep:Repository;
rep:repositoryID "Test";
rep:repositoryImpl [
rep:repositoryType "graphdb:FreeSailRepository";
<http://www.openrdf.org/config/repository/sail#sailImpl> [
<http://www.ontotext.com/trree/owlim#base-URL> "http://example.org/owlim#";
<http://www.ontotext.com/trree/owlim#check-for-inconsistencies> "false";
<http://www.ontotext.com/trree/owlim#defaultNS> "";
<http://www.ontotext.com/trree/owlim#disable-sameAs> "false";
<http://www.ontotext.com/trree/owlim#enable-context-index> "false";
<http://www.ontotext.com/trree/owlim#enable-literal-index> "true";
<http://www.ontotext.com/trree/owlim#enablePredicateList> "true";
<http://www.ontotext.com/trree/owlim#entity-id-size> "32";
<http://www.ontotext.com/trree/owlim#entity-index-size> "10000000";
<http://www.ontotext.com/trree/owlim#imports> "";
<http://www.ontotext.com/trree/owlim#in-memory-literal-properties> "true";
<http://www.ontotext.com/trree/owlim#query-limit-results> "0";
<http://www.ontotext.com/trree/owlim#query-timeout> "0";
<http://www.ontotext.com/trree/owlim#read-only> "false";
<http://www.ontotext.com/trree/owlim#repository-type> "file-repository";
<http://www.ontotext.com/trree/owlim#ruleset> "owl2-rl-optimized";
<http://www.ontotext.com/trree/owlim#storage-folder> "storage";
<http://www.ontotext.com/trree/owlim#throw-QueryEvaluationException-on-timeout> "false";
sail:sailType "graphdb:FreeSail"
]
];
rdfs:label "Test" .
The following code was used to create the repository.
RemoteRepositoryManager manager = new RemoteRepositoryManager("http://localhost:7200");
manager.init();
String repositoryId = "Test1";
// create a configuration for the SAIL stack
boolean persist = true;
// create a configuration for the SAIL stack
SailImplConfig spinSailConfig = new MemoryStoreConfig(persist);
spinSailConfig = new SpinSailConfig(spinSailConfig);
RepositoryImplConfig repositoryTypeSpec = new SailRepositoryConfig(spinSailConfig);
// create a configuration for the repository implementation
// RepositoryImplConfig repositoryTypeSpec = new SailRepositoryConfig(backendConfig);
RepositoryConfig repConfig = new RepositoryConfig(repositoryId, repositoryTypeSpec);
manager.addRepositoryConfig(repConfig);
Once I created this repository I was able to insert the following rule into it through the SPARQL section (by using INSERT DATA):
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sp: <http://spinrdf.org/sp#>
PREFIX spin: <http://spinrdf.org/spin#>
PREFIX ex: <http://example.org/>
INSERT DATA {
ex:Person a rdfs:Class ;
spin:rule [
a sp:Construct ;
sp:text """PREFIX ex: <http://example.org/>
CONSTRUCT { ?this ex:childOf ?parent . }
WHERE { ?parent ex:parentOf ?this . }"""
] . }
and then similarly add the following statements:
PREFIX ex: <http://example.org/>
INSERT DATA {
ex:John a ex:Father ;
ex:parentOf ex:Lucy .
ex:Lucy a ex:Person .
}
After that by running the following query:
PREFIX ex: <http://example.org/>
SELECT ?child WHERE { ?child ex:childOf ?parent }
I was able to confirm that the Spin rule was executed successfully.
So my question is:
Is there a way to create a remote repository supporting all the features of GraphDB and then stack upon it the SpinSail component?
As of the moment GraphDB (version 8.10.0) does not support SpinSail. Such option is under consideration for one of the next GraphDB releases.

SPARQL INSERT not working with PUT method. why?

I am trying to create a new object with PUT method and to add some of my own prefixes with SPARQL query. But, the object is being created without the added prefixes. It works with POST and PATCH though. Why and is there alternative way for SPARQL to use with PUT method and add using user-defined prefixes?
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
DELETE { }
INSERT {
<> indexing:hasIndexingTransformation "default";
rdf:type indexing:Indexable;
dc:title "title3";
dc:identifier "test:10";
}
WHERE { }
What I am saying was all the above values specified in the insert clause are not added at all.
EDIT1:
url = 'http://example.com/rest/object1'
payload = """
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
PREFIX custom: <http://customnamespaces/custom#/>
DELETE { }
INSERT {
<> indexing:hasIndexingTransformation "default";
rdf:type indexing:Indexable;
dc:title "title1";
custom:objState "Active";
custom:ownerId "Owner1";
dc:identifier "object1";
}
WHERE { }
"""
headers = {
'content-type': "application/sparql-update",
'cache-control': "no-cache"
}
response = requests.request("PUT", url, data=payload, headers=headers, auth=('username','password'))
Prefixes are not triples and therefore cannot be added using a SPARQL query. You can always specify prefixes in the SPARQL query and it will generate the correct URI for storage in your triple store.
Also note that your custom namespace is errantly defined by ending with both a hash and a slash. It should be either PREFIX custom: <http://customnamespaces/custom#> or PREFIX custom: <http://customnamespaces/custom/>.
I.e. by your query indexing:hasIndexingTransformation will be stored in the triple store as <http://fedora.info/definitions/v4/indexing#hasIndexingTransformation>.
There is no reason to store the prefix in the triple store (actually, prefixes are an artifact of the text serialization, not the data itself), so you can subsequently query this data in one of two ways.
1) Using a prefix
PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
SELECT ?o {
[] indexing:hasIndexingTransformation ?o .
}
2) Using the full URI:
SELECT ?o {
[] <http://fedora.info/definitions/v4/indexing#hasIndexingTransformation> ?o .
}

sparql queries with round brackets throw exception

I am trying to extract labels from DBpedia for some persons. I am partially successful now, but I got stuck in the following problem. The following code works.
public class DbPediaQueryExtractor {
public static void main(String [] args) {
String entity = "Aharon_Barak";
String queryString ="PREFIX dbres: <http://dbpedia.org/resource/> SELECT * WHERE {dbres:"+ entity+ "<http://www.w3.org/2000/01/rdf-schema#label> ?o FILTER (langMatches(lang(?o),\"en\"))}";
//String queryString="select * where { ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>; <http://www.w3.org/2000/01/rdf-schema#label> ?o FILTER (langMatches(lang(?o),\"en\")) } LIMIT 5000000";
QueryExecution qexec = getResult(queryString);
try {
ResultSet results = qexec.execSelect();
for ( ; results.hasNext(); )
{
QuerySolution soln = results.nextSolution();
System.out.print(soln.get("?o") + "\n");
}
}
finally {
qexec.close();
}
}
public static QueryExecution getResult(String queryString){
Query query = QueryFactory.create(queryString);
//VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, graph);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
return qexec;
}
}
However, when the entity contains brackets, it does not work. For example,
String entity = "William_H._Miller_(writer)";
leads to this exception:
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "(" "( "" at line 1, column 86.`
What is the problem?
It took some copying and pasting to see what exactly was going on. I'd suggest that you put newlines in your query for easier readability. The query you're using is:
PREFIX dbres: <http://dbpedia.org/resource/>
SELECT * WHERE
{
dbres:??? <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER (langMatches(lang(?o),"en"))
}
where ??? is being replaced by the contents of the string entity. You're doing absolutely no input validation here to ensure that the value of entity will be legal to paste in. Based on your question, it sounds like entity contains William_H._Miller_(writer), so you're getting the query:
PREFIX dbres: <http://dbpedia.org/resource/>
SELECT * WHERE
{
dbres:William_H._Miller_(writer) <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER (langMatches(lang(?o),"en"))
}
You can paste that into the public DBpedia endpoint, and you'll get a similar parse error message:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'writer' before ')'
SPARQL query:
define sql:big-data-const 0
#output-format:text/html
define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> PREFIX dbres: <http://dbpedia.org/resource/>
SELECT * WHERE
{
dbres:William_H._Miller_(writer) <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER (langMatches(lang(?o),"en"))
}
Better than hitting DBpedia's endpoint with bad queries, you can also use the SPARQL query validator, which reports for that query:
Syntax error: Lexical error at line 4, column 34. Encountered: ")" (41), after : "writer"
In Jena, you can use the ParameterizedSparqlString to avoid these sorts of issues. Here's your example, reworked to use a parameterized string:
import com.hp.hpl.jena.query.ParameterizedSparqlString;
public class PSSExample {
public static void main( String[] args ) {
// Create a parameterized SPARQL string for the particular query, and add the
// dbres prefix to it, for later use.
final ParameterizedSparqlString queryString = new ParameterizedSparqlString(
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"SELECT * WHERE\n" +
"{\n" +
" ?entity rdfs:label ?o\n" +
" FILTER (langMatches(lang(?o),\"en\"))\n" +
"}\n"
) {{
setNsPrefix( "dbres", "http://dbpedia.org/resource/" );
}};
// Entity is the same.
final String entity = "William_H._Miller_(writer)";
// Now retrieve the URI for dbres, concatentate it with entity, and use
// it as the value of ?entity in the query.
queryString.setIri( "?entity", queryString.getNsPrefixURI( "dbres" )+entity );
// Show the query.
System.out.println( queryString.toString() );
}
}
The output is:
PREFIX dbres: <http://dbpedia.org/resource/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE
{
<http://dbpedia.org/resource/William_H._Miller_(writer)> rdfs:label ?o
FILTER (langMatches(lang(?o),"en"))
}
You can run this query at the public endpoint and get the expected results. Notice that if you use an entity that doesn't need special escaping, e.g.,
final String entity = "George_Washington";
then the query output will use the prefixed form:
PREFIX dbres: <http://dbpedia.org/resource/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE
{
dbres:George_Washington rdfs:label ?o
FILTER (langMatches(lang(?o),"en"))
}
This is very convenient, because you don't have to do any checking about whether your suffix, i.e., entity, has any characters that need to be escaped; Jena takes care of that for you.

How to get data type property values using SPARQL

I have created some sample ontology in protege.According to my ontology there is a class called person and which has sub class called Student.There are some student individuals(john,paul,marry,...).
I have defined some data property called "email" and assigned their email addresses.
Following query which is resulting all the individuals in ontology.But I want to get each individual and their email address.
String queryStr =
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
"select ?ind "+
"where { "+
"?ind rdf:type <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#Student> ;"+
"}\n ";
Above query was tested on jena in eclipse IDE.
any idea..?
Thank in advance!
To get the email addresses you need to 1) add a variable for them to the SELECT line, and 2) bind that variable in the WHERE pattern. And you'll probably want to add a prefix for your own ontology, since you'll now need to refer to it twice. Something like:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#>
SELECT ?ind ?email
WHERE {
?ind rdf:type my:Student .
?ind my:Email ?email
}
A more compact way would be the following:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX my: <http://www.semanticweb.org/ontologies/2010/5/Ontology1275975684120.owl#>
SELECT ?ind ?email
WHERE {
?ind rdf:type my:Student ;
my:Email ?email .
}