SPARQL Query to find Instances with the same values for property

39 views
Skip to first unread message

Marie Valadez

unread,
Feb 21, 2024, 4:14:23 PM2/21/24
to TopBraid Suite Users
I am in EDG trying to find instances where another Concept has the exact same values for the property (no more or less). The values can be inherited through skos:broader or directly stated on the concept itself. The property connects to a blank node which contains 2 additional properties and values. 
Example:
ConceptA ex:category [ex:map ex:default ;
                                          ex:attribute ex:S03.01] ;
                                         [ex:map ex:alternate ;
                                           ex:attribute ex:S03.02] .
ConceptB ex:category [ex:map ex:default ;
                                          ex:attribute ex:S03.01] ;
                                         [ex:map ex:alternate ;
                                           ex:attribute ex:S03.02] .
ConceptC ex:category [ex:map ex:default ;
                                          ex:attribute ex:S03.01] .
ConceptD ex:category [ex:map ex:default ;
                                          ex:attribute ex:S03.01] ;
                                         [ex:map ex:alternate ;
                                           ex:attribute ex:S03.02] ;
                                         [ex:map ex:alternate ;
                                           ex:attribute ex:S03.03] .

I want to return both ConceptA and ConceptB but not ConceptC or ConceptD since it doesn't match ConceptA exactly. 

I have tried different variations of a sparql query shown below (removed FILTER clause) but haven't been able to return the results I am expecting.

SELECT DISTINCT $this ?concept ?map ?attr
WHERE { $this skos:broader*/ex:category ?blank .
?blank ex:map ?map ;
          ex:attribute ?attr .
?concept skos:broader*/ex:category ?blank1 .
?blank1 ex:map ?map ;
                      ex:attribute ?attr .
  FILTER(?concept != $this)
   
}

#find concept with the same modeling
SELECT DISTINCT $this ?map ?attr
WHERE { $this skos:broader*/ex:category ?blank .
?blank ex:map ?map ;
           ex:attribute ?attr .
 FILTER EXISTS{?concept skos:broader*/ex:category ?blank1 .
?blank1 ex:map ?map ;
             ex:attribute ?attr .
      FILTER(?concept != $this)}
}

Holger Knublauch

unread,
Feb 22, 2024, 4:55:09 AM2/22/24
to topbrai...@googlegroups.com
The results look correct )with ex:ConceptA the selected resource):

PastedGraphic-1.png

because all of the concepts match with at least one blank node. SPARQL operates with "EXISTS" semantics and will here return any concept where the same linked values exist, even if they also have other values.

Are you saying you want C and D not to match because they also have other values?

Holger


--
The topics of this mailing list include TopBraid EDG and related technologies such as SHACL.
To post to this group, send email to topbrai...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/topbraid-users/310db76e-c5bc-4448-9c14-1afba788bd8en%40googlegroups.com.

Marie Valadez

unread,
Feb 22, 2024, 10:37:35 AM2/22/24
to TopBraid Suite Users
Hi Holger, 

Thanks for getting back to me so quickly. 

I only want to return Concept B since it has exactly the same blank nodes as Concept A. I don't want to return Concept C and Concept D since they either have 1 less or 1 more than Concept A.

Holger Knublauch

unread,
Feb 22, 2024, 10:45:00 AM2/22/24
to 'Richard Nagelmaeker' via TopBraid Suite Users
Ok, to express that, I believe you need to 

1) Make sure that the number of property values is the same for both concepts, and that
2) The second concept does NOT have any values that the first concept does not also have.
    This can be expressed using FILTER NOT EXISTS { … }.

To count values of a property (for step 1), you can use this function

BIND (<http://spinrdf.org/spl#objectCount>(?conceptA, ex:property) AS ?countA) .

HTH
Holger


Marie Valadez

unread,
Feb 22, 2024, 1:05:43 PM2/22/24
to TopBraid Suite Users
Thank you Holger. That's a good idea to use count to narrow down the ones that are the same. 

Do you happen to have more documentation on the spl:ObjectCount? 

I am trying to find a way to utilize this with inherited ones as well through skos:broader*/ex:category where in some cases they might have both directly stated and inherited connections to the blank node. 

Holger Knublauch

unread,
Feb 22, 2024, 1:10:46 PM2/22/24
to 'Richard Nagelmaeker' via TopBraid Suite Users
To get the COUNT of a path expression, spl:objectCount will not help.

Normally, counting can be done via a nested SELECT query with a COUNT in SPARQL. However, nested queries by default do not "see" variables from the outside, making them rather inefficient. There is a new SPARQL keyword, LATERAL, supported in places but I have not used that yet.

It's a tricky topic, and further help may become too complex for this mailing list. If you are a paying TopBraid customer, maybe you want to contact our professional services/premium support?

Holger


Reply all
Reply to author
Forward
0 new messages