See sparql with specific information - sparql

I want to visualize everything (well not all only those attributes) concerning the area to be called, without the filter all are displayed and even information that I do not consider relevant, so I only want to see those attributes (predicates).
It's not even hard for me to understand sparql and I have to show results now !! so thanks in advance
Query
PREFIX dco: <http://192.168.1.10:8890/task/ontology/>
PREFIX dcr: <http://192.168.1.10:8890/digcomp/resource/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select * where {
?Sujeto ?Predicado ?Objeto .
?Sujeto rdf:type dco:Areas .
FILTER (?Objeto="Information and data literacy")
}
Result
| Subject | Predicate | Object |
| ------------- |:-------------:| -----:|
| dcr:areas/1 | rdfs:label | "Information and data literacy" |
Desired result
| Subject | Predicate | Object |
| ------------- |:-------------:| -----:|
| dcr:areas/1 | rdf:type | dco:Areas |
| dcr:areas/1 | rdfs:label | "Information and data literacy" |
| dcr:areas/1 | dcr:name_area | "Information and data literacy"|

Related

Why does this SPARQL query return certain items as subjects?

I have the following data graph:
#prefix hr: <http://learningsparql.com/ns/humanResources#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix sch: <http://schema.org/> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
hr:Another a rdfs:Class .
hr:Employee a rdfs:Class ;
rdfs:label "model" ;
rdfs:comment "a good employee" .
hr:Longer a hr:Employee ;
rdfs:label "model" ;
rdfs:comment "a good employee" .
hr:freestanding a rdf:Property ;
sch:rangeIncludes sch:Text .
hr:missing rdfs:comment "some comment about missing" .
hr:name a rdf:Property ;
sch:domainIncludes hr:Employee .
hr:nosuper a rdf:Property ;
sch:domainIncludes hr:Uncreated ;
sch:rangeIncludes sch:Text .
hr:randomtype a hr:invalidtype ;
rdfs:label "some label about randomtype" ;
rdfs:comment "some comment about randomtype" .
hr:typo a rdfs:Classs ;
rdfs:label "some label about typo" ;
rdfs:comment "some comment about typo" .
The only subjects defined in the triples are hr:Another, hr:Employee, hr:Longer, hr:freestanding, hr:missing, hr:name, hr:nosuper, hr:randomtype, and hr:typo. As proof, when I run the query:
SELECT DISTINCT ?s
WHERE {
?s ?p ?o .
}
I get the result:
--------------------------------------------------------------
| s |
==============================================================
| <http://learningsparql.com/ns/humanResources#freestanding> |
| <http://learningsparql.com/ns/humanResources#Another> |
| <http://learningsparql.com/ns/humanResources#typo> |
| <http://learningsparql.com/ns/humanResources#nosuper> |
| <http://learningsparql.com/ns/humanResources#Employee> |
| <http://learningsparql.com/ns/humanResources#randomtype> |
| <http://learningsparql.com/ns/humanResources#Longer> |
| <http://learningsparql.com/ns/humanResources#missing> |
| <http://learningsparql.com/ns/humanResources#name> |
--------------------------------------------------------------
However, if I execute this SPARQL query:
SELECT DISTINCT ?s
WHERE {
{
?s rdf:type* ?o .
}
}
I get the following results:
--------------------------------------------------------------
| s |
==============================================================
| <http://learningsparql.com/ns/humanResources#Longer> |
| rdfs:Class |
| sch:Text |
| "some comment about typo" |
| "some label about typo" |
| <http://learningsparql.com/ns/humanResources#nosuper> |
| "a good employee" |
| <http://learningsparql.com/ns/humanResources#Uncreated> |
| <http://learningsparql.com/ns/humanResources#missing> |
| "some comment about randomtype" |
| "model" |
| <http://learningsparql.com/ns/humanResources#freestanding> |
| "some label about randomtype" |
| "some comment about missing" |
| <http://learningsparql.com/ns/humanResources#Another> |
| <http://learningsparql.com/ns/humanResources#invalidtype> |
| <http://learningsparql.com/ns/humanResources#typo> |
| <http://learningsparql.com/ns/humanResources#randomtype> |
| <http://learningsparql.com/ns/humanResources#Employee> |
| rdfs:Classs |
| rdf:Property |
| <http://learningsparql.com/ns/humanResources#name> |
--------------------------------------------------------------
I am not sure why items like rdf:Property, "a good employee", sch:Text, etc. are being returned as subjects when they are not subjects in the data graph.
Why are they?
Clearly, this has something to do with how SPARQL handles property paths which I do not yet understand.
You are using a property path with a zero-or-more wildcard ('*'). Effectively, the pattern ?s rdf:type* ?o means "every value ?s connected through zero or more type relations to any value ?o".
Every value that appears in your graph (in fact every value in any graph) matches that, because every value has at least zero type relations. Therefore, a value like "some comment about typo" get returned, because although it never occurs as a subject in your data, it is true that it is a value that has zero type relations to something else.

