How to get the arithmetic result of a comment in TopBraid Composer? - sparql

In TopBraid Composer, I save all my SPARQL queries as Comments in separate instances. I want to calculate their value and I want that value in a separate instance. For example, my SPARQL queries is 2 * 3 and when I run it, the result is 6. Here my comment contain "2 * 3".
What I want is the result "6" to be displayed in a separate instance. I will use the value of that instance in some other calculation, say "6 (of this) * 4" = 24. Please let me know if there is any way to deal with it.
It will save us from writing and arranging so many inner queries. All we would need is write simple queries and connect them through this method and the final query will get us the result. Thank you so much.

SPARQL is pretty good about casting to basic XML schema types, so you can simply cast these to xs:integer - as an example:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xs: <http://www.w3.org/2001/XMLSchema#>
SELECT ?product
WHERE {
<x> rdfs:comment ?c1 .
<y> rdfs:comment ?c2 .
BIND (xs:integer(?c1) * xs:integer(?c2) AS ?product)
}

Related

Unexpected behavior in Visual Graph, and when returning triples with SPARQL

How are you?
I 'm taking my first steps with GraphDb and I've found a couple of behaviors that I don't quite understand. Let me build the case.
I have the following Ontology, which I have loaded into GraphDb.
enter image description here
Let's only consider the tree under Persona.
When I go to Visual Graph and type :Persona in the search bar, this is what returns:
enter image description here
As you can see all the nodes corresponding to the ontology are there, but also some other nodes like Class (in different colors), and Thing and Nothing. (Ignore Paul which is an instance I added)
What are these other nodes? and how can I prevent them from appearing in this view?
Now, when I query with SPARQL, and I run
select * where {
?s ?p ?o .
}
If I have "Include inferred data in results" Off, I get this
enter image description here
Which is fine. It's what I would expect.
But ... when I turn inference On , which I believe is one of the true powers of working with rdf, I get around 800 records with all sort of triples with definitions for owl and rdf generic objects.
In order to get only my stuff and keep the inference capability on, I filtered as follows
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pe: <http://www.semanticlab.com/ontologias/EmpresasYPersonas#>
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 * where {
?s ?p ?o .
FILTER (REGEX(str(?s),"Persona*" )).
FILTER (!REGEX(str(?o),"owl*" )).
FILTER (!REGEX(str(?o),"rdf*" )).
}
order by ?s
Which returns
enter image description here
Here I see some inferencing, for example Paul classified as a Persona, which is not explicitly stated.
But on the other hand there are a bunch of new triples that I don't understand nor want, as they don't bring any value: for example the ones with owl:sameAs, owl:equivalentClass, or the ones stating that Persona is a subClassOf Persona (which in fact I believe is wrong).
Could you please explain why this is happening, and how to prevent this behavior?
I'm aware that I might be making some mistakes, so if you spot any, please let me know.

How to reverse lookup in SPARQL query?

I'm trying to create a SPARQL query to return all the Land Registry House Price Index data for a region by searching for the name. I am stuck on one part of this, which I have set out below.
This code returns up the Region Name, based on the Region Identifier. This works and returns "Barnet".
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT
?region ?regionName
WHERE
{
<http://landregistry.data.gov.uk/id/region/barnet> rdfs:label ?regionName .
}
However, I would like to reverse this so I can get the Region Identifier by entering the Region Name. The below code result is "No Data in Table". I've spent a good few hours looking at tutorials and think it should be working based on what they say. Am I missing something obvious?
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT
?region
WHERE
{
?region rdfs:label "Barnet" .
}
As mentioned in the comments, if you click to view the data as plain text then you can see that there is also a language tag on the data. The tag needs to be included when searching, otherwise the match won't be made.

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)
}

Use SPARQL property path on DBpedia

