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

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".

Related

Should I give up grammatical correctness when naming my functions to offer regularity?

I implement several global functions in our library that look something like this:
void init_time();
void init_random();
void init_shapes();
I would like to add functions to provide a check whether those have been called:
bool is_time_initialized();
bool is_random_initialized();
bool are_shapes_initialized();
However, as you can see are_shapes_initialized falls out of the row due to the fact that shapes is plural and therefore the function name must start with are and not is. This could be a problem, as the library is rather large and not having a uniform way to group similiar functions under the same naming convention might be confusing / upsetting.
E.g. a user using IntelliSense quickly looking up function names to see if the libary offers a way to check if their initialization call happened:
They won't find are_shapes_initialized() here unless scrolling through hundreds of additional function / class names.
Just going with is_shapes_initialized() could offer clarity:
As this displays all functions, now.
But how can using wrong grammar be a good approach? Shouldn't I just assume that the user should also ask IntelliSense for "are_initialized" or just look into the documentation in the first place? Probably not, right? Should I just give up on grammatical correctness?
The way I see it, a variable is a single entity. Maybe that entity is an aggregate of other entities, such as an array or a collection, in which case it would make sense to give it a plural name e.g. a set of Shape objects could be called shapes. Even so, it is still a single object. Looking at it that way, it is grammatically acceptable to refer to it as singular. After all, is_shapes_initialized actually means "Is the variable 'shapes' initialized?"
It's the same reason we say "The Bahamas is" or "The Netherlands is", because we are referring to the singular country, not whatever plural entity it is comprised of. So yes, is_shapes_initialized can be considered grammatically correct.
It's more a matter of personal taste. I would recommend putting "is" before functions that return Boolean. This would look more like:
bool is_time_initialized();
bool is_random_initialized();
bool is_shapes_initialized();
This makes them easier to find and search for, even if they aren't grammatically correct.
You can find functions using "are" to show it is plural in places such as the DuckDuckGo app, with:
areItemsTheSame(...)
areContentsTheSame(...)
In the DuckDuckGo app, it also uses "is" to show functions return boolean, and boolean variables:
val isFullScreen: Boolean = false
isAssignableFrom(...)
In OpenTK, a C# Graphics Library, I also found usage of "are":
AreTexturesResident(...)
AreProgramsResident(...)
In the same OpenTK Libary, they use "is" singularly for functions that return boolean and boolean variables:
IsEnabledGenlock(...)
bool isControl = false;
Either usage could work. Using "are" plurally would make more sense grammatically, and using "if" plurally could make more sense for efficiency or simplifying Boolean functions.
Here's what I would do, assuming you are trying to avoid calling this function on each shape.
void init_each_shape();
bool is_each_shape_initialized();
Also assuming that you need these functions, it seems like it would make more sense to have the functions throw an exception if they do not succeed.

Velocity template - retrieving hashmap values

I have a HashMap< String, List> which I fill inside the Java class. When I try to print it out in the Velocity template, it looks fine.
$!valuesMap ##gives {33=[texxxxt], 34=[2019-03-31], 35=[admin], 37=[P1], 40=[value1, value2]}
When I try to access the values directly, it also looks fine.
$!valuesMap.get("40") ##gives [value1, value2]
Problem arises when I try to use a dynamic variable to access the map. I have a list of objects over which I iterate, and each of these objects has an ID. However I cant figure out how to retrieve the value from the map using this ID.
#foreach( $field in $fields )
$!field.ID ##gives the id of the object, i.e. 40
##I would assume this would give me [value1, value2] when ID is 40, but it returns nothing
$!valuesMap.get($!field.ID)
#end
I have tried assigning the ID to a new variable (variable itself prints out fine, but again when I try to access the map, I get nothing). I have tried the notation suggested here and nothing ever prints out, it is honestly driving me up the wall, because I am probably missing something very simple, but cant figure out what it is.
Velocity Engine 1.7 does not convert method arguments towards expected types. So if $field.ID is a number, you have to enclose it in double quotes to get a string:
$valuesMap.get("$field.ID")
Otherwise, the engine simply doesn't find a proper method to call.
Starting with 2.0, Velocity Engine will automatically convert method arguments towards expected types, and your code will work as expected.

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.

How do I retrieve variables from an OPTIONAL statement in 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.

Browse sort criteria

I'm trying to write a upnp/dlna client for videos and I would like to allow the option to sort by title and date.
With Windows7/wmp as the server, I can use "dc:title" or "dc:date" for sorting and it seems to work but testers have told me it doesn't work on other servers. Is there a universal way to know if sorting is allowed and what the sorting criteria should be?
Thanks.
There is a way to query this (but be prepared for broken implementations that lie about their abilities as well). Quoting ContentDirectory service spec (v3):
2.3.3
SortCapabilities
This state variable is a CSV list of property names that the ContentDirectory service can use to sort
Search() or Browse() action results. An empty string indicates that the device does not support any kind of
sorting. A wildcard (“*”) indicates that the device supports sorting using all property names supported by
the ContentDirectory service. The property names returned MUST include the appropriate namespace
prefixes, except for the DIDL-Lite namespace. Properties in the DIDL-Lite namespace MUST always be
returned without the prefix. All property names MUST be fully qualified using the double colon (“::”)
syntax as defined in Section 2.2.20, “property”. For example,
“upnp:foreignMetadata::fmBody::fmURI”