SPARQL filter/aggregate results by subgraph

I would like to write a query which filters out some results by using some other properties of the graph. I explain with this example.
I am modelling a permissions system. There are some subjects, resources and permissions. Subjects have certain permissions on resources. Some permissions imply other permissions. A simplified example with 3 permissions, and 3 resources that the single subject has one (two in one case) of those permissions on.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://schema#>
PREFIX d: <http://data#>
INSERT DATA {
:Permission rdf:type rdf:Property .
:Read rdf:type rdf:Property ;
rdfs:subPropertyOf :Permission .
:Write rdf:type rdf:Property ;
rdfs:subPropertyOf :Permission ;
:implies :Read .
:Admin rdf:type rdf:Property ;
rdfs:subPropertyOf :Permission ;
:implies :Write ,
:Read .
d:s1 rdf:type :Subject .
d:r1 rdf:type :Resource .
d:r2 rdf:type :Resource .
d:r3 rdf:type :Resource .
d:s1 :Admin d:r1 .
d:s1 :Read d:r1 .
d:s1 :Write d:r2 .
d:s1 :Read d:r3 .
}
I can easily write a query to see for a given subject, what the implied permissions are on some resources.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://schema#>
PREFIX d: <http://data#>
SELECT DISTINCT ?subject ?resource ?implied (?permission AS ?implied_by)
WHERE {
BIND(d:s1 AS ?subject)
?resource rdf:type :Resource .
?subject rdf:type :Subject .
?permission rdfs:subPropertyOf :Permission ;
:implies* ?implied .
?subject ?permission ?resource .
}
This will give some results like:
+----------------+----------------+---------------------+---------------------+
| subject | resource | implied | implied_by |
+----------------+----------------+---------------------+---------------------+
| http://data#s1 | http://data#r1 | http://schema#Admin | http://schema#Admin |
| http://data#s1 | http://data#r1 | http://schema#Read | http://schema#Admin |
| http://data#s1 | http://data#r1 | http://schema#Write | http://schema#Admin |
| http://data#s1 | http://data#r1 | http://schema#Read | http://schema#Read |
| http://data#s1 | http://data#r3 | http://schema#Read | http://schema#Read |
| http://data#s1 | http://data#r2 | http://schema#Write | http://schema#Write |
| http://data#s1 | http://data#r2 | http://schema#Read | http://schema#Write |
+----------------+----------------+---------------------+---------------------+
What I would like to do is write a query which only returns the minimum list of permissions per resource which imply all the others. Any ideas on how I would achieve this efficiently would be most welcome. This seems a bit like a MAX aggregation, but where a graph is actually needed to determine the results. For example, the ideal results would be:
+----------------+----------------+---------------------+---------------------+
| subject | resource | implied | implied_by |
+----------------+----------------+---------------------+---------------------+
| http://data#s1 | http://data#r1 | http://schema#Admin | http://schema#Admin |
| http://data#s1 | http://data#r3 | http://schema#Read | http://schema#Read |
| http://data#s1 | http://data#r2 | http://schema#Write | http://schema#Write |
+----------------+----------------+---------------------+---------------------+
Note that the data and queries are actually more complicated that this so it is absolutely necessary for me to use the parameter path even though I then want to get rid of most of the information it introduces.
?permission rdfs:subPropertyOf :Permission ;
:implies* ?implied .
Note also that I thought of adding an integer value to each permission so that I could simply get the minimum value of this field, but my permission structure is not single rooted in reality, so sometimes this would not be appropriate.
Effectively the question is how to remove any permissions (for a resource) which are implied by other results (for that resource)

Following namespaces in SPARQL query

