1

I have three variables ?leaf and ?superclass, and ?supersuperclass containing uri's. Now I would like to end up with a table with 2 columns, one column being the ?leaf label and the other being a concatenation of ?superclass and ?supersuperclass with a - in between them.

For instance, a fictional example: | leaf?| ?superclass-supersuperclass| |:----: | :-----:| | bed | bedroom-house | |fridge| kitchen-house| |diningroom|house-street|

I have tried concatenating superclass and supersuperclass in the select portion of my query, but this didn't work. I'm not sure where else I could place it. Do I need to create a new variable that somehow assigns the string values of each variabe to that new variable, concatenated? I do know how to access the labels of each uri, which is the string I end up wanting to concatenate.

tldr: how to concatenate the string values within 2 variables?

Edit: My query up to now: I hope it's readable, removing the comments may help, i mostly wrote those for myself.

 PREFIX owl: <http://www.w3.org/2002/07/owl#>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX : <*databaselocation*>

    SELECT DISTINCT ?leaf  #?subclasslabel ?subclasslabel #?superclasslabel ? 
    supersuperclasslabel 
    WHERE {GRAPH <*insertyourgraphhere*>

    #a leaf is the lowest level class: A class that does not have a subclass
    {{
            #I want anything which is a class
            ?leaf rdf:type owl:Class.
            #i also want the subclass of any superclass if that exists
            optional {?leaf rdfs:subClassOf ?superclass .}
            
            #give me the label of that subclass
            #optional {?leaf skos:prefLabel ?subclasslabel.}

        
            #?superclass rdfs:subClassOf ?supersuperclass.
            #give me the label of the superclass
            #?superclass skos:prefLabel ?superclasslabel.

            
            #if it exists, give me also the superclass of the superclass 
            creating a supersuperclass
            # {?superclass rdfs:subClassOf ?supersuperclass.
            #give me the label of the supersuperclass
            # {?supersuperclass skos:prefLabel ?supersuperclasslabel.}
                
            
            #keep the leafs that are NOT The values whereby the subclass is 
     not empty. (double negative for remove the leafs where the subclass has a 
    subclass below it)
    FILTER NOT EXISTS
    {?subclass rdfs:subClassOf ?leaf 
                FILTER (?sublass != ?leaf && ?subclass != owl:Nothing ) }}}}    
Robin
  • 135
  • 10
  • Can you share the query you wrote? It's much easier to see where you're going wrong that way. – Valerio Cocchi Jan 27 '21 at 13:13
  • Hi Valerio, thanks for you response. I added in my code, hopefully it helps. I did take out the prefix database location and graph location, because those won't work for you anyway. – Robin Jan 27 '21 at 14:00
  • Were you aware of the GROUP_CONCAT aggregate? E.g. SELECT ?person (GROUP_CONCAT(STR(?friend), SEPARATOR=";") AS ?friendList) WHERE {?person :hasFriend ?friend} GROUP BY ?person Is this what you are after? – Valerio Cocchi Jan 27 '21 at 15:12
  • 1
    given two variables `?v1` and `?v2`, you can concatenate it via `CONCAT` function. Either put it into a `BIND` or the projection part. For example, `SELECT ?res WHERE { ... BIND(concat(str(?v1), str(?v2) ) as ?res) }` – UninformedUser Jan 27 '21 at 15:55
  • I think UninformedUser solution works well for my case: I think I must have just messed up the syntax when attempting it myself first, as I forgot to "BIND". I think the solution from Valerio might work better in another case, but I have not tried it, so I'm not sure. Thank you both! – Robin Jan 28 '21 at 08:06

1 Answers1

1
 BIND(concat(?lastName," ",firstName) AS ?fullName)
Hussain
  • 106
  • 8