SPARQL-DL (derivo API) : Retrieving instances with a specific dataproperty string value - sparql

I'm trying to query an ontology in order to get all the instances of Person having the name "Andrea" with SPARQL-DL via owlapi:
SELECT ?i
WHERE { Type(?i, ns:Person), PropertyValue(?i, ns:name, "Andrea") }
I'm getting this:
[http-nio-8080-exec-10] org.primefaces.application.exceptionhandler.PrimeExceptionHandler.logException uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl.getInstance()Lorg/semanticweb/owlapi/model/OWLDataFactory;
java.lang.NoSuchMethodError: uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl.getInstance()Lorg/semanticweb/owlapi/model/OWLDataFactory;
at de.derivo.sparqldlapi.impl.QueryParserImpl.appendLiteral(QueryParserImpl.java:514)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parseVariableBlankURILiteral(QueryParserImpl.java:450)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parseAtom(QueryParserImpl.java:314)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parseAtoms(QueryParserImpl.java:267)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parseWhere(QueryParserImpl.java:192)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parseSelect(QueryParserImpl.java:139)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parseQueryHead(QueryParserImpl.java:111)
at de.derivo.sparqldlapi.impl.QueryParserImpl.parse(QueryParserImpl.java:67)
at de.derivo.sparqldlapi.Query.create(Query.java:106)
Thanks to all,
Andrea

Related

Extract all rdfs:type from a specific DBPedia entry