Given this data structure
#prefix core <http://www.w3.org/2004/02/skos/core#>
<http://localhost/myvocab/1> core#notation 1 .
<http://localhost/myvocab/1> <http://www.w3.org/2006/time#inDateTime> <http://localhost/myvocab/item1#DateDescription> .
<http://localhost/myvocab/item1#DateDescription> <http://www.w3.org/2006/time#year> 2016 ;
<http://www.w3.org/2006/time#month> "June"
If I run
select * where {?s ?p ?o .
<http://www.w3.org/2004/02/skos/core#notation> 1 . }
then only the first 2 triples (:hasID and :hasTime) are returned. Is there a sparql query (preferably not using a regex filter for matching the id) to return all triples from the child namespaces too?
I'm hoping I can achieve a result something along the lines of
-------------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
=======================================================================================================================================================
| <http://localhost/myvocab/item1> | <http://www.w3.org/2006/time#inDateTime> | <http://localhost/myvocab/item1#DateDescription |
| <http://localhost/myvocab/item1> | <http://www.w3.org/2004/02/skos/core#notation> | 1 |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#month> | "June" |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#year> | 2016 |
-------------------------------------------------------------------------------------------------------------------------------------------------------
It always helps if you provide data that we can actually load and property formed queries that you've tried. The data that you've shown isn't actually legal, nor is the query that you provided. Here's some sample data that's actually loadable:
#prefix core: <http://www.w3.org/2004/02/skos/core#>
<http://localhost/myvocab/1> core:notation 1 ;
<http://www.w3.org/2006/time#inDateTime> <http://localhost/myvocab/item1#DateDescription> .
<http://localhost/myvocab/item1#DateDescription> <http://www.w3.org/2006/time#year> 2016 ;
<http://www.w3.org/2006/time#month> "June"
If I understand you, you want to follow the :item1_DateTime object's properties too. You can do that with a property path that follows :item1's properties and value, and so on. In this query, I use (:|!:) as a wildcard, since every property is either : or it isn't. The * at the end means to follow paths of arbitrary length.
prefix core: <http://www.w3.org/2004/02/skos/core#>
select ?s ?p ?o where {
?s_ core:notation 1 ;
(<>|!<>)* ?s .
?s ?p ?o .
}
--------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
==================================================================================================================================================
| <http://localhost/myvocab/1> | <http://www.w3.org/2006/time#inDateTime> | <http://localhost/myvocab/item1#DateDescription> |
| <http://localhost/myvocab/1> | core:notation | 1 |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#month> | "June" |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#year> | 2016 |
--------------------------------------------------------------------------------------------------------------------------------------------------

SPARQL Get all the properties from a Class or an Individual

What I want to do is to get the property list from the Individual1 or from a class,
Get all properties from "something"
The result should be something like this (for Secret_Data):
| Asset_has_Confidentiality_Importance | High |
| Asset_has_Availability_Importance....| Moderate |
| Asset_has_Integrity_Importance.......| Moderate |
| Asset_threatenedBy_ThreatEvent.......| Compromise_sensitive_information |
| Asset_threatenedBy_ThreatEvent.......| Disclosure_of_sensitive_information |
| Asset_threatenedBy_ThreatEvent.......| Exploit_exposed_unauthorized_information |
| Asset_threatenedBy_ThreatEvent.......| Integrity_loss_by_corrupting_critital_data |
<owl:NamedIndividual rdf:about="&securityOntology_ITESM_UC3M;Secret_Data">
<rdf:type rdf:resource="&securityOntology_ITESM_UC3M;Data"/>
<Asset_has_Confidentiality_Importance rdf:datatype="&xsd;string">High</Asset_has_Confidentiality_Importance>
<Asset_has_Availability_Importance rdf:datatype="&xsd;string">Moderate</Asset_has_Availability_Importance>
<Asset_has_Integrity_Importance rdf:datatype="&xsd;string">Moderate</Asset_has_Integrity_Importance>
<Asset_threatenedBy_ThreatEvent rdf:resource="&securityOntology_ITESM_UC3M;Compromise_sensitive_information"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="&securityOntology_ITESM_UC3M;Disclosure_of_sensitive_information"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="&securityOntology_ITESM_UC3M;Exploit_exposed_unauthorized_information"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="&securityOntology_ITESM_UC3M;Integrity_loss_by_corrupting_critital_data"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="&securityOntology_ITESM_UC3M;Obtain_unauthorized_access"/>
</owl:NamedIndividual>
I think the query is something like this, (But the value is not well formatted, and some properties are not importart like "type property"):
PREFIX css:<http://www.semanticweb.org/evalues/ontologies/2014/3/securityOntology_ITESM_UC3M#>
SELECT DISTINCT ?property ?value
WHERE {
css:Secret_Data ?property ?value.
}
Answer to updated question (a query about individuals)
It's much easier to answer question with complete sample data. If we bundle up the data that you've given with appropriate namespace declarations, we end up with something like this:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns="http://stackoverflow.com/q/23223447/1281433/">
<owl:NamedIndividual rdf:about="http://stackoverflow.com/q/23223447/1281433/Secret_Data">
<Asset_has_Availability_Importance rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Moderate</Asset_has_Availability_Importance>
<rdf:type rdf:resource="http://stackoverflow.com/q/23223447/1281433/Data"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="http://stackoverflow.com/q/23223447/1281433/Integrity_loss_by_corrupting_critital_data"/>
<Asset_has_Integrity_Importance rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>Moderate</Asset_has_Integrity_Importance>
<Asset_threatenedBy_ThreatEvent rdf:resource="http://stackoverflow.com/q/23223447/1281433/Obtain_unauthorized_access"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="http://stackoverflow.com/q/23223447/1281433/Disclosure_of_sensitive_information"/>
<Asset_threatenedBy_ThreatEvent rdf:resource="http://stackoverflow.com/q/23223447/1281433/Compromise_sensitive_information"/>
<Asset_has_Confidentiality_Importance rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>High</Asset_has_Confidentiality_Importance>
<Asset_threatenedBy_ThreatEvent rdf:resource="http://stackoverflow.com/q/23223447/1281433/Exploit_exposed_unauthorized_information"/>
</owl:NamedIndividual>
</rdf:RDF>
It's often helpful to view this in Turtle serialization, since it will look more like the query. The data is the same though, and the query will run against the data in either serialization, of course.
#prefix : <http://stackoverflow.com/q/23223447/1281433/> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:Secret_Data a owl:NamedIndividual , :Data ;
:Asset_has_Availability_Importance
"Moderate"^^<http://www.w3.org/2001/XMLSchema#string> ;
:Asset_has_Confidentiality_Importance
"High"^^<http://www.w3.org/2001/XMLSchema#string> ;
:Asset_has_Integrity_Importance
"Moderate"^^<http://www.w3.org/2001/XMLSchema#string> ;
:Asset_threatenedBy_ThreatEvent
:Integrity_loss_by_corrupting_critital_data , :Obtain_unauthorized_access , :Disclosure_of_sensitive_information , :Compromise_sensitive_information , :Exploit_exposed_unauthorized_information .
You can use a query like the following:
prefix : <http://stackoverflow.com/q/23223447/1281433/>
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#>
select distinct ?property ?value where {
:Secret_Data ?property ?value .
}
---------------------------------------------------------------------------------------
| property | value |
=======================================================================================
| rdf:type | owl:NamedIndividual |
| :Asset_has_Availability_Importance | "Moderate"^^xsd:string |
| rdf:type | :Data |
| :Asset_threatenedBy_ThreatEvent | :Integrity_loss_by_corrupting_critital_data |
| :Asset_has_Integrity_Importance | "Moderate"^^xsd:string |
| :Asset_threatenedBy_ThreatEvent | :Obtain_unauthorized_access |
| :Asset_threatenedBy_ThreatEvent | :Disclosure_of_sensitive_information |
| :Asset_threatenedBy_ThreatEvent | :Compromise_sensitive_information |
| :Asset_has_Confidentiality_Importance | "High"^^xsd:string |
| :Asset_threatenedBy_ThreatEvent | :Exploit_exposed_unauthorized_information |
---------------------------------------------------------------------------------------
If you don't want rdf:type in there, you could use filter ?property != rdf:type, or if there are multiple properties that you want to exclude, filter ( ?property NOT IN ( rdf:type … ) ):
prefix : <http://stackoverflow.com/q/23223447/1281433/>
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#>
select distinct ?property ?value where {
:Secret_Data ?property ?value .
filter ( ?property not in ( rdf:type ) )
}
---------------------------------------------------------------------------------------
| property | value |
=======================================================================================
| :Asset_has_Availability_Importance | "Moderate"^^xsd:string |
| :Asset_threatenedBy_ThreatEvent | :Integrity_loss_by_corrupting_critital_data |
| :Asset_has_Integrity_Importance | "Moderate"^^xsd:string |
| :Asset_threatenedBy_ThreatEvent | :Obtain_unauthorized_access |
| :Asset_threatenedBy_ThreatEvent | :Disclosure_of_sensitive_information |
| :Asset_threatenedBy_ThreatEvent | :Compromise_sensitive_information |
| :Asset_has_Confidentiality_Importance | "High"^^xsd:string |
| :Asset_threatenedBy_ThreatEvent | :Exploit_exposed_unauthorized_information |
---------------------------------------------------------------------------------------
Answer to the original question (a query about classes)
In the original version of the question, you gave this image of your ontology and asked for results that are after it:
| numberChapters | 1 |
| numberPages | 5 |
In the RDF/XML serialization of that ontology (since removed from the question), the description of the Psicology class was as follows:
<owl:Class rdf:about="http://webprotege.stanford.edu/RiMIUJzVuVHCr4Ke4TkFwX">
<rdfs:label>Psicology</rdfs:label>
<rdfs:subClassOf rdf:resource="http://webprotege.stanford.edu/RDHbMG5sh4tyJ6ZoHwxLBHk"/>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://webprotege.stanford.edu/R9DdxIZDrtXApiHoBJ6NmAy"/>
<owl:hasValue rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">5</owl:hasValue>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://webprotege.stanford.edu/R8WDOefPWzIoTChbjV31ela"/>
<owl:hasValue rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">1</owl:hasValue>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
or in the more human-readable Turtle notation:
<http://webprotege.stanford.edu/RiMIUJzVuVHCr4Ke4TkFwX>
a owl:Class ;
rdfs:label "Psicology" ;
rdfs:subClassOf <http://webprotege.stanford.edu/RDHbMG5sh4tyJ6ZoHwxLBHk> ;
rdfs:subClassOf [ a owl:Restriction ;
owl:hasValue 5 ;
owl:onProperty <http://webprotege.stanford.edu/R9DdxIZDrtXApiHoBJ6NmAy>
] ;
rdfs:subClassOf [ a owl:Restriction ;
owl:hasValue 1 ;
owl:onProperty <http://webprotege.stanford.edu/R8WDOefPWzIoTChbjV31ela>
says that Psicology (which is usually spelled Psychology in English, by the way) is a subclass of the class of things that have the value 5 for the property numPages and the things that have the value 1 for the property numChapters. Psicology is also a subclass of Libray. (Is this supposed to be Library?)
These seem like strange axioms to me, and I wonder if there might be some sort of modeling error going on. I'm not sure what a Psicology is, but you're saying that for any Psicology p, it's the case that numPages(p,5) and numChapters(p,1). Is that really what you want to do?
At any rate, given your data, we can retrieve the results that you want. Looking at the Turtle serialization is useful, because it's very similar to the SPARQL query syntax. The query you want is:
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix : <http://webprotege.stanford.edu/project/Bnag2Z5E4j78tB27NnLq1L#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?property ?value where {
?class rdfs:label "Psicology" ;
rdfs:subClassOf [ owl:hasValue ?value ;
owl:onProperty [ rdfs:label ?property ] ] .
}
-------------------------
| property | value |
=========================
| "numChapters" | 1 |
| "numPages" | 5 |
-------------------------

Regarding SPARQL queries on DBPedia SPARQL end point in a loop

while((reader=br.readLine())!=null)
{
System.out.println(reader);
type="";
String queryNew="select ?y ?z where {"+reader+" <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z . }";
Query query=QueryFactory.create(queryNew);
ARQ.getContext().setTrue(ARQ.useSAX);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qexec.execSelect();
while(results.hasNext())
{
QuerySolution soln=results.nextSolution();
type=type+" "+soln.get("?z");
System.out.println(soln.get("?y")+" "+soln.get("?z"));
}
bw.write(reader+" "+type+"\n");
}
From a file I am reading actor names and then trying to get all the rdf:type links for each actor. While the result prints for the first actor, the rest of the execution gets stuck. The 'reader' is nothing but an actor's name which is the dbpedia resource. Can anyone kindly tell me what might have gone wrong in the code?
You don't show your data, but assuming that the file contains simply the names of actors, as you say, then the query you're actually asking is something like:
select ?y ?z where {
"Matt Damon" rdf:type ?z
}
That's never going to return any results, because the subject of an RDF triple is always an RDF resource, not a string literal.
You should change your query to:
prefix dbpprop: <http://dbpedia.org/property/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?actor ?type where {
?actor dbpprop:name "Matt Damon"#en ;
rdf:type ?type
}
in other words "what are the rdf:types of the resource whose name (in English) is Matt Damon?"
As Ian Dickinson pointed out in his answer, you haven't shown us the data, nor what exactly the value of reader is, so it's hard to tell exactly what's going wrong. It does sound like you need to adjust your query to something along the lines of
?actor rdfs:label ?label
where ?label is the name of the actor that you're interested in. If you use string concatenation like you did in
String queryNew = "select ?y ?z where {" + reader + " <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?z . }";
you're opening yourself up to SPARQL injection attacks. E.g., what would happen if reader contains more SPARQL query text? (Also, because rdf:type is so commonly used, SPARQL actually allows you to abbreviate it with a, as in ?actor a dbpedia-owl:Person.) To avoid this sort of issue, Jena provides a ParameterizedSparqlString that will take care of proper escaping. Also note that RDF distinguishes between plain literals, e.g., "Richard Dreyfuss" and language tagged strings, e.g., "Richard Dreyfuss"#en. This means that you might want to include language tags in your query. Finally, note that there might well be more than one thing in DBpedia with a given label. Heres's code using a parameterized SPARQL string that finds resources with the label "Richard Dreyfuss"#en, along with all of their RDF types:
import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
public class DBpediaQueryExample {
public static void main(String[] args) {
/*
* A typed literal: the name Richard Dreyfuss in English. In SPARQL
* this is written as "Richard Dreyfuss"#en.
*/
final Literal richardDreyfuss = ResourceFactory.createLangLiteral( "Richard Dreyfuss", "en" );
/*
* A parameterized SPARQL string for the query. Using this, along with
* the various setZZZ(...) methods can prevent some SPARQL injection
* attacks by doing proper escaping.
*/
final ParameterizedSparqlString queryString = new ParameterizedSparqlString(
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"\n" +
"select ?resource ?type where {\n" +
" ?resource a ?type ;\n" +
" rdfs:label ?label .\n" +
"}" );
/*
* Fill in "Richard Dreyfuss"#en for the ?label.
*/
queryString.setLiteral("label", richardDreyfuss );
/*
* Get a query execution that will run against the DBpedia SPARQL
* endpoint, and will use the parameterized query.
*/
final QueryExecution exec = QueryExecutionFactory.sparqlService(
"http://dbpedia.org/sparql",
queryString.asQuery() );
/*
* Execute the query to produce a result set, and format it nicely.
*/
final ResultSet results = exec.execSelect();
ResultSetFormatter.out( results );
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
| resource | type |
=================================================================================================================================================
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://www.w3.org/2002/07/owl#Thing> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/ActorsFromLosAngeles,California> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanFilmActors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanTelevisionActors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/ontology/Agent> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/ontology/Person> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://umbel.org/umbel/rc/Actor> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://schema.org/Person> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://xmlns.com/foaf/0.1/Person> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanComedians> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Comedian109940146> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/JewishActors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PeopleFromBrooklyn> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Adult109605289> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Communicator109610660> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/LivingPeople> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Object100002684> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PeopleWithBipolarDisorder> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Person100007846> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Whole100003553> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/YagoLegalActor> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Entertainer109616922> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AlternateHistoryWriters> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Actor109765278> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanConscientiousObjectors> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/AmericanHistoricalNovelists> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/ConscientiousObjector109957013> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Dissenter110018021> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Novelist110363573> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Performer110415638> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Writer110794014> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/CausalAgent100007347> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/LivingThing100004258> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Organism100004475> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/JewishComedians> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/JewishPacifists> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/Pacifist110390199> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PhysicalEntity100001930> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/YagoLegalActorGeo> |
| <http://dbpedia.org/resource/Richard_Dreyfuss> | <http://dbpedia.org/class/yago/PeopleFromQueens> |
| <http://sw.opencyc.org/2008/06/10/concept/en/RichardDreyfuss> | <http://sw.opencyc.org/2008/06/10/concept/en/ActorInMovies> |
| <http://sw.opencyc.org/2008/06/10/concept/en/RichardDreyfuss> | <http://sw.opencyc.org/2008/06/10/concept/en/MaleHuman> |
| <http://sw.opencyc.org/2008/06/10/concept/Mx4rwOREVpwpEbGdrcN5Y29ycA> | <http://sw.opencyc.org/2008/06/10/concept/Mx4rvVjWoZwpEbGdrcN5Y29ycA> |
| <http://sw.opencyc.org/2008/06/10/concept/Mx4rwOREVpwpEbGdrcN5Y29ycA> | <http://sw.opencyc.org/2008/06/10/concept/Mx4rwMRyTJwpEbGdrcN5Y29ycA> |
-------------------------------------------------------------------------------------------------------------------------------------------------