Can anyone tell me what is wrong in this query - sparql

Image of prefixes with query
when I remove filter it gives output but on adding FILTER nothing comes up.
SELECT distinct ?ID
WHERE { ?ID rdf:type foaf:ID
FILTER (foaf:has_Rel.WinRate >= "0.3"^^xsd:double)
}
After all the suggestions I came up with this query, but filter is still not working
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://www.semanticweb.org/abhin/ontologies/2021/11/stock_project#>
PREFIX xml: <http://www.w3.org/XML/1998/namespace#>
SELECT ?ID
WHERE { ?ID foaf:has_AnnualReturn ?AnnualReturn;
foaf:has_ExcessReturn ?ExcessReturn.
FILTER (?AnnualReturn > "0.3")
}
In filter I have also tried type conversion but that did not work
snap of ontograph
snap of an individual

Your filter relies on a property foaf:has_ref.WinRate existing with certain values in your data. As you do not get any results, no such property is found.
With the snippet provided, the issue becomes clear:
<ObjectPropertyAssertion>
<ObjectProperty IRI="#has_AnnualReturn"/>
<NamedIndividual IRI="#10-ID"/>
<NamedIndividual IRI="#0.5853214994439921"/>
</ObjectPropertyAssertion>
The statements are about object properties, between individuals, where the IRIs happen to contain numbers. The SPARQL functions apply to numeric literals, not to individual IRIs, so the query will not find any matches because the input cannot be compared to the condition.
For your query to work, it is necessary that the properties be data properties, and instead of connecting an individual to another individual they should connect an individual to a literal.
The data should look something like this (adapting a different snippet of data from your file):
<DataPropertyAssertion>
<DataProperty IRI="#has_AnnualReturn"/>
<NamedIndividual IRI="#10-ID"/>
<Literal datatypeIRI="http://www.w3.org/2001/XMLSchema#double">0.5853214994439921</Literal>
</DataPropertyAssertion>

Related

Construct loop in SPARQL

The following SPARQL query
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix msc: <http://msc.org/resources/MSC/msc2020/>
construct {?s skos:broader msc:00-XX . }
where
{
?s a skos:Concept ; skos:notation ?notation.
filter regex (?notation, "00-\d\d")
}
searches all notations 00-01, 00-02, etc. and constructs a relation to the top level class 00-XX. However, this is only the first of 63 top level classes altogether, so I would like to "loop" over all top level classes automatically. On top, I would like to adapt this to other patterns. Is there a way to do this with SPARQL? If not, what would you recommend instead?
In the meantime we found a solution without SPARQL.
The SPARQL CONSTRUCT query was supposed to create a skos:broader relation between a skos:Concept with a notation like "00-01" (and all other concepts with 00-\d\d notation) and its proper subordinate concept, which for 00-01 is the skos:Concept with the notation 00-XX.
The data originate from a table and Open Refine is much faster in creating the skos:broader statements than using the SPARQL query proposed above and adjusting it to other notation patterns.
We use GREL's value.replace on the cells with the notations to create a new column:
value.replace(/-\d\d/, "-XX").replace(/\d\d>/, "xx>")
The two replacements give us the notation of the original notation's superordinate concept in one step. The second replace already adapts to the other patterns mentioned in the question (e.g. 00A01).
With the original notation and the value in the new column, we can easily create the skos:broader triples by concatenating text and the values from both columns. These can then be exported from OpenRefine and just be copy-pasted to our SKOS vocabulary.
Here is a SPARQL answer based on the query in the question. Using filters and regex (as suggested in the comment by Yahalnaut as a reply to UninforomedUser above) is needed. Creating a skos:broader relation based on two concept's notations requires them to hava an identical sequence of digits before the - . The comparison should only between the first part of the notations, so each 00- should match another 00-but not a 01-. As asked, the solution below only considers topConcepts of the Vocabulary as potential objects for skos:broader. The concepts should also not relate to themselves, therefore the last filter. This should then be adoptable to other patterns as well. Depending on the number of Concepts and the memory available for the query, this may last a while or even stop before finished. It eliminates lot of the effort though.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix msc: <http://msc.org/resources/MSC/msc2020/>
construct {?s skos:broader ?y . }
where {
?s a skos:Concept ; skos:notation ?notation.
?y skos:topConceptOf msc: ; skos:notation ?not2.
bind (REPLACE (?not2 , "-XX" , "") as ?1)
bind (REPLACE (?notation , "-\d\d", "" ) as ?2 )
filter (?1 = ?2)
filter (?not2 != ?notation)
}

How to Read Specific Range value of an Object Property