all.
I'm trying to write a simple SPARQL query generator to fetch all rdf:type relations of a specific DBPedia resource.
query = """SELECT * WHERE {{ <""" + resource """> rdfs:type ?subject.}}"""
This yields the Query
SELECT * WHERE {{ <http://dbpedia.org/page/Energy> rdfs:type ?subject.}}
But the query returns empty. What am I doing wrong? The DBPedia entry clearly has rdfs:type relations:
owl:Thing
dbo:Building
yago:Abstraction100002137
yago:Assets113329641
yago:NaturalResource113332009
yago:Possession100032613
yago:Relation100031921
yago:Resource113331778
yago:WikicatNaturalResources
Thanks in advance!
Change the energy address from page to resource, the query looks like this (in addition, I suggest you to use the a instead of therdf:type):
SELECT * WHERE {{ <http://dbpedia.org/resource/Energy> a ?subject.}}
In order to avoid this issue, chech the exact resurces addresses in a raw data format. For example, the XML triples can be reviewed with a web browser from the dbpedia webpage. http://dbpedia.org/page/Energy, in the top bar there is a button named formats.

How to filter query SPARQL for property "type"

I have a data source file that one of its properties is an actual class instance:
<clinic:Radiology rdf:ID="rad1234">
<clinic:diagnosis>Stage 4</clinic:diagnosis>
<clinic:ProvidedBy rdf:resource="#MountSinai"/>
<clinic:ReceivedBy rdf:resource="#JohnSmith"/>
<clinic:patientId>7890123</clinic:patientId>
<clinic:radiologyDate>01-01-2017</clinic:radiologyDate>
</clinic:Radiology>
so clinic:ProvidedBy is pointing to this:
<clinic:Radiologists rdf:ID="MountSinai">
<clinic:name>Mount Sinai</clinic:name>
<clinic:npi>1234567</clinic:npi>
<clinic:specialty>Oncology</clinic:specialty>
</clinic:Radiologists>
How do I query using the property clinic:providedBy (which is of type clinic:Radiologists)? Whatever I have tried does not bring back results.
It's also not clear what exactly you want to have, so my answer will return "all radiology resources that are provided by MountSinai":
PREFIX clinic: <THE NAMESPACE OF_THE_CLINIC_PREFIX>
PREFIX : <THE_BASE_NAMESPACE_OF_YOUR_RDF_DOCUMENT>
SELECT DISTINCT ?s WHERE {
?s clinic:ProvidedBy :MountSinai
}
But, I really suggest to start with an RDF and SPARQL tutorial, since form your comment your query
SELECT * WHERE { ?x rdf:resource "#MountSinai" }
is missing fundamental SPARQL basics. And for writing a matching SPARQL query it'S always good to have a look at the data in Turtle resp. N-Triples format both of which being closer to the SPARQL syntax.

Result of semantic's query sqwrl:makeBag

I have an example about ontology with only class Person and a try to create a collection throw sqwrl:makeBag
Person(?p) ˚ sqwrl:makeBag(?bag, ?p) ˚ sqwrl:size(?size, ?bag) -> sqwrl:select(?size)
And I have 4 individuals with type Person but when I run the query result is 1. I don't know why please help me thanks so much

Adding Individuals in Jena OntMOdel and accessing it. Exception ObjectFileStorage.read Impossibly large object

I am trying to add some individuals to my existing Ontology (OntModel) with an objective to add the values/literals for DatatypeProperty with a specific datatype known at runtime from the range of the datatypeproperty. My OntModel is backed by a TDBStore, linked to a dataset(so any changes made to the OntModel ins reflected back to my TDBStore/dataset). Individuals are added as per the following code :
Individual ind =oc.createIndividual(namespace+nameOfIndividual); // oc is OntClass object
Literal l = ontModel.createTypedLiteral("1230",dp.getRange().getURI()); //dp is a DatatypeProperty object
ind.addLiteral(dp,l);
When the code executes the the literal is added and a sparql query :
"SELECT * WHERE { ?s :ABCConstant ?o }"; //:ABCConstant is the datatypeProperty for which the literal is added by the above code.
Gives me the following result:
------------------------------------------------------------------------------------------------------------------------------------------------
| s | o |
================================================================================================================================================
| <http://www.semanticweb.org/ontologies/2012/10/Ontology.owl#individual1ABCDConstant> | "10000.0"^^<http://www.w3.org/2001/XMLSchema#int> |
| <http://www.semanticweb.org/ontologies/2012/10/Ontology.owl#individual2ABCDConstant> | 1230 |
------------------------------------------------------------------------------------------------------------------------------------------------
But when i try to use the same query in a second run(without creating any individual this time) I get the following Exception when the program tries to display/access the result:
Exception in thread "main" com.hp.hpl.jena.tdb.base.file.FileException: ObjectFileStorage.read[nodes](181624)[filesize=248062][file.size()=248062]: Impossibly large object : 1634628966 bytes > filesize-(loc+SizeOfInt)=66434
at com.hp.hpl.jena.tdb.base.objectfile.ObjectFileStorage.read(ObjectFileStorage.java:346)
at com.hp.hpl.jena.tdb.lib.NodeLib.fetchDecode(NodeLib.java:78)
at com.hp.hpl.jena.tdb.nodetable.NodeTableNative.readNodeFromTable(NodeTableNative.java:178)
at com.hp.hpl.jena.tdb.nodetable.NodeTableNative._retrieveNodeByNodeId(NodeTableNative.java:103)
at com.hp.hpl.jena.tdb.nodetable.NodeTableNative.getNodeForNodeId(NodeTableNative.java:74)
at com.hp.hpl.jena.tdb.nodetable.NodeTableCache._retrieveNodeByNodeId(NodeTableCache.java:103)
at com.hp.hpl.jena.tdb.nodetable.NodeTableCache.getNodeForNodeId(NodeTableCache.java:74)
at com.hp.hpl.jena.tdb.nodetable.NodeTableWrapper.getNodeForNodeId(NodeTableWrapper.java:55)
at com.hp.hpl.jena.tdb.nodetable.NodeTableInline.getNodeForNodeId(NodeTableInline.java:67)
at com.hp.hpl.jena.tdb.lib.TupleLib.triple(TupleLib.java:126)
at com.hp.hpl.jena.tdb.lib.TupleLib.triple(TupleLib.java:114)
at com.hp.hpl.jena.tdb.lib.TupleLib.access$000(TupleLib.java:45)
at com.hp.hpl.jena.tdb.lib.TupleLib$3.convert(TupleLib.java:76)
at com.hp.hpl.jena.tdb.lib.TupleLib$3.convert(TupleLib.java:72)
at org.apache.jena.atlas.iterator.Iter$4.next(Iter.java:317)
at org.apache.jena.atlas.iterator.Iter$4.next(Iter.java:317)
at org.apache.jena.atlas.iterator.Iter$4.next(Iter.java:317)
at org.apache.jena.atlas.iterator.Iter.next(Iter.java:915)
at com.hp.hpl.jena.util.iterator.WrappedIterator.next(WrappedIterator.java:94)
at com.hp.hpl.jena.util.iterator.WrappedIterator.next(WrappedIterator.java:94)
at com.hp.hpl.jena.util.iterator.FilterIterator.hasNext(FilterIterator.java:55)
at com.hp.hpl.jena.graph.compose.CompositionBase$2.hasNext(CompositionBase.java:94)
at com.hp.hpl.jena.util.iterator.NiceIterator$1.hasNext(NiceIterator.java:103)
at com.hp.hpl.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIterTriplePattern$TripleMapper.hasNextBinding(QueryIterTriplePattern.java:151)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIterRepeatApply.hasNextBinding(QueryIterRepeatApply.java:81)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIterBlockTriples.hasNextBinding(QueryIterBlockTriples.java:64)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.hasNext(ResultSetStream.java:75)
at com.hp.hpl.jena.sparql.resultset.ResultSetMem.<init>(ResultSetMem.java:97)
at com.hp.hpl.jena.query.ResultSetFactory.makeRewindable(ResultSetFactory.java:420)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:149)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:132)
at com.hp.hpl.jena.sparql.resultset.TextOutput.write(TextOutput.java:120)
at com.hp.hpl.jena.sparql.resultset.TextOutput.format(TextOutput.java:67)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:122)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:74)
at com.hp.hpl.jena.query.ResultSetFormatter.out(ResultSetFormatter.java:65)
at senseXploreApi.SparqlQuery.sparqlQuery(SparqlQuery.java:82)
at senseXploreApi.TryMain.main(TryMain.java:39)
Note: Other queries to view the added individuals execute fine.
But Queries like:
"SELECT * WHERE { ?s ?o 1230}"
lead to the same error.
Also query like :
"SELECT * WHERE { ?s ?o 10000.0}"
OR
"SELECT * WHERE { ?s ?o 10000}"
don't give any error but return nothing in result.
the literal 10000 was added using the statement :
ind.addLiteral(opp,new Integer(10000));
Please help me!! where am i wrong.. Is the procedure used to create individuals is wrong? if yes! then what can be other possible ways to add literals with specific datatype known at runtime?
The Impossibly Large Object message means that your TDB database is (partially) corrupted, specifically part of the node table that maps between database internal identifiers and the original RDF terms is corrupted. Any query that touches that part of the node table will see this error, other queries may still run without issue.
The only way to recover your database is to rebuild it from the original data, once the corruption has happened it cannot be repaired.
Corruption can happen in various ways, the most common of which are:
Accessing TDB in a non-transactional manner and failing to call sync() on the dataset after making changes (or more likely in your example failing to call close() on the Model after making changes)
Having multiple JVMs accessing the same TDB dataset at the same time, if your application requires this use a server like Fuseki to centralise access to the database.
See the TDB Transactions for how to use TDB transactionally or see the Fuseki documentation if you need to expose a TDB database to multiple JVMs simultaneously.

