I am currently trying to see if it's possible to extract a certain value from a document and bind it to a variable in SPARQL
For example if i have such a document in MarkLogic.
/person/John
<person_data>
<name>John</name>
<age>25</age>
</person_data>
using this data I attempted various ways to bind it such as using XPath in sem:sparql as shown below
xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy";
sem:sparql('
PREFIX fn : <http://www.w3.org/2005/xpath-functions>
SELECT *
WHERE {
?s ?p ?o .
BIND (fn:doc("/person/John")//name/text() AS ?name)
}
',
(),
(),
()
)
However, this resulted in an error. Hence, I greatly appreciate any advise given on accomplishing this.
The SPARQL engine has no access to documents, but there is a better solution anyhow. You can use Template Driven Extraction for this. It can expose an SQL view on documents, but also 'Identify Triples in Documents'. It effectively means that particular values can be projected into the triple index, and will become accessible as RDF data like any other RDF data in your database.
HTH!
Related
I am looking to run a SPARQL query over any dataset. We dont know the names of the named graphs in the datasets.
These are lots of documentation and examples of selection from named graphs when you know the name of the named graph/s. There are examples showing listing named graphs.
We are running the Jena from Java so it would be possible to run 2 queries, the first gets the named graphs and we inject these into the 2nd.
But surely you can write a single query that reads from all named graphs when you dont know their names?
Note: we are looking to stay away from using default graph/s as their behaviour seems implementation dependent.
Example:
{
?s foaf:name ?name ;
vCard:nickname ?nickName .
}
If you want the pattern to match within one graph and wish to try each graph, use the GRAPH ?g form.
GRAPH ?g
{ ?s foaf:name ?name ;
vc:nickname ?nickName .
}
If you want to make a query where the pattern matches across named graphs, -- e.g. foaf:name in one graph and vCard:nickname in another, same subject --
then set union default graph tdb2:unionDefaultGraph true then the default graph as seen by the query is the union (actually, RDF merge - no duplicates) of all the named graphs. Use the pattern as originally given.
Fuseki configuration file extract:
:dataset_tdb2 rdf:type tdb2:DatasetTDB2 ;
tdb2:location "DB2" ;
## Optional - with union default for query and update WHERE matching.
tdb2:unionDefaultGraph true ;
.
In code, not Fuseki, the application can use Dataset.getUnionModel().
I'm quite new to sparql.
I founded this query to get all country in the UN
select distinct ?s
where { ?s a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> }
So I tried to adapt it to Geonames with:
select distinct ?s
where { ?s a <http://dbpedia.org/page/GeoNames> }
But it doesn't work. How can I get every place's name in geonames?
I hope someone can help me with that!
Every publisher uses its own namespace and method to generate URIs of the published entities. The nice thing about Linked Open Data is that it allows such independence while URIs can still be linked using agreed open standards. When different URI represent the same thing, this is declared by linking them with owl:sameAs.
Your query attempt assumes that DPpedia and Geonames use the same URIs, if I understood correctly the intention (I'm not sure qhat you mean by "to adapt"). What you need to do is use two separate variables, and then specify that from the owl:sameAs mappings, you want only those from Geonames.
select distinct *
where { ?cuntryDBpedia a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> ;
owl:sameAs ?countryGeonames .
FILTER REGEX (?countryGeonames,"geonames.org")
}
In the GraphDB documentation, I see that "the dataset’s default graph contains the merge of the database’s default graph AND all the database named graphs." This means that "if a statement ex:x ex:y ex:z exists in the database in the graph ex:g" then a query such as SELECT * { ?s ?p ?o } will return the triple ex:x ex:y ex:z
I am wondering if there is a setting which can be triggered either via the web interface or via the RDF4J/OpenRDF API which will disable this behavior in a specified GraphDB repository. That is, for the purposes of my project I would prefer to have triples which are stored in named graphs to only appear in results which specifically query that named graph.
I have not seen anything like this searching through the documentation or on the settings available on the web interface, but maybe somebody here knows something I don't.
EDIT: I am not looking for a SPARQL solution to this problem. I know that I can query just the default graph using SPARQL, but I want to be able to use the query SELECT * { ?s ?p ?o } and only see results which are in the default graph by default.
GraphDB/RDF4J have a different interpretation than Jena how to query the default graph. The only easy way to query only explicit statements in the default graph is to use the special graph sesame:nil. The SPARQL-based solution is to write:
PREFIX sesame: <http://www.openrdf.org/schema/sesame#>
SELECT ?s ?p ?o
FROM sesame:nil
WHERE {
?s ?p ?o .
} LIMIT 100
I don't think there is any easy non-SPARQL based solution like changing a configuration option or even use this special graph over the SPARQL Graph Store protocol.
Hi I'm trying to learn how to query DBpedia using SPARQL. I can't find any website/source that shows me how do this and I'm finding it difficult to learn how to use all the properties (like the ones available at http://mappings.dbpedia.org/index.php?title=Special%3AAllPages&from=&to=&namespace=202 ). Any good source I can learn from?
So for example if I want to check if the wikipedia page http://en.wikipedia.org/wiki/Inception is a movie (property film) or not, how do I do that?
The wikipedia URL http://en.wikipedia.org/wiki/Inception maps to the dbpedia URI http://dbpedia.org/resource/Inception. Dbpedia has a SPARQL endpoint at: http://dbpedia.org/sparql, which you may use to run queries either programmatically or via the html interface.
To check if http://dbpedia.org/page/Inception is a "movie", you have many options. To give you an idea:
If you know the URI of "movie" in dbpedia (it is http://schema.org/Movie), then run an ASK query to check against that type. ASK will return true/false based on whether the pattern in the where clause is valid against the data:
ASK where {
<http://dbpedia.org/resource/Inception> a <http://schema.org/Movie>
}
If you don't know the URI of "movie" then you have a number of options. For example:
Execute an ASK query with a filter on whether the resource has a type that contains the word "movie" somewhere in its uri (or its associated rdfs:label, or both). You would use a regular expression for this:
ASK where {
<http://dbpedia.org/resource/Inception> a ?type .
FILTER regex(str(?type), "^.*movie", "i")
}
Same idea, but return all matches and post-process the results (programmatically I pressume) to see if they match your request:
select distinct ?type where {
<http://dbpedia.org/resource/Inception> a ?type .
FILTER regex(str(?type), "^.*movie", "i")
}
Return all the types of the resource without applying a filter and post-process to see if they match your request:
select distinct ?type where {
<http://dbpedia.org/resource/Inception> a ?type
}
Many options. The SPARQL spec is you number one resource.
First I suggest you start reading up on what exactly SPARQL is. There are tons of really good tutorials such as: this.
If you want to write SPARQL queries on dbpedia, there are various endpoints that you can use. They don't always accept all features that are supported by SPARQL, but if you don't want to go through the trouble of installing one locally, they can be a relatively reliable test environment. The queries that I am going to write below, have been tested on Virtuoso endpoint.
Let's say you want to find all the movies in dbpedia. You first need to know what is the URI for a movie type in dbpedia. If you open Inception in dbpedia, you can see that the type dbpedia-owl:Film is associated to it. So if you want to get the first 100 movies, you just need to call:
select distinct *
where {
?s ?o dbpedia-owl:Film
} LIMIT 100
If you want o know more about each of these movies, you just need to expand your queries by expanding the triples.
select distinct *
where {
?s ?p dbpedia-owl:Film.
?s ?x ?y.
} LIMIT 100
I've the following Ontology built in Protege 4.
In this Ontology : The main class Frame has an datatypeProperty hasDuration with domain 'Frame' and range UnsignedShort. the ClassShortFrame and LongFrame are inferred from the class SizedFrame with the followiing restriction
Rectriction for ShortFrame class
SizedFrame that hasDuration some unsignedLong[<=20]
Rectriction for LongFrame class
SizedFrame that hasDuration some unsignedLong[>=200]
I've manually created an instance of the class frame named frame0, which has a property hasDuration set to 12.
What is the SPARQL query that I need to get the all shortFrame. I hope that frame0 will be inferred like a shortFrame ?
Thanks for any reply !
Edition: sample query
PREFIX frame: <http://www.semantic.org/sample.owl#>
SELECT ?y WHERE {?y rdf:type frame:Frame}
but It is not working ! maybe It is not correct !
I believe, You're going to write some queries for OWL restriction information in SPARQL language. SPARQL is a RDF query language and has no understanding the concepts of OWL. Instead of making a restriction, you can use a data property to define duration value and from that you can get all the shortFrames using SPARQL. Other option I would recommend is use SWRL rules instead of SPARQL. Hope this helps !!
The query you give asks for all instance of type frame:Frame. Since you want just the short frames, you should adapt it like so:
SELECT ?y WHERE {?y a frame:ShortFrame}
...but the above will only work if the reasoner understands your restriction and can correctly classify frame0 as an instance of ShortFrame. I am not overly familiar with Protege's syntax for owl restrictions, so I am not 100% sure your restriction expresses what you want it to express.
As an alternative, you can actually express the restriction you require in SPARQL. To query for all frames with a duration of less than 20:
SELECT ?y
WHERE {
?y a frame:Frame;
frame:hasDuration ?d .
FILTER (?d <= 20)
}