How do I retrieve variables from an OPTIONAL statement in SPARQL? - sparql

If I give my SPARQL query a known item which could be a number of different types. Depending on which type I'm currently interested in I want to get a certain property from this type (the way to do this will be different for each type) but once I have found this property I want to perform the same operation.
currently I have the following pseudocode:
?object rdf:hasProperty "known Property Type"
OPTIONAL {
?object rdf:hasProperty "property type 1"
#do this thing and store thing of interest in ?variableOfInterest
}
OPTIONAL{
?object rdf:hasProperty "property type 2"
#do different thing and store thing of interest in ?variableOfInterest
}
?thingIAmActuallyInterestedIn rdf:has type ?variableOfInterest
#now do long query
My problem is that outside of the OPTIONAL statement ?variableOfInterest does not get passed out, instead ?thingIAmActuallyInterestedIn is just a list off all objects.
I could put the 'long query' in both of the optional blocks but that would be a huge amount of code replication.
Is there a way to output the ?variableOfInterest from the optional statement rather than it being a dummy variable?

Ensure that ?variableOfInterest is in the SELECT clause, or it's *
?variableOfInterest will be bound in result rows when the OPTIONAL matched, and unbound when the OPTIONAL did not match.
How that gets reflected to your code depends on the API of your SPARQL engine.

Related

How can I perform a regex/text search on a SUPER type?

What I'm doing now:
I have a table with one field that is a json value that is stored as a super type in my staging schema.
the field containing the json is called elements
In my clean table, I typecast this field to VARCHAR in order to search it and use string functions
I want to search for the string net within that json in order to determine the key/value that I want to use for my filter
I tried the following:
select
elements
, elements_raw
from clean.events
where 1=1
and lower(elements) like '%net%'
or strpos(elements,'net')
My output
When running the above query, I keep getting an empty set returned.
My issue
I tried running the above code and using the elements_raw value instead but I got an issue :ERROR: function strpos(super, "unknown") does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts.
I checked the redshift super page and it doesn't list any specifics on searching strings within super types
Desired result:
Perform string operations on super field
Cast super field to a string type
There are some super related idiosyncrasies that are being run into here:
You cannot change the type of a super field via :: or cast()
String functions like and strpos do not work on super types
To address both of these issues, you can use the function json_serialize to return your super as a string.

SPARQL - Returning label when object is URI or string when object is Literal

I would like to get the labels (rdfs:label) of objects when the object is a URI. But I would also like to get the string values when the object is a literal string. The issue is, I do not know beforehand if the object is storing a literal or a URI, and in some cases I see a mix of both literals and URIs, like in the image attached.
Any suggestions on how I can return the strings, and if there's an object, return the rdfs:label?
Thanks for your help!
First, this problem shouldn't occur in an ideal world because a property is supposed to be either an object property or a datatype property.
However, when dealing with low quality data where this does occur, I suggest the following workaround:
SELECT ?x ?desc
{
?x dbp:keyPeople ?y.
{?y rdfs:label ?desc. FILTER(isIRI(?y))} UNION
{BIND(STR(?y) AS ?desc). FILTER(!isIRI(?y))}
}
Warning
This is not thoroughly tested. I could only verify that it works correctly on DBpedia Live, which uses Virtuoso 08.03.3319. On the default DBpedia endpoint https://dbpedia.org/sparql on Virtuoso version 07.20.3235 it seems to not work correctly. Also, you need to uncheck both "Strict checking of void variables" and
"Strict checking of variable names used in multiple clauses but not logically connected to each other".

Makes using a URI as predicate them to a property?

I have only the following Turtle statement:
x isAuthorOf y
If I have only this statement where isAuthorOf is used as a predicate, means this that I can conclude that isAuthorOf is also a Property without instanciation (isAuthorOf rdf:type rdfs:Property)?
Thanks in advance.
Yes, an IRI used ad a property implies it is a property. However, without declaration you won't know if it's a datatype or an object property.

Camunda: find process instance by variable value

Can I find a process instance by a process instance variable value via the Camunda REST API as described in this request:
https://groups.google.com/forum/#!topic/camunda-bpm-dev/gJfXkbkY8fc
(the question above is from 2014, maybe there is a standard way now?)
Looks like this is supported now:
GET /process-instance
variables: Only include process instances that have variables with certain values. Variable filtering expressions are comma-separated and are structured as follows:
A valid parameter value has the form key_operator_value. key is the variable name, operator is the comparison operator to be used and value the variable value.
There is also a POST endpoint which allows to express the filtering more cleanly:
POST /process-instance
variables: A JSON array to only include process instances that have variables with certain values.
The array consists of objects with the three properties name, operator and value. name (String) is the variable name, operator (String) is the comparison operator to be used and value the variable value.

why this variable is never have a value

I am checking that if an instance has a value for a specific predicate, bound that value to a specific variable, otherwise, bound that variable to the value 1 of type integer. this is my code
select ?boosted where {
:r1 a ?x
optional
{
?item rs:boostedBy ?boostedOptional
bind (if(bound(?boostedOptional), ?boostedOptional, "1"^^xsd:integer) as ?boosted)
}
}
the value of ?boosted is always empty, look please
why please?
Note
I think you don't need data to test why my code is not working, because for me it sounds like a general mistake about using the bound. however, if you want data, i give you data.
note2
there is no rs:boostedBy predicate from the first place, so i was looking to have the default value always , which is 1 of type integer in this case.
The IF needs to be outside of the optional graph pattern:
SELECT ?boosted WHERE {
:r1 a ?x
OPTIONAL{ ?item rs:boostedBy ?boostedOptional . }
BIND (IF(bound(?boostedOptional), ?boostedOptional, "1"^^xsd:integer) as ?boosted)
}
Secondly, I don't see the relationship between the rs:boostedBy property and the {:r1 a ?x} triple pattern. I.e. are you trying to see if the subject has a boostedBy property? In that case :r1 and ?item should be the same, i.e. both should be :r1 or both should be ?item, if I'm understanding your intent here.