I'd like to find out if property paths exist between two entities on DBpedia. This is a sample query that I tried on snorql:
SELECT * WHERE {
:Braveheart (:|!:)* :Mel_Gibson
}
LIMIT 100
The queries runs into a memory error:
Virtuoso 42000 Error TN...: Exceeded 1000000000 bytes in transitive temp memory. use t_distinct, t_max or more T_MAX_memory options to limit the search or increase the pool SPARQL query: define sql:big-data-const 0 #output-format:application/sparql-results+json define input:default-graph-uri PREFIX owl: PREFIX xsd: PREFIX rdfs: PREFIX rdf: PREFIX foaf: PREFIX dc: PREFIX : PREFIX dbpedia2: PREFIX dbpedia: PREFIX skos: SELECT * WHERE { :Braveheart (:|!:)* :Mel_Gibson } LIMIT 100
I suspect someone's going to suggest setting up a local dbpedia mirror. If that's the case, I'd love some detailed steps on how to do so.
I think your query is a bit wrong for what you're trying to answer... also as there are no variables in it select * can't project anything out (i'd consider it a bug to even compile this), so let me rephrase your query to
ASK { dbr:Braveheart (<>|!<>)+ dbr:Mel_Gibson }
Sadly that query errs with the same problem you described.
While i agree, that complicated should be executed against local endpoints, the above query isn't complicated at all, especially considering that there are several direct edges between the two nodes:
SELECT * { dbr:Braveheart ?p dbr:Mel_Gibson }
I consider this a bug in Virtuoso's query planner and reported it: https://github.com/openlink/virtuoso-opensource/issues/641
Having said all that, i'd like to point out that in real cases you're probably interested in paths that don't only point forward. The direction of edges greatly depends on modelling. So consider using queries like these instead:
ASK { dbr:Braveheart ((<>|!<>)|^(<>|!<>))+ dbr:Mel_Gibson }
The expression says follow any edge in their direction or against it (^) for at least one step. (Yes, i also wonder why property paths didn't a short syntax for arbitrary edges ;) )
Spinning off #JörnHees's answer, a couple of points:
<> is an invalid predicate identifier. For Virtuoso, <> identifies a document (Location of Content that returns 200 OK on HTTP GET) which is why <#> or <#this> work. This isn't a parsing issue since it has more to do with the semantics of an identifier.
The public DBpedia endpoint isn't configured to accept that kind of query, hence the error.
Using <#this> rather than <>, we have --
prefix dbpedia: <http://dbpedia.org/resource/>
ASK { dbpedia:Braveheart (<#this>|!<#this>)+ dbpedia:Mel_Gibson }
Two alternative instances, both hosted by OpenLink Software (my employer, and producer of Virtuoso), that produce solutions for that query:
DBpedia-Live instance
LOD Cloud Cache instance

sparql empty result for dbpedia-owl:influenced property

I am trying to retrieve the value of the dbpedia-owl:influenced in this page e.g: Andy_Warhol
The query I write is:
PREFIX rsc : http://dbpedia.org/resource
PREFIX dbpedia-owl :http://dbpedia.org/ontology
SELECT ?o WHERE {
rsc:Andy_Warhol dbpedia-owl:infuenced ?o .
}
but it is EMPTY.
Strange is that when I have the same query for another property from the ontology type like "birthPlace", the sparql engine gives the result back:
SELECT ?o WHERE {
rsc:Andy_Warhol dbpedia-owl:birthplace ?o .
}
which is a link to another resource:
dbpedia.org/resource/Pittsburgh
I am just confused how to write this query?
besides several formal errors addressed in the answer of #Joshua, there is also the semantic problem that the properties you are looking for - in this case - seem to be found on the entities that were influenced.
this query might give you the desired results
PREFIX rsc: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?s WHERE {
?s dbpedia-owl:influencedBy rsc:Andy_Warhol .
}
run query
There are a few issues here. One is that the SPARQL, as presented, isn't correct. I edited to make the prefix syntax legal, but the prefixes were still wrong (they didn't end with a final slash). You don't want to be querying for http://dbpedia.org/resourceAndy_Warhol after all; you want to query for http://dbpedia.org/resource/Andy_Warhol. Some standard namespaces for DBpedia are listed on their SPARQL endpoint. Using those namespaces and the SPARQL endpoint, we can ask for all the triples that have http://dbpedia.org/resource/Andy_Warhol as the subject with this query:
SELECT * WHERE {
dbpedia:Andy_Warhol ?p ?o .
}
In the results produced there, you'll see the one using http://dbpedia.org/ontology/birthPlace (note the captial P in birthPlace), but you won't see any triples with the predicate http://dbpedia.org/ontology/infuenced, so it makes sense that your first query has no results. Do you have some reason to suppose that there should be some results?