How to assign parameter values in a SparqlParameterizedString

I'm playing around some with Dotnetrdf's sparql engine and I'm trying to create parametered queries with no success yet.
Say I'm working on a graph g with a blank node identified as _:1690 with the code
Dim queryString As SparqlParameterizedString = New SparqlParameterizedString()
queryString.Namespaces.AddNamespace("rdfs", UriFactory.Create("http://www.w3.org/2000/01/rdf-schema#"))
queryString.CommandText = "SELECT ?label { #context rdfs:label ?label } "
queryString.SetParameter("context", g.GetBlankNode("1690"))
Dim result As VDS.RDF.Query.SparqlResultSet = g.ExecuteQuery(New SparqlQueryParser().ParseFromString(queryString))
Whenever I run this, I get all nodes having a rdfs:label property instead of filtering the result on my blank node only.
Please, how to set the parameter's value properly so I get only one item in the result ?
Thanks in advance,
Max.
Blank Nodes in a SPARQL query differ from Blank Nodes in a RDF Graph
In a SPARQL Query a blank node is treated as a temporary variable which has limited scope, it does not match a specific blank node so you cannot write a SPARQL query to select by blank node identifier.
So your code creating your query is giving a result the same as if you replaced #context with a variable e.g. ?s
If you need to find the value associated with a specific blank node then you need to formulate a query that uniquely selects that blank node based on the triples it participates in. If you can't do that then you need to re-think your data modelling since if this is the case then you should likely be using URIs instead of blank nodes.
As a workaround since you are using dotNetRDF and have the original graph you are querying you can use the IGraph API instead e.g.
INode label = g.GetTriplesWithSubjectPredicate(g.GetBlankNode("1690"), g.CreateUriNode("rdfs:label")).Select(t => t.Object).FirstOrDefault();
Just remember that label could always be null if the triple you are looking for doesn't exist