i'm new dealing with Ontologies and finding problems to get my SPARQL Query working , trying to read value of specific Object property that has multiple Ranges Object Property Screenshot
trying this Query Return all Object Properties Execution Result , Protege Visualization
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
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 rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT *
WHERE {
ns:star1086 ns:possesses ?z .
}
The Desired Result that i want to read only the desired Range value, Thanks in advance.
I think there is something wrong about your ontology.
Specifying muliple ranges to your predicate creates an intersection. Take the following statement:
?star ns:possesses ?something
Then ?something is a SpectralType and a StarTemperature and a StarCoordinates and a StarName at the same time, which is not what you want.
Instead, you should use unions. Using unions, you can state that the object of a ns:possesses statement can be either a SpectralType or a StarTemperature or a StarCoordinates or a StarName. Then, in your SPARQL query you can write the following to get only statements from a single type.
SELECT * WHERE {
ns:star1086 ns:possesses ?z .
?z a ns:SpectralType .
}
In Protégé, to write a union, open the class expression editor (by clicking on the "plus" next to "Ranges" for instance) and separate the different members with or :
SpectralType or StarTemperature or StarCoordinates or StarName
And click "OK" to create the new range.
Further considerations
Let's take a step a back and look at your ontology.
You should not use a single predicate to store all these information in the first place. Instead, I suggest you use different sub-predicates so that your graphs and queries hold more semantic value.
Furthermore, StarName and Temperature are literal values. You should not use classes for that. Use datatype properties instead.
Here is a Gist you can download and open in Protégé. It contains some sample data so you can try the following SPARQL queries.
PREFIX : <http://www.richarddegenne.com/ontology/astronomy#>
# Get all statements about :star1086
SELECT * WHERE {
:star1086 ?predicate ?object
}
# Get some statement about :star1086
SELECT * WHERE {
VALUES ?predicate {
:hasSpectralType :temperate
}
:star1086 ?predicate ?object
}
# Ask whether a given pattern is true
ASK WHERE {
:star1086 :hasSpectralType :yellowDwarf
}
# Filter stars based on their temperature
# Note: You might want to add more stars with different temperature
# if you want useful results.
SELECT ?star WHERE {
?star :temperature ?temperature
FILTER(?temperature > 5000)
}

SPARQL - Inverse Path of dynamic discovered relationship

I have a Graph Database that implements only one side of relationships in the model structures. For instance, All Broader relationships are mapped as skos:broader. All Narrower relationships are mapped as ^skos:broader (for whatever reasons they chose to do this, I have no control over this.)
I am trying to write a SPARQL Query that delivers something like
Query for all relationships of a given IRI, and their associated values.
The goal is to make a JSON or XML Representation of all the content.
Here is what I have working.
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?relation ?value
where
{
<CONCEPT_URI> ?r ?c
OPTIONAL {
?r rdfs:label|skosxl:prefLabel ?l
}
OPTIONAL {
?c skosxl:prefLabel/skosxl:literalForm|skosxl:literalForm ?d .
}
BIND ( if (bound(?d), ?d, ?c) as ?value )
BIND ( if (bound(?l), ?l, strafter(?r, '#')) as ?relation )
}
This works to give me all the concept relationships (IE: Broader), but it does not give me the narrower items (IE: ^broader).
Is there anyway that I can extend this generic query to produce the inverses of any relationships it finds, as well? I Tried simply adding the ^ to the ?r in the where clause but it does not like that syntactically.
Thanks,

Running sparql query for getting float values

Hi Below is my sparql query which I am executing on https://dbpedia.org/sparql. However I am getting an error "Invalid floating point value converting 'United States:'" because of the presence of some characters values in grossincome and If I change my query to str(?grossincome) as ?grossincome1) then it executes correctly. My question is how can I convert all currencies on same scale to avoid this error?
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select distinct (str(?resource) as ?movietitle) (xsd:float(?budget) as ?budget1) (xsd:float(?grossincome) as ?grossincome1) (str(?Country) as ?country1) ?ReleaseDate where {
?movie foaf:name ?resource.
?movie dbo:budget ?budget.
?movie dbp:gross ?grossincome.
?movie dbp:country ?Country.
?movie dbo:releaseDate ?ReleaseDate.
FILTER (lang(?resource) = 'en').
}
The data in DBpedia is not perfect as you already recognized it's quite heterogeneous for the untyped property dbp:gross. Among others, it contains values like BDT 2.52 crores, $34,994,648 USD, AU$178,000, 6892098.0, so it is almost impossible to handle different currencies via a SPARQL query such that it returns the values in the same currency.
I mean, you would have to know all different currencies and formats that occur and then handle each separately. But that's more the task of application logic and not the query itself.
Alternatively, you could try to use the data property http://dbpedia.org/ontology/gross instead, which is supposed to relate to values in $. At least that's what its label "gross ($)" states.

How to create RDF model from DBPedia data using SPARQL

I am a new one on semantic web. I would like to get all object/values for Microsoft from DBPedia using SPARQL query and save result in RDF format. I have made a query on http://dbpedia.org/sparql which works well and returns all pair/values regarding Microsoft.The code is as follows:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select * where
{{ <http://dbpedia.org/resource/Microsoft> ?property ?value }
UNION
{?property ?value <http://dbpedia.org/resource/Microsoft>}}
What I want is to create RDF format for the results. I read tutorial on https://www.w3.org/TR/rdf-sparql-query/#construct and understood it can be done by using CONSTRUCT query. I changed SELECT to CONSTRUCT, but that did not work. If possible could you tell me what is my mistake and how can I apply CONSTRUCT to my query to get RDF model from the query please? Thanks in advance!
In order to get more clear distinction of the actual triples you retrieve, I'd suggest changing the variables in the following way:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select * where
{{ <http://dbpedia.org/resource/Microsoft> ?property ?value }
UNION
{?subject ?property <http://dbpedia.org/resource/Microsoft>}}
And regarding the result format, just choose "Turtle" or "RDF/XML", instead of "HTML" from the results menu of the SPARQL interface.