noahjb
noahjb

Reputation: 107

SPARQL unique combination of objects

I want to create an edge list from a sparql query in which two nodes are connected by an edge if they are in the same document. The two nodes are both the same object in my ontology which is leading to the issue that the same combination of nodes is always listed twice in reverse order. the nodes are ?neighbor and ?topic. My SPARQL Query is:

select  ?neighbor ?topic ?document ?addresee

where { 
    ?document rdf:type paco:report ;
              paco:hasTopic ?topic;
              paco:hasTopic ?neighbor ;
              paco:hasSender  pacp:16027;
              paco:hasAddressee ?addresee .
            FILTER (?status != paco:Entwurf && ?topic!=?neighbor)
}

My results currently look something like this:

neighbor    topic        document       addresee
pact:Rt     pact:PUH     pacd:doc5782   pacp:per7008
pact:PUH    pact:Rt      pacd:doc5782   pacp:per7008
pact:R19    pact:B       pacd:doc1143   pacp:per7008
pact:B      pact:R19     pacd:doc1143   pacp:per7008

Because rows 1 and 2, as well as 3 and 4 are the same in a undirected graph I really only want 1 line for each. But I am not sure how to change the logic of my sparql query to reflect that.

I am working with a local ontology, that is why I am unable to produce a fully reproducible example.

Upvotes: 2

Views: 188

Answers (1)

HES
HES

Reputation: 157

This approach feels like a bit of a hack, but something like this could work:

select ?neighbor ?topic ?document ?addresee where 
{ 
{

select distinct  ?document group_concat(distinct str(?topic);SEPARATOR=",") as ?topic

where 
{ 
?document rdf:type paco:report ;
              paco:hasSender  pacp:16027;
              paco:hasTopic ?topic .
}
group by ?document 
}
?document rdf:type paco:report ;
              paco:hasTopic ?topic;
              paco:hasAddressee ?addresee .
bind( strbefore( ?topic, "," ) as ?topic).
bind( strafter( ?topic, "," ) as ?neighbor).
}
  1. A subquery fetches all the topics and concatenates them via with a group_concat clase of distinct ?topic. We group_by ?document.
  2. We separate out this comma separated list into respective columns.

If this is a usual workflow in your graph, you might want to consider adapting the ontology a little to make this a bit cleaner.

FYI - in your original query, this filter isn't doing anything: FILTER (?status != paco:Entwurf) as ?status isn't specified anywhere.

Upvotes: 1

Related Questions